Getting TF-IDF Scores Of Words Using Gensim
<p>I am trying to find the most important words in a corpus based on their TF-IDF scores.</p>

<p>Been following along the example at <a href="https://radimrehurek.com/gensim/tut2.html" rel="noreferrer">https://radimrehurek.com/gensim/tut2.html</a>. Based on</p>

<pre><code>&gt;&gt;&gt; for doc in corpus_tfidf:
...     print(doc)
</code></pre>

<p>the TF-IDF score is getting updated in each iteration. For example,</p>

<ul>
<li>Word 0 ("<em>computer</em>" based on <a href="https://radimrehurek.com/gensim/tut1.html" rel="noreferrer">https://radimrehurek.com/gensim/tut1.html</a>), has a TF-IDF score of 0.5773 (Doc #1), 0.4442 (Doc #2).</li>
<li>Word 10 ("<em>graph</em>") has a TF-IDF score of 0.7071 (Doc #7), 0.5080 (Doc #8), 0.4588 (Doc #9)</li>
</ul>

<p>So here's how I am currently getting the final TF-IDF score for each word,</p>

<pre><code>tfidf = gensim.models.tfidfmodel.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
d = {}
for doc in corpus_tfidf:
    for id, value in doc:
        word = dictionary.get(id)
        d[word] = value
</code></pre>

<p>Is there a better way?</p>

<p>Thanks in advance.</p>