添加语法规则 - NLTK解析到python中的语法树

时间:2015-03-18 19:44:45

标签: python parsing nltk

假设我们有以下不完整的语法规则:

grammar2 = nltk.parse_cfg("""
S -> NP VP
NP -> Det N
VP -> V NP
PN -> 'David' 
Det -> 'the'
N -> 'man' 
V -> 'saw' | 'helped' 
Pro -> 'him'
Conj -> 'and'
""")

我想为下面的句子创建语法树:

sent = ['the', 'man', 'saw', 'David', 'and', 'helped', 'him']
parser = nltk.ChartParser(grammar2)
trees = parser.nbest_parse(sent)
for tree in trees:
    print tree

但我不知道如何在语法中添加and连词?

1 个答案:

答案 0 :(得分:0)

递归工作。这应该有用。

S -> NP VP 
VP -> V NP | V NP Conj VP
NP -> Det N | PN | Pro
PN -> 'David' 
Det -> 'the'
N -> 'man' 
V -> 'saw' | 'helped' 
Pro -> 'him'
Conj -> 'and'
相关问题