简体   繁体   中英

Issues in calculating tf-idf in gensim

I am using Gensim to calculate tf-idf scores for my corpus mentioned below.

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

My current code is as follows.

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]

However, I get the error RecursionError: maximum recursion depth exceeded while calling a Python object (PS: if my code is wrong I am happy to have a different code). Please help me to calculate tf-idf values for my current corpus. Moreover, I want to get the 3 terms that has the highest tf-idf score in my corpus.

Please help me!

Well you are redefining your original corpus list as a MyCorpus object, so of course this is going into an infinite recursion when trying to iterate over it (since you iterate over corpus in the __iter__ method). Maybe you want to define my_corpus = MyCorpus() ? Otherwise explain what you are trying to do...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM