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); } } }
private static List<Time> getSubordinateTimes(Event event, Sentence sentence, JCas jCas) { List<Time> times = new ArrayList<Time>(); TreebankNode eventNode = selectMatchingLeaf(jCas, event); for (Time time : JCasUtil.selectCovered(jCas, Time.class, sentence)) { TreebankNode timeNode = selectHighestCoveredTreebankNode(jCas, time); if (eventNode != null && timeNode != null) { String path = noLeavesPath(TreebankNodeUtil.getPath(eventNode, timeNode)); if (SUBORDINATE_PATH_PATTERN.matcher(path).matches()) { times.add(time); } } } return times; } }
/** * Get the path from the source TreebankNode to the target TreebankNode via the least common * ancestor. * * @param source * The TreebankNode where the path should start. * @param target * The TreebankNode where the path should end. * @return The path from the source node to the target node. */ public static TreebankNodePath getPath(TreebankNode source, TreebankNode target) { List<TreebankNode> sourceToRoot = getPathToRoot(source); List<TreebankNode> targetToRoot = getPathToRoot(target); TreebankNode ancestor = null; while (sourceToRoot.size() > 0 && targetToRoot.size() > 0 && sourceToRoot.get(sourceToRoot.size() - 1) == targetToRoot.get(targetToRoot.size() - 1)) { ancestor = sourceToRoot.remove(sourceToRoot.size() - 1); ancestor = targetToRoot.remove(targetToRoot.size() - 1); } return new TreebankNodePath(ancestor, sourceToRoot, targetToRoot); }
private List<Event> getSubordinateEvents(JCas jCas, Event source, Sentence sentence) { List<Event> targets = new ArrayList<Event>(); TreebankNode sourceNode = TreebankNodeUtil.selectMatchingLeaf(jCas, source); for (Event target : JCasUtil.selectCovered(jCas, Event.class, sentence)) { if (!target.equals(source)) { TreebankNode targetNode = TreebankNodeUtil.selectMatchingLeaf(jCas, target); if (sourceNode != null && targetNode != null) { String path = noLeavesPath(TreebankNodeUtil.getPath(sourceNode, targetNode)); if (SUBORDINATE_PATH_PATTERN.matcher(path).matches()) { targets.add(target); } } } } return targets; } }
@Override public List<Feature> extract(JCas view, T focusAnnotation) { List<Feature> features = new ArrayList<Feature>(); TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(view, focusAnnotation); if (node != null) { node = node.getParent(); } if (node != null) { node = node.getParent(); } if (node != null) { node = node.getChildren(0); } if (node != null && node.getLeaf()) { features.add(new Feature(this.featureName, node.getCoveredText())); } return features; } }
/** * 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; }
private static String print(TreebankNode node, int tabs) { StringBuffer returnValue = new StringBuffer(); String tabString = getTabs(tabs); returnValue.append(tabString + node.getNodeType()); if (node.getNodeValue() != null) returnValue.append(":" + node.getNodeValue() + "\n"); else { returnValue.append(":" + node.getCoveredText() + "\n"); } if (node.getChildren().size() > 0) { Collection<TreebankNode> children = JCasUtil.select(node.getChildren(), TreebankNode.class); for (TreebankNode child : children) { returnValue.append(print(child, (tabs + 1))); } } return returnValue.toString(); }
SemanticArgument arg = new SemanticArgument(jCas, span[0], span[1]); arg.addToIndexes(); Annotation relation = TreebankNodeUtil.selectHighestMatchingTreebankNode(jCas, arg); if (relation == null) { Chunk chunk = new Chunk(jCas, span[0], span[1]);
/** * A "pretty print" of this node that may be useful for e.g. debugging. */ public static void print(PrintStream out, TreebankNode node) { out.println(print(node, 0)); }
/** * Format the TreebankNode as a Penn-Treebank-style parenthesized string. * * @param node * The TreebankNode to be formatted. * @return A parenthesized Penn-Treebank-style string. */ 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(); }
public List<Feature> extract(JCas jCas, T source, U target) { List<Feature> features = new ArrayList<Feature>(); TreebankNode sourceNode = TreebankNodeUtil.selectMatchingLeaf(jCas, source); TreebankNode targetNode = TreebankNodeUtil.selectMatchingLeaf(jCas, target); if (sourceNode != null && targetNode != null) { TreebankNodePath path = TreebankNodeUtil.getPath(sourceNode, targetNode); TreebankNode ancestor = path.getCommonAncestor(); features.add(new Feature("CommonAncestor", ancestor == null ? null : ancestor.getNodeType())); features.add(new Feature("SourceToAncestor", pathString(path.getSourceToAncestorPath()))); features.add(new Feature("TargetToAncestor", pathString(path.getTargetToAncestorPath()))); } return features; }
@Override public List<Feature> extract(JCas view, Token token) throws CleartkExtractorException { TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(view, token); List<Feature> features = new ArrayList<Feature>(); if (node != null) { TreebankNode parent = node.getParent(); if (parent != null) { features.add(new Feature("ParentNodeType", parent.getNodeType())); TreebankNode firstSibling = parent.getChildren(0); if (firstSibling != node && firstSibling.getLeaf()) { features.add(new Feature("FirstSiblingText", firstSibling.getCoveredText())); } } } return features; } }
/** * 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; }
SemanticArgument arg = new SemanticArgument(jCas, span[0], span[1]); arg.addToIndexes(); Annotation relation = TreebankNodeUtil.selectHighestMatchingTreebankNode(jCas, arg); if (relation == null) { Chunk chunk = new Chunk(jCas, span[0], span[1]);
throws CleartkExtractorException { List<TreebankNode> fromStart = TreebankNodeUtil.getPathToRoot(leftConstituent); List<TreebankNode> fromEnd = TreebankNodeUtil.getPathToRoot(rightConstituent); String pathFeatureName = null; String lengthFeatureName = null;
Annotation relation = null; try { relation = TreebankNodeUtil.selectHighestMatchingTreebankNode(view, nem); } catch (NoSuchElementException e) {
Annotation relation = null; try { relation = TreebankNodeUtil.selectHighestMatchingTreebankNode(view, nem); } catch (NoSuchElementException e) {