第三次作业
1.个人信息
学号:2017xxxxx7201
姓名:杨清魁
仓库地址:https://gitee.com/Ywqfkr520/projects
2.程序分析
(1)读取文件到缓冲区
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
bvffer=bvffer.lower()
for x in '~!@#$%^&*()_+/*-+\][':
bvffer=bvffer.replace(x, " ")
words=bvffer.strip().split()
for word in words:
word_freq[word]=word_freq.get(word,0)+1
return word_freq
(3)输出频率前十的单词
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)执行函数
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)
5)运行结果
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。3.性能分析及改进
(1)共有457891次函数调用,程序总共耗时0.248秒
2)执行次数最多
(3)执行时间耗时最长
5.总结
1.通过作业熟悉了词频统计及效能分析的具体操作和含义
2.这次作业让我知道了怎样去分析代码脚本的意义,加强自己的分析能力。
3.通过三次作业让我知道在Python开发语言中的不足,现在在课余时间不断的学习。

更多精彩