Python 3.6 利用NLTK 统计多个文本中的词频
来源:互联网 发布:网络云台控制软件 编辑:程序博客网 时间:2024/05/19 17:58
#!/usr/bin/env python# encoding: utf-8"""@author: wg@software: PyCharm@file: word_frequency_statistics.py@time: 2017/3/16 0016 10:46"""import osimport nltk'''利用NLTK 统计多个文本中的词频'''dirs = os.listdir('../../data/大秦帝国/') # 获取根目录dictionary = {} # 空词典,用于保存最终的词频stopwords = ['、','(',')',',','。',':','“','”','\n\u3000','\u3000','的','‘','’'] # 停用词'''def process(): for d in dirs: #遍历根目录下的文件夹 subdir = os.listdir('../../data/大秦帝国/') for f in subdir: # 遍历文件夹下的文件 text = open('', 'r', encoding='utf-8').read() # 读取文本内容 print('D:/sogouOutput/'+d+'/'+f) fredist = nltk.FreqDist(text.split(' ')) # 获取单文件词频 for localkey in fredist.keys(): # 所有词频合并。 如果存在词频相加,否则添加 if localkey in stopwords: # 检查是否为停用词 print('-->停用词:', localkey) continue if localkey in dictionary.keys(): # 检查当前词频是否在字典中存在 dictionary[localkey] = dictionary[localkey] + fredist[localkey] # 如果存在,将词频累加,并更新字典值 print('--> 重复值:', localkey, dictionary[localkey]) else: # 如果字典中不存在 dictionary[localkey] = fredist[localkey] # 将当前词频添加到字典中 print('--> 新增值:', localkey, dictionary[localkey]) print('===================================================') print(sorted(dictionary.items(), key = lambda x:x[1])) # 根据词频字典值排序,并打印'''def process(): subdir = os.listdir('../../data/wordcloud/') for f in subdir: # 遍历文件夹下的文件 text = open('../../data/wordcloud/'+f, 'r', encoding='utf-8').read() # 读取文本内容 print('../../data/wordcloud/'+f) fredist = nltk.FreqDist(text.split(' ')) # 获取单文件词频 for localkey in fredist.keys(): # 所有词频合并。 如果存在词频相加,否则添加 if localkey in stopwords: # 检查是否为停用词 print('-->停用词:', localkey) continue if localkey in dictionary.keys(): # 检查当前词频是否在字典中存在 dictionary[localkey] = dictionary[localkey] + fredist[localkey] # 如果存在,将词频累加,并更新字典值 print('--> 重复值:', localkey, dictionary[localkey]) else: # 如果字典中不存在 dictionary[localkey] = fredist[localkey] # 将当前词频添加到字典中 print('--> 新增值:', localkey, dictionary[localkey]) print('===================================================') print(sorted(dictionary.items(), key = lambda x:x[1])) # 根据词频字典值排序,并打印if __name__ == '__main__': process()
4 0
- Python 3.6 利用NLTK 统计多个文本中的词频
- 使用Python+NLTK实现英文单词词频统计
- 使用Python+NLTK实现英文单词词频统计
- Python 中的词频统计
- 词频统计包----nltk
- python 文本单词提取和词频统计
- 如何利用python统计英文文章词频
- 【python】文本处理:利用NLTK断句
- NLTK 词频统计(一) 词频统计,绘图,词性标注
- 英文文本词频统计
- c++ 统计文本词频
- python 统计TXT中的英文词频
- 利用word分词来对文本进行词频统计
- 【java】以词频升序统计文本词频
- python 统计词频
- python统计汉字词频
- python 统计词频
- python统计文档词频
- Mybatis之高级映射collection (递归查出树形数据 )
- KMSAuto Net 2016 1.4.9 Portable
- Java 并发常用工具说明
- 使用 Docker 搭建 Java Web 运行环境
- opencv3.0学习
- Python 3.6 利用NLTK 统计多个文本中的词频
- 用jQuery来实现图片轮播
- tensorflow-002-MNIST
- MySQL控制台中文乱码
- org.apache.subversion.javahl.ClientException: Item is not writable
- 产品经理如何用数据分析发现问题
- Python 后台修改request表单
- 对象池
- Ext.js的window添加遮罩效果