我已經在OS X(Lion 10.7.5)上安裝了nltk的實現,用於Python2.7。NLTK FCFG的:超過最大遞歸深度
的前幾章的基本上下文無關文法出色的工作,但是當我試圖加載,基於特徵的上下文無關文法的連基本的例子,如:
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的錯誤總體來說並沒有太大的價值,這讓我很驚訝。
我對錯誤信息的理解是,出於某種原因grammar.FeatureGrammar.fromstring(g)被抓到了看起來是無限循環的東西。用sys模塊增加遞歸深度的大小根本沒有幫助;在看到相同的錯誤信息之前,我只是稍等一會兒。
我注意到與其他nltk示例模塊似乎已經移動;例如,文本「使用Python進行自然語言處理」經常使用形式爲'lp = nltk.LogicParser()'的命令,但該類似乎已移至nltk.sem.logic.LogicParser()。但是,這似乎不是當前問題的原因。
是否有一個衆所周知的或顯而易見的錯誤信息記錄在nltk中?而且,可能是一個糾正?