Skip to main content
 首页 » 编程设计

python之如何打印gensim词典和语料库

2024年10月01日9exmyth

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

# 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("")