是否有一个库或其他简单的方法来检测 Python 中属于哪些字母字符?我知道我可以为此使用 unicode 代码范围,但如果已经有内置方式或库或提供映射的类似方式,我宁愿不重新发明轮子。
注意:我问的是alphabet 而不是language。 “hello”和“hola”都将映射到拉丁字母,而“Поиск”将映射到西里尔字母。
最佳答案
Python 的 unicodedata在这里非常有帮助 this question/answer
如果不编写整个模块,我找不到任何检测语言的简单方法,而且我认为我会遇到很多极端情况,所以我编写了一个库。 Github页面是here .有了它,您就可以:
pip install alphabet-detector
然后直接使用:
from alphabet_detector import AlphabetDetector
ad = AlphabetDetector()
ad.only_alphabet_chars(u"ελληνικά means greek", "LATIN") #False
ad.only_alphabet_chars(u"ελληνικά", "GREEK") #True
ad.only_alphabet_chars(u"frappé", "LATIN") #True
ad.only_alphabet_chars(u"hôtel lœwe", "LATIN") #True
ad.only_alphabet_chars(u"123 ångstrom ð áß", "LATIN") #True
ad.only_alphabet_chars(u"russian: гага", "LATIN") #False
ad.only_alphabet_chars(u"гага", "CYRILLIC") #True
我还写了几个主要语言的便捷方法:
ad.is_cyrillic(u"гага") #True
ad.is_latin(u"howdy") #True
ad.is_cjk(u"hi") #False
ad.is_cjk(u'汉字') #True
关于python - 检测Python中属于哪些字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28756796/