1. 程式人生 > >文本預處理常用操作

文本預處理常用操作

words 是我 text github tro 標點符號 != util item

這裏介紹一下文本預處理中常用的操作:

1.英文統一小寫

text = text.lower()

2.分詞

    def cut(text):
        # return list(jieba.cut(text))
        return [item for item in jieba.cut(text.lower())] if text != "" else []

3.去噪

兩種方式

(1)去停用詞

包括中英文標點符號、以及噪音詞,參考附錄[1]

    stopwords = set([line.strip() for line in
codecs.open("data/stopwords.txt", "r")]) def cut_and_remove_stopwords(text): return [item for item in jieba.cut(text.lower()) if item not in Utils.stopwords] if text != "" else []

(2)只保留指定詞典中的詞

這個詞典與任務強相關,通常是當前任務重點關註的特征詞

    def cut_and_in_vocabulary(text):
        return [item for
item in jieba.cut(text.lower()) if item in Utils.vocabulary] if text != "" else []

其中,為了保證分詞的結果是我們想要的,通常需要調整jieba詞典

    file_vocabulary = "data/vocabulary.txt"
    jieba.load_userdict(file_vocabulary)
    vocabulary = set([line.strip() for line in codecs.open(file_vocabulary, "r")])

    file_jieba_delete_dict 
= "data/jieba_delete_dict.txt" for wd in [line.strip() for line in codecs.open(file_jieba_delete_dict, "r")]: jieba.del_word(wd)

詳細說明參考:fxsjy/jieba: 結巴中文分詞

附錄[1]:停用詞表(其中有兩行分別是中英文的空格)

,
.
?
!
 
,
。
?
!
不好意思
抱歉
謝謝
這邊
那邊
那個
這個
那樣
這種
那種
我想
這兒
這樣
還
也
額
呃
嗯
噢
那
哎
先
後
啊
哦
吧
呀
啦
哈
誒
咯
恩
阿
呢
嗎
的
了

待補充~

文本預處理常用操作