TreebankFormatParser { public static String inferPlainText(String treebankText) { StringBuilder sb = new StringBuilder(); for (String parse : splitSentences(treebankText)) { Matcher matcher = leafNodePattern.matcher(parse); while (matcher.find()) { TreebankNode node = getLeafNode(matcher.group()); if (node.getText() != null && node.getText().length() > 0) { int lastIndex = sb.length() - 1; if (lastIndex > 0 && !needsSpaceBefore(node.getText()) && sb.charAt(lastIndex) == ' ') { sb.deleteCharAt(lastIndex); } sb.append(node.getText()); if (needsSpaceAfter(node.getText())) { sb.append(" "); } } } int lastIndex = sb.length() - 1; if (lastIndex >= 0 && sb.charAt(lastIndex) == ' ') { sb.deleteCharAt(lastIndex); } sb.append('\n'); } return sb.toString().trim(); }  static TreebankNode getLeafNode(String parseFragment); static String getType(String parseFragment); static String prepareString(String parse); static String inferPlainText(String treebankText); static TopTreebankNode parse(String parse); static TopTreebankNode parse(String parse, String text, int textOffset); static int movePastWhiteSpaceChars(String text, int textOffset); static String[] splitSentences(String mrgContents); static boolean parensMatch(String contents); static List<TopTreebankNode> parseDocument(String parse, int textOffset, String text); static final String LEAF_NODE_REGEX; static final String TYPE_REGEX; static final String cleanUPRegex1; static final String cleanUPRegex2; static final String cleanUPRegex3; static final String cleanUPRegex4; }
@Test public void testInferPlainText() { String treebankParse = "( (X (NP (NP (NML (NN Complex ) (NN trait )) (NN analysis )) (PP (IN of ) (NP (DT the ) (NN mouse ) (NN striatum )))) (: : ) (S (NP-SBJ (JJ independent ) (NNS QTLs )) (VP (VBP modulate ) (NP (NP (NN volume )) (CC and ) (NP (NN neuron ) (NN number)))))) )\n"; String expectedText = "Complex trait analysis of the mouse striatum: independent QTLs modulate volume and neuron number"; String actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); treebankParse = "(T 's)(T there)(T 's)(T going)(T .)"; expectedText = "'s there's going."; actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); treebankParse = "(T ')(T .)(T ,)(T :)(T ;)(T 's)(T can)(T 't)"; expectedText = "'.,:;'s can't"; actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); treebankParse = "(T \")(T Howdy)(T !)(T \")"; expectedText = "\"Howdy!\""; actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); treebankParse = "(QP (CD 44.4) (CC +\\/-) (CD 4.4))"; expectedText = "44.4 +/- 4.4"; actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); treebankParse = "(NP (NP (NN dexamethasone))(PRN (LRB -LRB-)(NP (NN Dex))(RRB -RRB-)))"; expectedText = "dexamethasone (Dex)"; actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); treebankParse = "(PRN (: --) (CC and) (ADVP (RB randomly) ) (PP (IN with) (NP-LGS (DT the) (NNP Cccccc) (NNP Bbbbbb) (NNP Aaaaaa) )) (: --) )"; expectedText = "-- and randomly with the Cccccc Bbbbbb Aaaaaa --"; actualText = TreebankFormatParser.inferPlainText(treebankParse); assertEquals(expectedText, actualText); }