源码如下:

 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的更多相关文章

  1. jieba文本分词,去除停用词,添加用户词

    import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplo ...

  2. python去除停用词(结巴分词下)

    python 去除停用词  结巴分词 import jieba #stopwords = {}.fromkeys([ line.rstrip() for line in open('stopword. ...

  3. (3.1)用ictclas4j进行中文分词,并去除停用词

    酒店评论情感分析系统——用ictclas4j进行中文分词,并去除停用词 ictclas4j是中科院计算所开发的中文分词工具ICTCLAS的Java版本,因其分词准确率较高,而备受青睐. 注:ictcl ...

  4. python利用jieba进行中文分词去停用词

    中文分词(Chinese Word Segmentation) 指的是将一个汉字序列切分成一个一个单独的词. 分词模块jieba,它是python比较好用的分词模块.待分词的字符串可以是 unicod ...

  5. 使用Python中的NLTK和spaCy删除停用词与文本标准化

    概述 了解如何在Python中删除停用词与文本标准化,这些是自然语言处理的基本技术 探索不同的方法来删除停用词,以及讨论文本标准化技术,如词干化(stemming)和词形还原(lemmatizatio ...

  6. python编程基础知识—字典

    字典 在python中,字典是一系列键-值对,每个键都与一个值相关联,可使用键来访问相关联的值.与键相关联的值可以是数字.字符串.列表乃至字典,即可将任何python对象用在字典中的值. 在pytho ...

  7. 如何在java中去除中文文本的停用词

    1.  整体思路 第一步:先将中文文本进行分词,这里使用的HanLP-汉语言处理包进行中文文本分词. 第二步:使用停用词表,去除分好的词中的停用词. 2.  中文文本分词环境配置 使用的HanLP-汉 ...

  8. 词项邻近 & 停用词 & 词干还原

    [词项邻近] 邻近操作符(proximity)用于指定查询中的两个词项应该在文档中互相靠近,靠近程度通常采用两者之间的词的个数或者是否同在某个结构单元(如句 子或段落)中出现来衡量. [停用词] 一些 ...

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

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

随机推荐

  1. js位运算-按位非

    正数 取得二进制表示 取反(发现符号位是1,表示负数) 符号位不变,其余位取反 取反后加一 负数 取得负数的二进制表示(即绝对值的二进制反码加一) 取反即可

  2. .net core 中如何运用 appsettings.json 进行配置开发、生产不同配置

    .net core 默认会有 appsettings.Development.json 文件,这是根据ASPNETCORE_ENVIRONMENT来读取的. 新建架构appsettings.Produ ...

  3. junit 运行(eclipse + IDEA)

    记得刚用IDEA  开发的时候, 什么都还不熟,以为junit 运行还跟eclipse 一样, 结果试了后才知道是不一样的. 现在刚好写junit 相关的,也就都记录下来吧 Eclipse:eclip ...

  4. PHP-CMS代码审计(4)

    这次找了个发卡平台,url:  https://files.cnblogs.com/files/b1gstar/kamiphp.zip 从52破解上下载的 : 先把网站搭建起来. 网站没有采用mvc框 ...

  5. Linux高性能服务器编程:Linux服务器程序规范

    Linux服务器程序一般以后台进程形式运行,后台进程又称守护进程.它没有控制终端,不会接收到用户输入.守护进程的父进程通常是init进程(PID为1). Linux服务器程序有一套日志系统 Linux ...

  6. 最新2019Pycharm安装教程,亲测!最新2019pycharm安装!如何安装Pycharm2019版本!如何安装2019Pycharm永久教程!2019Pycharm永久安装!

    Pycharm安装 在这插一个小话题哈,Pycharm只是一个编译器,并不能代替Python,如果要使用Python,还是需要安装Python的哈 1.Pycharm下载安装 Pycharm下载 Py ...

  7. P2853 [USACO06DEC]牛的野餐Cow Picnic

    ------------------------- 长时间不写代码了,从学校中抽身出来真的不容易啊 ------------------------ 链接:Miku ----------------- ...

  8. PTA Is Topological Order

    Write a program to test if a give sequence Seq is a topological order of a given graph Graph. Format ...

  9. PTA甲级B1061 Dating

    目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...

  10. Spring Boot源码(二):SPI去除web.xml

    SPI广泛用于dubbo,spring boot,spring cloud alibaba等 关于SPI,可见SPI-Service Provider Interface 继续上篇文章 上面三句代码的 ...