from nltk.util import ngrams
import collections
with open('text.txt', 'rU') as f:
    sixgrams = ngrams(f.read().decode('utf8').split(), 2)
result = collections.Counter(sixgrams)
print(result)
with open('output.txt', 'w') as f:
    for item, count in sorted(result.items()):
        if count >= 2:
            text = '{} {}'.format(' '.join(item).encode('utf8'), count)
print(text)
print(text, file=f)
Частота подсчета энграмм