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 如何展平解析树并存储在字符串中以进行进一步的字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!