python jieba分词如何去除停用词

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python jieba分词如何去除停用词相关的知识,希望对你有一定的参考价值。

-*- coding: utf-8 -*-
import jieba
import jieba.analyse
import sys
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')

#使用其他编码读取停用词表
#stoplist = codecs.open('../../file/stopword.txt','r',encoding='utf8').readlines()
#stoplist = set(w.strip() for w in stoplist)
#停用词文件是utf8编码
stoplist = .fromkeys([ line.strip() for line in open("../../file/stopword.txt") ])

#经过分词得到的应该是unicode编码,先将其转成utf8编码
参考技术A import jieba

# 创建停用词list
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('./test/stopwords.txt')  # 这里加载停用词的路径
    outstr = ''
    for word in sentence_seged:
        if word not in stopwords:
            if word != '\\t':
                outstr += word
                outstr += " "
    return outstr


inputs = open('./test/input.txt', 'r', encoding='utf-8')
outputs = open('./test/output.txt', 'w')
for line in inputs:
    line_seg = seg_sentence(line)  # 这里的返回值是字符串
    outputs.write(line_seg + '\\n')
outputs.close()
inputs.close()

用python对单一微博文档进行分词——jieba分词(加保留词和停用词)

当爬取完所需微博保存在一个csv文件中后,可用如下代码对其进行分词、保留所需词、去除停用词操作,并将分词结果放在新的文档中。

停用词和保留词网上都能搜到,我们也可以另外对停用词表进行编辑,也可以在保留词表中加入专业词汇。

目前很多我们常用的词汇jieba分词都无法识别,比如“微博热搜”,这里我也列举了一些我们可以加入保留词表中常用的单词:(我本人搜集的是科技相关微博,所以里面很多次都跟科技相关)

热搜

带节奏 
搞事情
社交平台
牛逼
大V
营销号
公众号
区块链
人工智能
云计算
深度学习
机器学习
雷达技术
全自动物流
自动驾驶
无人驾驶
互联网企业
语音识别
图像识别
智慧城市
智慧交通
制造行业
标配
用户需求
刷脸
核心业务
字节跳动
社交媒体

import jieba
import re
import csv

# 创建停用词列表
def stopwordslist():
    stopwords = [line.strip() for line in open('E:/Chinese_stop_words.txt',encoding='UTF-8').readlines()]
    return stopwords

def processing(text):
    """
    数据清洗, 可以根据自己的需求进行重载
    """
    text = re.sub("@.+?( |$)", "", text)           # 去除 @xxx (用户名)
    text = re.sub("【.+?】", "", text)             # 去除 【xx】 (里面的内容通常都不是用户自己写的)
    text = re.sub(".*?:", "", text)                #去除微博用户的名字
    text = re.sub("#.*#", "", text)                #去除话题引用
    text = re.sub("\\n","",text)
    return text

# 对句子进行中文分词
def seg_depart(sentence):
    jieba.load_userdict('E:/保留词.txt')
    sentence_depart = jieba.cut(sentence.strip())
    print(sentence_depart)
    stopwords = stopwordslist()        # 创建一个停用词列表
    outstr = ''        # 输出结果为outstr
    for word in sentence_depart:          # 去停用词
        if word not in stopwords:
            if word != '\\t':
                outstr += word
                outstr += " "
    return outstr

# 给出文档路径
filename = "E:/data/input.csv"   #原文档路径
outputs = open("E:/data/output.csv", 'w', encoding='UTF-8')  #输出文档路径
with open(filename, 'r', encoding='utf-8-sig') as csvfile:
    reader = csv.reader(csvfile,delimiter=',',quotechar='"',doublequote=False)
    for line in reader:
        print(line[0])     #微博在文档的第一列
        line = processing(line[0])
        line_seg = seg_depart(line)
        outputs.write(line_seg + '\\n')
outputs.close()
print("分词成功!!!")

以上是关于python jieba分词如何去除停用词的主要内容,如果未能解决你的问题,请参考以下文章

python怎么去除停用词的

python使用jieba实现中文文档分词和去停用词

中文分词-jieba#人工智能

[Python]jieba切词 添加字典 去除停用词单字 python 2020.2.10

jieba分词的词性表

jieba分词怎样 去掉或替换 默认词库里的词