急求一个python程序可以统计一个小的txt文档里面英文单词最多出现的6个单词,显示它们频率

并且统计一个单词出现情况下,紧跟的那个单词出现的两个词形成的词组出现的概率,并且显示它们的最高概率的6个词组按大小排列

import collections
import re
#匹配单词排名

patt = re.compile("\w+")
counter = collections.Counter(patt.findall(
open('a.txt','r').read()
))

# top 6

for word, times in counter.most_common(6):
print word, times

#匹配词组排名
patt = re.compile("\w+\s\w+")
counter = collections.Counter(patt.findall(
open('a.txt','r').read()
))

# top 6

for word, times in counter.most_common(6):
print word, times
温馨提示:答案为网友推荐,仅供参考
相似回答