词频统计
1·
学号:2017035107205
姓名:郑文龙
我的码云仓库:https://gitee.com/zhengwenlong205/word_frequency_count/tree/SE7205
1.读取文件到缓冲区
# filename: word_freq.py
# 阅读注释,在所有pass处删除pass,添加代码
# 阅读注释,在所有pass处删除pass,添加代码
from string import punctuation
def process_file(dst): # 读文件到缓冲区
try: # 打开文件
pass
except IOError, s:
print s
return None
try: # 读文件到缓冲区
pass
except:
print "Read File Error!"
return None
pass
return bvffer
2.统计每个单词的频率
def process_buffer(bvffer):
if bvffer:
word_freq = {}
# 下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq
pass
if bvffer:
word_freq = {}
# 下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq
pass
return word_freq
def output_result(word_freq):
if word_freq:
sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
for item in sorted_word_freq[:10]: # 输出 Top 10 的单词
print(item)
if word_freq:
sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
for item in sorted_word_freq[:10]: # 输出 Top 10 的单词
print(item)
3.输出Top10单词
def output_result(word_freq):
if word_freq:
sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
for item in sorted_word_freq[:10]: # 输出 Top 10 的单词
print(item)
if word_freq:
sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
for item in sorted_word_freq[:10]: # 输出 Top 10 的单词
print(item)
4.封装
![](https://img2018.cnblogs.com/blog/1626711/201903/1626711-20190328210202691-1495480655.png)