How to calculate the sentence similarity using word2vec model of gensim with python

后端 未结 14 1329
一向
一向 2020-11-28 00:31

According to the Gensim Word2Vec, I can use the word2vec model in gensim package to calculate the similarity between 2 words.

e.g.

trained_model.simi         


        
14条回答
  •  忘掉有多难
    2020-11-28 01:23

    you can use Word Mover's Distance algorithm. here is an easy description about WMD.

    #load word2vec model, here GoogleNews is used
    model = gensim.models.KeyedVectors.load_word2vec_format('../GoogleNews-vectors-negative300.bin', binary=True)
    #two sample sentences 
    s1 = 'the first sentence'
    s2 = 'the second text'
    
    #calculate distance between two sentences using WMD algorithm
    distance = model.wmdistance(s1, s2)
    
    print ('distance = %.3f' % distance)
    

    P.s.: if you face an error about import pyemd library, you can install it using following command:

    pip install pyemd
    

提交回复
热议问题