通过Spacy的解析器计算基于单词向量的两个单词的相似性后没有结果?

时间:2018-03-11 05:01:05

标签: parsing nlp spacy

我在spacy代码中有一个例子:

    from numpy import dot
    from numpy.linalg import norm
    from spacy.lang.en import English

    parser = English()
    # you can access known words from the parser's vocabulary
    nasa = parser.vocab[u'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_vector and 
    w.orth_.islower() and w.lower_ != unicode("nasa")})

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

结果如下:

    Top 10 most similar words to NASA:

    Process finished with exit code 0

所以没有类似的话出来。 我试图通过cmd安装解析器和手套:

    python -m spacy.en.download parser
    python -m spacy.en.download glove

但失败了,原来是:

    C:\Python\python.exe: No module named en

顺便说一下,我用:

    Python 2.7.9
    Spacy  2.0.9

它有什么问题?谢谢

1 个答案:

答案 0 :(得分:1)

您实例化的解析器不包含单词向量。查看https://spacy.io/models/以获取模型概述。