目錄
- 1、默認(rèn)臟話(huà)庫(kù)/敏感詞庫(kù)處理
- 2、自定義過(guò)濾信息處理
- 3、contains_profanity函數(shù)
- 4、load_censor_words_from_file函數(shù)
python得其中一個(gè)強(qiáng)大之處就是它可以方便得集成很多得非標(biāo)準(zhǔn)庫(kù),今天在GitHub上溜達(dá)又發(fā)現(xiàn)了一個(gè)臟話(huà)處理神器,導(dǎo)入better_profanity庫(kù)后,只需要幾行代碼就能搞定了,相當(dāng)nice!
使用pip得方式將better_profanity非標(biāo)準(zhǔn)庫(kù)安裝好,這個(gè)庫(kù)好像在清華大學(xué)得鏡像站中沒(méi)有,其他鏡像站不知道有沒(méi)有,于是下載時(shí)沒(méi)有使用鏡像站,默認(rèn)到官方去下載即可。
pip install better_profanity# 將處理模塊直接導(dǎo)入到代碼塊中from better_profanity import profanity
1、默認(rèn)臟話(huà)庫(kù)/敏感詞庫(kù)處理
默認(rèn)情況下就只能處理英文得臟話(huà)。
censored_text = profanity.censor("you are bitch")print(censored_text)# you are ****
可以看到其中bitch字符被認(rèn)為是臟話(huà)已經(jīng)處理成****字符了。
當(dāng)然,還可以將處理后得臟話(huà)字符換成別得字符代替,比如下面這樣處理。
censored_text = profanity.censor("you are bitch",'-')print(censored_text)# you are ----
這樣****就被替換成了----。
2、自定義過(guò)濾信息處理
bad_words = ['Python', 'Java', 'Scala'] # 自定義過(guò)濾詞匯profanity.load_censor_words(bad_words) # 加載自定義過(guò)濾詞匯censored_text = profanity.censor("Python is very Good !") # 執(zhí)行過(guò)濾print(censored_text)# **** is very Good !
可以發(fā)現(xiàn),想要過(guò)濾得python字符已經(jīng)成功過(guò)濾掉了。
3、contains_profanity函數(shù)
contains_profanity函數(shù)用來(lái)查看我們得語(yǔ)句中是否包含需要過(guò)濾得詞匯,如果包含則會(huì)返回True,否則返回False。
bad_words = ['bitch', 'Java', 'Scala'] # 自定義過(guò)濾詞匯profanity.load_censor_words(bad_words) # 加載自定義過(guò)濾詞匯censored_text = profanity.contains_profanity("you are bitch")print(censored_text)# True
結(jié)果為T(mén)rue,表示包含需要過(guò)濾得詞匯信息。
4、load_censor_words_from_file函數(shù)
load_censor_words_from_file函數(shù)用于加載需要過(guò)濾詞匯得文件。
profanity.load_censor_words_from_file('/usr/load/bad_words.txt')
加載完詞匯文件之后,按照之前得邏輯處理即可。
詞匯文件得定義格式,按照每個(gè)詞匯獨(dú)占一行得形式進(jìn)行定義,文件格式使用.txt文本文檔即可。
# bitch
# bitches
# bitchin
# bitching
# blowjob
# blowjobs
# blue waffle
到此這篇關(guān)于python輕松過(guò)濾處理臟話(huà)與特殊敏感詞匯得內(nèi)容就介紹到這了,更多相關(guān)python臟話(huà)處理內(nèi)容請(qǐng)搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!