第三次作业

 

 

 

1) 博客开头给出自己的基本信息,格式建议如下:
20170****7020;
白京鑫
码云:https://gitee.com/zerois0/word_frequency/tree/SE7005

2) 程序分析,对程序中的四个函数做简要说明。要求附上每一段代码及对应的说明。

  1. def process_file(dst): # 读文件到缓冲区
    try: # 打开文件
    p = open(dst,"r")
    except IOError as s:
    print (s)
    return None
    try: # 读文件到缓冲区
    bvffer = txt.read();
    except:
    print ("Read File Error!")
    return None
    txt.close()
    2)
    def process_buffer(bvffer):
    if bvffer:
    word_freq = {}
    下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq
    for ch in'"!?;,."':
    bvffer = bvffer.lower().replace(ch,"")
    word = bvffer.strip().split()
    for word in word:
    word_freq[word] = word_freq.get(word,0)+1
    return word_freq

输出前10单词

  1. 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)

  2. import argparse 导入argparse文件 并执行函数

3) 性能分析结果及改进。
指出执行次数最多的代码,执行时间最长的代码。
次数最多;strip 时间最长:if word in word_freq.keys():

给出改进优化的方法以及你的改进代码
把key 去掉 if word in word_freq:

4) 程序运行命令、运行结果截图以及改进后的程序运行命令及结果截图

 

 

结果

 

 

5) 给出你对此次任务的总结与反思。
一次很艰难的任务,

posted @ 2019-04-08 13:59  12121q  阅读(148)  评论(2)    收藏  举报