首页 > 解决方案 > 解析树的 Python 列表

问题描述

在 Python 中,我有一个列表输入,如下所示 -

[('S', ['NP', 'VP']),
('A', ['V', 'NP']),
('VP', ['V', 'NP']),
('NP', ['DET', 'NP']),
('N', "'mouse'"),
('NP', "'mouse'"),
('DET', "'the'"),
('V', "'saw'"),
('N', "'Ron'"),
('NP', "'Ron'")]

这是以下 CYK 算法的结果 -

S -> NP VP
VP -> A NP | V NP
NP -> N N | DET NP | 'chocolate' | 'cat' | 'John' | 'Ron' | 'mouse'
DET -> 'the'
N -> 'chocolate' | 'cat' | 'John' | 'Ron' | 'mouse'
V -> 'saw' | 'bought' | 'ate'
A -> V NP

我要匹配的字符串是“Ron saw the mouse”

我想像这样关联输出-

(S (NP Ron) (VP (V saw) (NP (DET the) (NP mouse))))

我不确定应该如何构造算法,尤其是使用可能包含多个输出的模棱两可的算法。我应该如何构建代码?有什么建议有/没有递归应该是更好的方法吗?

更新 - -

在使用输入列表添加额外的父节点和子节点位置值后,我设法获得了一个精确的解析树。但是我的问题并没有用模棱两可的句子解决。

标签: parsingrecursiongrammarparse-treecyk

解决方案


推荐阅读