python - 从解析结果中提取语法规则

标签 python recursion nltk stanford-nlp

当我从 nltk 执行斯坦福解析器时,我得到以下结果。

(S (VP (VB get) (NP (PRP me)) (ADVP (RB now))))

但我需要它的形式

S -> VP
VP -> VB NP ADVP
VB -> get
PRP -> me
RB -> now

我怎样才能得到这个结果,也许使用递归函数。 是否已经有内置功能?

最佳答案

首先导航树,请参阅 How to iterate through all nodes of a tree?How to navigate a nltk.tree.Tree? :

>>> from nltk.tree import Tree
>>> bracket_parse = "(S (VP (VB get) (NP (PRP me)) (ADVP (RB now))))"
>>> ptree = Tree.fromstring(bracket_parse)
>>> ptree
Tree('S', [Tree('VP', [Tree('VB', ['get']), Tree('NP', [Tree('PRP', ['me'])]), Tree('ADVP', [Tree('RB', ['now'])])])])
>>> for subtree in ptree.subtrees():
...     print subtree
... 
(S (VP (VB get) (NP (PRP me)) (ADVP (RB now))))
(VP (VB get) (NP (PRP me)) (ADVP (RB now)))
(VB get)
(NP (PRP me))
(PRP me)
(ADVP (RB now))
(RB now)

而您正在寻找的是 https://github.com/nltk/nltk/blob/develop/nltk/tree.py#L341 :

>>> ptree.productions()
[S -> VP, VP -> VB NP ADVP, VB -> 'get', NP -> PRP, PRP -> 'me', ADVP -> RB, RB -> 'now']

注意 Tree.productions() 返回一个 Production 对象,参见 https://github.com/nltk/nltk/blob/develop/nltk/tree.py#L22https://github.com/nltk/nltk/blob/develop/nltk/grammar.py#L236 .

如果你想要语法规则的字符串形式,你可以这样做:

>>> for rule in ptree.productions():
...     print rule
... 
S -> VP
VP -> VB NP ADVP
VB -> 'get'
NP -> PRP
PRP -> 'me'
ADVP -> RB
RB -> 'now'

或者

>>> rules = [str(p) for p in ptree.productions()]
>>> rules
['S -> VP', 'VP -> VB NP ADVP', "VB -> 'get'", 'NP -> PRP', "PRP -> 'me'", 'ADVP -> RB', "RB -> 'now'"]

关于python - 从解析结果中提取语法规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140945/

相关文章:

python - 如何将变量连接到 Entry 小部件?

python - 自定义回溯输出

python - 从 sqlite3 切换到 postgres 后 django 迁移失败

python - 叠瓦循环和 Python 语法

python - (python) 不使用内置函数存储数据

java - 等待递归线程生产者

python - Python 中的 N 皇后回溯 : how to return solutions instead of printing them?

perl - 使用 Perl,如何重命名驱动器所有子目录中的文件?

python - NLTK 与距离度量的一致性

python - Porter Stemmer 可以返回词缀而不是词干吗?