简体   繁体   中英

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 .

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM