如何打印gensim词典和语料库

How to print gensim dictionary and corpus

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

# 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 分数

谢谢

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

完整代码如下: 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("")