I know that this post is quite old, but I am convinced that my solution could be relevant for others as well.
I have written a library called Constituent Treelib that offers a convenient way to parse sentences into constituent trees, modify them according to their structure, as well as visualize and export them into various file formats. In addition, one can extract phrases according to their phrasal categories (which can be used e.g., as features for various NLP tasks), validate already parsed sentences in bracket notation or convert them back into sentences. The latter is what the OP asked for. Here are the steps to achieve this:
First, install the library via:
pip install constituent-treelib
Next, load the respective components from library and create the constituent tree given the sentence in a bracketed tree representation:
from constituent_treelib import ConstituentTree, BracketedTree, Language
# Define the language for the sentence as well as for the spaCy and benepar models
language = Language.English
# Define which specific SpaCy model should be used (default is Medium)
spacy_model_size = ConstituentTree.SpacyModelSize.Medium
# Create the pipeline (note, the required models will be downloaded and installed automatically)
nlp = ConstituentTree.create_pipeline(language, spacy_model_size)
# Your sentence
bracketed_tree_string = """(ROOT
(FRAG
(NP (NN sent28))
(: :)
(S
(NP (NNP Rome))
(VP (VBZ is)
(PP (IN in)
(NP
(NP (NNP Lazio) (NN province))
(CC and)
(NP
(NP (NNP Naples))
(PP (IN in)
(NP (NNP Campania))))))))
(. .)))""".splitlines()
bracketed_tree_string = " ".join(bracketed_tree_string)
sentence = BracketedTree(bracketed_tree_string)
# Create a constituent tree from which the original sentence will be recovered
tree = ConstituentTree(sentence, nlp)
Finally, we recover the original sentence from the constituent tree using the followng:
tree.leaves(tree.nltk_tree, ConstituentTree.NodeContent.Text)
Result:
'sent28 : Rome is in Lazio province and Naples in Campania .'
from nltk.draw.tree import draw_trees >>> draw_trees(tree)
to visualize it as a real tree :-) [Oh and I can't take offsent28
, it's part of an assignment...] – Stephanstephana