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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java中main方法的运行

    java中main方法的运行转载自:https://blog.csdn.net/WGYH_3767/article/details/76933676(最近要把一个main方法启动的项目集入web项目里,参考了main方法的运行机制才解决。)学过java的都知道main方法是学习java的开始,也是程序的入口,不过你有多少个类或程序,线程,他们的入口方法都是main()。main方法是一个静态的…

    2022年5月20日
    50
  • leetcode 接雨水2_雨水口连接管

    leetcode 接雨水2_雨水口连接管题目链接给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。示例 1:输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]输出:6解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。示例 2:输入:height = [4,2,0,3,2,5]输出:9 提示:n == height.length0 <= n &lt

    2022年8月8日
    8
  • Postman报错Unsupported Media Type

    Postman报错Unsupported Media Type报错信息如下:{“timestamp”:”2018-12-06T01:37:11.184+0000″,”status”:415,”error”:”UnsupportedMediaType”,”message”:”Contenttype’text/plain;charset=UTF-8’notsupported”,”path”:”/testgu/ycyzha…

    2022年5月9日
    230
  • nginx实现负载均衡配置_nginx负载性能

    nginx实现负载均衡配置_nginx负载性能nginxnginx的安装下载nginx安装包并解压tarzxfnginx-1.14.0.tar.gz对nginx源码文件进行修改,隐藏版本号cd/root/nginx-1.14.0/src/core/vimnginx.h14#defineNGINX_VER&quot;nginx/&quot;#不显示版本号cd/root/nginx-1….

    2022年8月24日
    10
  • Java和Python哪个更适合初学者的问题

    Java和Python哪个更适合初学者的问题我个人不是很喜欢讨论这个问题,为什么呢,每个人都学习能力不一样,你要是不行,哪个对于你也不简单。客观分析,这两种语言都在程序员的工具箱中都占有一席之地。学习哪个并不重要,个人觉得对于入门级同学来说,你要学的是“编程思想的基础知识“,而不是”特定的编程语言“。那这两门语言有区别吗?当然有,对于有一定编程经验的人来说…

    2022年7月7日
    20
  • Linux安装gcc的四种方法「建议收藏」

    Linux安装gcc的四种方法「建议收藏」相比于windows系统,Linux安装程序就比较复杂了,很多需要root用户才能安装。常见的有以下几种安装方法 源码安装 rpm包安装 yum安装(RedHat、CentOS) apt-get安装(debian,ubuntu) 源码安装以安装gcc为例,登陆https://gcc.gnu.org/,下载自己想要的版本的gcc安装包上传gcc-4.1.2.tar.gz到Linux服务器任意目录,解压解压目录执行shell命令./configurat.

    2022年5月26日
    90

发表回复

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

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