我想要得到這樣的:Python:如何確定語言?
Input text: "ру́сский язы́к"
Output text: "Russian"
Input text: "中文"
Output text: "Chinese"
Input text: "にほんご"
Output text: "Japanese"
Input text: "العَرَبِيَّة"
Output text: "Arabic"
我怎麼能做到這一點在Python?謝謝。
我想要得到這樣的:Python:如何確定語言?
Input text: "ру́сский язы́к"
Output text: "Russian"
Input text: "中文"
Output text: "Chinese"
Input text: "にほんご"
Output text: "Japanese"
Input text: "العَرَبِيَّة"
Output text: "Arabic"
我怎麼能做到這一點在Python?謝謝。
你吃過看看langdetect?
from langdetect import detect
lang = detect("Ein, zwei, drei, vier")
print lang
#output: de
您可以嘗試確定輸入字符串中的Unicode字符組的指出語言的類型,(西里爾俄羅斯,例如),然後搜索文本中的特定語言的符號。
TextBlob。需要NLTK套件,使用Google。
b = TextBlob("bonjour")
b.detect_language()
pip install textblob
Polyglot。需要numpy和一些神祕的庫,不太可能讓它在Windows上運行。能夠檢測混合語言的文本。
mixed_text = u"""
China (simplified Chinese: 中國; traditional Chinese: 中國),
officially the People's Republic of China (PRC), is a sovereign state
located in East Asia.
"""
for language in Detector(mixed_text).languages:
print(language)
pip install polyglot
chardet還具有檢測語言的特徵,如果有在範圍(字符的字節127-255]:
>>> chardet.detect("Я люблю вкусные пампушки".encode('cp1251'))
{'encoding': 'windows-1251', 'confidence': 0.9637267119204621, 'language': 'Russian'}
pip install chardet
langdetect需要大部分文本。它使用非確定性方法。這意味着對於相同的文本樣本你會得到不同的結果。文件說,你必須使用下面的代碼,使之確定:
from langdetect import detect, DetectorFactory
DetectorFactory.seed = 0
detect('今一はお前さん')
pip install langdetect
pip install guess_language-spirit
那你試試? – Raskayu
這可能有助於http://stackoverflow.com/questions/4545977/python-can-i-detect-unicode-string-language-code –