您可以使用nltk、Textblob、SpaCy 或许多其他库中的任何一个来获得良好的结果。这些库都可以完成这项工作,但效率不同。
import nltk
from textblob import TextBlob
import spacy
nlp = spacy.load('en')
nlp1 = spacy.load('en_core_web_lg')
txt = """Natural language processing (NLP) is a field of computer science, artificial intelligence, and computational linguistics concerned with the interactions between computers and human (natural) languages."""
在我的 windows 10 2 核、4 个处理器、8GB ram i5 hp 笔记本电脑上,在 jupyter notebook 上,我进行了一些比较,结果如下。
对于 TextBlob:
%%time
print([w for (w, pos) in TextBlob(txt).pos_tags if pos[0] == 'N'])
输出是
>>> ['language', 'processing', 'NLP', 'field', 'computer', 'science', 'intelligence', 'linguistics', 'inter', 'actions', 'computers', 'languages']
Wall time: 8.01 ms #average over 20 iterations
对于 nltk:
%%time
print([word for (word, pos) in nltk.pos_tag(nltk.word_tokenize(txt)) if pos[0] == 'N'])
输出是
>>> ['language', 'processing', 'NLP', 'field', 'computer', 'science', 'intelligence', 'linguistics', 'inter', 'actions', 'computers', 'languages']
Wall time: 7.09 ms #average over 20 iterations
对于spacy:
%%time
print([ent.text for ent in nlp(txt) if ent.pos_ == 'NOUN'])
输出是
>>> ['language', 'processing', 'field', 'computer', 'science', 'intelligence', 'linguistics', 'inter', 'actions', 'computers', 'languages']
Wall time: 30.19 ms #average over 20 iterations
似乎nltk 和TextBlob 相当快,这是意料之中的,因为没有存储关于输入文本txt 的任何其他内容。 Spacy的速度要慢得多。还有一件事。 SpaCy 错过了名词 NLP 而 nltk 和 TextBlob 得到了它。我会为nltk 或TextBlob 拍摄,除非我想从输入txt 中提取其他内容。
查看spacy here 的快速入门。
查看有关 TextBlob here 的一些基础知识。
查看 nltk HowTos here