TreebankNodeUtil { public static String toTreebankString(TreebankNode node) { StringBuilder builder = new StringBuilder(); builder.append('(').append(node.getNodeType()); if (node.getLeaf()) { builder.append(' ').append(node.getCoveredText()); } else { for (TreebankNode child : JCasUtil.select(node.getChildren(), TreebankNode.class)) { builder.append(' ').append(toTreebankString(child)); } } builder.append(')'); return builder.toString(); }  static TreebankNode selectMatchingLeaf(JCas jCas, Annotation annotation); static TreebankNode selectHighestMatchingTreebankNode(JCas jCas, Annotation annotation); static TreebankNode selectHighestCoveredTreebankNode(JCas jCas, Annotation annotation); static int getDepth(TreebankNode node); static List<TreebankNode> getPathToRoot(TreebankNode startNode); static TreebankNodePath getPath(TreebankNode source, TreebankNode target); static String toTreebankString(TreebankNode node); static TreebankNode getParent(TreebankNode node); static TreebankNode getAncestorWithType(TreebankNode node, String type); static TreebankNode newNode(JCas jCas, int begin, int end, String nodeType); static TreebankNode newNode(JCas jCas, String nodeType, TreebankNode... children); static TopTreebankNode getTopNode(TreebankNode node); static void print(PrintStream out, TreebankNode node);  }
@Test public void testToTreebankString() throws Exception { this.jCas.setDocumentText("The cat chased mice ."); String expected1 = "(S (NP (DT The) (NN cat)) (VP (VBD chased) (NP (NNS mice))) (. .))"; top( "S", jCas, node("NP", jCas, leaf("DT", "The", jCas), leaf("NN", "cat", jCas)), node("VP", jCas, leaf("VBD", "chased", jCas), node("NP", jCas, leaf("NNS", "mice", jCas))), leaf(".", ".", jCas)); TopTreebankNode root1 = JCasUtil.selectSingle(this.jCas, TopTreebankNode.class); Assert.assertEquals(expected1, TreebankNodeUtil.toTreebankString(root1)); this.jCas.reset(); this.jCas.setDocumentText("The skunk thought the stump stunk ."); String expected2 = "(S (NP (DT The) (NN skunk)) (VP (VBD thought) (S (NP (DT the) (NN stump)) (VP (VBD stunk)))) (. .))"; top( "S", jCas, node("NP", jCas, leaf("DT", "The", jCas), leaf("NN", "skunk", jCas)), node( "VP", jCas, leaf("VBD", "thought", jCas), node( "S", jCas, node("NP", jCas, leaf("DT", "the", jCas), leaf("NN", "stump", jCas)), node("VP", jCas, leaf("VBD", "stunk", jCas)))), leaf(".", ".", jCas)); TopTreebankNode root2 = JCasUtil.selectSingle(this.jCas, TopTreebankNode.class); Assert.assertEquals(expected2, TreebankNodeUtil.toTreebankString(root2)); }