NLTK CFG语法有多个单词

时间:2015-03-27 14:02:16

标签: python parsing nlp nltk python-3.4

NLTK 3.0:

使用如下的CFG配置(非终端团队有4个值,其中1个值有2个单词(sri lankan)。

当我生成可能代的列表时,我可以看到结果中出现了两个措辞。但是,当我尝试使用这两个措辞语法解析输入句子时,它不会解析。

import nltk
from nltk.parse import generate
from nltk.grammar import Nonterminal


cfg = nltk.CFG.fromstring("""
root -> who_player has the most runs
who_player -> who
who_player -> which player
who_player -> which team player
who -> 'who'
which -> 'which'
player -> 'player'
team -> 'indian' | 'australian' | 'england' | 'sri lankan'
has -> 'has'
the -> 'the'
this -> 'this'
most -> 'most'
runs -> 'runs'
""")

print(list((n,sent) for n, sent in enumerate(generate.generate(cfg, n=100, start=Nonterminal('root')), 1)))

# Above generate generates ['which', 'sri lankan', 'player', 'has', 'the', 'most', 'runs']
# But the same sentence is not parsable by ChartParser.

result1 = nltk.ChartParser(cfg).parse('which england player has the most runs'.split())
print(list(result1))
result2 = nltk.ChartParser(cfg).parse('which sri lankan player has the most runs'.split()) # Does not work.
print(list(result2))

如何使用ChartParser进行多步骤配置。

1 个答案:

答案 0 :(得分:2)

管道分隔图表中的节点,空格将单个单词与多字表达式分开。多字表达式将创建一个包含列表中两个项目的树。

team -> 'indian' | 'australian' | 'england' | 'sri' 'lankan'

[OUT]:

[Tree('root', [Tree('who_player', [Tree('which', ['which']), Tree('team', ['sri', 'lankan']), Tree('player', ['player'])]), Tree('has', ['has']), Tree('the', ['the']), Tree('most', ['most']), Tree('runs', ['runs'])])]