NLP 預(yù)處理總結(jié)

在處理 NLP 相關(guān)任務(wù)的時候(文本分類、聚類,智能客服等),首要任務(wù)是對文本數(shù)據(jù)進行預(yù)處理。結(jié)合自己的實踐經(jīng)驗,總結(jié)了 N 條預(yù)處理的方法。

去掉一些無用的符號

文本中可能會出現(xiàn)連續(xù)的符號(比如感嘆號?。?!或一些奇怪的單詞等。)我們將文本按照符號進行分割然后再組裝。

def tokenizer(ori_list):
   SYMBOLS = re.compile('[\s;\"\",.!?\\/\[\]]+')
   new_list = []
   for q in ori_list:
       words=SYMBOLS.split(q.lower().strip())
       new_list.append(' '.join(words))
   return new_list

停用詞過濾

網(wǎng)上有很多開源的停用詞集合,也可以根據(jù)自己業(yè)務(wù)建立領(lǐng)域停用詞表。(或者直接使用NLTK自帶的)

def removeStopWord(ori_list):
   new_list = []
   #nltk中stopwords包含what等,但是在QA問題中,這算關(guān)鍵詞,所以不看作stop words
   restored = ['what','when','which','how','who','where']
   english_stop_words = list(set(stopwords.words('english')))
   for w in restored:
       english_stop_words.remove(w)
   for q in ori_list:
       sentence = ' '.join([w for w in q.strip().split(' ') if w not in english_stop_words])
       new_list.append(sentence)
   return new_list

去掉出現(xiàn)頻率很低的詞

我們?nèi)コ皖l詞,可以基于詞典設(shè)置一個閾值,比如出現(xiàn)次數(shù)少于10,20....

def removeLowFrequence(ori_list,vocabulary,thres = 10):
    #根據(jù)thres篩選詞表,小于thres的詞去掉
    new_list = []
    for q in ori_list:
        sentence = ' '.join([w for w in q.strip().split(' ') if w in vocabulary and vocabulary[w] >= thres])
        new_list.append(sentence)
    return new_list

對于數(shù)字的處理

分詞完只有有些單詞可能就是數(shù)字比如44,415,把所有這些數(shù)字都看成是一個單詞,這個新的單詞我們可以定義為 "#number"

def replaceDigits(ori_list,replace = '#number'):
    #將數(shù)字統(tǒng)一替換replace,默認#number
    DIGITS = re.compile('\d+')
    new_list = []
    for q in ori_list:
        q = DIGITS.sub(replace,q)
        new_list.append(q)
    return new_list

關(guān)于我

dreampai(公眾號,簡書,知乎同名),專注于 NLP和金融。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容