我正在尝试使用Python 3.5中的以下解析历史创建一个括号中的树:
parse = [('S', 0), ('NP', 1), ('Det', 0), 'scan', ('N', 0), 'scan', ('VP', 1), ('V', 4), 'scan', ('NP', 2), ('NP', 0), ('PN', 1), 'scan', ('NP', 1), ('Det', 0), 'scan', ('N', 3), 'scan']
这句话是:'那个男人向玛丽展示了狗'
使用的语法是:
grammar = {'S': [['NP', 'VP']],
'NP': [['PN'], ['Det', 'N'], ['NP', 'NP']],
'VP': [['V'], ['V', 'NP']],
'PN': [['John'], ['Mary'], ['Bill']],
'Det': [['the'], ['a']],
'N': [['man'], ['woman'], ['drill sergeant'], ['dog']],
'V': [['slept'], ['cried'], ['assaulted'],
['devoured'], ['showed']]}
列表中每个元组的格式是元组中的第一项是字典中的一个键,其中包含可能的规则列表,元组中的第二项是给定列表中规则的索引语法[key] [index]每当'scan'出现时就意味着
输出必须是:
(S(NP(Det the)(N man))(VP (V showed)(NP (NP(PN Mary))(NP(Det the)(N dog)))))
到目前为止,我得到的最佳输出是:
(S
NP)
VP)
(NP
Det)
N)
(Det
the)
(N
man)
(VP
V)
NP)
(V
showed)
(NP
NP)
NP)
(NP
PN)
(PN
Mary)
(NP
Det)
N)
(Det
the)
(N
dog)
给出以下代码:
for item in parse:
if item != 'scan':
print("(" + item[0])
for rule in grammar[item[0]][item[1]]:
print(rule + ")")
请帮助!
谢谢