python jieba分词小说与词频统计
1、知识点
- """
- 1)cut()
- a) codecs.open() 解决编码问题
- b) f.readline() 读取一行,也可以使用f.readlines()读取多行
- c) words =" ".join(jieba.cut(line))分词,每个词用空格分隔
- 2)lcut()
- 返回一个list列表
- """
2、标点符号处理,并分词,存储到文件中
- def fenCi():
- """
- 标点符号处理,并分词,存储到文件中
- :return:
- """
- f = codecs.open("深渊主宰系统.txt",'r',encoding='utf-8')
- f1 = open("seg.txt",'w',encoding='utf-8')
- line = f.readline()
- while line:
- line = line.strip(' ')
- words =" ".join(jieba.cut(line))
- words = words.replace(",","").replace("!","").replace("“","")\
- .replace("”","").replace("。","").replace("?","").replace(":","")\
- .replace("...","").replace("、","").strip(' ')
- print(len(words))
- if words.startswith('-') or words == '\r\n' or words.startswith('.') or len(words)<10 :
- line = f.readline()
- continue
- words = words.strip('\n')
- f1.writelines(words)
- line = f.readline()
3、中文分词统计
- def zhongwen():
- """
- 中文分词统计
- 对两个词以上的次数进行统计
- lcut 进行分词,返回分词后list列表
- :return:
- """
- f = codecs.open("深渊主宰系统.txt", 'r', encoding='utf-8').read()
- counts = {}
- wordsList =jieba.lcut(f)
- for word in wordsList:
- word = word.replace(",", "").replace("!", "").replace("“", "") \
- .replace("”", "").replace("。", "").replace("?", "").replace(":", "") \
- .replace("...", "").replace("、", "").strip(' ').strip('\r\n')
- if len(word) == 1 or word == "":
- continue
- else:
- counts[word]=counts.get(word,0)+1 #单词计数
- items = list(counts.items()) #将字典转为list
- items.sort(key=lambda x:x[1],reverse=True) #根据单词出现次数降序排序
- #打印前15个
- for i in range(15):
- word,counter = items[i]
- print("单词:{},次数:{}".format(word,counter))
4、英文分词统计
- def get_txt():
- txt = open("1.txt", "r", encoding='UTF-8').read()
- txt = txt.lower()
- for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
- txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
- return txt
- def yingwen():
- """
- 英文分词统计
- :return:
- """
- file_txt = get_txt()
- words = file_txt.split() # 对字符串进行分割,获得单词列表
- counts = {}
- for word in words:
- if len(word) == 1:
- continue
- else:
- counts[word] = counts.get(word, 0) + 1
- items = list(counts.items())
- items.sort(key=lambda x: x[1], reverse=True)
- for i in range(5):
- word, count = items[i]
- print("{0:<5}->{1:>5}".format(word, count))
python jieba分词小说与词频统计的更多相关文章
- python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库 -转载
转载请注明出处 “结巴”中文分词:做最好的 Python 中文分词组件,分词模块jieba,它是python比较好用的分词模块, 支持中文简体,繁体分词,还支持自定义词库. jieba的分词,提取关 ...
- python jieba分词(添加停用词,用户字典 取词频
中文分词一般使用jieba分词 1.安装 pip install jieba 2.大致了解jieba分词 包括jieba分词的3种模式 全模式 import jieba seg_list = jieb ...
- $好玩的分词——python jieba分词模块的基本用法
jieba(结巴)是一个强大的分词库,完美支持中文分词,本文对其基本用法做一个简要总结. 安装jieba pip install jieba 简单用法 结巴分词分为三种模式:精确模式(默认).全模式和 ...
- python瓦登尔湖词频统计
#瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...
- python复合数据类型以及英文词频统计
这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753. 1.列表,元组,字典,集合分别如何增删改查及遍历. 列 ...
- python jieba 分词进阶
https://www.cnblogs.com/jiayongji/p/7119072.html 文本准备 到网上随便一搜"三体全集",就很容易下载到三体三部曲的全集文本(txt文 ...
- Python jieba 分词
环境 Anaconda3 Python 3.6, Window 64bit 目的 利用 jieba 进行分词,关键词提取 代码 # -*- coding: utf-8 -*- import jieba ...
- python jieba分词工具
源码地址:https://github.com/fxsjy/jieba 演示地址:http://jiebademo.ap01.aws.af.cm/ 特点 1,支持三种分词模式: a,精确模式,试图将句 ...
- python——jieba分词过程
import jieba """函数2:分词函数""" def fenci(training_data): ""&quo ...
随机推荐
- MySQL数据库 、数据表、数据的增删改查简版
数据库操作 # 增 CREATE(DATABASE | SCHEMA)[IF NOT EXISTS] db_name [[DEFAULT] CHARACTER SET[=]charset_name] ...
- Percona MongoDB 4 搭建副本集
什么是副本集: 是一组维护相同数据集的mongod进程 提供冗余,自动故障转移和高可用性 提供读取可伸缩性 内部概念或多或少与MySQL的概念相似 PRIMARY概念与MySQL复制中的MASTER大 ...
- 从零开始学会GAN 0:第一部分 介绍生成式深度学习(连载中)
本书的前四章旨在介绍开始构建生成式深度学习模型所需的核心技术.在第1章中,我们将首先对生成式建模领域进行广泛的研究,并从概率的角度考虑我们试图解决的问题类型.然后,我们将探讨我们的基本概率生成模型的第 ...
- 浅析jsp
什么是jsp?jsp的全称是 java Server Page ,也就是俗称的动态网页,什么是静态网页和动态网页呢,在我理解看来,HTML等网页就属于静态网页,jsp等网页属于动态网页,为什么这么说呢 ...
- linux内核 进程调度
概念: 进程调度决定那个进程投入运行,运行多长时间. 进程调度没有太复杂的原理,最大限度的利用处理器时间的原则是:只要有可执行的程序,那么总会有进程在执行,如果可运行的进程比处理器数目要多,那么注定要 ...
- 计划任务 at,cron
示例:每3小时echo和wall命令
- (九)zabbix监控web应用
1)web应用监控介绍 使用zabbix自带的web场景可以监控url的状态码,响应时间,url的下载速度,非常的棒 思路:定义模板-->创建应用集--->定义web场景--->定义 ...
- 《Learning Structured Representation for Text Classification via Reinforcement Learning》论文翻译.md
摘要 表征学习是自然语言处理中的一个基本问题.本文研究了如何学习文本分类的结构化表示.与大多数既不使用结构又依赖于预先指定结构的现有表示模型不同,我们提出了一种强化学习(RL)方法,通过自动覆盖优化结 ...
- web文件夹上传源码
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...
- [sdoi2015]排序(搜索+剪枝优化)
Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<=N),第i中 ...