Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError when words parameter is too high with keywords.keywords() #83

Open
Achuttarsing opened this issue Mar 17, 2021 · 1 comment

Comments

@Achuttarsing
Copy link

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
@kinoc
Copy link

kinoc commented Dec 28, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants