This bug could happen whenever you list of sentences contains one of length < 12. A good workaround is to remove the [:12] index completely and use the whole sentence as the sentenceValue keys. Does that make sense? So instead it would be:
Hm sounds like you may have forgotten to remove the [:12] index from the other parts of your code where you use sentenceValue, maybe that is the issue? If not, feel free to share a snippet of your code so we can be on the same page.
The code should be like this..
for sentence in sentences:
for wordValue in freqTable:
if wordValue in sentence.lower():
if sentence in sentenceValue:
sentenceValue[sentence] += freqTable[wordValue]
else:
sentenceValue[sentence] = freqTable[wordValue]
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Excellent post!
@davidisrawi can you please help me with text extraction (3-4 Keywords) from each paragraph from an article.
I went through your article i got stuck with an Error "string index out of range".
sentenceValue[sentence[:12]] += wordValue[1]
IndexError: string index out of range
I have tried changing [sentence[:12]] to 7,8,9 but unable to resolve my error.
Please help regarding this
Thank you very much Dhairya!
This bug could happen whenever you list of sentences contains one of length < 12. A good workaround is to remove the
[:12]index completely and use the whole sentence as thesentenceValuekeys. Does that make sense? So instead it would be:Let me know if that fixes the problem!
I have changed it but it is still giving me some KeyError: and showing 3-4 starting lines from my text.
How to solve this error ?
Hm sounds like you may have forgotten to remove the [:12] index from the other parts of your code where you use
sentenceValue, maybe that is the issue? If not, feel free to share a snippet of your code so we can be on the same page.Hi, I am still facing the same error index out of range...help me on this
The code should be like this..
for sentence in sentences:
for wordValue in freqTable:
if wordValue in sentence.lower():
if sentence in sentenceValue:
sentenceValue[sentence] += freqTable[wordValue]
else:
sentenceValue[sentence] = freqTable[wordValue]