汉字字频统计程序(Python版)
2015-06-29 15:26
1066 查看
用Python写了一个汉字词频统计程序,针对已经分好词的文本。
功能并不完善,期待解决。
#python 3.4.3 #功能:统计文本中的词频。 #缺陷:标点符号的频数也会计算在内。 with open('test.txt', mode='r', encoding='utf-8') as inFile: dict={} # 创建一个空字典 word='' # 空字符串以便于连接字符 for char in inFile.read(): if char != ' ': word += char # 连接字符 else: if word in dict: dict[word]+=1 word='' # 将word置为空,否则,word值无限增大 else: dict.setdefault(word, 1) word='' with open('WordFreq.xls', mode='w', encoding='gbk') as outFile: for word, freq in dict.items(): s = '{0}\t{1}\n'.format(word, freq) outFile.write(s)
功能并不完善,期待解决。
相关文章推荐
- python学习一
- Python正则表达式指南
- python使用xmlrpc实例讲解
- python opencv 笔记
- Python字典 (dictionary)
- python 中的列表解析和生成器表达式
- python 的一些特性
- test python
- python 2.7 + pip + Django 安装
- 关于Python中如何使用静态、类、抽象方法的权威指南(译)
- 关于Python中如何使用静态、类、抽象方法的权威指南
- 关于Python中如何使用静态、类、抽象方法的权威指南
- python\split\join
- 《Python核心编程》P21输入数值字符串→转整型
- Python中yield的用法
- Python中startswith和endswith的一些说明
- 用python将jenkins构建的apk下载地载生成二维码提供下载
- 递归:若函数包含了对其自身的调用,该函数为递归的。《Python核心编程》P305
- python __init__.py
- python 正则表达式的处理