如何扁平化解析树并存储在字符串中以进行进一步的字符串操作python nltk [英] How to flatten the parse tree and store in a string for further string operations python nltk
本文介绍了如何扁平化解析树并存储在字符串中以进行进一步的字符串操作python nltk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图从树形结构中获得扁平树,如下所示.
I am trying to get flat tree from the tree structure like the one given below.
我希望以字符串形式获取整棵树,而不会检测到坏树错误:
I want to get this whole tree in a string like without Bad tree detected error:
( (S (NP-SBJ (NP (DT The) (JJ high) (JJ seven-day) )(PP (IN of) (NP (DT the) (CD 400) (NNS money) )))(VP (VBD was) (NP-PRD (CD 8.12) (NN %) )(, ,) (ADVP (RB down) (PP (IN from) (NP (CD 8.14) (NN %) ))))(. .) ))
推荐答案
您可以使用str函数将树转换为字符串,然后按如下所示拆分和合并:
You can convert the tree into string using str function then split and join as follow:
parse_string = ' '.join(str(tree).split())
print parse_string
这篇关于如何扁平化解析树并存储在字符串中以进行进一步的字符串操作python nltk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文