Hi I am trying to generate n and n-1 grams and to compute the probabilities of the ngrams. However, the n-1 grams generated is not taking the last element of each sublist. Can somebody help me figure out where I am going wrong.
Input:
input1 = [['A', 'B', 'C', 'D', 'E'],
['D', 'E', 'C', 'D', 'E'],
['A', 'C', 'D', 'D']]
for line in input_text:
for i in range (len(line)-n+1):
g = ' '.join(line[i:i+n])
ngram.setdefault(g, 0)
ngram[g] += 1
h = ' '.join(line[i:i+n-1])
history.setdefault(h, 0)
history[h] +=1
The output of the n-1 grams i.e. history is as follow: {'D': 4, 'A': 2, 'C': 3, 'B': 1, 'E': 1}
However, it should be {'D': 4, 'A': 2, 'C': 3, 'B': 1, 'E': 3}
Can someone help me to debug this. Thanks