词频统计及其效能分析

---恢复内容开始---

1)

  • 学号:2017*****1027;
  • 姓名:王益鑫;
  • 码云仓库地址:https://gitee.com/shirt----2580/word_frequency;

2) 程序分析

  1、 打开并读取文件

 

【2、添加处理 bvffer代码,统计单词的频率,存放在word_freq

 

def process_buffer(bvffer):
if bvffer:
word_freq = {}
# 下面添加处理 bvffer代码,统计单词的频率,存放在word_freq
for item in bvffer.strip().split():
word = item.strip(punctuation+' ')
if word in word_freq.keys():
word_freq[word] += 1
else:
word_freq[word] = 1
return word_freq

 

3、设置输出函数,进行排序并输出Top 10 的单词

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)

4、利用main方法输出


if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('dst')
args = parser.parse_args()
dst = args.dst
bvffer = process_file(dst)
word_freq = process_buffer(bvffer)
output_result(word_freq)                    】由于电脑原因没有截图

3) 性能分析结果及改进。

程序运行命令、运行结果截图

用命令python word_freq.py Gone_with_the_wind.txt运行:

 

调用最多312次

最长用时0.003

 4) 程序运行命令、运行结果截图以及改进后的程序运行命令及结果截图 。电脑挺快的

5) 给出你对此次任务的总结与反思。

学习到了词频统计这个程序以及相关的一些代码,让我对Python这一编程语言了解的更多一点

---恢复内容结束---

posted @ 2019-04-08 15:02  渐如枯枝  阅读(247)  评论(1编辑  收藏  举报