我在OS X(Lion 10.7.5)上安装了nltk的实现,用于Python2.7。
早期章节的基本无上下文语法工作非常出色,但当我尝试加载基于特征的无上下文语法的基本示例时,例如:
from __future__ import print_function
import nltk
from nltk import grammar, parse
g = """
% start DP
DP[AGR=?a] -> D[AGR=?a] N[AGR=?a]
D[AGR=[NUM='sg', PERS=3]] -> 'this' | 'that'
D[AGR=[NUM='pl', PERS=3]] -> 'these' | 'those'
D[AGR=[NUM='pl', PERS=1]] -> 'we'
D[AGR=[PERS=2]] -> 'you'
N[AGR=[NUM='sg', GND='m']] -> 'boy'
N[AGR=[NUM='pl', GND='m']] -> 'boys'
N[AGR=[NUM='sg', GND='f']] -> 'girl'
N[AGR=[NUM='pl', GND='f']] -> 'girls'
N[AGR=[NUM='sg']] -> 'student'
N[AGR=[NUM='pl']] -> 'students'
"""
grammar = grammar.FeatureGrammar.fromstring(g)
tokens = 'these girls'.split()
parser = parse.FeatureEarleyChartParser(grammar)
trees = parser.parse(tokens)
for tree in trees: print(tree)
(来自:http://www.nltk.org/howto/featgram.html)
...导致错误:
File "test_fcfg.py", line 18, in <module>
grammar = grammar.FeatureGrammar.fromstring(g)
File "/Library/Python/2.7/site-packages/nltk/grammar.py", line 796, in fromstring
encoding=encoding)
File "/Library/Python/2.7/site-packages/nltk/grammar.py", line 1270, in read_grammar
productions += _read_production(line, nonterm_parser, probabilistic)
File "/Library/Python/2.7/site-packages/nltk/grammar.py", line 1220, in _read_production
return [Production(lhs, rhs) for rhs in rhsides]
File "/Library/Python/2.7/site-packages/nltk/grammar.py", line 270, in __init__
self._hash = hash((self._lhs, self._rhs))
File "/Library/Python/2.7/site-packages/nltk/grammar.py", line 203, in __hash__
self.freeze()
File "/Library/Python/2.7/site-packages/nltk/featstruct.py", line 373, in freeze self._freeze(set())
File "/Library/Python/2.7/site-packages/nltk/featstruct.py", line 395, in _freeze
for (fname, fval) in sorted(self._items()):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
'__lt__': [('__gt__', lambda self, other: other < self),
...
...
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
'__lt__': [('__gt__', lambda self, other: other < self),
RuntimeError: maximum recursion depth exceeded while calling a Python object
(省略号......表示在它们出现之前和之后的行的多次重复)
谷歌搜索没有太多的使用;实际上,对于nltk的错误而言,它并没有太大的价值,这让我很吃惊。
我对错误信息的理解是语法.FeatureGrammar.fromstring(g)由于某种原因被捕获在看似无限循环的东西中。使用sys模块增加递归深度大小根本没有帮助;我只是等了一会儿才看到相同的错误信息。
我注意到其他nltk示例中的模块似乎已被移动;例如,文本“使用Python进行自然语言处理”经常使用“lp = nltk.LogicParser()”形式的命令,但此类似乎已移至nltk.sem.logic.LogicParser()。但是,这似乎不是当前问题的原因。
nltk中记录的错误消息是否有一个众所周知或明显的原因?而且,可能是一个纠正措施?
答案 0 :(得分:0)
问题似乎已解决!
根据StackOverflow问题,问题似乎与functools.py有关,而不是nltk: