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分词小说与词频统计的更多相关文章

  1. python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库 -转载

    转载请注明出处  “结巴”中文分词:做最好的 Python 中文分词组件,分词模块jieba,它是python比较好用的分词模块, 支持中文简体,繁体分词,还支持自定义词库. jieba的分词,提取关 ...

  2. python jieba分词(添加停用词,用户字典 取词频

    中文分词一般使用jieba分词 1.安装 pip install jieba 2.大致了解jieba分词 包括jieba分词的3种模式 全模式 import jieba seg_list = jieb ...

  3. $好玩的分词——python jieba分词模块的基本用法

    jieba(结巴)是一个强大的分词库,完美支持中文分词,本文对其基本用法做一个简要总结. 安装jieba pip install jieba 简单用法 结巴分词分为三种模式:精确模式(默认).全模式和 ...

  4. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  5. python复合数据类型以及英文词频统计

    这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753. 1.列表,元组,字典,集合分别如何增删改查及遍历. 列 ...

  6. python jieba 分词进阶

    https://www.cnblogs.com/jiayongji/p/7119072.html 文本准备 到网上随便一搜"三体全集",就很容易下载到三体三部曲的全集文本(txt文 ...

  7. Python jieba 分词

    环境 Anaconda3 Python 3.6, Window 64bit 目的 利用 jieba 进行分词,关键词提取 代码 # -*- coding: utf-8 -*- import jieba ...

  8. python jieba分词工具

    源码地址:https://github.com/fxsjy/jieba 演示地址:http://jiebademo.ap01.aws.af.cm/ 特点 1,支持三种分词模式: a,精确模式,试图将句 ...

  9. python——jieba分词过程

    import jieba """函数2:分词函数""" def fenci(training_data): ""&quo ...

随机推荐

  1. deep_learning_Function_ Matplotlib 3D 绘图函数 plot_surface 的 rstride 和 cstride 参数

    今晚开始接触 Matplotlib 的 3D 绘图函数 plot_surface,真的非常强大,图片质量可以达到出版级别,而且 3D 图像可以旋转 ,可以从不同角度来看某个 3D 立体图,但是我发现各 ...

  2. python对ip地址排序、对列表进行去重

    一:使用python对ip地址排序所用代码示例一: import socket iplist = ['10.5.11.1','192.168.1.33','10.5.2.4','10.5.1.3',' ...

  3. CNN for NLP(2)

    参考链接: 卷积神经网络(CNN)在句子建模上的应用, 卷积神经网络CNN在自然语言处理中的应用, CNN在NLP中的应用.

  4. Spring 事务相关

    事务类型 数据库事务类型有本地事务和分布式事务: 本地事务:就是普通事务,能保证单台数据库上的操作的ACID,被限定在一台数据库上: 分布式事务:涉及两个或多个数据库源的事务,即跨越多台同类或异类数据 ...

  5. java 枚举类(简单使用)

    直接上代码 用法一(常量): package com.ou.test; import com.sun.corba.se.impl.util.SUNVMCID; public class Enum { ...

  6. 01_3大配置管理工具、SaltStack安装、修改minion_id

    1.配置管理 1.1 puppet /'pʌpɪt/  木偶:傀儡:受他人操纵的人 使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等. 问题: 学习曲线非常陡峭 ...

  7. vue实例之组件开发:图片轮播组件

    一.普通方式: 其中,index是关键. <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  8. java+大文件上传+下载

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  9. BZOJ 3270: 博物馆 概率与期望+高斯消元

    和游走挺像的,都是将概率转成期望出现的次数,然后拿高斯消元来解. #include <bits/stdc++.h> #define N 23 #define setIO(s) freope ...

  10. 用python进行服务器的监控

    用python进行服务器的监控 在linux服务器中,一切皆为文件,就是说,服务器运行的个中信息,其实是可以从某些文件中查询得到的:百度后,你会知道,在Linux系统中,有一个/proc的虚拟文件系统 ...