一:基本信息
1.1本次作业的地址:https://edu.cnblogs.com/campus/ntu/Embedded_Application/homework/2088
1.2项目git的地址:https://gitee.com/ntucs/PairProg
1.3结队成员:李赟波 1613072025
江文浩 1613072024
1.4目录
1.5例子
二:项目分析
2.1 程序运行模块(方法、函数)介绍
①任务一:读取文件、统计行数写入result.txt方法
1 def process_file(dst): # 读文件到缓冲区并统计非空白行数
2 global count
3 try: # 打开文件
4 file=open(dst,‘r‘,encoding=‘ISO-8859-1‘)
5 file1= open(dst, ‘r‘, encoding=‘ISO-8859-1‘)
6 except IOError as s:
7 print(‘IOError‘,s)
8 return None
9 try: # 读文件到缓冲区
10 bvffer=file.read()
11 except:
12 print("Read File Error!")
13 return None
14 lines=file1.readlines()
15 print(lines)
16 for line in lines:
17 # line=line.replace(‘\n‘,‘ ‘)
18 line=line.strip()#去除每一行中的开头和结尾空白字符
19 # print(line)
20 # line.strip(‘\n‘)
21 # line.strip(‘\t‘)
22 #
23
24 if len(line)!=0:
25 count+=1
26 else:
27 continue
28 file.close()
29 file1.close()
30 return bvffer
②任务一:使用正则表达式统计词频,存放如字典模块
1 def process_buffer(bvffer):#使用正则表达式并统计词频
2 if bvffer:
3 word_freq = {}
4 # 下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq
5 content=bvffer.lower()
6 content = content.replace(‘\n‘, ‘ ‘)
7 regex=re.compile(‘[^a-zA-Z0-9\s]‘)
8 content_result=regex.sub(‘‘,content)
9 content_result.lower()
10 # [a-zA-z]{4,}[a-zA-Z0-9]{0,}
11 content_result=content_result.split(‘ ‘)
12 while ‘‘ in content_result:
13 content_result.remove(‘‘)
14 print(content_result)
15 for word in content_result:
16 if word in list_stopwords:
17 continue
18 else:
19 # print(re.match(‘[a-zA-z]{4,}[a-zA-Z0-9]{0,}‘,word,re.I))
20 if re.match(‘[a-zA-z]{4,}[a-zA-Z0-9]{0,}‘,word,re.I)!=None:
21 if word_freq.__contains__(word):
22 word_freq[word] = word_freq[word] + 1
23 else:
24 word_freq[word] = 1
25 else:
26 continue
27 return word_freq
③任务一:对单词出现次数进行排序输出
1 def output_result(word_freq):#按要求输出结果至文件
2 doc=open(‘result.txt‘,‘w‘)
3 if word_freq:
4 print(‘lines‘,count,file=doc)
5 print(‘words:‘ + str(word_freq.__len__()),file=doc)
6 for word in word_freq[:10]:#输出字典前十个
7 # print(str(word),‘:‘,word_freq[word]
8 print(word[0],‘:‘,word[1],file=doc)
9 doc.close()
④任务一:排序
1 def dic_sorted(dict):
2 return sorted(dict.items(), key=lambda x: x[1], reverse=True)#给字典按照从大到小的顺序排序
⑤任务一:主函数
1 if __name__ == "__main__":
2 parser = argparse.ArgumentParser()
3 parser.add_argument(‘dst‘)
4 args = parser.parse_args()
5 dst = args.dst
6 bvffer = process_file(dst)
7 word_freq = process_buffer(bvffer)
8 word_freq=dic_sorted(word_freq)
9 print(word_freq)
10 output_result(word_freq)
任务二:停词表模块
功能实现方法:使用 nltk(Natural Language Toolkit,自然语言处理工具包,在NLP领域中,最常使用的一个Python库。)下载英文停词表,存放到list_stopWords集合中,接着对将要处理的英文单词进行判断是否与list_stopWords中的词汇相等,如果相等则跳过,即停词功能。
1 def load_stopwords(): #加载停词表
2 global list_stopwords
3 file_stopwords=open(‘stopwords.txt‘,‘r‘,encoding=‘utf-8‘)
4 content=file_stopwords.readlines()
5 for word in content:
6 list_stopwords.append(word)#将停词表表中的单词添加到list_stopwords列表中
2.2 程序算法的时间、空间复杂度分析
对文本单词列表进行遍历放入字典中,时间复杂度为O(n),空间复杂度为O(n)。
2.3 程序运行案例截图
三、性能分析
1、单词的运行时间为0.005秒。
四、其他
4.1 结队编程时间开销
每天1小时,大概一周左右完成全部功能。主要时间开销分两个部分:查阅文档、结队编程。大体分工查阅技术文档,交流讨论。对各个技术方式实践结队编程最后选择最合适的方案。
4.2 结队编程照片
五、事后分析与总结
1、 简述结对编程时,针对某个问题的讨论决策过程
读文件file对象因为没有关闭没法继续进行行数统计,所以引入了另一个文件对象file1。
2、评价
(1)江文浩对李赟波的评价:李赟波同学个人能力非常优秀,能够灵活运用已经学习的各门专业知识,在代码编写与调试方面也非常熟练。期待与他下一次的合作。
(2)李赟波对江文浩的评价:虽然编程能力方面不足但积极查阅资料,为项目做出贡献。
3、关于结对过程的建议
结对编程能够增强我们的合作能力,能够及时发现一些问题,而不是自己一个人困在一个问题里面出不来,合作更有利于发现问题和解决问题,还能够提供不同的思路和解决方法,合作是开发项目的非常重要的一个环节。
4 、其它:
希望下次还能有一起合作完成别的语言项目的机会。
原文:https://www.cnblogs.com/jwh24/p/9824189.html