【问题标题】:Issues in calculating tf-idf in gensim在 gensim 中计算 tf-idf 的问题
【发布时间】:2017-11-15 16:13:48
【问题描述】:

我正在使用 Gensim 为下面提到的我的语料库计算 tf-idf 分数。

corpus=['human interface computer',
 'survey user computer system response time',
 'eps user interface system',
 'system human system eps',
 'user response time']

我目前的代码如下。

dictionary = corpora.Dictionary(line.lower().split() for line in corpus)

class MyCorpus(object):
    def __iter__(self):
        for line in corpus:
            yield dictionary.doc2bow(line.lower().split())

corpus = MyCorpus()

tfidf = models.TfidfModel(corpus)

corpus_tfidf = tfidf[corpus]

但是,我收到错误 RecursionError: maximum recursion depth exceeded while calling a Python object(PS:如果我的代码有误,我很高兴有不同的代码)。请帮我计算我当前语料库的 tf-idf 值。此外,我想在我的语料库中获得 tf-idf 分数最高的 3 个词。

请帮帮我!

【问题讨论】:

    标签: python gensim tf-idf


    【解决方案1】:

    好吧,您正在将原始 corpus 列表重新定义为 MyCorpus 对象,所以当尝试对其进行迭代时,这当然会进入无限递归(因为您在 __iter__ 方法中迭代 corpus )。也许你想定义my_corpus = MyCorpus()?否则请解释您要做什么...

    【讨论】:

    • 非常感谢您的回答。老实说,我想为我的语料库计算 tf-idf 值并获得 tf-idf 分数最高的 3 个术语。如果你知道怎么做,请告诉我?
    • 你试过这个修复吗?如果我仍然不工作,你需要解释为什么......
    • 修复后出现以下错误:ValueError: not enough values to unpack (expected 2, got 1)
    • 那么我认为您应该接受这个答案并发布反映您的新问题的新问题,并提及生成错误的行和整个调用堆栈...
    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2015-04-17
    • 2019-05-24
    • 2023-04-06
    • 1970-01-01
    • 2012-04-23
    相关资源
    最近更新 更多