Converting Fasttext vector to word

Viewed 2056

I am having trouble converting a fast FastText vector back to a word. Here is my python code:

from gensim.models import KeyedVectors
en_model = KeyedVectors.load_word2vec_format('wiki.en/wiki.en.vec')
vect = en_model.get_vector("turtles")

How can I take the vector (especially an arbitrary vector with the proper dimensions) and have it spit out a word?

1 Answers

You want to use ret_vals = en_model.similar_by_vector(vect) (see similar_by_vector). Since vect is any arbitrary vector, you'll get back the closest matches. You can control the number you get back with the param topn=XX. If not supplied, you'll get back the top 10. The return values are a list of tuples, formatted (str, float) where str is the word and float is the similarity.

Related