The following snippet will give you a vectorial representation of a bigram. Please note that the bigram you want to convert to a vector must have an underscore instead of a space between words, for example. bigram2vec(unigrams, "this report")wrong, it should be bigram2vec(unigrams, "this_report"). For more information on creating unigrams, see the gensim.models.word2vec.Word2Vecclass here .
from gensim.models import word2vec
def bigram2vec(unigrams, bigram_to_search):
bigrams = Phrases(unigrams)
model = word2vec.Word2Vec(bigrams[unigrams])
if bigram_to_search in model.vocab.keys():
return model[bigram_to_search]
else:
return None