[Python3] 简单的jieba分词以及停用词去除

本文主要为【爬取百度搜索内容页广告均数】提供关键词文件,主要做输入文件的分词功能,并写入key_word.txt文件,以供下一模块使用。

https://blog.csdn.net/qq_36791314/article/details/86724025

函数功能主要为调用简单的jiaba分词(stripdata函数)并进行停用词去除(stripword函数)
main函数为creat(),可修改为if __name__ ==’__main__’: 进行调用。

文件解释:

  • Rawdata 初始数据,即一个段落或文章
  • stop 停用词文件,用\n间隔
  • keyword 关键词表
import jieba

#分词
def stripdata(Test):
    # jieba 默认启用了HMM(隐马尔科夫模型)进行中文分词
    seg_list = jieba.cut(Test,cut_all=True)  # 分词

    #获取字典,去除停用词
    line = "/".join(seg_list)
    word = stripword(line)
    #print(line)
    #列出关键字
    print("\n关键字:\n"+word)

#停用词分析
def stripword(seg):
    #打开写入关键词的文件
    keyword = open('key_word.txt', 'w+', encoding='utf-8')
    print("去停用词:\n")
    wordlist = []

    #获取停用词表
    stop = open('stopword.txt', 'r+', encoding='utf-8')
    stopword = stop.read().split("\n")

    #遍历分词表
    for key in seg.split('/'):
        #print(key)
        #去除停用词,去除单字,去除重复词
        if not(key.strip() in stopword) and (len(key.strip()) > 1) and not(key.strip() in wordlist) :
            wordlist.append(key)
            print(key)
            keyword.write(key+"\n")

    #停用词去除END
    stop.close()
    keyword.close()
    return '/'.join(wordlist)

def creat():
    Rawdata = open('raw.txt','r+',encoding='utf-8')
    text = Rawdata.read()
    #调用分词
    stripdata(text)

    #END
    Rawdata.close()

  • 10
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值