2022(春)软工作业2:个人编程练习

李灏0 2022-05-25 19:18:17

1.程序分析,对程序中的四个函数做简要说明

 
  1. # 读文件到缓冲区

  2. def process_file(dst):

  3. try: # 打开文件

  4. f = open(dst, 'r') # dst为文本的目录路径

  5. except IOError as s:

  6. print(s)

  7. return None

  8. try: # 读文件到缓冲区

  9. bvffer = f.read()

  10. except:

  11. print('Read File Error!')

  12. return None

  13. f.close()

  14. return bvffer

 
  1. # 统计词频函数

  2. def process_buffer(bvffer):

  3. if bvffer:

  4. new_bvffer = re.sub(r'[^A-Za-z]', ' ', bvffer) # 使用正则表达式把除了字母和空格以外的符号都去除

  5. words = new_bvffer.split()

  6. word_freqs = {}

  7. for word in words:

  8. if word.lower() in word_freqs:

  9. word_freqs[word.lower()] = word_freqs[word.lower()] + 1

  10. else:

  11. word_freqs[word.lower()] = 1

  12. return word_freqs

 

 
  1. # 输出结果

  2. def output_result(word_freq):

  3. if word_freq:

  4. sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)

  5. for item in sorted_word_freq[:10]: # 输出 Top 10 的单词

  6. print(item)

 
  1. #函数调用

  2.  
  3.  
  4. def main():

  5. dst = "C:\Users\hp\Documents\Tencent Files\2190820592\FileRecv\Gone_with_the_wind.txt

  6. "

  7. bvffer = process_file(dst)

  8. word_freq = process_buffer(bvffer)

  9. output_result(word_freq)

2.代码风格说明。

   一个缩进级别四个空格。

  (1). 连续行使用两种方式使封装元素成为一行:括号内垂直隐式连接 & 悬挂式缩进。 使用悬挂式缩进应该注意第一行不应  该 有参数,连续行要使用进一步的缩进来区分。

 
  1. # 括号内隐式连接,垂直对齐

  2.  
  3. foo = long_function_name(var_one, var_two,

  4.  
  5. var_three, var_four)

 
  1. # 悬挂缩进,进一步缩进区分其他语句def long_function_name(

  2.  
  3. var_one, var_two, var_three,

  4.  
  5. var_four):

  6.  
  7. print(var_one)

  8.  
  9. # 悬挂缩进,一般是四个空格,但非必须

  10.  
  11. foo = long_function_name(

  12.  
  13. var_one, var_two,

  14.  
  15. var_three, var_four)

  16.  
  17.  
  18.  
  19. 否:

  20.  
  21. # 括号内隐式连接,没有垂直对齐时,第一行的参数被禁止

  22.  
  23. foo = long_function_name(var_one, var_two,

  24.  
  25. var_three, var_four)

  26.  
  27. # 悬挂缩进,需要进一步的缩进区分其他行def long_function_name(

  28.  
  29. var_one, var_two, var_three,

  30.  
  31. var_four):

  32.  
  33. print(var_one)

  34.  
  35.  
  36. 2.当 if 语句过长时,可选的处理方式,但不限于此:

  37.  
  38. # 不使用额外缩进if (this_is_one_thing and

  39.  
  40. that_is_another_thing):

  41.  
  42. do_something()

 
  1.  
  2. # 增加注释区分,支持语法高亮if (this_is_one_thing and

  3.  
  4. that_is_another_thing):

  5.  
  6. # Since both conditions are true, we can frobnicate.

  7.  
  8. do_something()

  9.  
  10. # 条件连续行额外缩进if (this_is_one_thing

  11.  
  12. and that_is_another_thing):

  13.  
  14. do_something()

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

 

 4.性能分析结果及改进

     运行次数最多的代码

 sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)

运行时间最长的代码

 
  1. def process_buffer(bvffer):

  2. if bvffer:

  3. word_freq = {} # 新建一个空字典word_freq

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

  5. for word in bvffer.split(): # .split()函数将bvffer切片

  6. if word not in word_freq:

  7. word_freq[word] = 0

  8. word_freq[word] += 1

  9. return word_freq

存在的问题

句首的大写单词被当作新单词,应该改写process_buffer,使用.lower()将句首的大写字母改为小写,同时去除文本中的中英文标点符号

 
  1. def process_buffer(bvffer): # 处理缓冲区,返回存放每个单词频率的字典word_freq

  2. if bvffer:

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

  4. word_freq = {}

  5. # 将文本内容都改为小写且去除文本中的中英文标点符号

  6. for ch in '“‘!;,.?”':

  7. bvffer = bvffer.lower().replace(ch, " ")

  8. # strip()删除空白符(包括'/n', '/r','/t');split()以空格分割字符串

  9. words = bvffer.strip().split()

  10. for word in words:

  11. word_freq[word] = word_freq.get(word, 0) + 1

  12. return word_freq

通过cprofile性能评估可知,调用次数、执行时间最多的部分代码是process_buffer函数部分。

...全文
94 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

197

社区成员

发帖
与我相关
我的任务
社区描述
用于软件工程专业的课程教学,讲义、视频、资料、问题讨论 鞠小林老师、蒋峥峥老师,欢迎您的到来!
社区管理员
  • juking@ntu
  • qinzuibaozi
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

只讨论科学和技术问题

试试用AI创作助手写篇文章吧