首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我生成这个NLTK语法的方式有问题吗?

我生成这个NLTK语法的方式有问题吗?
EN

Stack Overflow用户
提问于 2017-03-30 10:49:42
回答 1查看 507关注 0票数 0

我用NLTK编写了这个简单的程序,它只是打印出语法树。但是,即使正在创建RecursiveDescentParser,它也不会输出任何内容。我有什么问题?我对语法的定义有误吗?我试图迭代解析器的方式有问题吗?提前谢谢你。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-03-30 11:28:57

首先,一定要从一口一口开始写语法。

让我们从一个简单的句子Peace is rising开始。

我们需要结构S -> NP VP,其中:

  • VP是一个不及物动词短语,在这种情况下,is rising与辅助is连用,rise-ing进行词尾变化连用。
  • NP只是一个单独的名词。

代码

代码语言:javascript
复制
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))

输出

代码语言:javascript
复制
[Tree('NP', [Tree('N', ['peace'])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]

现在使用NP -> DT NP | N将限定词添加到NP中

代码语言:javascript
复制
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))

输出

代码语言:javascript
复制
[Tree('NP', [Tree('DT', ['the']), Tree('NP', [Tree('N', ['price'])])]), Tree('VP', [Tree('AUX', ['is']), Tree('V', ['rising'])])]

最后,我们可以使用NP -> NP PPPP -> P NP在NP中简单地添加PP结构

代码语言:javascript
复制
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))

这给了我们在顶部结果中最好的解析。

输出

代码语言:javascript
复制
[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'])])]

但它也伴随着一些令人讨厌的递归循环错误,看起来像这样:

代码语言:javascript
复制
  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 PPPP -> P NP规则可以无限地重复。如果您想知道原因,请尝试在StackOverflow上以单独问题的形式提问;P

一个简单的解决方案是使用try-except

代码语言:javascript
复制
try:
    for tree in parser.parse(sentence):
        print (list(tree))
except RecursionError:
    exit()

但那是丑陋的!相反,您可以使用ChartParser

代码语言:javascript
复制
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))

输出

代码语言:javascript
复制
[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'])])]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43107634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档