如何获取path_similarity得分最高的synset

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

我理解以下代码使用syn1的同义词迭代syn2的所有同义词集。

我的问题是,如何获得最高分的synset?

    from nltk.corpus import wordnet
    syn1 = wordnet.synsets('speed',pos='n')
    syn2 = wordnet.synsets('performance',pos='n')
    for word1 in syn1:
        best = max(word1.path_similarity(word2) for word2 in syn2)
        ps_list.append(best)
python nlp nltk
1个回答
0
投票

也许你需要这样的东西:

import numpy as np
from nltk.corpus import wordnet

syn1 = wordnet.synsets('speed',pos='n')
syn2 = wordnet.synsets('performance',pos='n')

def getMaxPath(synset1,synset2):
    sim=[]
    a=[]
    b=[]
    for i in synset1:
        for j in synset2:
            sim.append(wordnet.path_similarity(i,j))
            a.append(i.name())   # save the names from synsets1 into list
            b.append(j.name())   # save the names from synsets2 into list

    max_sim=max(sim)
    idx=np.argmax(sim)
    s1=a[idx]        # get the name of synset1 for which path sim is max
    s2=b[idx]        # get the name of synset2 for which path sim is max
    return max_sim, s1, s2

getMaxPath(syn1, syn2)

输出:

(0.2, 'speed.n.03', 'performance.n.03')
© www.soinside.com 2019 - 2024. All rights reserved.