1、知识点

  1. """
  2. 1)cut()
  3. a) codecs.open() 解决编码问题
  4. b) f.readline() 读取一行,也可以使用f.readlines()读取多行
  5. c) words =" ".join(jieba.cut(line))分词,每个词用空格分隔
  6. 2)lcut()
  7. 返回一个list列表
  8. """

2、标点符号处理,并分词,存储到文件中

  1. def fenCi():
  2. """
  3. 标点符号处理,并分词,存储到文件中
  4. :return:
  5. """
  6. f = codecs.open("深渊主宰系统.txt",'r',encoding='utf-8')
  7. f1 = open("seg.txt",'w',encoding='utf-8')
  8. line = f.readline()
  9. while line:
  10. line = line.strip(' ')
  11. words =" ".join(jieba.cut(line))
  12. words = words.replace(",","").replace("!","").replace("“","")\
  13. .replace("”","").replace("。","").replace("?","").replace(":","")\
  14. .replace("...","").replace("、","").strip(' ')
  15. print(len(words))
  16. if words.startswith('-') or words == '\r\n' or words.startswith('.') or len(words)<10 :
  17. line = f.readline()
  18. continue
  19. words = words.strip('\n')
  20. f1.writelines(words)
  21. line = f.readline()

3、中文分词统计

  1. def zhongwen():
  2. """
  3. 中文分词统计
  4. 对两个词以上的次数进行统计
  5. lcut 进行分词,返回分词后list列表
  6. :return:
  7. """
  8. f = codecs.open("深渊主宰系统.txt", 'r', encoding='utf-8').read()
  9. counts = {}
  10. wordsList =jieba.lcut(f)
  11. for word in wordsList:
  12. word = word.replace(",", "").replace("!", "").replace("“", "") \
  13. .replace("”", "").replace("。", "").replace("?", "").replace(":", "") \
  14. .replace("...", "").replace("、", "").strip(' ').strip('\r\n')
  15. if len(word) == 1 or word == "":
  16. continue
  17. else:
  18. counts[word]=counts.get(word,0)+1 #单词计数
  19. items = list(counts.items()) #将字典转为list
  20. items.sort(key=lambda x:x[1],reverse=True) #根据单词出现次数降序排序
  21. #打印前15个
  22. for i in range(15):
  23. word,counter = items[i]
  24. print("单词:{},次数:{}".format(word,counter))

4、英文分词统计

  1. def get_txt():
  2. txt = open("1.txt", "r", encoding='UTF-8').read()
  3. txt = txt.lower()
  4. for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
  5. txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
  6. return txt
  7.  
  8. def yingwen():
  9. """
  10. 英文分词统计
  11. :return:
  12. """
  13. file_txt = get_txt()
  14. words = file_txt.split() # 对字符串进行分割,获得单词列表
  15. counts = {}
  16. for word in words:
  17. if len(word) == 1:
  18. continue
  19. else:
  20. counts[word] = counts.get(word, 0) + 1
  21.  
  22. items = list(counts.items())
  23. items.sort(key=lambda x: x[1], reverse=True)
  24.  
  25. for i in range(5):
  26. word, count = items[i]
  27. 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. MySQL数据库 、数据表、数据的增删改查简版

    数据库操作 # 增 CREATE(DATABASE | SCHEMA)[IF NOT EXISTS] db_name [[DEFAULT] CHARACTER SET[=]charset_name] ...

  2. Percona MongoDB 4 搭建副本集

    什么是副本集: 是一组维护相同数据集的mongod进程 提供冗余,自动故障转移和高可用性 提供读取可伸缩性 内部概念或多或少与MySQL的概念相似 PRIMARY概念与MySQL复制中的MASTER大 ...

  3. 从零开始学会GAN 0:第一部分 介绍生成式深度学习(连载中)

    本书的前四章旨在介绍开始构建生成式深度学习模型所需的核心技术.在第1章中,我们将首先对生成式建模领域进行广泛的研究,并从概率的角度考虑我们试图解决的问题类型.然后,我们将探讨我们的基本概率生成模型的第 ...

  4. 浅析jsp

    什么是jsp?jsp的全称是 java Server Page ,也就是俗称的动态网页,什么是静态网页和动态网页呢,在我理解看来,HTML等网页就属于静态网页,jsp等网页属于动态网页,为什么这么说呢 ...

  5. linux内核 进程调度

    概念: 进程调度决定那个进程投入运行,运行多长时间. 进程调度没有太复杂的原理,最大限度的利用处理器时间的原则是:只要有可执行的程序,那么总会有进程在执行,如果可运行的进程比处理器数目要多,那么注定要 ...

  6. 计划任务 at,cron

    示例:每3小时echo和wall命令

  7. (九)zabbix监控web应用

    1)web应用监控介绍 使用zabbix自带的web场景可以监控url的状态码,响应时间,url的下载速度,非常的棒 思路:定义模板-->创建应用集--->定义web场景--->定义 ...

  8. 《Learning Structured Representation for Text Classification via Reinforcement Learning》论文翻译.md

    摘要 表征学习是自然语言处理中的一个基本问题.本文研究了如何学习文本分类的结构化表示.与大多数既不使用结构又依赖于预先指定结构的现有表示模型不同,我们提出了一种强化学习(RL)方法,通过自动覆盖优化结 ...

  9. web文件夹上传源码

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  10. [sdoi2015]排序(搜索+剪枝优化)

    Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<=N),第i中 ...