关于这个项目的一些背景。我有带有标识符和文本的副本,例如{name: "sports-football", text: "与足球运动相关的内容"}
。
我需要在这个语料库中找到给定文本输入的正确匹配项。 但是,我使用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
非常感谢您帮助我处理这个问题。
期待着令人惊叹的回复。