python 树结构 库_树库样式的树解析器python

Recently i have been trying to parse syntactic trees returned by the stanford parser in python. I have been trying to do that with nltk tree = Tree.parse(result['sentences'][0]['parsetree']) and the parsing succeeds but the tree class of nltk offers very few processing methods. I will need methods like tree.isPrePreTerminal() which are not included from what i understand. I found this alternative but it seems that it doesnt like 64bit architectures and it gives me this error ImportError: InputTree/_InputTree.so: wrong ELF class: ELFCLASS32 even though i compiled with the -m64 flag.

I have been looking into this the last 2 days, if you know a way to make the above module to work with 64bit systems or an alternate library or at least a good nltk.tree documentation so that i implement the methods myself please let me know.

解决方案

Unfortunately, PyInputTree is no longer maintained. However, the InputTree class from the Charniak parser lives on in wrapped form as the Tree class in BLLIP Parser. It doesn't implement isPrePreTerminal() but here's one way to do it:

import bllipparser

def is_prepreterminal(node):

"""Returns True iff all children of this node are preterminals."""

subtrees = node.subtrees()

return len(subtrees) > 0 and \

all(subtree.is_preterminal() for subtree in subtrees)

# testing code

tree = bllipparser.Tree('(S1 (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (ADJP (RB fairly) (JJ simple)) (NN parse) (NN tree))) (. .)))')

for subtree in tree.all_subtrees():

print subtree, is_prepreterminal(subtree)

See bllipparser on PyPI for more information.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值