def ngrams(n, f):
    deque = collections.deque(maxlen=n)
for line in f:
    deque.clear()
words = ['<s>'] + line.split() + ['</s>']
deque.extend(words[:n - 1])
for word in words[n - 1:]:
    deque.append(word)
yield tuple(str(w) for w in deque)
counters = [collections.Counter(ngrams(i, open('somefile.txt'))) for i in
    range(5)]
Effective 1-5 grams extraction with python