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 ...
随机推荐
- -bash: ls: No such file or directory 错误的原因及解决办法
ubuntu出现如下错误: { Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-42-generic x86_64) * Documentation: ...
- 解决myeclipse没有代码提示的问题
今天和室友安装了一样的myeclipse版本,结果室友的自动提示功能有,我的输入“.”后却不能提示,这对我们敲代码简直来说是一个折磨,不能自动提示,本来还以为是系统问题,一个是win7,一个是win1 ...
- 对路径“xxxxx”的访问被拒绝。
对路径“D:\\Weixin\\WechatWeb\\wapMxApi\\JsonFile\\WaterPrice.json”的访问被拒绝. 本地vs2013编译调试是没有问题的但是发布后就不能倍访问 ...
- moment.js 使用方法记录
操作 设值/赋值 1. 具体方法. 1)毫秒(millisecond) moment().millisecond(Number); moment().millisecond(); // Number ...
- Oracle11g adump目录下面.aud增长导致空间撑满无法删除导致CRS无法启动的解决方法
[root@node1 adump]# pwd /u01/app/oracle/admin/node/adump 大概有10000个文件 rm -rf * 屏幕显示: -bash: /bin/rm: ...
- java线程基础巩固---线程生产者消费者的综合实战结合Java8语法
基于上一次[http://www.cnblogs.com/webor2006/p/8909558.html]学习的多个生产者与多个消费者模型,此次用另外一个案例来进一步巩固线程之间的调度处理,这里还是 ...
- iOS View的一些操作定义为宏
#define ViewOf(__View__,__TAG__) [__View__ viewWithTag:__TAG__]#define LabelOf(__View__,__TAG__) ((U ...
- NTP服务及时间同步
环境: centos7 server 192.168.2.171 client 192.168.2.173.192.168.2.174 整体思路:173.174同步171的时间,171定时同 ...
- Windows10启用或关闭Windows功能一直显示请稍候
1.运行service.msc打开服务列表,找到Windows Modules Installer服务进行重启.如果重启失败,可以重启电脑后再次启动该服务. 2.此时运行controller打开控制面 ...
- JavaScript一元运算符
㈠一元运算符 一元运算符,只需要一个操作数 ⑴+ 正号 正号不会对数值产生影响 示例: ⑵- 负号 负号可以对数字进行负号的取反 示例: ⑶对于非Number类型的值,它会将其先转换 ...