python字典移除_python修改字典

python字典移除_python修改字典源码如下:1importjieba2importio3importre45#jieba.load_userdict(“E:/xinxi2.txt”)6patton=re.compile(r’..’)78#添加字典9defadd_dict():10f=open(“E:/xinxi2.txt”,”r+”,encodi…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

源码如下:

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()

Jetbrains全家桶1年46,售后保障稳定

luntan.txt的来源,地址:https://www.cnblogs.com/zlc364624/p/12285055.html

其中停用词可自行百度下载,或者自己创建一个txt文件夹,自行添加词汇用换行符隔开。

百度爬取的字典在前几期博客中可以找到,地址:https://www.cnblogs.com/zlc364624/p/12289008.html

效果如下:

python字典移除_python修改字典

 

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()

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223073.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 文本分类算法之–贝叶斯文本分类算法[通俗易懂]

    文本分类算法之–贝叶斯文本分类算法[通俗易懂]文本分类过程例如文档:GoodgoodstudyDaydayup可以用一个文本特征向量来表示,x=(Good,good,study,Day,day,up)。在文本分类中,假设我们有一个文档d∈X,类别c又称为标签。我们把一堆打了标签的文档集合作为训练样本,∈X×C。例如:={BeijingjoinstheWorldTradeOrganization,C

    2022年6月1日
  • javascript字典中添加数组_JS数组添加字典的方法

    javascript字典中添加数组_JS数组添加字典的方法varary_RoleType=[];//申明数组变量for(varj=0;jif($.inArray(treeData[j].value,ary_RoleType)<0){//使用jquery进行判断该数组是否包含该值ary_RoleType.push({//类似于JS添加JSON的字典方法,Key对应键值,value对应值‘key‘:treeData[j]….

    2022年5月3日
  • 10秒钟脱口而出十位数相同两位数的乘法

    10秒钟脱口而出十位数相同两位数的乘法10秒钟脱口而出十位数相同两位数的乘法一、范围十位数相同的两位数。二、目标计算两位数的相乘。10秒钟脱口而出。三、基本公式以尾数之和展开讨论:假设两个数分别是10a+b以及10a+c,那么尾数之和就是b+c。序号分类公式举例1尾数之和小于10即b+c(10a+b)(10a+c)=100a²+10a(b+c)+bc=10a((10a+b)+c)+bc  21X23=(20+1)(20+

    2022年6月4日
  • Linux 的解压缩文件命令「建议收藏」

    Linux 的解压缩文件命令「建议收藏」博客园首页新随笔联系管理随笔-122 文章-0 评论-14 压缩文件 tar -zcvf my.tarabcabc.txtar命令可以用来压缩打包单文件、多个文件、单个目录、多个目录。常用格式:单个文件压缩打包tarczvfmy.tarfile1多个文件压缩打包tarczvfmy.tarfile1file2,…单个目录压缩打包tarczvfmy.tar…

    2022年5月16日
  • 一篇带你从小白到入门的vue教程

    一篇带你从小白到入门的vue教程一篇带你从小白到入门的vue教程

    2022年5月17日
  • JSP/Servlet工作原理[通俗易懂]

    JSP/Servlet工作原理[通俗易懂]JSP/Servlet工作原理ServletServlet没有main方法,不能够独立运行,它的运行需要容器的支持,Tomcat是最常用的JSP/Servlet容器。Servlet运行在Servlet容器中,并由容器管理从创建到销毁的整个过程Servlet的生命周期加载和实例化Servlet容器装载和实例化一个Servlet。创建出该Servlet类的一个实例。初始化在Servlet

    2022年10月4日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号