python统计文档词频
来源:互联网 发布:淘宝的千人千面 编辑:程序博客网 时间:2024/05/10 02:20
python统计文档中词频的小程序
python版本2.7
程序如下,测试文件完整程序在我的github中
#统计空格数与单词数 本函数只返回了空格数 需要的可以自己返回多个值
def count_space(path):
number_counts = 0 space_counts = 0 number_list = [] with open(path, 'r') as f: for line in f: line = line.strip() space_split_list = line.split(' ') space_counts += len(space_split_list) - 1 for word in space_split_list: if word.isdigit(): number_list.append(word) number_counts = len(number_list) return space_counts
#大写转小写 过滤特殊字符等def count_word(path): result = {} with open(path) as fileread: alltext = fileread.read() alltext = alltext.lower() alltext = re.sub("\"|,|\.", "", alltext) for word in alltext.split(): if word not in result: result[word] = 0 result[word] += 1 return resultdef sort_by_count(d): d = collections.OrderedDict(sorted(d.items(), key = lambda t: -t[1])) return dif __name__ == '__main__': try: filename = 'read.txt' dword = count_word(filename) dword = sort_by_count(dword) countspace = count_space(filename) print "space_counts", countspace count_word(filename) for key,value in dword.items(): print key + ":%d" % value except IOError: print 'cannot open file %s for read' % filename
1 0
- python统计文档词频
- 使用python对中文文档进行词频统计
- Python基础知识(3)——中英文文档词频统计
- python 统计词频
- python统计汉字词频
- python 统计词频
- Python新闻联播词频统计
- python词频统计
- Python 中的词频统计
- 【python技能】词频统计
- 词频统计python
- Python进行词频统计
- python统计词频的方法
- Hadoop学习之莎士比亚文档词频统计
- python jieba分词并统计词频后输出结果到Excel和txt文档
- WordsCount(词频统计)-Python语言编写!
- Python实现中文小说词频统计
- python--10行代码搞定词频统计
- iOS开发之NSError code错误列表信息
- Space Shooter之飞船的运动、边界控制和运动旋转
- git 远程操作
- 阿里菜鸟java岗面试
- hdu 2044 一只小蜜蜂...
- python统计文档词频
- ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)
- [C++]Virtual_World_1(练习)
- apache CXF web 客户端去调用服务器端
- iOS 蓝牙技术
- RedHat(CentOS)和Ubuntu安装软件命令对比(区别)
- PE文件结构解析
- mysqldump 用法
- 游太平记