我要求創建two
不同的解析樹下面的句子:的Python NLTK:採用聯合結構解析句子,進入無限遞歸
foo while bar and baz
基於這兩種結構:
S-> S while S
S-> S and S
我有兩種不同的樹種:
樹A)
S
/| \
P U S
| /|\
W P U P
| |
W W
下面是一個的代碼:
import nltk
groucho_grammar = nltk.CFG.fromstring ("""
S -> P U S | P U P
P -> W
U -> 'while' | 'and'
W -> 'foo'|'bar'|'baz'
""")
print(groucho_grammar)
sentence = "foo while bar and baz"
rd_parser = nltk.RecursiveDescentParser(groucho_grammar)
for tree in rd_parser.parse(sentence.split()):
print(tree)
而結果對於A:)
(S (P (W foo)) (U while) (S (P (W bar)) (U and) (P (W baz))))
樹乙
S
/| \
S U P
/| \ \
P U P W
| |
W W
現在對於部分B,I只是將語法修改爲以下內容:
groucho_grammar = nltk.CFG.fromstring ("""
S -> S U P | P U P
P -> W
U -> 'while' | 'and'
W -> 'foo'|'bar'|'baz'
""")
但我得到無限遞歸錯誤:
if isinstance(index, (int, slice)):
RuntimeError: maximum recursion depth exceeded in __instancecheck__
任何幫助,將不勝感激。
謝謝。
謝謝你這麼多。謝謝。 – Apha