Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 530
Skinish Avatar asked Jan 30 '26 12:01

Skinish


1 Answers

Please split string:

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

Arguments need to be list of strings.

like image 71
Hironsan Avatar answered Feb 02 '26 00:02

Hironsan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!