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)
上一篇 2025年6月29日 下午5:15
下一篇 2025年6月29日 下午5:43


相关推荐

  • OpenClaw 超强 AI 助理实测:自动订票\会议邀请\卡路里计算\安装全攻略

    OpenClaw 超强 AI 助理实测:自动订票\会议邀请\卡路里计算\安装全攻略

    2026年3月13日
    2
  • Spring-boot_Spring Boot

    Spring-boot_Spring Boot1概述Jasypt是一个加密库,Github上有一个集成了Jasypt的SpringBoot库,叫jasypt-spring-boot,本文演示了如何使用该库对配置文件进行加密。2依赖首先添加依赖:<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId>

    2026年4月14日
    5
  • 《梦断代码》摘录及感悟「建议收藏」

    《梦断代码》摘录及感悟

    2022年1月22日
    39
  • 浅谈Console.WriteLine();「建议收藏」

    今日看《老粱故事汇》,介绍日本已逝巨星高仓健,才初步对此人有所了解。老高一生演绎了许多经典的影幕形象,男主角都收获了真挚的爱情……可现实生活中,真实的老高,却一辈子只经历过一次婚姻,在离婚后终究不能从失败的婚姻中走出,最终孤老一生……所以,看事物,看人,不能光看表面,要有一双发现的眼睛,看到他的本质……   借此机会,讲一个C#中用于输出的最常见不过的语句的一个注意事项……谈到Cons

    2022年4月17日
    67
  • C语言的文件操作_C语言调用文件

    C语言的文件操作_C语言调用文件文件打开与关闭C文件操作用库函数实现,包含在stdio.h中。文件使用方式:打开文件→文件读/写→关闭文件系统自动打开和关闭三个标准文件:标准输入——键盘 stdin标准输出——显示器 stdout标准出错输出—–显示器 stderr文件读写操作当我们把文件打开之后,就可以对它进行读与…

    2022年8月18日
    8
  • encrypt函数_crypt12

    encrypt函数_crypt12数据加密数据加密利用密码技术对信息进行加密,实现信息的隐蔽,从而起到保护信息安全的作用。它通过加密算法和加密秘钥将原来是明文的文件或数据进行处理,使其成文不可读的一段代码,也就是所谓的“密文”,来达到

    2022年8月6日
    10

发表回复

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

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