【发布时间】:2018-01-23 22:19:37
【问题描述】:
关于这个项目的一些背景知识。我有带有标识符和文本的副本,例如{name: "sports-football", text: "Content related to football sports"}.
我需要在这个语料库中为给定的文本输入找到正确的匹配。 但是,我能够使用 Gensim 实现一些目标。与 LDA 和 LSI 模型相似。
如何使用新文档更新Genism.Similarity 索引。这里的想法是在实时阶段继续训练模型。
这是我遵循的步骤。
QueryText = "瓜迪奥拉将莱昂内尔·梅西调到了 9 号位置,这样他就不必靠得太深了,我认为阿圭罗经常退回到更靠后的位置。"
注意:有些代码只是外行
索引是使用
创建的`similarities.Similarity(indexpath, model,topics)`
-
创建字典
dictionary = Dictionary(QueryText ) -
创建语料库
corpus = Corpus(QueryText, dictionary) -
创建 LDA 模型
LDAModel = ldaModel(corpus,dictionary)
更新现有的字典、模型和索引
更新现有字典
existing_dictionary.add_document(dictionary)
更新现有的 LDA 模型
existing_lda_model.update(corpus)
更新现有的相似度索引
existing_index.add_dcoument(LDAModel[corpus])
除了以下警告更新似乎有效。
gensim\models\ldamodel.py:535: RuntimeWarning: overflow encountered in exp2 perwordbound, np.exp2(-perwordbound), len(chunk), corpus_words
让我们对查询文本运行相似度
vec_bow = dictionary.doc2bow(QueryText)
vec_model = existing_lda_model[vec_bow]
sims = existing_index[vec_model]
但是,它失败并出现以下错误。
Similarity index with 723 documents in 1 shards (stored under \Files\models\lda_model)
Similarity index with 725 documents in 0 shards (stored under \Files\models\lda_model)
\lib\site-packages\gensim\models\ldamodel.py:535: RuntimeWarning: overflow encountered in exp2
perwordbound, np.exp2(-perwordbound), len(chunk), corpus_words
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-8fe711724367> in <module>()
45 trigram = Trigram.apply_trigram_model(queryText, bigram, trigram)
46 vec_bow = dictionry.doc2bow(trigram)
---> 47 vec_model = lda_model[vec_bow]
48 print(vec_model)
49
~\Anaconda3\envs\lf\lib\site-packages\gensim\models\ldamodel.py in __getitem__(self, bow, eps)
1103 `(topic_id, topic_probability)` 2-tuples.
1104 """
-> 1105 return self.get_document_topics(bow, eps, self.minimum_phi_value, self.per_word_topics)
1106
1107 def save(self, fname, ignore=('state', 'dispatcher'), separately=None, *args, **kwargs):
~\Anaconda3\envs\lf\lib\site-packages\gensim\models\ldamodel.py in get_document_topics(self, bow, minimum_probability, minimum_phi_value, per_word_topics)
944 return self._apply(corpus, **kwargs)
945
--> 946 gamma, phis = self.inference([bow], collect_sstats=per_word_topics)
947 topic_dist = gamma[0] / sum(gamma[0]) # normalize distribution
948
~\Anaconda3\envs\lf\lib\site-packages\gensim\models\ldamodel.py in inference(self, chunk, collect_sstats)
442 Elogthetad = Elogtheta[d, :]
443 expElogthetad = expElogtheta[d, :]
--> 444 expElogbetad = self.expElogbeta[:, ids]
445
446 # The optimal phi_{dwk} is proportional to expElogthetad_k * expElogbetad_w.
IndexError: index 718 is out of bounds for axis 1 with size 713
我真的很感激,帮助我解决这个问题。 期待精彩的回复。
【问题讨论】:
标签: python nlp similarity gensim