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


相关推荐

  • 面向APP抓包方法

    面向APP抓包方法面向APP抓包方法,采用Fiddler软件进行抓包。

    2022年5月30日
    34
  • 40 道基础Dubbo 面试题及答案

    40 道基础Dubbo 面试题及答案转载自史上最全40道Dubbo面试题及答案,看完碾压面试官想往高处走,怎么能不懂Dubbo?Dubbo是国内最出名的分布式服务框架,也是Java程序员必备的必会的框架之一。Dubbo更是中高级面试过程中经常会问的技术,无论你是否用过,你都必须熟悉。下面我为大家准备了一些Dubbo常见的的面试题,一些是我经常问别人的,一些是我过去面试遇到的一些问题,总结给大家,希…

    2022年5月10日
    50
  • RSA加密算法简单介绍以及python实现

    RSA加密算法简单介绍以及python实现RSA 加密算法简单介绍 RSA 是一种公钥加密算法 它具有公钥和私钥两种密钥 公钥用来加密 并且是公开的 私钥是用来解密的 是不公开的 也不需要和数据一起传送 这样就能防止密钥在网络传输时泄露 RSA 算法设计的原理是依靠着模幂运算 例如加密 解密以及密钥的产生 1 密钥设计首先 我们需要了解密钥设计的思想 加密计算 c m emodn 解密计算 m c dmodn 其中 m 为明文 c 为密文 e 为公钥 d 为私钥 n 为一个我们要产生的大数 所以 根据以上两个式子有 dk ek

    2026年3月26日
    2
  • java中JSONArray、JSONObject、List、String之间的转换「建议收藏」

    java中JSONArray、JSONObject、List、String之间的转换「建议收藏」一、JASSONArray转为JSONObject     JSONArrayresult_type=newJSONArray();       StringBuffercdsIdxType=newStringBuffer();       cdsIdxType.append("selectidfromtable_type");       result_type=…

    2022年6月3日
    45
  • html5游戏网页代码大全,HTML网页代码大全

    html5游戏网页代码大全,HTML网页代码大全1 贴图 2 加入连接 写上你想写的字 1 贴图 2 加入连接 写上你想写的字 3 在新窗口打开连接 写上要写的字消除连接的下划线在新窗口打开连接 写上你想写的字 4 移动字体 走马灯 写上你想写的字 5 字体加粗 写上你想写的字 6 字体斜体 写上你想写的字 7 字体下划线 写上你想写的字 8 字体删除线 写上你想写的字 9 字体加大 写上你想写的字 10 字体控制大小 写上你想写的字 其中字体大小可从

    2026年3月16日
    2
  • height:100vh的应用

    height:100vh的应用今天改移动端页面样式的时候因为height:100vh,导致我想超出部分滚动页面的效果没有做出来。就查查这玩意是啥意思。别人解释的height:100vhvh就是当前屏幕可见高度的1%,也就是说height:100vh==height:100%;但是当元素没有内容时候,设置height:100%,该元素不会被撑开,此时高度为0,但是设置height:100vh,该元素会被撑开屏幕高…

    2022年5月27日
    35

发表回复

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

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