简体   繁体   English

NLTK将复杂列表解析为树

[英]NLTK parse complex list into tree

I am trying to parse a list returned from a max entropy syntactic parser into a nltk tree but from what i see here the tree is created by initialising every single node, is there an easier way to do that? 我试图解析从返回的列表最大熵句法分析器成NLTK树但是从我看到这里的树被初始化的每一个节点创建的,是有更简单的方法来做到这一点? This is a list example. 这是一个列表示例。

[u'SBAR', [u'WHADVP+WRB', 'how'], [u'S', [u'VP', [u'VBD', 'did'], [u'VP', [u'VB', 'serfdom'], [u'VP', [u'VB', 'develop'], [u'SBAR', [u'IN', 'in'], [u'S', [u'CC', 'and'], [u'S', [u'ADVP+RB', 'then'], [u'VP', [u'VB', 'leave'], [u'NP+NNP', 'Russia']]]]]]]], [u'.', '?']]]

I made the following function for a recursive solution to the above. 我针对上述递归解决方案做了以下功能。

def List_To_Tree(lst):
    if(not isinstance(lst, basestring)):
        if(len(lst) == 2 and isinstance(lst[0], basestring) and isinstance(lst[1], basestring)):                
            lst = Tree(str(lst[0]).split('+')[0],[str(lst[1])])
        elif(isinstance(lst[0], basestring) and not isinstance(lst[1], basestring)):
            lst = Tree(str(lst[0]).split('+')[0],map(List_To_Tree, lst[1: len(lst) ]))
    return lst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM