我想使用 word2vec 工具为文档构建词嵌入。我知道如何找到与单个单词(unigram)对应的向量嵌入。现在,我想找到一个二元组的向量。可以使用word2vec吗?如果是,如何?
最佳答案
以下代码段将为您提供二元组的向量表示。请注意,要转换为向量的二元组需要在单词之间使用下划线而不是空格,例如bigram2vec(unigrams, "this report")
错了,应该是bigram2vec(unigrams, "this_report")
.有关生成 unigram 的更多详细信息,请参阅 gensim.models.word2vec.Word2Vec
类(class)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
关于nlp - 双元组到向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35747245/