我正在处理telugu文本以分析几个文本标记。Python可读格式的非英文(telugu)文本
>>> sent = "నా పేరు కరీం ఉంది. నేను భారత ఆహార ప్రేమ.".decode('utf-8')
>>> text = sent
>>> text = nltk.word_tokenize(text)
>>> result = nltk.pos_tag(text)
>>> for val in result:
... print list(val)[0], list(val)[1]
...
నా JJ
పేరు NNP
కరీం NNP
ఉంది NNP
. .
నేను VB
భారత JJ
ఆహార NNP
ప్రేమ NNP
这样我可以看到在泰卢固语的结果。
对于同样的文字,当我尝试这种方式时,它给出了unicode字符串的结果。 如何在telugu打印令牌?
>>> s = "నా పేరు కరీం ఉంది. నేను భారత ఆహార ప్రేమ.".decode('utf-8')
>>> res = s.split(' ')
>>> res[0]
u'\u0c28\u0c3e'
>>> type(res[0])
<type 'unicode'>
>>> res[0].encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> res[0].encode('utf-8')
'\xe0\xb0\xa8\xe0\xb0\xbe'
编辑:
print res[0]
正确地给了它。但是,当我执行把这个代码放入.py脚本并运行它。它给
[email protected]:~/Documents/codes$ python test.py
File "test.py", line 1
SyntaxError: Non-ASCII character '\xe0' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
test.py包含:
s = "నా పేరు కరీం ఉంది. నేను భారత ఆహార ప్రేమ.".decode('utf-8')
a = s.split()
for i in a:
print i
尝试'打印清晰度[0]'来代替。 – nu11p01n73R
你的问题是什么?为什么不只是'print()'结果:'print(res [0])'? – Leon
对不起,我的错。打印给它正确。但问题仍然存在。我很感激,如果你可以看到有问题的编辑 – user123