如何使用SpaCy解析器?

时间:2017-05-22 15:13:02

标签: python python-3.x nlp spacy

我有一个在spacy代码中使用NLP的示例:

from numpy import dot
from numpy.linalg import norm

# you can access known words from the parser's vocabulary
nasa = parser.vocab['NASA']

# cosine similarity
cosine = lambda v1, v2: dot(v1, v2) / (norm(v1) * norm(v2))

# gather all known words, take only the lowercased versions
allWords = list({w for w in parser.vocab if w.has_repvec and w.orth_.islower() and w.lower_ != "nasa"})

# sort by similarity to NASA
allWords.sort(key=lambda w: cosine(w.repvec, nasa.repvec))
allWords.reverse()
print("Top 10 most similar words to NASA:")
for word in allWords[:10]:   
    print(word.orth_)

# Let's see if it can figure out this analogy
# Man is to King as Woman is to ??
king = parser.vocab['king']
man = parser.vocab['man']
woman = parser.vocab['woman']

result = king.repvec - man.repvec + woman.repvec

# gather all known words, take only the lowercased versions
allWords = list({w for w in parser.vocab if w.has_repvec and w.orth_.islower() and w.lower_ != "king" and w.lower_ != "man" and w.lower_ != "woman"})
# sort by similarity to the result
allWords.sort(key=lambda w: cosine(w.repvec, result))
allWords.reverse()
print("\n------------------\nTop 3 closest results for king - man + woman:")
for word in allWords[:3]:   
    print(word.orth_)

在我为spacy安装所有词典之前:

$ sudo python -m spacy.en.download all
$ python -m spacy.en.download parser
$ python -m spacy.en.download glove

但是,结果,我有一个错误:

Traceback (most recent call last):
  File "/home/vv/PythProj/sp_ex_1.py", line 5, in <module>
    nasa = parser.vocab['NASA']
NameError: name 'parser' is not defined

拜托,我的错误在哪里?谢谢

1 个答案:

答案 0 :(得分:1)

我想你错过了这个:

# Set up spaCy
from spacy.en import English
parser = English()