我无法理解如何为以下代码打印输出

# make gensim dictionary and corpus
dictionary = gensim.corpora.Dictionary(boc_texts)
corpus = [dictionary.doc2bow(boc_text) for boc_text in boc_texts]
tfidf = gensim.models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]


我想打印关键短语及其tfidf分数

谢谢

最佳答案

我正在使用博客文章上找到的相同代码,并且遇到了与您相同的问题。

这是完整的代码:
https://gist.github.com/bbengfort/efb311aaa1b52814c284d3b21ae752d6

基本上你只需要添加

if __name__ == '__main__':
tfidfs, id2word = score_keyphrases_by_tfidf(texts)
fileids = texts.fileids()

# Print top keywords by TF-IDF
for idx, doc in enumerate(tfidfs):
    print("Document '{}' key phrases:".format(fileids[idx]))
    # Get top 20 terms by TF-IDF score
    for wid, score in heapq.nlargest(20, doc, key=itemgetter(1)):
        print("{:0.3f}: {}".format(score, id2word[wid]))

    print("")

08-20 02:04