NLTK CFG 多词文法

NLTK CFG Grammar with multiple words

NLTK 3.0:

使用如下 CFG 配置(非终端团队有 4 个值,其中 1 个值有 2 个词(斯里兰卡语))。

当我生成可能世代的列表时,我可以在结果中看到这两个词。但是当我尝试用那个双词语法解析输入句子时,它不解析。

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 一起工作。

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

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

[输出]:

[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'])])]