Python 2.7 NLTK CFG解析器无法解析葡萄牙语单词

Python 2.7 NLTK CFG解析器无法解析葡萄牙语单词,python-2.7,nltk,anaconda,Python 2.7,Nltk,Anaconda,我试图使用NLTK CFG解析器,但出现了错误“语法不包含一些输入单词”。我使用的代码是: import nltk import codecs strProductions = '' f = codecs.open('C://nltk_data//corpora//CINTIL_TreeBank//producoes_S.txt', 'r', encoding= 'latin-1') for line in f: strProductions= strProductions +

我试图使用NLTK CFG解析器,但出现了错误“语法不包含一些输入单词”。我使用的代码是:

import nltk
import codecs
strProductions = ''
f = codecs.open('C://nltk_data//corpora//CINTIL_TreeBank//producoes_S.txt', 'r', 
encoding= 'latin-1')
for line in f:    
    strProductions= strProductions + line
f.close()
grammar = nltk.grammar.CFG.fromstring(strProductions)
cp = nltk.ChartParser(grammar)
print grammar

S -> V PNT
V -> 'Choveu'    
NP -> DEM N
PP -> P NP
P -> 'de'
NP -> N_
N_ -> N A
N -> 'crian\\xe7a'

tokens = []    
a = u'criança'
b = '.'
a= a.encode('latin-1')
for tree in cp.parse(tokens):       
    print tree
C:\Anaconda2\lib\site-packages\nltk\grammar.pyc in check_coverage(self, tokens)
629             missing = ', '.join('%r' % (w,) for w in missing)
630             raise ValueError("Grammar does not cover some of the "
--> 631                              "input words: %r." % missing)
632 
633     def _calculate_grammar_forms(self):

ValueError: Grammar does not cover some of the input words:
u"'crian\\xe7a'".
有人能帮我确定发生了什么吗


提前感谢

如果将特殊字符“\\xe7”替换为常规ASCII“c”,会发生什么情况?(在语法和a两方面)它都有效。当我使用葡萄牙语的扩展字符,如ç、á、á、ã等时,问题就出现了。我认为这与CFG类有关。它似乎不适用于编码。如果将特殊字符“\\xe7”替换为常规ASCII“c”,会发生什么情况?(在语法和a两方面)它都有效。当我使用葡萄牙语的扩展字符,如ç、á、á、ã等时,问题就出现了。我认为这与CFG类有关。看起来它与编码不兼容。