计算与软件工程作业四

感情迁移 提交于 2020-04-05 18:03:50
问题 回答
作业要求 第四周作业
课程目标 代码复审,结对编程
在哪方面帮我实现课程目标 能够帮助我更好的规范代码风格;通过合作学习提高自己的团队意识与操作能力
参考文献 (https://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html) (https://www.cnblogs.com/xinz/archive/2011/10/22/2220872.html)
gitee (https://gitee.com/yzzzw/Array)

作业1

每个人针对之前两次作业所写的代码,针对要求,并按照代码规范(风格规范、设计规范)要求评判其他学生的程序,同时进行代码复审(按照代码复审核表)
评价内容直接放在你被评价的作业后面评论中
同时另建立一个博客,将你作的评论的截图或者链接,放在博客中,并在你的博客中谈谈自己的总体看法
作业一链接:(https://www.cnblogs.com/yzzzw/p/12636544.html)

作业2

1、实现一个简单而完整的软件工具(中文文本文件人物统计程序):针对小说《红楼梦》要求能分析得出各个人物在每一个章回中各自出现的次数,将这些统计结果能写入到一个csv格式的文件。
2、进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。
3、进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。
4、使用源代码管理系统 (GitHub, Gitee, Coding.net, 等);
5、针对上述形成的软件程序,对于新的文本小说《水浒传》分析各个章节人物出现次数,来考察代码。
将上述程序开发结对编程过程记录到新的博客中,尤其是需要通过各种形式展现结对编程过程,并将程序获得的《红楼梦》与《水浒传》各个章节人物出现次数与全本人物出现总次数,通过柱状图、饼图、表格等形式展现。
《红楼梦》与《水浒传》的文本小说将会发到群里。
注意,要求能够分章节自动获得人物出现次数
队友链接:(https://www.cnblogs.com/tang-yuan-yuan/p/12637150.html)

python准备工作

先下载python,并安装好所需要的模块——jieba和matplotlib

python运行结果(红楼梦)

在python中按题目要求先将出现的高频词汇去除,再统计出红楼梦和水浒传人物出现的次数,并导入到csv文件中。

PSP

PSP各个阶段 预估时间 实际记录
计划:明确需求和其他因素,估计以下的各个任务需要多少时间 一小时 一天
开发
需求分析 一小时 三小时
生成设计文档 一小时 一小时
具体代码 五小时 五小时
测试 一小时 两小时
事后总结 两小时 两小时
总共花费的时间 十一小时 两天

代码

#统计红楼梦人物出现次数代码
print("红楼梦人物出场次数:")   
import jieba
import time
start=time.perf_counter()
txt=open("红楼梦.txt","r",encoding="utf-8").read()
excludes={"什么","一个","如今","人马","你们","我们","说道","知道","起来","今日","这里",        
          "什么","他们","两个","只见","如何","那里","姑娘","众人","这里","出来","奶奶","怎么","没有",
          "太太","这个","听见","这样","进来","三个","不得","如此","次日","只是","不知","不是","却是","不曾",
          "告诉","东西","就是","咱们","不敢","一面","自己","只得","正是","回来","老爷","大家","这些",
          "丫头","出去"}
words=jieba.lcut(txt)
counts={}
for word in words:
    if len(word)==1:       
        continue    
    elif word=="老太太":        
        rword="贾母"       
    else:        
        rword=word        
        counts[word]=counts.get(word,0)+1
for word in excludes:    
    del counts[word]
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):    
    word,count=items[i]    
    print("{0:<10}{1:>5}次".format(word,count))
dur=time.perf_counter()-startprint("运行时间为{:.2f}s".format(dur))
print("-----------------------------")

#红楼梦人物出现次数导入csv
#导入csv安装包
import csv

#1、创建文件对象
with open('HLMcsv.csv','w',newline="") as csvfile:

#2、基于文件对象构建csv写入对象
    writer=csv.writer(csvfile)

#3、构建列表头
    writer.writerow(["人物","出现次数"])

#4、写入csv文件内容
    writer.writerow(["宝玉","3445"])
    writer.writerow(["贾母","1144"])
    writer.writerow(["凤姐","1038"])
    writer.writerow(["王夫人","969"])
    writer.writerow(["贾琏","671"])
    writer.writerow(["平儿","585"])
    writer.writerow(["袭人","551"])
    writer.writerow(["黛玉","540"])
    writer.writerow(["宝钗","521"])
    writer.writerow(["薛姨妈","455"])

#红楼梦树状图与饼状图
import jieba
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np


class HlmNameCount():
    # 此函数用于绘制条形图
    def showNameBar(self,name_list_sort,name_list_count):
        # x代表条形数量
        x = np.arange(len(name_list_sort))
        # 处理中文乱码
        plt.rcParams['font.sans-serif'] = ['SimHei']
        # 绘制条形图,bars相当于句柄
        bars = plt.bar(x,name_list_count)
        # 给各条形打上标签
        plt.xticks(x,name_list_sort)
        # 显示各条形具体数量
        i = 0
        for bar in bars:
            plt.text((bar.get_x() + bar.get_width() / 2), bar.get_height(), '%d' % name_list_count[i], ha='center', va='bottom')
            i += 1
        # 显示图形
        plt.show()

    # 此函数用于绘制饼状图
    def showNamePie(self, name_list_sort, name_list_fracs):
        # 处理中文乱码
        plt.rcParams['font.sans-serif'] = ['SimHei']
        # 绘制饼状图
        plt.pie(name_list_fracs, labels=name_list_sort, autopct='%1.2f%%', shadow=True)
        # 显示图形
        plt.show()

    def getNameTimesSort(self,name_list,txt_path):
        # 将所有人名临时添加到jieba所用字典,以使jieba能识别所有人名
        for k in name_list:
            jieba.add_word(k)
        # 打开并读取txt文件
        file_obj = open(txt_path, 'rb').read()
        # jieba分词
        jieba_cut = jieba.cut(file_obj)
        # Counter重新组装以方便读取
        book_counter = Counter(jieba_cut)
        # 人名列表,因为要处理凤姐所以不直接用name_list
        name_dict ={}
        # 人名出现的总次数,用于后边计算百分比
        name_total_count = 0
        for k in name_list:
            if k == '熙凤':
                # 将熙凤出现的次数合并到凤姐
                name_dict['凤姐'] += book_counter[k]
            else:
                name_dict[k] = book_counter[k]
            name_total_count += book_counter[k]
        # Counter重新组装以使用most_common排序
        name_counter = Counter(name_dict)
        # 按出现次数排序后的人名列表
        name_list_sort = []
        # 按出现次数排序后的人名百分比列表
        name_list_fracs = []
        # 按出现次数排序后的人名次数列表
        name_list_count = []
        for k,v in name_counter.most_common():
            name_list_sort.append(k)
            name_list_fracs.append(round(v/name_total_count,2)*100)
            name_list_count.append(v)
            # print(k+':'+str(v))
        # 绘制条形图
        self.showNameBar(name_list_sort, name_list_count)
        # 绘制饼状图
        self.showNamePie(name_list_sort,name_list_fracs)
        

if __name__ == '__main__':
    # 参与统计的人名列表,可修改成自己想要的列表
    name_list = ['宝玉', '贾母', '凤姐', '王夫人', '贾琏', '平儿', '袭人', '黛玉', '宝钗', '薛姨妈']
    # 红楼梦txt文件所在路径,修改成自己文件所在路径
    txt_path = 'F:/Python/红楼梦.txt'
    hnc = HlmNameCount()
    hnc.getNameTimesSort(name_list,txt_path)

#水浒传人物出现次数
print("水浒传人物出场次数:")   
import jieba
import time
start=time.perf_counter()
txt=open("水浒传.txt","r",encoding="utf-8").read()
excludes={"二人","一个","来到","人马","你们","我们","好汉","军马","小人","今日","这个","先锋","宋江道",        
          "知府","什么","他们","银子","梁山","两个","只见","如何","那里","说道","众人","这里","兄弟","出来",
          "妇人","便是","起来","问道","因此","三个","不得","如此","次日","只是","不知","不是","却是","呼延","不曾",
          "梁山泊","一面","且说","看时","不敢","如今","原来","将军","山寨","只得","正是","喝道","一齐","当下",
          "兄长"}
words=jieba.lcut(txt)
counts={}
for word in words:
    if len(word)==1:       
        continue    
    elif word=="哥哥":        
        rword="宋江"    
    elif word=="头领":        
        rword="林冲"    
    else:        
        rword=word        
        counts[word]=counts.get(word,0)+1
for word in excludes:    
    del counts[word]
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):    
    word,count=items[i]    
    print("{0:<10}{1:>5}次".format(word,count))
dur=time.perf_counter()-startprint("运行时间为{:.2f}s".format(dur))
print("-----------------------------")

#水浒传任务出现次数导入csv
#导入csv安装包
import csv

#1、创建文件对象
with open('SHZcsv.csv','w',newline="") as csvfile:

#2、基于文件对象构建csv写入对象
    writer=csv.writer(csvfile)

#3、构建列表头
    writer.writerow(["人物","出现次数"])

#4、写入csv文件内容
    writer.writerow(["宋江","2447"])
    writer.writerow(["李逵","1093"])
    writer.writerow(["武松","1027"])
    writer.writerow(["林冲","693"])
    writer.writerow(["吴用","626"])
    writer.writerow(["卢俊义","547"])
    writer.writerow(["鲁智深","325"])
    writer.writerow(["柴进","305"])
    writer.writerow(["戴宗","301"])
    writer.writerow(["公孙胜","272"])

#水浒传树状图和饼状图
import jieba
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np


class HlmNameCount():
    # 此函数用于绘制条形图
    def showNameBar(self,name_list_sort,name_list_count):
        # x代表条形数量
        x = np.arange(len(name_list_sort))
        # 处理中文乱码
        plt.rcParams['font.sans-serif'] = ['SimHei']
        # 绘制条形图,bars相当于句柄
        bars = plt.bar(x,name_list_count)
        # 给各条形打上标签
        plt.xticks(x,name_list_sort)
        # 显示各条形具体数量
        i = 0
        for bar in bars:
            plt.text((bar.get_x() + bar.get_width() / 2), bar.get_height(), '%d' % name_list_count[i], ha='center', va='bottom')
            i += 1
        # 显示图形
        plt.show()

    # 此函数用于绘制饼状图
    def showNamePie(self, name_list_sort, name_list_fracs):
        # 处理中文乱码
        plt.rcParams['font.sans-serif'] = ['SimHei']
        # 绘制饼状图
        plt.pie(name_list_fracs, labels=name_list_sort, autopct='%1.2f%%', shadow=True)
        # 显示图形
        plt.show()

    def getNameTimesSort(self,name_list,txt_path):
        # 将所有人名临时添加到jieba所用字典,以使jieba能识别所有人名
        for k in name_list:
            jieba.add_word(k)
        # 打开并读取txt文件
        file_obj = open(txt_path, 'rb').read()
        # jieba分词
        jieba_cut = jieba.cut(file_obj)
        # Counter重新组装以方便读取
        book_counter = Counter(jieba_cut)
        # 人名列表,因为要处理宋江所以不直接用name_list
        name_dict ={}
        # 人名出现的总次数,用于后边计算百分比
        name_total_count = 0
        for k in name_list:
            if k == '哥哥':
                name_dict['宋江'] += book_counter[k]
            else:
                name_dict[k] = book_counter[k]
            name_total_count += book_counter[k]
        # Counter重新组装以使用most_common排序
        name_counter = Counter(name_dict)
        # 按出现次数排序后的人名列表
        name_list_sort = []
        # 按出现次数排序后的人名百分比列表
        name_list_fracs = []
        # 按出现次数排序后的人名次数列表
        name_list_count = []
        for k,v in name_counter.most_common():
            name_list_sort.append(k)
            name_list_fracs.append(round(v/name_total_count,2)*100)
            name_list_count.append(v)
            # print(k+':'+str(v))
        # 绘制条形图
        self.showNameBar(name_list_sort, name_list_count)
        # 绘制饼状图
        self.showNamePie(name_list_sort,name_list_fracs)
        

if __name__ == '__main__':
    # 参与统计的人名列表,可修改成自己想要的列表
    name_list = ['宋江', '李逵', '武松', '林冲', '吴用', '卢俊义', '鲁智深', '柴进', '戴宗', '公孙胜']
    # 水浒传txt文件所在路径,修改成自己文件所在路径
    txt_path = 'F:/Python/水浒传.txt'
    hnc = HlmNameCount()
    hnc.getNameTimesSort(name_list,txt_path)


运行结果(水浒传)




上传到码云


码云链接:(https://gitee.com/yzzzw/Array)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!