python 统计一个目录中每个txt文件最重要的的词
2017-08-06 09:58
519 查看
Python 练习册,每天一个小程序
第 0006 题: 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。0000-0010题链接
代码如下:
import collections import os.path def judgeit(words): for i in range(6): if len(words[i]) > 2 and words[i] != 'the' and words[i] != 'her' and words[i] != 'his' and words[i] != 'and' and words[i] != 'she': return words[i] return words[7] def mainKeywords(dirPath): f_list = os.listdir(dirPath) for i in f_list: if os.path.splitext(i)[1] == '.txt': print('the keywords of' + i + ' is:' ) with open(i, 'r') as fp: str1 = fp.read().split(' ') b = collections.Counter(str1) keywords = sorted(b, key=lambda x: b[x],reverse = True) print(judgeit(keywords)) mainKeywords('D:\PyCharm 2017.1.3\projects')</pre><br>
测试结果如下:
相关文章推荐
- Python练习册 第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
- [python]你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
- 利用Python编写一个程序 统计当前目录下的每个文件类型的文件数目
- python读取一个目录下的文件,并且写入.txt文件中
- Python实现将目录中TXT合并成一个大TXT文件的方法
- 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出
- 第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词
- java读取txt文件,并统计每行中每个字母出现的次数,并将产生的数字保存到一个新的txt文件中(有加分)
- 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
- Python实现将目录中TXT合并成一个大TXT文件的方法
- Python:遍历一个目录下所有的文件及文件夹,然后计算每个文件的字符和line的小程序
- 第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
- 【练习题】 使用程序建立一个多级的目录,在每个目录下,新建一个和目录名字一样的txt文件,并写入1990
- 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出例如: a: 21 次 b: 15 次 c:: 15 次 把: 7 次 当: 9 次 前: 3 次
- python读取一个目录下所有txt里面的内容
- python打开一个本地目录文件路径
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- 本文以python实现了一个日志文件中ip提取与统计程序
- 如何用python生成一个顺序标号的txt文件?