1. 程式人生 > 其它 >jieba分詞去除停用詞

jieba分詞去除停用詞

jieba分詞去除停用詞

import jieba
import re
# 停用詞
# 建立停用詞列表
def get_stopwords_list():
    stopwords = [line.strip() for line in open('停用詞.txt',encoding='UTF-8').readlines()]
    return stopwords
# 對句子進行中文分詞
def seg_depart(sentence):
    # 對文件中的每一行進行中文分詞
    sentence_depart = jieba.lcut(sentence.strip())
    return sentence_depart
def remove_digits(input_str):
    punc = u'0123456789.'
    output_str = re.sub(r'[{}]+'.format(punc), '', input_str)
    return output_str
# 去除停用詞
def move_stopwords(sentence_list, stopwords_list):
    # 去停用詞
    out_list = []
    for word in sentence_list:
        if word not in stopwords_list:
            if not remove_digits(word):
                continue
            if word != '\t':
                out_list.append(word)
    return out_list
sentence = '1、判令被告趙軍霞償還原告借款本息及應收費用共計4278.6元(計算至2017年1月10日,實際還款額以合同約定的計費方式計算至最終還款日)'
stopwords = get_stopwords_list()
sentence_depart = seg_depart(sentence)
print(sentence_depart)
sentence_depart = move_stopwords(sentence_depart, stopwords)
print(sentence_depart)
['1', '、', '判令', '被告', '趙軍', '霞', '償還', '原告', '借款', '本息', '及', '應收', '費用', '共計', '4278.6', '元', '(', '計算', '至', '2017', '年', '1', '月', '10', '日', ',', '實際', '還款額', '以', '合同', '約定', '的', '計費', '方式', '計算', '至', '最終', '還款', '日', ')']

['判令', '被告', '趙軍', '霞', '償還', '原告', '借款', '本息', '應收', '費用', '共計', '元', '計算', '還款額', '合同', '約定', '計費', '方式', '計算', '最終', '還款']