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 ...
随机推荐
- Django REST Framework(DRF)_第三篇
DRF版本控制 介绍 我们在看APIView源码时可以看到,版本和版本控制类是通过determine_version的返回值获取的 version, scheme = self.determine_v ...
- JavaScript设计模式与开发实践(一)
一.this this的指向大致可以分为以下几种: 作为对象的方法调用 作为普通函数调用 构造器调用 Function.prototype.call或Function.prototype.apply ...
- Docuemnt 的 NamespaceURI为空问题
创建doc的方式不同,需要增加 DocumentBuilderFactory.setNamespaceAware(true); 这样Element Node.getNamespaceURI 才不为空 ...
- 【树形dp 思维题】HHHOJ#483. NOIP司马懿
要注意利用一些题目的特殊条件吧. 题目大意 有一颗$n$个点带点权$a_i$的树,$q$次询问树上是否存在长度为$l$的路径. $n,q,l\le 10^5,0 \le a_i \le 2$ 题目分析 ...
- ssh远程连接centos7故障排除
导致故障的原因在两个方面 1.网络问题---物理链路就不通可以通过在客户端 telnet目标主机地址,例如:telnet 192.168.1.107 22 千万别忘了端口号!!!,如果通了还连不上则按 ...
- 浅析BFC及其作用
本文链接:https://blog.csdn.net/riddle1981/article/details/52126522
- 关于.Net中Process和ProcessStartInfor的使用
本文主要是介绍在.Net中System.Diagnostics命名空间下Process类和ProcessStartInfo类的使用 用于启动一个外部程序所使用的类是Process,至于ProcessS ...
- vs2017 gitee项目无法提交同步的解决办法
下载扩展跟更新: gitee插件 然后命令行输入: git push --set-upstream origin master 参考 https://www.cnblogs.com/slwangzi ...
- BFC的理解
一.BFC概念 BFC即Block Formatting Contexts(块级格式化上下文),它属于普通流.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其他子元素将如何定位,以及和其他元素 ...
- C#窗体的resx文件
这些图片在项目文件中没找到,原来都存在了resx文件中. 属性界面的Image.BackgroundImage属性手动选择的图片会自动存储到resx文件中,之后这些图片源文件就可以删除了.resx中的 ...