Why do I get an IndexError?

Question:

I am trying to get trigrams out of a sentence and save them in a dictionary, with their frequenz as value.
I wrote this:

trigrams = {}
sentence = ["What", "is", "happening", "right", "now"]

for word in sentence:
      if word != sentence[-1] or sentence[-2] and tuple((word, sentence[sentence.index(word) +1], sentence[sentence.index(word) +2])) not in trigrams:
             trigrams.update({tuple((word, sentence[sentence.index(word) +1], sentence[sentence.index(word) +2])):1})

Should look like this:
(“what”,”is”,”happening”):1
(“is”,”happening”,”right”):1
etc

But now I am keep getting an IndexError in the update-line.

Asked By: spiderkitty

||

Answers:

You can use lists as your tuples’ contents are all of the same datatype (string)

It’s probably easier to do:

trigrams = []
sentence = ["What", "is", "happening", "right", "now"]

for i in range(2,len(sentence)):
    trigrams.append([sentence[i-2],sentence[i-1],sentence[i]])
Answered By: Ben Stobbs

I guess if word != sentence[-1] or sentence[-2] is not what you want. Do you mean if word != sentence[-1] and word != sentence[-2], meaning word does not equal either sentence[-1] nor sentence[-2]?

Answered By: jmd_dk

Given you would like to keep your code structure with the tuple and change minimally your code, you can do this (not saying this might be a good approach for your problem, etc.):

trigrams = {}
sentence = ["What", "is", "happening", "right", "now"]

for index, word in enumerate(sentence):
    print index, word  # to understand how the iteration goes on
    if index < len(sentence)-2:
        if tuple((word, sentence[index+1], sentence[index+2])) not in trigrams:
            trigrams.update({tuple((word, sentence[index+1], sentence[index+2])):1})

You were getting an index error because you were accessing an element that didn’t exist in tuple()… because the way you were doing the checking to see if were near the end of the list (the last two elements) wasn’t done right.

The code you were using:

if word != sentence[-1] or sentence[-2]

is not right and you were comparing strings eventually and not the indexes, which is what is important here! Compare the indexes, not the values at those positions.

Answered By: fedepad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.