简体   繁体   English

Python NLTK:使用联合结构解析句子,进入无限递归

[英]Python NLTK: parse sentence using conjoint structure, getting into infinite recursion

I am asked to create two different parse tree for the following sentence:我被要求为以下句子创建two不同的解析树:

foo while bar and baz

Based on these two constructions:基于这两个结构:

S-> S while S
S-> S and S

The two different trees I have are the following:我拥有的两种不同的树如下:

Tree A)树 A)

     S
   / | \
  P  U  S
  |    /|\
  W   P U P  
      |   |
      W   W

Here is the code for A:这是A的代码:

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)

And the result for A: A的结果:

(S (P (W foo)) (U while) (S (P (W bar)) (U and) (P (W baz))))

Tree B)树 B)

       S
     / | \
    S  U  P
  / | \    \
 P  U  P    W
 |     |
 W     W

Now for part B, I just modified the grammar to the following:现在对于 B 部分,我只是将语法修改为以下内容:

groucho_grammar = nltk.CFG.fromstring ("""
S -> S U P | P U P
P -> W
U -> 'while' | 'and'
W -> 'foo'|'bar'|'baz'
""")

But I am getting infinite recursion error:但我收到无限递归错误:

    if isinstance(index, (int, slice)):
RuntimeError: maximum recursion depth exceeded in __instancecheck__

Any help would be appreciated.任何帮助,将不胜感激。

Thanks.谢谢。

Your problem is this rule: S -> SUP | PUP你的问题是这个规则: S -> SUP | PUP S -> SUP | PUP

By allowing S to begin with an instance of S, you allow this infinite recursion:通过允许 S 以 S 的一个实例开始,您就允许了这种无限递归:

S -> S U P
S -> (S U P) U P
S -> ((S U P) U P) U P
S -> (((S U P) U P) U P) U P

This is called left recursion, and it is caused by a symbol expanding to itself, in this case S expanding to S.这称为左递归,它是由符号扩展到自身引起的,在这种情况下,S 扩展到 S。

From the NLTK book, chapter 8 :NLTK 书,第 8 章

Recursive descent parsing has three key shortcomings.递归下降解析有三个主要缺点。 First, left-recursive productions like NP -> NP PP send it into an infinite loop.首先,像 NP -> NP PP 这样的左递归产生式将其送入无限循环。

A solution一个办法

Luckily, you can simply change the parser you use to one that does not share the left-recursive Achilles heel.幸运的是,您可以简单地将您使用的解析器更改为不共享左递归阿喀琉斯之踵的解析器。 Simple change this:简单改变这个:

rd_parser = nltk.RecursiveDescentParser(groucho_grammar)

to this:对此:

rd_parser = nltk.parse.chart.BottomUpLeftCornerChartParser(groucho_grammar)

This way you make use of the left-recursive-resistant BottomUpLeftCornerChartParser这样你就可以使用抗左递归的BottomUpLeftCornerChartParser

Further reading进一步阅读

The left-recursive problem is well-known in automata theory.左递归问题在自动机理论中是众所周知的。 There are ways to make your grammar non-recursive, as explained in these links:有一些方法可以使您的语法成为非递归的,如以下链接中所述:

  1. http://www.cs.engr.uky.edu/~lewis/essays/compilers/rec-des.html http://www.cs.engr.uky.edu/~lewis/essays/compilers/rec-des.html
  2. http://www.umsl.edu/~kjs9rc/CS4890/presentation.pdf http://www.umsl.edu/~kjs9rc/CS4890/presentation.pdf
  3. http://research.microsoft.com/pubs/68869/naacl2k-proc-rev.pdf http://research.microsoft.com/pubs/68869/naacl2k-proc-rev.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM