我用NLTK编写了这个简单的程序,它只是打印出语法树。但是,即使正在创建RecursiveDescentParser,它也不会输出任何内容。我有什么问题?我对语法的定义有误吗?我试图迭代解析器的方式有问题吗?提前谢谢你。
import nltk
'''The price of peace is rising.'''
grammar = nltk.CFG.fromstring("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "is" | "rising"
NP -> Det N | Det N PP
Det -> "the" | "of"
N -> "price" | "peace"
P -> "in" | "on" | "by" | "with"
""")
sentence = "the price of peace is rising"
wordArray = sentence.split()
print(wordArray)
parser = nltk.RecursiveDescentParser(grammar)
for tree in parser.parse(wordArray):
print(tree)发布于 2017-03-30 11:28:57
首先,一定要从一口一口开始写语法。
让我们从一个简单的句子Peace is rising开始。
我们需要结构S -> NP VP,其中:
is rising与辅助is连用,rise与-ing进行词尾变化连用。代码
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> NP VP
VP -> AUX V
V -> "rising"
AUX -> "is"
NP -> N
N -> "peace"
""")
parser = nltk.RecursiveDescentParser(your_grammar)
sentence = "peace is rising".split()
for tree in parser.parse(sentence):
print (list(tree))输出
[Tree('NP', [Tree('N', ['peace'])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]现在使用NP -> DT NP | N将限定词添加到NP中
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> NP VP
VP -> AUX V
V -> "rising"
AUX -> "is"
NP -> N | DT NP
N -> "peace" | "price"
DT -> "the"
""")
parser = nltk.RecursiveDescentParser(your_grammar)
sentence = "the price is rising".split()
for tree in parser.parse(sentence):
print (list(tree))输出
[Tree('NP', [Tree('DT', ['the']), Tree('NP', [Tree('N', ['price'])])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]最后,我们可以使用NP -> NP PP和PP -> P NP在NP中简单地添加PP结构
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> NP VP
VP -> AUX V
V -> "rising"
AUX -> "is"
NP -> N | DT NP | NP PP
N -> "peace" | "price"
DT -> "the"
PP -> P NP
P -> "of"
""")
parser = nltk.RecursiveDescentParser(your_grammar)
sentence = "the price of peace is rising".split()
for tree in parser.parse(sentence):
print (list(tree))这给了我们在顶部结果中最好的解析。
输出
[Tree('NP', [Tree('DT', ['the']), Tree('NP', [Tree('NP', [Tree('N', ['price'])]), Tree('PP', [Tree('P', ['of']), Tree('NP', [Tree('N', ['peace'])])])])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]但它也伴随着一些令人讨厌的递归循环错误,看起来像这样:
File "/usr/local/lib/python3.5/site-packages/nltk/tree.py", line 158, in __getitem__
return self[index[0]][index[1:]]
File "/usr/local/lib/python3.5/site-packages/nltk/tree.py", line 156, in __getitem__
return self[index[0]]
File "/usr/local/lib/python3.5/site-packages/nltk/tree.py", line 150, in __getitem__
if isinstance(index, (int, slice)):
RecursionError: maximum recursion depth exceeded in __instancecheck__这是因为nltk.RecursiveDescentParser试图以递归方式查找解析,因为NP -> NP PP和PP -> P NP规则可以无限地重复。如果您想知道原因,请尝试在StackOverflow上以单独问题的形式提问;P
一个简单的解决方案是使用try-except
try:
for tree in parser.parse(sentence):
print (list(tree))
except RecursionError:
exit()但那是丑陋的!相反,您可以使用ChartParser
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> NP VP
VP -> AUX V
V -> "rising"
AUX -> "is"
NP -> N | DT NP | NP PP
N -> "peace" | "price"
DT -> "the"
PP -> P NP
P -> "of"
""")
parser = nltk.ChartParser(your_grammar)
sentence = "the price of peace is rising".split()
for tree in parser.parse(sentence):
print (list(tree))输出
[Tree('NP', [Tree('NP', [Tree('DT', ['the']), Tree('NP', [Tree('N', ['price'])])]), Tree('PP', [Tree('P', ['of']), Tree('NP', [Tree('N', ['peace'])])])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]
[Tree('NP', [Tree('DT', ['the']), Tree('NP', [Tree('NP', [Tree('N', ['price'])]), Tree('PP', [Tree('P', ['of']), Tree('NP', [Tree('N', ['peace'])])])])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]https://stackoverflow.com/questions/43107634
复制相似问题