I have a list of sentences:
text = [\'cant railway station\',\'citadel hotel\',\' police stn\'].
I need to form bigram pairs and store the
I think the best and most general way to do it is the following:
n = 2
ngrams = []
for l in L:
for i in range(n,len(l)+1):
ngrams.append(l[i-n:i])
or in other words:
ngrams = [ l[i-n:i] for l in L for i in range(n,len(l)+1) ]
This should work for any n
and any sequence l
. If there are no ngrams of length n
it returns the empty list.