使用tf-idf(Gensim)获取语料库中最重要的单词

时间:2017-11-17 14:48:50

标签: python gensim tf-idf

我正在计算tf-idf如下。

texts=['human interface computer',
 'survey user computer system response time',
 'eps user interface system',
 'system human system eps',
 'user response time']

dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
analyzedDocument = namedtuple('AnalyzedDocument', 'word tfidf_score')
d=[]
for doc in corpus_tfidf:
    for id, value in doc:
        word = dictionary.get(id)
        score = value
        d.append(analyzedDocument(word, score))

但是,现在我想使用idf值最高的单词来识别语料库中最重要的3个单词。请告诉我怎么做?

1 个答案:

答案 0 :(得分:0)

假设你的名单没问题,你应该可以按如下方式安排:在顶部:

from operator import itemgetter

然后在底部:

e=sorted(d, key=itemgetter(1))
top3 = e[:3]
print(top3)
相关问题