第三次作业呀

第三次作业呀
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说第三次作业呀,希望能够帮助大家进步!!!

杨昀昊
2017*****1008
https://gitee.com/yyh0322/DiSanCiZuoYeYa/commit/b68a3a0dc8f1ddb6aafed2ab6883c892cf7fed2e
程序分析
读文件到缓冲区
def process_file(dst):
try: # 打开文件
l = open(dst, 'r')
except IOError as s:
print (s)
return None
try: # 读文件到缓冲区
bvffer=l.read()
except:
print ("Read File Error!")
return None
l.close()

return bvffer
统计单词频率
def process_buffer(bvffer):
if bvffer:
word_freq = {}

下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq

for i in bvffer.split():
word1 = i.strip(punctuation + " ")
if word1 in word_freq:
word_freq[word1] += 1
else:
word_freq[word1] = 1
return word_freq

输出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 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)

性能分析
执行次数最多:word1 = i.strip(punctuation + " ")
执行时间最长:ocess_buffer(bvffer):函数

程序运行结果截图
1627302-20190404105804182-1817615839.png
1627302-20190404105827307-472417275.png
1627302-20190404105838043-2146508212.png

转载于:https://www.cnblogs.com/YYH0322/p/10653649.html

本文来源weixin_30595035,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/29921

发表评论