作业词频统计——基本功能

一、基本信息

作业地址:https://edu.cnblogs.com/campus/ntu/Embedded_Application/homework/2088

项目git地址:https://gitee.com/ntucs/PairProg/tree/SE032_033/

结对成员:1613072032 赵亦明

                  1613072033 王楠楠

二、项目分析

2.1 程序运行模块(方法、函数)介绍

2.2.1 读取文件到缓冲区

 1 def process_file(dst):  # 读文件到缓冲区
 2     try:  # 打开文件s
 3         d = open(dst, "r")
 4     except IOError as s:
 5         print(s)
 6         return None
 7     try:  # 读文件到缓冲区
 8         bvffer = d.read()
 9     except:
10         print('Read File Error!')
11         return None
12     d.close()
13     return bvffer

 

2.1.2 统计文件的有效行数

 1 def process_rowCount(bvffer):  # 计算文章的行数
 2     if bvffer:
 3         count = 1
 4         for word in bvffer:  # 开始计数
 5             if word == '\n':
 6                 count = count + 1
 7         print("lines:{:}".format(count))
 8         f = open('result.txt', 'w')
 9         print("lines:{:}".format(count),file=f)
10         f.close()

2.1.3 统计文件的单词总数

 1 def process_wordNumber(words):  
 2     if words:
 3         wordNew = []
 4         words_select = '[a-z]{4}(\w)*'
 5         for i in range(len(words)):
 6             word = re.match(words_select, words[i])  # 如果不匹配,返回NULL类型
 7             if word:
 8                 wordNew.append(word.group())
 9         print("words:{:}".format(len(wordNew)))
10         f = open('result.txt', 'a')
11         print("words:{:}".format(len(wordNew)),file=f)
12         f.close()
13         return wordNew

 

2.1.4 输出频率最高的前10个词组

1 def output_result(word_freq):
2     if word_freq:
3         sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
4         for item in sorted_word_freq[:10]:                 # 输出 Top 10 的单词
5             print("词:%-5s 频:%-4d " % (item[0], item[1]))

 

2.1.5 停用词模块

 1 def process_twoPhrase(words):
 2     useless_twoPhrase =['they were','would have','there were','have been','that would']
 3     words_group = []
 4     for i in range(len(words) - 1):
 5         str = '%s %s' % (words[i], words[i + 1])
 6         words_group.append(str)
 7     word_freq = {}
 8     for word in words_group:
 9         if word in useless_twoPhrase:
10             continue
11         else:
12             word_freq[word] = word_freq.get(word, 0) + 1  # 将词组进行计数统计
13     return word_freq
14 
15 
16 def process_threePhrase(words):
17     words_group = []
18     for i in range(len(words) - 2):
19         str = '%s %s %s' % (words[i], words[i + 1], words[i + 2])
20         words_group.append(str)
21     word_freq = {}
22     for word in words_group:
23         word_freq[word] = word_freq.get(word, 0) + 1  # 将词组进行计数统计
24     return word_freq

 

2.1.6 高频词组模块

1 def output_result(word_freq):
2     if word_freq:
3         sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
4         for item in sorted_word_freq[:10]:  # 输出 Top 10 频率高的
5             print("<{:}>:{:}".format(item[0], item[1]))
6             f = open('result.txt','a')
7             print("<{:}>:{:}".format(item[0], item[1]), file=f)
8             f.close()

 

2.1.7 主函数 

 1 if __name__ == "__main__":
 2     # 把分析结果保存到文件中
 3     cProfile.run("main()", filename="result.wordcount")
 4     p = pstats.Stats("result.wordcount")
 5     p.strip_dirs().sort_stats("calls").print_stats(10)
 6     p.strip_dirs().sort_stats("cumulative", "name").print_stats(10)
 7     p.print_callers(0.5, "process_transform")
 8     p.print_callers(0.5, "process_rowCount")
 9     p.print_callers(0.5, "process_wordNumber")
10     p.print_callers(0.5, "process_stopwordSelect")
11     p.print_callers(0.5, "process_twoPhrase")
12     p.print_callers(0.5, "process_threePhrase")
13     p.print_callers(0.5, "output_result")
14     p.print_callees("process_buffer")

 

2.2 程序算法的时间、空间复杂度分析

   假设停词表文件有N个单词,待分析的文本单词集合有n个单词,根据两个for循环分析,则该模块的时间复杂度大概为O(N*n),又根据操作系统的空间内存重复调用可知,该模块的时间复杂度经优化后应该小于O(N*n)。

 

2.3 程序运行案例截图

2.3.1 task1

 

2.3.2 task2

 

 

 

三、性能分析

3.1 运行时间

 

3.2 性能图表

四、其他

4.1 结对编程时间开销

   利用课余时间完成全部内容。团队成员共同讨论,查阅资料,完成代码部分的编写。同时,讨论代码中不完善的内容。

4.2 结对编程照片

五、事后分析与总结

5.1 简述结对编程时,针对某个问题的讨论决策过程

  • 明确任务和分工。
  • 针对需要完成功能,分享各自的想法,同时,进行对比,选取简单明晰或者性能好一点的方法。
  • 完成基本代码编写后,再根据任务要求,检查运行结果,完善优化代码。

5.2 评价对方

  王楠楠评价赵亦明:很有想法,动手能力强,认真负责,会主动交流想法。遇到问题时能一起分析并解决,也会认真听取他人的意见。

  赵亦明评价王楠楠:学习能力很强,遇到问题时思路清晰,遇到不懂的会积极主动的查阅资料。

5.3 评价整个过程

   整个过程是非常顺利和愉快的,过程大概经历一个星期左右,在确定过基本任务后,编写代码过程中,王楠楠提供想法,然后结合赵亦明的想法,最后由赵亦明编写代码,然后王楠楠最后再阅读检查,出现错误时,两个人都会交流想法,不断尝试,寻找解决办法,共同解决问题。平时也会一起交流想法,所以此次结对编程更加深了彼此的默契,同时也在结对过程中能够对比学习到彼此的优缺点,是一次很好的成长。

5.4 其他

   结对编码是一个很好的过程,通过团队合作完成整个项目,可以避免单人完成时出现的考虑问题的片面性。

猜你喜欢

转载自www.cnblogs.com/athens026/p/9823446.html
今日推荐