我正在尝试从如下所示的树结构中获取扁平树。
我想把整棵树放在一个字符串中,就像没有检测到坏树错误一样:
( (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 函数将树转换为字符串,然后拆分和连接如下:
parse_string = ' '.join(str(tree).split())
print parse_string
关于python - 如何展平解析树并存储在字符串中以进行进一步的字符串操作python nltk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28704060/