第二周词频统计
作业的要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2126
代码地址https://git.coding.net/xucs/cptj.git
功能一
实现小文件输入,先用open打开文件后,将字符替换成空格,大写字母转化为小写字母,然后将文本转化为列表。进行词频统计后排序输出
name = raw_input('enter the novel:') file = open(name) novel = file.read()
然后将字符替换成空格,大写字母转化为小写字母
for ch in ['~','!','@','#', '$','^','&','*',':','(',')','_','+','=','[',']','"','{','}',',','.','?',';','|','/']: novel = article.replace(ch, ' ') exchange = novel.lower()
然后将文本转化为列表。进行词频统计后排序
list = exchange.split() xs = {} for i in list: count = list.count(i) xs[i] = count xs1 = sorted(xs.items(), key=lambda d: d[1], reverse=True)
结果如下:

功能二
配置Python运行的环境变量后在cmd输入Python后空格再输入文件名即可

功能三
先调用三个模块
import os import os.path import glob
然后读取目录下的txt文件
elif path1 != '': os.chdir(path1) files = glob.glob('*.txt') for file in files: # name = os.path.basename(file) # path2 = os.path.abspath(name) novel = open(file) novel = novel.read()
运行之后没有结果,我没能实现这个功能。
功能四
从控制台输入英文单篇作品
当从article输入是使用功能四。
article = raw_input('enter the article:')
if article != '': for ch in ['~','!','@','#', '$','^','&','*',':','(',')','_','+','=','[',']','"','{','}',',','.','?',';','|','/']: novel = article.replace(ch, ' ') exchange = novel.lower()

 
psp表格
| 
 Psp阶段  | 
 估计时间  | 
 实际时间  | 
 时间差原因  | 
| 
 实现功能一  | 
 120分钟  | 
 207分钟  | 
 符号替换失误  | 
| 
 实现功能二  | 
 20分钟  | 
 413分钟  | 
 不会命令行  | 
| 
 实现功能三  | 
 100分钟  | 
 未能实现  | 
 不会读取文件  | 
| 
 实现功能四  | 
 20min  | 
 259min  | 
 编译错误  | 
                    
                
                
            
        
浙公网安备 33010602011771号