我使用 spaCy 和 pandas 得到奇怪的重复结果

问题描述 投票:0回答:1

我正在制作一个机器人来解决《纽约时报》的“连接”游戏。我正在尝试制作一个具有单词余弦相似度的矩阵,以便我可以检查哪些单词最有可能是同义词,但是该矩阵具有重复行的奇怪现象,我无法弄清楚为什么。

这是我的代码:

nlp = spacy.load("en_core_web_md")
def synonym_detector(words):
    #takes in a list of strings, calculates the cosine similarity between all words.

    word_matrix = pd.DataFrame(columns = words, index = words)
    vectors = [nlp(x) for x in words]
    
    for i in range(len(vectors)):
        for j in range(i+1, len(vectors)):
            similarity = vectors[i].similarity(vectors[j])
            word_matrix.loc[words[i],words[j]] = similarity
    
    return word_matrix   
synonym_detector(['DAISY', 'ROSE', 'TULIP', 'VIOLET', 'BARN', 'CHICKEN', 'FARMER', 'TRACTOR','ASTER', 'CARPENTER', 'CRAVEN', 'WAN','DUST', 'LIFE', 'SPORTS', 'YELLOW'])

Here's the output

输出奇怪地有重复的行,我不明白为什么。 此外,.similar 测试给了我一些奇怪的答案。例如,

dust = nlp('dust')
yellow = nlp('yellow')
dust.similarity(yellow)

我得到了 1.0。

感谢您的帮助!

python pandas debugging spacy
1个回答
0
投票

具有默认向量(不是小花向量)的

md
模型对多个单词使用相同的向量以节省空间。如果您希望向量表中的每个单词都有一个唯一的向量,请改用
lg
模型。

md
向量是使用
prune
选项创建的:https://spacy.io/api/cli#init-vectors

© www.soinside.com 2019 - 2024. All rights reserved.