首页 > 解决方案 > NLTK wordnet 计算两个列表中单词的路径相似度

问题描述

我正在尝试查找文本文件中单词的相似性。我附上了下面的代码,我从文本文件中读取并将内容分成两个列表,但现在我想将列表 1 中的单词与列表 2 进行比较。

file = open('M:\ThirdYear\CE314\Assignment2\sim_data\Assignment_Additional.txt', 'r')
word1 = []
word2 = []

split = [line.strip() for line in file]
count = 0

for line in split:
    if count == (len(split) - 1):
        break
    else:
        word1.append(line.split('\t')[0])
        word2.append(line.split('\t')[1])
        count = count + 1  

print(word1)
print(word2)

for x, y in zip(word1, word2):
    w1 = wordnet.synset(x + '.n.1')
    w2 = wordnet.synset(y + '.n.1')
    print(w1.path_similarity(w2))

我想遍历这两个列表并打印它们的 path_similarity 但只有当它们遵守规则 wordnet.synset(x + '.n.1') 意味着任何没有'.n.1'的单词我想忽略和跳过,但我不完全确定如何在 python 中进行此检查

标签: pythonerror-handlingnltkwordnettraceback

解决方案


推荐阅读