TreebankFormatParser { public static TopTreebankNode parse(String parse) { parse = prepareString(parse); String plainText = inferPlainText(parse).trim(); return parse(parse, plainText, 0); }  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 testParse1() { String parse = "(PRN (-LRB- -LRB- ) (S-ADV (ADVP (FW i.e. )) (, , ) (NP (NP (NP (DT a ) (NN residual )) (PP (IN of ) (-NONE- CD_) (HYPH - ) (NP (CD 116,000 )))) (: ; ) (S (NP-SBJ (NN n )) (VP (SYM = ) (NP (CD 12 )))))) (-RRB- -RRB- ))"; String text = "(i.e., a residual of -116,000; n = 12)"; TopTreebankNode topNode = TreebankFormatParser.parse(parse, text, 0); assertEquals(text, topNode.getText()); List<TreebankNode> children = topNode.getChildren(); testNode(children.get(0), "(", "-LRB-", "(-LRB- -LRB- )"); testNode( children.get(1), "i.e., a residual of -116,000; n = 12", "S", "(S-ADV (ADVP (FW i.e. )) (, , ) (NP (NP (NP (DT a ) (NN residual )) (PP (IN of ) (-NONE- CD_) (HYPH - ) (NP (CD 116,000 )))) (: ; ) (S (NP-SBJ (NN n )) (VP (SYM = ) (NP (CD 12 ))))))"); testNode(children.get(2), ")", "-RRB-", "(-RRB- -RRB- )"); children = children.get(1).getChildren(); testNode(children.get(0), "i.e.", "ADVP", "(ADVP (FW i.e. ))"); testNode(children.get(1), ",", ",", "(, , )"); testNode( children.get(2), "a residual of -116,000; n = 12", "NP", "(NP (NP (NP (DT a ) (NN residual )) (PP (IN of ) (-NONE- CD_) (HYPH - ) (NP (CD 116,000 )))) (: ; ) (S (NP-SBJ (NN n )) (VP (SYM = ) (NP (CD 12 )))))"); TreebankNode node1 = children.get(2); children = children.get(0).getChildren(); testNode(children.get(0), "i.e.", "FW", "(FW i.e. )"); children = node1.getChildren(); testNode( children.get(0), "a residual of -116,000", "NP", "(NP (NP (DT a ) (NN residual )) (PP (IN of ) (-NONE- CD_) (HYPH - ) (NP (CD 116,000 ))))"); testNode(children.get(1), ";", ":", "(: ; )"); testNode(children.get(2), "n = 12", "S", "(S (NP-SBJ (NN n )) (VP (SYM = ) (NP (CD 12 ))))"); node1 = children.get(0); TreebankNode node2 = children.get(2); children = node1.getChildren(); testNode(children.get(0), "a residual", "NP", "(NP (DT a ) (NN residual ))"); testNode( children.get(1), "of -116,000", "PP", "(PP (IN of ) (-NONE- CD_) (HYPH - ) (NP (CD 116,000 )))"); children = children.get(0).getChildren(); testNode(children.get(0), "a", "DT", "(DT a )"); testNode(children.get(1), "residual", "NN", "(NN residual )"); children = node1.getChildren().get(1).getChildren(); testNode(children.get(0), "of", "IN", "(IN of )"); testNode(children.get(1), "", "-NONE-", "(-NONE- CD_)"); testNode(children.get(2), "-", "HYPH", "(HYPH - )"); testNode(children.get(3), "116,000", "NP", "(NP (CD 116,000 ))"); children = children.get(3).getChildren(); testNode(children.get(0), "116,000", "CD", "(CD 116,000 )"); children = node2.getChildren(); testNode(children.get(0), "n", "NP", "(NP-SBJ (NN n ))"); testNode(children.get(0).getChildren().get(0), "n", "NN", "(NN n )"); testNode(children.get(1), "= 12", "VP", "(VP (SYM = ) (NP (CD 12 )))"); testNode(children.get(1).getChildren().get(0), "=", "SYM", "(SYM = )"); testNode(children.get(1).getChildren().get(1), "12", "NP", "(NP (CD 12 ))"); testNode(children.get(1).getChildren().get(1).getChildren().get(0), "12", "CD", "(CD 12 )"); }
@Test public void testBadInputText() { IllegalArgumentException iae = null; try { String treebankParse = "(NML (NN Complex ) (NN trait ))"; String plainText = "Complex strait"; TreebankFormatParser.parse(treebankParse, plainText, 0); } catch (IllegalArgumentException e) { iae = e; } assertNotNull(iae); }
@Test public void testOffset() { String parse = "(S (NP (NNS dogs )) (VP (VB chase ) (NP (NNS cats ))) (. . ))"; String text = "I believe that dogs chase cats."; TopTreebankNode topNode = TreebankFormatParser.parse(parse, text, 14); testNode(topNode, "dogs chase cats.", "S", parse); List<TreebankNode> children = topNode.getChildren(); testNode(children.get(0), "dogs", "NP", "(NP (NNS dogs ))"); testNode(children.get(1), "chase cats", "VP", "(VP (VB chase ) (NP (NNS cats )))"); }
@Test public void testSpecialCasePeriods() { String parse = "(S (NP (DT The) (NNP U.S.)) (. .))"; String text = "The U.S. "; TopTreebankNode topNode = TreebankFormatParser.parse(parse, text, 0); testNode(topNode, "The U.S..", "S", parse); testNode(topNode.getChildren().get(0).getChildren().get(1), "U.S.", "NNP", "(NNP U.S.)"); text = "The U.S.."; topNode = TreebankFormatParser.parse(parse, text, 0); testNode(topNode, "The U.S..", "S", parse); testNode(topNode.getChildren().get(0).getChildren().get(1), "U.S.", "NNP", "(NNP U.S.)"); }