python分词与去停用词简单实操

一、前期准备

**主要工具:**jieba
**数据介绍:**从万方数据平台中收集到的区块链技术领域的专利文献。
**stopwordsHIT.txt:**停用词表。
**userdict.txt:**用户词典。为提高分词精度,本词典中包含大量区块链技术领域专业词汇。

二、操作过程

1、原始数据

2、去停用词

####加载停用词
def load_stopword():
    f_stop = open('stopwordsHIT.txt', encoding='utf-8')  # 自己的中文停用词表
    sw = [line.strip() for line in f_stop]  # strip() 方法用于移除字符串头尾指定的字符(默认为空格)
    f_stop.close()
    return sw

3、分词

# 中文分词并且去停用词
def seg_word(sentence):
    file_userDict = 'userdict.txt'  # 自定义的词典
    jieba.load_userdict(file_userDict)# 加载用户词典

    sentence_seged = jieba.cut(sentence.strip(),HMM=True) # HMM参数可选可不选,默认为False
    stopwords = load_stopword()
    outstr = ''
    for word in sentence_seged:
        if word not in stopwords:
            if word != '/t':
                outstr += word
                outstr += " "
    return outstr

4、完整代码

首先定义方法。

import pandas as pd 
import jieba

####加载停用词
def load_stopword():
    f_stop = open('stopwordsHIT.txt', encoding='utf-8')  # 自己的中文停用词表
    sw = [line.strip() for line in f_stop]  # strip() 方法用于移除字符串头尾指定的字符(默认为空格)
    f_stop.close()
    return sw

# 中文分词并且去停用词
def seg_word(sentence):
    file_userDict = 'userdict.txt'  # 自定义的词典
    jieba.load_userdict(file_userDict)# 加载用户词典

    sentence_seged = jieba.cut(sentence.strip(),HMM=True) # HMM参数可选可不选,默认为False
    stopwords = load_stopword()
    outstr = ''
    for word in sentence_seged:
        if word not in stopwords:
            if word != '/t':
                outstr += word
                outstr += " "
    return outstr

原始文件的数据量较大,本文只选取其中10条作为演示。

df=pd.read_excel("Patent_data.xlsx")
data=df[0:10]
## 专利名称对于反应主题也有较大作用。分词时考虑专利名称与专利摘要
data["cutted_Content"]=data["名称"].apply(seg_word)+data["摘要"].apply(seg_word)

查看分词结果

data["cutted_Content"]

在这里插入图片描述
所有数据展示

data

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值