简体   繁体   中英

How can I generate a bracketed tree string in NLTK from a list of nodes and their children

The NLTK documentation has plenty of examples of constructing a tree by parsing a bracketed string. Is there an easy way to construct the tree from a list of parents and their children? I want to be able to generate the bracketed string as the output.

parent : children 
14_fine : 12_the,13_nasd
8_allegations : 7_the
10_consented : 11_to
4_admitted : 2_morgan,3_neither,15_.,6_denied,5_nor
11_to : 14_fine
6_denied : 8_allegations,9_but,10_consented
2_morgan : 1_j.p.

Answer (thanks to frankov): Look at the demo functions here: http://www.nltk.org/_modules/nltk/parse/dependencygraph.html

Covert to conll format then do something like

dg = DependencyGraph(conll_data1)
tree = dg.tree()
tree.pprint()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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