使用jieba分词并去除停用词流程程序

本文介绍了一种使用jieba进行中文文本分词的方法,并通过自定义停用词列表来提升文本处理的质量。该过程包括构建文件夹结构、加载停用词、分词以及去除停用词等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

① 构建未分词文件、已分词文件两个文件夹,将未分词文件夹按类目定义文件名,各个类目的文件夹下可放置多个需要分词的文件。

② 准备一份停用词(jieba自身应该是没有停用词的)

③ 根据业务需要自定义词典(此处使用jieba自带字典)

 

分词去停词.py

""" 
@file: 分词去停词.py
@Time: 2018/08/27
@Author:hnq
"""
#本程序主要用于jieba分词,以及去除停用词

import os
import jieba

# 保存文件的函数
def savefile(savepath,content):
    fp = open(savepath,'w',encoding='utf8',errors='ignore')
    fp.write(content)
    fp.close()

# 读取文件的函数
def readfile(path):
    fp = open(path, "r", encoding='utf8', errors='ignore')
    content = fp.read()
    fp.close()
    return content

## 去除停用词的2个函数
# 创建停用词list
def stopwordslist(filepath):
    stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
    return stopwords

# 对句子去除停用词
def movestopwords(sentence):
    stopwords = stopwordslist('stop_words.txt')  # 这里加载停用词的路径
    outstr = ''
    for word in sentence:
        if word not in stopwords:
            if word != '\t'and'\n':
                outstr += word
                # outstr += " "
    return outstr

if __name__ == '__main__':

    corpus_path = "语料/train/"  # 未分词分类预料库路径
    seg_path = "语料/train_seg/"  # 分词后分类语料库路径

    catelist = os.listdir(corpus_path)  # 获取未分词目录下所有子目录
    for mydir in catelist:
        class_path = corpus_path + mydir + "/"  # 拼出分类子目录的路径
        seg_dir = seg_path + mydir + "/"  # 拼出分词后预料分类目录
        if not os.path.exists(seg_dir):  # 是否存在,不存在则创建
            os.makedirs(seg_dir)

        file_list = os.listdir(class_path) # 列举当前目录所有文件
        for file_path in file_list:
            fullname = class_path + file_path # 路径+文件名
            print("当前处理的文件是: ",fullname)  # 语料/train/pos/pos.txt
                            #  语料/train/neg/neg1.txt

            content = readfile(fullname).strip()  # 读取文件内容
            content = content.replace("\n", "").strip()  # 删除换行和多余的空格
            content_seg = jieba.cut(content)    # jieba分词
            print("jieba分词后:",content_seg)
            listcontent = ''
            for i in content_seg:
                listcontent += i
                listcontent += " "
                # listcontent.replace(' ','\n').replace('  ','\n')
            print(listcontent[0:10])
            listcontent = movestopwords(listcontent)    # 去除停用词
            print("去除停用词后:", listcontent[0:10])
            listcontent = listcontent.replace("   ", "\n").replace("  ", "\n")
            savefile(seg_dir + file_path, "".join(listcontent)) # 保存

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值