TreebankNodeUtil { public static TreebankNode selectHighestMatchingTreebankNode(JCas jCas, Annotation annotation) { TreebankNode highestNode = null; int smallestDepth = Integer.MAX_VALUE; for (TreebankNode node : JCasUtil.selectCovered(jCas, TreebankNode.class, annotation)) { if (node.getBegin() == annotation.getBegin() && node.getEnd() == annotation.getEnd()) { int depth = getDepth(node); if (depth < smallestDepth) { highestNode = node; smallestDepth = depth; } } } return highestNode; }  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 testSelectHighestMatchingTreebankNode() 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)); TreebankNode node; node = TreebankNodeUtil.selectHighestMatchingTreebankNode(this.jCas, this.newSpan(0, 3)); Assert.assertEquals("The", node.getCoveredText()); Assert.assertEquals("DT", node.getNodeType()); Assert.assertTrue(node.getLeaf()); node = TreebankNodeUtil.selectHighestMatchingTreebankNode(this.jCas, this.newSpan(15, 19)); Assert.assertEquals("mice", node.getCoveredText()); Assert.assertEquals("NP", node.getNodeType()); Assert.assertFalse(node.getLeaf()); node = TreebankNodeUtil.selectHighestMatchingTreebankNode(this.jCas, this.newSpan(8, 19)); Assert.assertEquals("chased mice", node.getCoveredText()); Assert.assertEquals("VP", node.getNodeType()); Assert.assertFalse(node.getLeaf()); int end = this.jCas.getDocumentText().length(); node = TreebankNodeUtil.selectHighestMatchingTreebankNode(this.jCas, this.newSpan(0, end)); Assert.assertEquals("The cat chased mice .", node.getCoveredText()); Assert.assertEquals("S", node.getNodeType()); Assert.assertFalse(node.getLeaf()); node = TreebankNodeUtil.selectHighestMatchingTreebankNode(this.jCas, this.newSpan(0, 4)); Assert.assertNull(node); node = TreebankNodeUtil.selectHighestMatchingTreebankNode(this.jCas, this.newSpan(0, end - 1)); Assert.assertNull(node); }