Search code examples
pythonpython-3.xtextnlpinformation-retrieval

Word Mover's Distance in Python


I am trying to calculate the similarity of 2 texts using WMD. I have tried to use the following code in Python 3, using gensim:

word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
word2vec_model.init_sims(replace=True) # normalizes vectors
distance = word2vec_model.wmdistance("string 1", "string 2")  # Compute WMD as normal.

However, I don't think this is returning me the right value. How should I do this in python?


Solution

  • Please split string:

    distance = word2vec_model.wmdistance("string 1".split(), "string 2".split())
    >>> 0.4114476676950455
    

    Arguments need to be list of strings.