private Event getMainEvent(JCas jCas, Sentence sentence) { // group events by their depth, and record the minimum depth Integer minDepth = null; Map<Integer, List<Event>> depthEvents = new HashMap<Integer, List<Event>>(); for (Event event : JCasUtil.selectCovered(jCas, Event.class, sentence)) { TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(jCas, event); Integer depth = node == null ? null : TreebankNodeUtil.getDepth(node); if (!depthEvents.containsKey(depth)) { depthEvents.put(depth, new ArrayList<Event>()); } depthEvents.get(depth).add(event); if (depth != null && (minDepth == null || depth < minDepth)) { minDepth = depth; } } // select the last event if (depthEvents.isEmpty()) { return null; } else { List<Event> events = depthEvents.get(minDepth); return events.get(events.size() - 1); } } }
/** * Selects the highest TreebankNode in the parse tree that has the same span as the given * annotation. * * @param jCas * The JCas containing the TreebankNodes. * @param annotation * The Annotation whose span should be matched. * @return The highest TreebankNode matching the given span, or null if no such annotation exists. */ 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; }
/** * Selects the highest TreebankNode in the parse tree that is at least partially covered by the * given annotation. * * @param jCas * The JCas containing the TreebankNodes. * @param annotation * The Annotation whose span should be matched. * @return The highest TreebankNode at least partially covered by the given span, or null if no * such annotation exists. */ public static TreebankNode selectHighestCoveredTreebankNode(JCas jCas, Annotation annotation) { TreebankNode highestNode = null; int smallestDepth = Integer.MAX_VALUE; for (TreebankNode node : JCasUtil.selectCovered(jCas, TreebankNode.class, annotation)) { if (annotation.getBegin() <= node.getBegin() && node.getEnd() <= annotation.getEnd()) { int depth = getDepth(node); if (depth < smallestDepth) { highestNode = node; smallestDepth = depth; } } } return highestNode; }