【发布时间】:2020-10-04 04:57:57
【问题描述】:
我的目标是找到单词和文档之间的相似之处。例如,我想查找“new”和文档之间的相似性,为简单起见,说“Hello World!”。
我使用了 gensim 的 word2vec,但问题是它没有找到一个看不见的单词的相似性。因此,我尝试使用 gensim 的 fastText,因为它可以找到词汇表之外的单词的相似性。
这是我的文档数据示例:
[['This', 'is', 'the', 'only', 'rule', 'of', 'our', 'household'],
['If',
'you',
'feel',
'a',
'presence',
'standing',
'over',
'you',
'while',
'you',
'sleep',
'do'],
['NOT', 'open', 'your', 'eyes'],
['Ignore', 'it', 'and', 'try', 'to', 'fall', 'asleep'],
['This',
'may',
'sound',
'a',
'bit',
'like',
'the',
'show',
'Bird',
'Box',
'from',
'Netflix']]
我只是像这样训练数据:
from gensim.models.fasttext import FastText
model = FastText(sentences_cleaned)
因此,我想找出“规则”与此文档之间的相似性。
model.wv.most_similar("rule")
然而,fastText 给了我这个:
[('the', 0.1334390938282013),
('they', 0.12790171802043915),
('in', 0.12731242179870605),
('not', 0.12656228244304657),
('and', 0.11071767657995224),
('of', 0.08563747256994247),
('I', 0.06609072536230087),
('that', 0.05195673555135727),
('The', 0.002402491867542267),
('my', -0.009009800851345062)]
显然,由于“规则”一词出现在文档的第一句话中,因此它必须具有“规则”作为最高相似度。我也尝试了词干提取/词形还原,但它也不起作用。
我的输入格式正确吗?我看到很多文档都使用 .cor 或 .bin 格式,但我不知道它们是什么。
感谢您的回复!
【问题讨论】:
-
我不知道您所说的“文档”是什么意思,因为正如 gojomo 提到的那样,您并没有显示自己在训练任何文档。但是,您的代码中的另一个问题是
most_similar需要一个 list 单词。试试most_similar(["rule"]),你至少应该得到类似于“规则”的词。 -
检查这个类似的问题:link。建议像这样获得最相似:words = model.most_similar(positive=['rule'], topn=10, restrict_vocab=50000)
标签: nlp word2vec sentiment-analysis fasttext sentence-similarity