TreebankNodeUtil { public static int getDepth(TreebankNode node) { int depth = -1; while (node != null) { depth += 1; node = node.getParent(); } return depth; }  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 testGetDepth() throws Exception { this.jCas.setDocumentText("The cat chased 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)); for (TreebankNode node : JCasUtil.select(this.jCas, TreebankNode.class)) { String text = node.getCoveredText(); int depth = TreebankNodeUtil.getDepth(node); if (text.equals("The")) { Assert.assertEquals(2, depth); } else if (text.equals("cat")) { Assert.assertEquals(2, depth); } else if (text.equals("The cat")) { Assert.assertEquals(1, depth); } else if (text.equals("chased")) { Assert.assertEquals(2, depth); } else if (text.equals("mice")) { if (node.getLeaf()) { Assert.assertEquals(3, depth); } else { Assert.assertEquals(2, depth); } } else if (text.equals("chased mice")) { Assert.assertEquals(1, depth); } else if (text.equals(".")) { Assert.assertEquals(1, depth); } else if (text.equals("The cat chased mice .")) { Assert.assertEquals(0, depth); } else { Assert.fail("Unexpected node: " + node); } } }