检查字符串是否按字母顺序排列为英语以外的语言

时间:2015-12-14 19:13:05

标签: python text unicode nlp

我正在处理混合使用多种语言的文本数据。现在尝试测试令牌/字符串是否按字母顺序排列,这意味着可能是一个单词。 是否有一些内置函数,如'somestring'.isAlpha()来测试字符串是否按字母顺序排列其他语言(葡萄牙语和西班牙语)?我尝试了'ó'.isalpha(),返回False

我现在想到的是获取Unicode表。找到开头和结尾字母并测试字母是否在字母范围内。

2 个答案:

答案 0 :(得分:2)

这会解决您的问题吗?

>>> u'é'.isalpha()
True

正如一个FYI,以下示例在Python 3中完美运行:

words = ['você', 'quer', 'uma', 'maçã']
for word in words:
    word.isalpha()

在python 2中,您可以执行以下操作:

for word in words:
    unicode(word, "utf-8").isalpha()

答案 1 :(得分:0)

这个库不是来自NLTK,但肯定有帮助。

1)安装langdetect Library $ pip install langdetect

支持的Python版本2.6,2.7,3.x。

2)测试你的代码

>>> from langdetect import detect

>>> detect("War doesn't show who's right, just who's left.")
'en'
>>> detect("Ein, zwei, drei, vier")
'de'

参考链接:

https://pypi.python.org/pypi/langdetect