python - Gensim 中的 TFIDIF 模型创建类型错误

标签 python nlp gensim tf-idf language-features

类型错误:“TfidfModel”对象不可调用

为什么初始化后无法计算每个文档的 TFIDF 矩阵?

我从 999 个文档开始:999 个段落,每个段落大约有 5-15 个句子。 在 spaCy 对所有内容进行标记后,我创建了字典(约 16k 个唯一标记)和语料库(元组列表的列表)

现在我准备为一些 ML 创建 tfidf 矩阵(以及后来的 LDA 和 w2V 矩阵);但是,在使用我的语料库初始化 tfidf 模型后(用于计算“IDF”) tfidf = models.TfidfModel(corpus) 当尝试查看每个文档的 tfidf tfidf(corpus[5]) 时,我收到以下错误消息 类型错误:“TfidfModel”对象不可调用

我可以使用不同的语料库创建这个模型,其中我有四个文档,每个文档仅包含一个句子。 在那里我可以确认预期的语料库格式是元组列表的列表: [doc1[(word1, count),(word2, count),...], doc2[(word3, count),(word4,count),...]...]

from gensim import corpora, models, similarities

texts = [['teenager', 'martha', 'moxley'...], ['ok','like','kris','usual',...]...]
dictionary = corpora.Dictionary(texts)
>>> Dictionary(15937 unique tokens: ['teenager', 'martha', 'moxley']...)

corpus = [dictionary.doc2bow(text) for text in texts]
>>> [[(0, 2),(1, 2),(2, 1)...],[(3, 1),(4, 1)...]...]

tfidf = models.TfidfModel(corpus)
>>> TfidfModel(num_docs=999, num_nnz=86642)

tfidf(corpus[0])
>>> TypeError: 'TfidfModel' object is not callable

corpus[0]
>>> [(0, 2),(1, 2),(2, 1)...]

print(type(corpus),type(corpus[1]),type(corpus[1][3]))
>>> <class 'list'> <class 'list'> <class 'tuple'>

最佳答案

而不是:tfidf(corpus[0])

尝试:tfidf[语料库[0]]

关于python - Gensim 中的 TFIDIF 模型创建类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368720/

相关文章:

unicode - 使用马尔可夫模型将全部大写转换为混合大小写和相关问题

python - 无法对齐图形,因为多个标签 doc2vec 在 doctag_syn0 中返回的项目多于训练数据中的项目

gensim - 如何使用gensim的LDA从查询中进行文本检索?

python - 用逗号和 - 作为分隔符循环 python

python - 为什么打开该文件时Python tkinter崩溃?

java - 使用标准 corenlp 包获取 corefrences

python - Python 3.5 上的 Gensim 1.0.1 TypeError : object of type 'map' has no len()?

python - 无法在 Jupyter 笔记本中读取 avro

python - 具有多个基元的显示列表的 VBO 等价物?

python - 根据词性分类的单词生成有意义的句子