python统计文档中词频
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 result def sort_by_count(d): d = collections.OrderedDict(sorted(d.items(), key = lambda t: -t[1]))
return d if __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
python统计文档中词频的更多相关文章
- java统计文档中相同字符出现次数(超详细)
public class test { public static void main(String[] args) throws Exception { InputStream file = new ...
- Python帮助文档中Iteration iterator iterable 的理解
iteration这个单词,是循环,迭代的意思.也就是说,一次又一次地重复做某件事,叫做iteration.所以很多语言里面,循环的循环变量叫i,就是因为这个iteration. iteration指 ...
- python读入文档中的一行
从文件log_fusion中读入数据 方法1 f = open("log_fusion.txt") # 返回一个文件对象 line = f.readline() # 调用文件的 r ...
- 利用python处理文档中各字段出现的次数并排序
import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...
- 教你用java统计目录下所有文档的词频
本文是统计目录下所有文档的词频top10,非单个文档,包含中文和英文. 直接上代码: package com.huawei.wordcount; import java.io.BufferedRead ...
- 2018-10-04 [日常]用Python读取word文档中的表格并比较
最近想对某些word文档(docx)的表格内容作比较, 于是找了一下相关工具. 参考Automate the Boring Stuff with Python中的word部分, 试用了python-d ...
- 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)
前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)
对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...
- 【python】使用HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies
一.从HTML文档中提取链接 模块HTMLParser,该模块使我们能够根据HTML文档中的标签来简洁.高效地解析HTML文档. 处理HTML文档的时候,我们常常需要从其中提取出所有的链接.使用HTM ...
随机推荐
- [WC2016]挑战NPC
Sol 这做法我是想不到\(TAT\) 每个筐子拆成三个相互连边 球向三个筐子连边 然后跑一般图最大匹配 这三个筐子间最多有一个匹配 那么显然每个球一定会放在一个筐子里,一定有一个匹配 如果筐子间有匹 ...
- 实现绘制图形的ToolBar
给地图添加绘制图形的ToolBar还是有必要的,比较人性化的功能.图形的样式可以自己定制,也提供了朴实的默认样式.对 dojo 不太懂,出现了许许多多问题,真是蛋疼的一天啊.令人惊喜的是 ArcGis ...
- 简单的自动升级提示AutoUpdater
看过网上“圣殿骑士”和其他人的升级做法,感觉不太适合几十M的小型软件. 之前用的一直都是clickonce,但是3年下来感觉弊端太多,比如安装不能选择文件夹.打包不全.版本等问题,于是决定另辟捷径. ...
- c# 修改winform中app.config的配置值
public bool ChangeConfig(string AppKey,string AppValue) { bool result = true; try { XmlDocument xDoc ...
- LeetCode-Container With Most Water-zz
先上代码. #include <iostream> #include <vector> #include <algorithm> using namespace s ...
- Angular2 前端代码规范
不要重置对象的引用!(重置只应该在组件或服务的初始化时) why:会使页面产生闪烁 不要给图片绑定一个空的值或空的图片引用(如果值从服务器异步过来,那应该在初始化时给它一个默认值)why:会触发一个4 ...
- 设计模式:享元(FlyWeight)模式
设计模式:享元(FlyWeight)模式 一.前言 享元(FlyWeight)模式顾名思义,既是轻量级的,原因就是享元,共享元素,这里的元素指的是对象.如何共享对象,那就是在检测对象产生的时候 ...
- July 16th 2017 Week 29th Sunday
Opportunities are like sunrises, if you wait too long, you miss them. 机会如同日出,等得太久就会错过. Indecision is ...
- July 03rd 2017 Week 27th Monday
Even if you are on the right track, you will get run over if you just sit there. 即使你处于正确的轨道上,但如果你只是坐 ...
- 我的第一个Python随笔
自学Python也很长时间了,注册博客园写了第一篇随笔.之前想过很多次,但是始终不知道该怎么开始,内容如何,现在想想,随笔嘛,是自己的想法,也自己的实践,又是自己的锻炼.话不多说,开始今天的正式内容. ...