[英]How to get the synset for which path_similarity score is the highest
I understand that the following code iterates over all the synsets of syn1
with synsets of syn2
. 我了解以下代码对
syn1
所有同义词集和syn1
的同义词集进行syn2
。
My question is, how to get the synset that gives the maximum score? 我的问题是,如何获得给出最高分的同义词集?
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)
Maybe you need something like this: 也许您需要这样的东西:
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)
Output: 输出:
(0.2, 'speed.n.03', 'performance.n.03')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.