20200917-2 词频统计

 

此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11206]

词频统计 SPEC

Coding公开地址: https://e.coding.net/lhbat/word_count/word.git   git pull前需要git init初始化仓库

欢迎访问:https://lhbat.coding.net/public/word_count/word/git/files

功能1

 

难点:要对Python命令行进行解析,判断具体执行哪种操作。读入文件后,将单词统计出来。

#读入文件路径,去除特殊字符,用空格分开,返回strr字符串
def openfile(file_path):
    s = '.txt'
    if s not in file_path:
        file_path = file_path + s
    with open(file_path,'r',encoding="utf-8") as file:
        strr = file.read().lower()
    words = re.findall(r'[a-z0-9^-]+',strr)
    count(words)
def count(words):
   collect = collections.Counter(words)
   num = 0
   for i in collect:
      num += 1
   print('total %d words\n' % num)
   result = collect.most_common(10)
   for j in result:
      print('%-8s%5d' % (j[0], j[1]))

 

功能2

 

难点:判断要执行的参数,并将输入的文件名加上后缀才能打开。

if __name__ == '__main__':
    if sys.argv[1] == '-s':
        openfile(sys.argv[2])
    elif len(sys.argv) == 2 and os.path.isfile(sys.argv[1] + '.txt'):
        openfile(sys.argv[1] + '.txt')
    elif len(sys.argv) == 2 and os.path.isdir(sys.argv[1]):
        os.path.isdir(sys.argv[1])
        folderCount(sys.argv[1])
    else:
        openfile(sys.argv[1])

 

 

功能3

 

难点:macOS操作系统与Windows操作系统文件路径等操作方式有很大不同,后来完全使用Windows操作系统来实现,还有就是判断文件夹下的文件与如何读入这部分,查找很多资料才会的。

def folderCount(path):
    files = os.listdir(path)
    for file in files:
        print(file)
        openfile(path + '\\' +file)
        print('----')

 

功能4

菜鸟程序员,请教了学长不太懂,找时间自己一个人继续完成下去。

 

程序测试:

将代码打包成exe格式并进行测试。

代码及版本控制

https://lhbat.coding.net/public/word_count/word/git/files

PSP

项目 预计花费时间 实际花费时间 时间花费差距 原因分析
功能一 60min 207min 147min

准备工作不充分,不好好读题(要求最好用熟悉的语言),

文件读写等功能现学现卖,随大流(好多同学用的Python)

功能二 60min 78min 18min 功能二要求输入书名,初期想法错误,改了好长时间
功能三 60min 232min 172min

以为很容易,结果总是不能正确读取文件,中间放弃使用Python,

改用C++实现,今天上午发现快要来不及了,继续使用Python,

问同学解决了这个问题(原来还挺容易的)

功能四 120min     暂时未完成,害怕自己忘了交作业,先提交,下午继续完成功能
测试 60min  13min  -47min 将程序打包成exe可执行文件,以为很难,挺简单的。
posted @ 2020-09-23 14:49  lhbat  阅读(161)  评论(0编辑  收藏  举报