Gensim:提出KeyError(“词'%s'不在词汇表中”%word)

问题描述 投票:1回答:1

我有这个代码,我有文章列表作为数据集。每个原始文章都有一篇文章

我运行这段代码:

import gensim    
docgen = TokenGenerator( raw_documents, custom_stop_words )    
# the model has 500 dimensions, the minimum document-term frequency is 20    
w2v_model = gensim.models.Word2Vec(docgen, size=500, min_count=20, sg=1)    
print( "Model has %d terms" % len(w2v_model.wv.vocab) )    
w2v_model.save("w2v-model.bin")    
# To re-load this model, run    
#w2v_model = gensim.models.Word2Vec.load("w2v-model.bin")    
    def calculate_coherence( w2v_model, term_rankings ):
        overall_coherence = 0.0
        for topic_index in range(len(term_rankings)):
            # check each pair of terms
            pair_scores = []
            for pair in combinations(term_rankings[topic_index], 2 ):
                pair_scores.append( w2v_model.similarity(pair[0], pair[1]) )
            # get the mean for all pairs in this topic
            topic_score = sum(pair_scores) / len(pair_scores)
            overall_coherence += topic_score
        # get the mean score across all topics
        return overall_coherence / len(term_rankings)

import numpy as np    
def get_descriptor( all_terms, H, topic_index, top ):    
    # reverse sort the values to sort the indices    
    top_indices = np.argsort( H[topic_index,:] )[::-1]    
    # now get the terms corresponding to the top-ranked indices    
    top_terms = []    
    for term_index in top_indices[0:top]:    
        top_terms.append( all_terms[term_index] )    
    return top_terms    
from itertools import combinations    
k_values = []    
coherences = []    
for (k,W,H) in topic_models:    
    # Get all of the topic descriptors - the term_rankings, based on top 10 terms
    term_rankings = []    
    for topic_index in range(k):
        term_rankings.append( get_descriptor( terms, H, topic_index, 10 ) )

    # Now calculate the coherence based on our Word2vec model
    k_values.append( k )
    coherences.append( calculate_coherence( w2v_model, term_rankings ) )
    print("K=%02d: Coherence=%.4f" % ( k, coherences[-1] ) )

我面对这个错误:

raise KeyError("word '%s' not in vocabulary" % word)

KeyError:你的“词'业务'不在词汇中”

原始代码适用于他们的数据集。

https://github.com/derekgreene/topic-model-tutorial

你能帮忙解决这个错误吗?

python nlp gensim word2vec topic-modeling
1个回答
0
投票

如果您在错误消息周围包含更多信息,例如多行调用帧将清楚地指示代码的哪一行触发了错误,它可以帮助回答者。

但是,如果你收到错误KeyError: u"word 'business' not in vocabulary",你可以相信你的Word2Vec实例,w2v_model,从未学过'business'这个词。

这可能是因为它没有出现在模型呈现的训练数据中,或者可能出现但少于min_count时间。

因为你没有显示你的raw_documents变量的类型/内容,或者你的TokenGenerator类的代码,所以不清楚为什么会出错 - 但这些都是要看的地方。仔细检查raw_documents是否具有正确的内容,并且docgen可迭代对象内的单个项看起来像Word2Vec的正确输入类型。

docgen可迭代对象中的每个项都应该是一个字符串标记列表,而不是普通字符串或其他任何东西。并且,docgen可迭代必须可以多次迭代。例如,如果执行以下两行,您应该看到相同的两个字符串列表标记(看起来像['hello', 'world']

print(iter(docgen).next())
print(iter(docgen).next())

如果你看到普通字符串,docgen没有为Word2Vec提供正确的项目。如果你只看到一个打印的项目,docgen可能是一个简单的单遍迭代器,而不是一个可迭代的对象。

您还可以在INFO级别启用日志记录并仔细观察Word2Vec步骤中的输出,并特别注意任何看似不协调的数字/步骤。 (例如,是否有任何步骤表明没有发生任何事情,或者单词/文本示例的计数是否已关闭?)

© www.soinside.com 2019 - 2024. All rights reserved.