github上找的源码,自己改的,记在这里。

对图中的文档做分词及词频统计,然后将统计生成的excel表格和分词后的text文本存入result文件夹里。
待分词的文本:

最后生成的文档:

文件批量处理函数:
主要用到os模块为新生成的文件命名,实现批量处理

def word_frequency_analysis(path):files = os.listdir(path)  # files为列表,存储的是path里面的所有文件名result_dir = os.path.abspath(os.path.join(path, 'result'))  # 返回result文档的路径csv_all = os.path.abspath(os.path.join(result_dir, 'csv_all.csv'))if not os.path.exists(result_dir):os.mkdir(result_dir)  #若不存在该文件路径,则创建一个for filename in files:if not fnmatch.fnmatch(filename, '*.txt'):continuetxt_path = os.path.join(path, filename)txt_content = open(txt_path, 'r').read()field_name = filename[:-4] + '年'  # eg:返回2014年,2015年header_filed.append(field_name)filename_fulltext = filename[:-4] + '_all.txt'filename_counter = filename[:-4] + '_tj.csv'# filename_key = filename[:-4] + '_hy_tj.csv'txt_to_all = os.path.join(os.path.join(path, 'result'), filename_fulltext)txt_to_counter = os.path.join(os.path.join(path, 'result'), filename_counter)#  txt_to_key = os.path.join(os.path.join(path, 'result'), filename_key)text_cutted = jiebaCutText(txt_content)text_cleared = clearText(text_cutted)text_counted = countwords(text_cleared, txt_to_counter)newfile = open(txt_to_all, 'w')newfile.write(text_cleared)newfile.close()

分词函数:
主要用jieba第三方库进行分词

def jiebaCutText(text):seg_list = jieba.cut(text, cut_all=False)liststr = '/'.join(seg_list)return liststr  # 返回的结果中会带标点符号

去除标点和单音节词:
将符合要求的词记入列表

def clearText(text):mywordlist = []for myword in text.split('/'):if len(myword.strip()) > 1 and contain_zh(myword.strip()):mywordlist.append(myword.strip())return '/'.join(mywordlist)

判断字符是否为汉字:
用到re模块,判断字符是否是汉字

def contain_zh(word):zh = re.compile(u'[\u4200-\u9fa5]+')match = zh.search(word)return match

词频统计函数:
用到字典和collections模块

def countwords(text, counter_file):count_dict = dict()for item in text.split('/'):if item in count_dict:count_dict[item] += 1else:count_dict[item] = 1d_sorted_by_value = OrderedDict(sorted(count_dict.items(), key=lambda x: x[1]))with open(counter_file, 'w',encoding='utf-8-sig') as f:#f.write(codecs.BOM_UTF8)w = csv.writer(f)w.writerows(d_sorted_by_value.items())

完整的代码:

import csv
import fnmatch
import os
import re
from collections import OrderedDict
import jieba#header_filed = []def word_frequency_analysis(path):files = os.listdir(path)  # files为列表,存储的是path里面的所有文件名result_dir = os.path.abspath(os.path.join(path, 'result'))  # 返回result文档的路径csv_all = os.path.abspath(os.path.join(result_dir, 'csv_all.csv'))if not os.path.exists(result_dir):os.mkdir(result_dir)  #若不存在该文件路径,则创建一个for filename in files:if not fnmatch.fnmatch(filename, '*.txt'):continuetxt_path = os.path.join(path, filename)txt_content = open(txt_path, 'r').read()field_name = filename[:-4] + '年'  # eg:返回2014年,2015年header_filed.append(field_name)filename_fulltext = filename[:-4] + '_all.txt'filename_counter = filename[:-4] + '_tj.csv'# filename_key = filename[:-4] + '_hy_tj.csv'txt_to_all = os.path.join(os.path.join(path, 'result'), filename_fulltext)txt_to_counter = os.path.join(os.path.join(path, 'result'), filename_counter)#  txt_to_key = os.path.join(os.path.join(path, 'result'), filename_key)text_cutted = jiebaCutText(txt_content)text_cleared = clearText(text_cutted)text_counted = countwords(text_cleared, txt_to_counter)newfile = open(txt_to_all, 'w')newfile.write(text_cleared)newfile.close()def jiebaCutText(text):seg_list = jieba.cut(text, cut_all=False)liststr = '/'.join(seg_list)return liststr  # 返回的结果中会带标点符号def clearText(text):mywordlist = []for myword in text.split('/'):if len(myword.strip()) > 1 and contain_zh(myword.strip()):mywordlist.append(myword.strip())return '/'.join(mywordlist)def contain_zh(word):zh = re.compile(u'[\u4200-\u9fa5]+')match = zh.search(word)return matchdef countwords(text, counter_file):count_dict = dict()for item in text.split('/'):if item in count_dict:count_dict[item] += 1else:count_dict[item] = 1d_sorted_by_value = OrderedDict(sorted(count_dict.items(), key=lambda x: x[1]))with open(counter_file, 'w',encoding='utf-8-sig', newline = '') as f: #newline参数防止生成的文件有空行#f.write(codecs.BOM_UTF8)w = csv.writer(f)w.writerows(d_sorted_by_value.items())if __name__=='__main__':path = 'E:/Programe/PySeg/jieba-wordcloud-demo-master/基础数据/韶关(分年度)'word_frequency_analysis(path)

Python3.7对文本批量进行词频分析相关推荐

  1. 【自然语言处理概述】文本词频分析

    [自然语言处理概述]文本词频分析 作者简介:在校大学生一枚,华为云享专家,阿里云专家博主,腾云先锋(TDP)成员,云曦智划项目总负责人,全国高等学校计算机教学与产业实践资源建设专家委员会(TIPCC) ...

  2. python文本txt词频统计_python实例:三国演义TXT文本词频分析

    0x00 前言 找不到要写什么东西了!今天有个潭州大牛讲师  说了个  文本词频分析 我基本上就照抄了一遍 中间遇到一些小小的问题 自我百度 填坑补全了  如下 : 效果演示 0x01   准备环境及 ...

  3. 大数据分析 | 用 Python 做文本词频分析

    老师教给我,要学骆驼,沉得住气的动物.看它从不着急,慢慢地走,慢慢地嚼,总会走到的,总会吃饱的. ---<城南旧事> 目录 一.前言 Python 简介 Python 特点 二.基本环境配 ...

  4. python文本聚类 词云图_有哪些软件可以进行中文词频分析?

    在现实生活中,人想做词云,也有了关键词的数据但自己又不会做词云可怎么办,我给大家推荐几款词云制作工具,让你瞬间呈现美观.酷炫的词云可视化.我们先来看看国外的词云制作工具: 1.Wordle Wordl ...

  5. python爬取微博评论并做词频分析_爬取李子柒微博评论并分析

    爬取李子柒微博评论并分析 微博主要分为网页端.手机端和移动端.微博网页版反爬太厉害,因此选择爬取手机端. 1 需求 爬取李子柒微博中视频的评论信息,并做词频分析. 2 方法 2.1 运行环境 运行平台 ...

  6. Python入门与词频分析初步

    一.python与其他语言的区别 1.python作为一门解释性语言,与java.C等语言相比,第一个特点就是python不用编译,可以像脚本一样直接运行.前几天咱们工作室有同学问我,他的编程界面为什 ...

  7. python单词词频字典_用python实现词频分析+词云

    2020.05.13更新:大家点个赞再收藏吧(点赞后观看,养成好习惯)TAT 如你所见.文章标题图是以 周杰伦的百度百科 词条为分析文档,以 周杰伦超话第一的那张图+PPT删除背景底色 为词频背景进行 ...

  8. 第三方库实现中文词频分析和词语可视化(jieba,wordcloud库)

    jieba,wordcloud库实现中文词频分析和词语可视化 文章目录 前言: 一.实验题目: 二.实验准备: 三.实验内容 1.全部代码: 2.实验结果: 3.难点分析: 结语: 前言: 这篇文章是 ...

  9. 用javascript自制ctf词频分析工具

    不废话,上代码: <!DOCTYPE html> <html> <head><title></title> </head> &l ...

最新文章

  1. 在Ubuntu Linux 16.04下(64位)打开.ipynb文件
  2. 更改python默认路径_Linux下多版本python共存时,默认执行路径修改方法
  3. 用 Python 实现打飞机
  4. F1 Query: Declarative Querying at Scale
  5. 华为抓取错误日志在哪里_抓取网址进行分析爬虫工具Screaming Frog SEO Spider for Mac...
  6. java碰到乱码如何解决方法_如何处理java的乱码
  7. 如何在SQL Server Reporting Services中自动创建KPI
  8. 机器人布罩_机器人防护罩案例分析
  9. Mongdb中常用的数据清洗
  10. 手机号码状态检测(空号检测)的原理
  11. 使用BabeLua在VS中创建Lua项目
  12. AIDA64内存与缓存测试过了算稳定吗_无需XMP默认3200MHz,十铨 开创者 内存开箱简测...
  13. java-php-python-ssm在线教学质量评价系统计算机毕业设计
  14. HOG特征,LBP特征,Haar特征(图像特征提取)
  15. 基于java《数据结构与算法》网上教学系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
  16. 机器学习应用——强化学习课程总结 实例 “自主学习Flappy Bird游戏”(MDP蒙特卡洛强化学习Q-learningDRLDQN)
  17. 华硕电脑连接不上wifi_华硕电脑连不上无线网_华硕电脑无法连接wifi
  18. matlab containers,matlab中的containers.Map()
  19. 互联网+大赛作品_“颂中国力量 绘美好梦想”全市中小学生互联网+书画大赛作品展示(二十二)...
  20. 关闭和打开445端口

热门文章

  1. 华为mate50pro和小米12ultea对比
  2. uni-app 倒计时组件
  3. 百度无人驾驶网约车起步价16元;美团回应共享单车涨价;谷歌公开抨击苹果阻碍跨平台交流|极客头条
  4. 音视频通话:​Linphone基于SIP协议的语音视频电话软件
  5. 右键快捷创建mk文件
  6. python安装以后怎么打开_安装python后如何打开
  7. 【智能家居平台】天猫精灵 -- 平台剖析
  8. iOS监听模式系列之本地通知Notification
  9. 不用下载软件也能打开EPUB?详解这款支持网页端的阅读器
  10. javaSE学习笔记_目录