首页 > 其他 > 详细

20200917-2 词频统计

时间:2020-09-23 15:47:33      阅读:46      评论:0      收藏:0      [点我收藏+]

词频统计 SPEC

Coding公开地址: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

快了快了,先交作业,晚上应该能完成。

代码及版本控制

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     暂时未完成,害怕自己忘了交作业,先提交,下午继续完成功能

20200917-2 词频统计

原文:https://www.cnblogs.com/lhbat/p/13718346.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!