kev*_*son 5 python nltk wordnet
我正在使用Python NLTK Wordnet API.我正在尝试找到代表一组单词的最佳synset.
如果我需要为"学校和办公用品"找到最好的同义词,我不知道如何解决这个问题.到目前为止,我已经尝试找到单个单词的同义词,然后计算最好的最低常见上限,如下所示:
def find_best_synset(category_name):
text = word_tokenize(category_name)
tags = pos_tag(text)
node_synsets = []
for word, tag in tags:
pos = get_wordnet_pos(tag)
if not pos:
continue
node_synsets.append(wordnet.synsets(word, pos=pos))
max_score = 0
max_synset = None
max_combination = None
for combination in itertools.product(*node_synsets):
for test in itertools.combinations(combination, 2):
score = wordnet.path_similarity(test[0], test[1])
if score > max_score:
max_score = score
max_combination = test
max_synset = test[0].lowest_common_hypernyms(test[1])
return max_synset
Run Code Online (Sandbox Code Playgroud)
然而,这不是很好,而且成本很高.有没有办法找出哪个synset最能代表多个单词?
谢谢你的帮助!
除了我在评论中所说的之外,我认为您选择最佳超义词的方式可能存在缺陷。您最终得到的同义词集并不是所有单词的最低常见上位词,而只是其中两个单词的最常见上位词。
让我们继续以“学校和办公用品”为例。对于表达式中的每个单词,您都会获得许多同义词集。因此该变量node_synsets将如下所示:
[[school_1, school_2], [office_1, office_2, office_3], [supply_1]]
Run Code Online (Sandbox Code Playgroud)
在此示例中,有 6 种方法将每个同义词集与任何其他同义词集组合:
[(school_1, office_1, supply_1),
(school_1, office_2, supply_1),
(school_1, office_3, supply_1),
(school_2, office_1, supply_1),
(school_2, office_2, supply_1),
(school_2, office_3, supply_1)]
Run Code Online (Sandbox Code Playgroud)
这些三元组是您在外for循环中迭代的内容(使用itertools.product)。如果表达式有 4 个单词,您将迭代四元组,如果表达式有 5 个单词,则迭代五元组,依此类推。
现在,通过内for循环,您可以将每个三元组配对。第一个是:
[(school_1, office_1),
(school_1, supply_1),
(office_1, supply_1)]
Run Code Online (Sandbox Code Playgroud)
...然后您确定每对中最低的上位词。所以最后你会得到最低的超义词,比如说,school_2and office_1,这可能是某种机构。这可能不是很有意义,因为它没有考虑最后一个单词的任何同义词。
也许您应该尝试在同义词集的每种组合中找到所有三个单词的最低常见超义词,并选择其中得分最高的一个。