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.