[Python]jieba切词 添加字典 去除停用词、单字 python 2020.2.10
源码如下:
import jieba
import io
import re #jieba.load_userdict("E:/xinxi2.txt")
patton=re.compile(r'..') #添加字典
def add_dict():
f=open("E:/xinxi2.txt","r+",encoding="utf-8") #百度爬取的字典
for line in f:
jieba.suggest_freq(line.rstrip("\n"), True)
f.close() #对句子进行分词
def cut():
number=0
f=open("E:/luntan.txt","r+",encoding="utf-8") #要处理的内容,所爬信息,CSDN论坛标题
for line in f:
line=seg_sentence(line.rstrip("\n"))
seg_list=jieba.cut(line)
for i in seg_list:
print(i) #打印词汇内容
m=patton.findall(i)
#print(len(m)) #打印字符长度
if len(m)!=0:
write(i.strip()+" ")
line=line.rstrip().lstrip()
print(len(line))#打印句子长度
if len(line)>1:
write("\n")
number+=1
print("已处理",number,"行") #分词后写入
def write(contents):
f=open("E://luntan_cut2.txt","a+",encoding="utf-8") #要写入的文件
f.write(contents)
#print("写入成功!")
f.close() #创建停用词
def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
return stopwords # 对句子进行去除停用词
def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
stopwords = stopwordslist('E://stop.txt') # 这里加载停用词的路径
outstr = ''
for word in sentence_seged:
if word not in stopwords:
if word != '\t':
outstr += word
#outstr += " "
return outstr #循环去除、无用函数
def cut_all():
inputs = open('E://luntan_cut.txt', 'r', encoding='utf-8')
outputs = open('E//luntan_stop.txt', 'a')
for line in inputs:
line_seg = seg_sentence(line) # 这里的返回值是字符串
outputs.write(line_seg + '\n')
outputs.close()
inputs.close() if __name__=="__main__":
add_dict()
cut()
luntan.txt的来源,地址:https://www.cnblogs.com/zlc364624/p/12285055.html
其中停用词可自行百度下载,或者自己创建一个txt文件夹,自行添加词汇用换行符隔开。
百度爬取的字典在前几期博客中可以找到,地址:https://www.cnblogs.com/zlc364624/p/12289008.html
效果如下:

import jieba
import io
import re #jieba.load_userdict("E:/xinxi2.txt")
patton=re.compile(r'..') #添加字典
def add_dict():
f=open("E:/xinxi2.txt","r+",encoding="utf-8") #百度爬取的字典
for line in f:
jieba.suggest_freq(line.rstrip("\n"), True)
f.close() #对句子进行分词
def cut():
number=0
f=open("E:/luntan.txt","r+",encoding="utf-8") #要处理的内容,所爬信息,CSDN论坛标题
for line in f:
line=seg_sentence(line.rstrip("\n"))
seg_list=jieba.cut(line)
for i in seg_list:
print(i) #打印词汇内容
m=patton.findall(i)
#print(len(m)) #打印字符长度
if len(m)!=:
write(i.strip()+" ")
line=line.rstrip().lstrip()
print(len(line))#打印句子长度
if len(line)>:
write("\n")
number+=1
print("已处理",number,"行") #分词后写入
def write(contents):
f=open("E://luntan_cut2.txt","a+",encoding="utf-8") #要写入的文件
f.write(contents)
#print("写入成功!")
f.close() #创建停用词
def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
return stopwords # 对句子进行去除停用词
def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
stopwords = stopwordslist('E://stop.txt') # 这里加载停用词的路径
outstr = ''
for word in sentence_seged:
if word not in stopwords:
if word != '\t':
outstr += word
#outstr += " "
return outstr #循环去除、无用函数
def cut_all():
inputs = open('E://luntan_cut.txt', 'r', encoding='utf-8')
outputs = open('E//luntan_stop.txt', 'a')
for line in inputs:
line_seg = seg_sentence(line) # 这里的返回值是字符串
outputs.write(line_seg + '\n')
outputs.close()
inputs.close() if __name__=="__main__":
add_dict()
cut()
[Python]jieba切词 添加字典 去除停用词、单字 python 2020.2.10的更多相关文章
- jieba文本分词,去除停用词,添加用户词
import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplo ...
- python去除停用词(结巴分词下)
python 去除停用词 结巴分词 import jieba #stopwords = {}.fromkeys([ line.rstrip() for line in open('stopword. ...
- (3.1)用ictclas4j进行中文分词,并去除停用词
酒店评论情感分析系统——用ictclas4j进行中文分词,并去除停用词 ictclas4j是中科院计算所开发的中文分词工具ICTCLAS的Java版本,因其分词准确率较高,而备受青睐. 注:ictcl ...
- python利用jieba进行中文分词去停用词
中文分词(Chinese Word Segmentation) 指的是将一个汉字序列切分成一个一个单独的词. 分词模块jieba,它是python比较好用的分词模块.待分词的字符串可以是 unicod ...
- 使用Python中的NLTK和spaCy删除停用词与文本标准化
概述 了解如何在Python中删除停用词与文本标准化,这些是自然语言处理的基本技术 探索不同的方法来删除停用词,以及讨论文本标准化技术,如词干化(stemming)和词形还原(lemmatizatio ...
- python编程基础知识—字典
字典 在python中,字典是一系列键-值对,每个键都与一个值相关联,可使用键来访问相关联的值.与键相关联的值可以是数字.字符串.列表乃至字典,即可将任何python对象用在字典中的值. 在pytho ...
- 如何在java中去除中文文本的停用词
1. 整体思路 第一步:先将中文文本进行分词,这里使用的HanLP-汉语言处理包进行中文文本分词. 第二步:使用停用词表,去除分好的词中的停用词. 2. 中文文本分词环境配置 使用的HanLP-汉 ...
- 词项邻近 & 停用词 & 词干还原
[词项邻近] 邻近操作符(proximity)用于指定查询中的两个词项应该在文档中互相靠近,靠近程度通常采用两者之间的词的个数或者是否同在某个结构单元(如句 子或段落)中出现来衡量. [停用词] 一些 ...
- python jieba分词(添加停用词,用户字典 取词频
中文分词一般使用jieba分词 1.安装 pip install jieba 2.大致了解jieba分词 包括jieba分词的3种模式 全模式 import jieba seg_list = jieb ...
随机推荐
- mac chrome
command + < 可以直接跳转到谷歌设置的页面去.
- docker启动nginx的ssl配置
前提条件 一台云服务器(阿里云.腾讯云等的centOS) 服务器上面要有docker(安装方法这里不做介绍) 一个域名 ssl证书(两个文件:一个key后缀,一个pem后缀:生成方法很多这里不再介绍) ...
- 关于存储最近N次数据的问题的实现
需求描述: 需要实现采集中,始终保持最近10次的数据 描述分析: 当采集第一次数据的存储的时候,开辟一个长度为11的list,和一个标记为来记录当前的采集轮询次数(记录1-10,第11次数值归1) 数 ...
- Vue.js 从源码理解v-for和v-if的优先级的高低
在vue.js里面,v-for和v-if是可以一起使用作用在某个元素上,网上看到一篇文章说永远不要把v-for和v-if同时用在同一个元素上,感觉有点瞎扯,官网也注明了可以一起使用的,还把两个指令的优 ...
- MySQL - SQL语句优化方法
1.使用 show status 了解各种 SQL 的执行频率 mysql> show status like 'Com%'; 该命令可以查询 sql 命令的执行次数. 2.定位执行效率较低的 ...
- 使用jdbc将mysql数据库中的内容封装为指定对象的list集合
使用jdbc将mysql数据库中的内容封装为指定对象的list集合 public List<User> findAll() { private JdbcTemplate template ...
- Windows10访问Ubuntu子系统(WSL)的桌面环境
原文地址:https://blog.csdn.net/xmh19936688/article/details/90212960 Windows10访问Ubuntu子系统(WSL)的桌面环境文章目录Wi ...
- Mac Docker Desktop "Mounts denied: EOF."解决方法
环境 系统: Mac OS Catalina Docker Desktop: 问题描述 在Mac环境下创建容器时用"-v"参数挂载目录出现"docker: Error r ...
- Git学习笔记(windows git之初体验)
阿里国内镜像地址: https://npm.taobao.org/mirrors/git-for-windows/ 最近在学习廖雪峰老师关于git的教程,链接可以在我的首页找到.首先使用国内镜像下载并 ...
- Android实战项目——家庭记账本(七)
今天主要实现了登录注册功能的客户端和服务端,但由于短信接口调用出现问题,导致注册功能还不完整. 截止到今天,APP的功能已经基本完成,后续还会陆陆续续的完善各功能模块与服务端的交互,因为需要和云端关联 ...