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

标签 python nlp gensim

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

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

关于python - 如何打印gensim词典和语料库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668207/

相关文章:

python - 当我在 KIVY 中运行我的程序时,如何更改窗口上的图标

python - 如何使用 NLTK ne_chunk 提取 GPE(位置)?

machine-learning - NLTK(python)中的语料库和词典有什么区别

python - 根据多个谓词对字符串列表进行排序

python - 如何按行压缩两个元组列表?

python - 无法访问 JSON 输出中的 key : string indices must be integers

python - 使用 'word groups' 创建字典

python - 使用 Spacy 处理语法错误

python - 了解使用 "gensim.corpora.Dictionary(TEXT)"后单词如何存储在 gensim 语料库中

python - 无法在 ubuntu 上使用 Python 3.5 安装 Gensim