實務上NLP Technology要面對的環境,往往充滿各種語言以及顏文字網路慣用語等,是非常嘈雜的語料。建議一般建立NLP資料的pipeline時,最好設計語言辨識,先把個語言分離獨立處理,再做後續的處理。
那又要怎麼辨識一個字串是什麼語言呢?
除了NLTK之外,這邊介紹一個實用的社群工具Polyglot來做,如果是做研究其實已經很夠用了。
Polyglot是支持大量多語言應用的自然語言處理pipeline。
- 免費軟體:GPLv3許可證
- Github:https://github.com/aboSamoor/polyglot
- 文檔:http : //polyglot.readthedocs.org。
❶ 安裝Polyglot
pip install polyglot
pip install PyICU
pip install pycld2
❷ 範例程式
假設現在有混合的兩組文本
- This is 台灣 Natural Language Processing 社團
- 台湾自然言語処理協会is台灣社團
第一句話是中英混合,第二句話是日英中混合,現在就來用Polyglot提供的API辨識吧,讓電腦能分辨語言。
Python
from polyglot.detect import Detector
mix_str = u"This is 台灣 Natural Language Processing 社團"
mix_str_2 = u"台湾自然言語処理協会is台灣社團"
for language in Detector(mix_str).languages:
print(language)
print("")
for language in Detector(mix_str_2).languages:
print(language)
執行程式之後,Polyglot會出出語言判斷:

如上圖,我們的程式可以作出約略判斷。更多詳細資料可以參考:https://polyglot.readthedocs.io/en/latest/Detection.html
本文允許重製、散布、傳輸以及修改,但不得為商業目的之使用
使用時必須註明出處自:楊明翰 , 台灣人工智慧與資料科學研究室 https://aistudio.tw