textrank
textrank copied to clipboard
IndexError when words parameter is too high with keywords.keywords()
How to reproduce the error :
from summa import keywords
text = """Automatic summarization is the process of reducing a text document with a \
computer program in order to create a summary that retains the most important points \
of the original document. As the problem of information overload has grown, and as \
the quantity of data has increased, so has interest in automatic summarization. \
Technologies that can make a coherent summary take into account variables such as \
length, writing style and syntax. An example of the use of summarization technology \
is search engines such as Google. Document summarization is another."""
keywords.keywords(text, words=30)
produces :
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-56-e1afaa84dab3> in <module>()
1 text = """Automatic summarization is the process of reducing a text document with a computer program in order to create a summary that retains the most important points of the original document. As the problem of information overload has grown, and as the quantity of data has increased, so has interest in automatic summarization. Technologies that can make a coherent summary take into account variables such as length, writing style and syntax. An example of the use of summarization technology is search engines such as Google. Document summarization is another."""
2
----> 3 keywords.keywords(text, words=30)
2 frames
/usr/local/lib/python3.7/dist-packages/summa/keywords.py in <listcomp>(.0)
101 # reduced by the provided ratio, else, the ratio is ignored.
102 length = len(lemmas) * ratio if words is None else words
--> 103 return [(scores[lemmas[i]], lemmas[i],) for i in range(int(length))]
104
105
IndexError: list index out of range
Facing the same error, the easiest way is to convert "words" into a "ratio" estimate, since the above code is always valid for "ratio". This would be my work around.
from summa.preprocessing.textcleaner import tokenize_by_word as _tokenize_by_word
maxWords=30
split_text = list(_tokenize_by_word(text))
numToks = len(split_text)
keyRatio = maxWords / numToks
if (keyRatio>1.0): keyRatio = 1.0
keyWords = keywords.keywords(text, ratio=keyRatio)