public static TreebankNode getLCA(TreebankNode t1, TreebankNode t2){ TreebankNode temp = null; if(t2.getBegin() < t1.getBegin()){ temp = t1; t1 = t2; t2 = temp; } TreebankNode lca = t2; while(lca != null && (lca.getBegin() > t1.getBegin() || lca.getEnd() < t1.getEnd())){ lca = lca.getParent(); } return lca; }
public static TreebankNode getLCA(TreebankNode t1, TreebankNode t2){ TreebankNode temp = null; if(t2.getBegin() < t1.getBegin()){ temp = t1; t1 = t2; t2 = temp; } TreebankNode lca = t2; while(lca != null && (lca.getBegin() > t1.getBegin() || lca.getEnd() < t1.getEnd())){ lca = lca.getParent(); } return lca; }
@Override public List<Feature> extract(JCas jcas, IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) throws AnalysisEngineProcessException { List<Feature> features = new ArrayList<Feature>(); // first get the root and print it out... TopTreebankNode root = AnnotationTreeUtils.getTreeCopy(jcas, AnnotationTreeUtils.getAnnotationTree(jcas, arg1)); // SimpleTree tempClone = TreeExtractor.getSimpleClone(root); // features.add(new Feature("TK_FULL", tempClone.toString())); TreebankNode t1 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg1, "ARG1"); TreebankNode t2 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg2, "ARG2"); SimpleTree tree = null; if(t1.getBegin() <= t2.getBegin() && t1.getEnd() >= t2.getEnd()){ // t1 encloses t2 tree = TreeExtractor.getSimpleClone(t1); }else if(t2.getBegin() <= t1.getBegin() && t2.getEnd() >= t1.getEnd()){ // t2 encloses t1 tree = TreeExtractor.getSimpleClone(t2); }else{ tree = TreeExtractor.extractPathEnclosedTree(t1, t2, jcas); } features.add(new Feature("TK_PET", tree.toString())); return features; }
@Override public List<Feature> extract(JCas jcas, IdentifiedAnnotation arg1, IdentifiedAnnotation arg2) throws AnalysisEngineProcessException { List<Feature> features = new ArrayList<Feature>(); // first get the root and print it out... TopTreebankNode root = AnnotationTreeUtils.getTreeCopy(jcas, AnnotationTreeUtils.getAnnotationTree(jcas, arg1)); // SimpleTree tempClone = TreeExtractor.getSimpleClone(root); // features.add(new Feature("TK_FULL", tempClone.toString())); TreebankNode t1 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg1, "ARG1"); TreebankNode t2 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg2, "ARG2"); SimpleTree tree = null; if(t1.getBegin() <= t2.getBegin() && t1.getEnd() >= t2.getEnd()){ // t1 encloses t2 tree = TreeExtractor.getSimpleClone(t1); }else if(t2.getBegin() <= t1.getBegin() && t2.getEnd() >= t1.getEnd()){ // t2 encloses t1 tree = TreeExtractor.getSimpleClone(t2); }else{ tree = TreeExtractor.extractPathEnclosedTree(t1, t2, jcas); } features.add(new Feature("TK_PET", tree.toString())); return features; }
/** * Is this pair of entities enclosed inside a noun phrase? */ public static boolean isEnclosed(IdentifiedAnnotationPair pair, TreebankNode np) { IdentifiedAnnotation arg1 = pair.getArg1(); IdentifiedAnnotation arg2 = pair.getArg2(); if((np.getBegin() <= arg1.getBegin()) && (np.getEnd() >= arg1.getEnd()) && (np.getBegin() <= arg2.getBegin()) && (np.getEnd() >= arg2.getEnd())) { return true; } return false; }
/** * Is this pair of entities enclosed inside a noun phrase? */ public static boolean isEnclosed(IdentifiedAnnotationPair pair, TreebankNode np) { IdentifiedAnnotation arg1 = pair.getArg1(); IdentifiedAnnotation arg2 = pair.getArg2(); if((np.getBegin() <= arg1.getBegin()) && (np.getEnd() >= arg1.getEnd()) && (np.getBegin() <= arg2.getBegin()) && (np.getEnd() >= arg2.getEnd())) { return true; } return false; }
public SyntaxAttributeCalculator(JCas jcas, Markable m1, Markable m2, WikiIndex wiki) { super(jcas,m1,m2); n1 = MarkableTreeUtils.markableNode(jcas, m1.getBegin(), m1.getEnd()); n2 = MarkableTreeUtils.markableNode(jcas, m2.getBegin(), m2.getEnd()); lca = n2; while(true){ if(n1 == null || lca == null || lca.getBegin() <= n1.getBegin()){ break; } lca = lca.getParent(); } ngrams = new HashMap<String,Integer>(); calcFullPath(); this.wiki = wiki; if(this.wiki != null) initWikiSim(); // c1 = MarkableDepUtils.markableNode(jcas, m1.getBegin(), m1.getEnd(), n1); // c2 = MarkableDepUtils.markableNode(jcas, m2.getBegin(), m2.getEnd(), n2); // depLca = getDepLCA(c1,c2); // calcDepPath(); }
public SyntaxAttributeCalculator(JCas jcas, Markable m1, Markable m2, WikiIndex wiki) { super(jcas,m1,m2); n1 = MarkableTreeUtils.markableNode(jcas, m1.getBegin(), m1.getEnd()); n2 = MarkableTreeUtils.markableNode(jcas, m2.getBegin(), m2.getEnd()); lca = n2; while(true){ if(n1 == null || lca == null || lca.getBegin() <= n1.getBegin()){ break; } lca = lca.getParent(); } ngrams = new HashMap<String,Integer>(); calcFullPath(); this.wiki = wiki; if(this.wiki != null) initWikiSim(); // c1 = MarkableDepUtils.markableNode(jcas, m1.getBegin(), m1.getEnd(), n1); // c2 = MarkableDepUtils.markableNode(jcas, m2.getBegin(), m2.getEnd(), n2); // depLca = getDepLCA(c1,c2); // calcDepPath(); }
private static SimpleTree buildSimpleClonePET(TreebankNode lca, TreebankNode t1, TreebankNode t2){ SimpleTree t = new SimpleTree(lca.getNodeType()); if(lca instanceof TerminalTreebankNode){ t.addChild(new SimpleTree(lca.getNodeValue())); }else{ for(int i = 0; i < lca.getChildren().size(); i++){ TreebankNode tn = lca.getChildren(i); if(tn.getEnd() > t1.getBegin() && tn.getBegin() < t2.getEnd()){ t.addChild(buildSimpleClonePET(lca.getChildren(i), t1, t2)); } } } return t; }
private static SimpleTree buildSimpleClonePET(TreebankNode lca, TreebankNode t1, TreebankNode t2){ SimpleTree t = new SimpleTree(lca.getNodeType()); if(lca instanceof TerminalTreebankNode){ t.addChild(new SimpleTree(lca.getNodeValue())); }else{ for(int i = 0; i < lca.getChildren().size(); i++){ TreebankNode tn = lca.getChildren(i); if(tn.getEnd() > t1.getBegin() && tn.getBegin() < t2.getEnd()){ t.addChild(buildSimpleClonePET(lca.getChildren(i), t1, t2)); } } } return t; }
private static MarkableProb processDem(Markable anaphor, List<Markable> anteList, JCas jcas){ double bestProb = 0.0; TreebankNode n = MarkableTreeUtils.markableNode(jcas, anaphor.getBegin(), anaphor.getEnd()); TreebankNode parent = (n != null ? n.getParent() : null); TreebankNode gparent = (parent != null ? parent.getParent() : null); Markable ante = null; for(Markable antecedent: anteList){ if(n!=null && parent != null && gparent != null && n.getNodeType().equals("WHNP") && parent.getNodeType().equals("SBAR") && gparent.getNodeType().equals("NP") && gparent.getChildren(1) == parent && gparent.getChildren(0).getNodeType().equals("NP")){ TreebankNode anteNode = gparent.getChildren(0); Markable trueAnte = MarkableTreeUtils.nodeMarkable(jcas, anteNode.getBegin(), anteNode.getEnd()); if(trueAnte == antecedent){ bestProb = 1.0; ante = antecedent; break; } } } return new MarkableProb(ante, bestProb); }
private static MarkableProb processDem(Markable anaphor, List<Markable> anteList, JCas jcas){ double bestProb = 0.0; TreebankNode n = MarkableTreeUtils.markableNode(jcas, anaphor.getBegin(), anaphor.getEnd()); TreebankNode parent = (n != null ? n.getParent() : null); TreebankNode gparent = (parent != null ? parent.getParent() : null); Markable ante = null; for(Markable antecedent: anteList){ if(n!=null && parent != null && gparent != null && n.getNodeType().equals("WHNP") && parent.getNodeType().equals("SBAR") && gparent.getNodeType().equals("NP") && gparent.getChildren(1) == parent && gparent.getChildren(0).getNodeType().equals("NP")){ TreebankNode anteNode = gparent.getChildren(0); Markable trueAnte = MarkableTreeUtils.nodeMarkable(jcas, anteNode.getBegin(), anteNode.getEnd()); if(trueAnte == antecedent){ bestProb = 1.0; ante = antecedent; break; } } } return new MarkableProb(ante, bestProb); }
private static void elevateAdjectives(JCas jcas){ FSIterator<Annotation> markables = jcas.getAnnotationIndex(NEMarkable.type).iterator(); while(markables.hasNext()){ NEMarkable mark = (NEMarkable) markables.next(); TreebankNode node = MarkableTreeUtils.markableNode(jcas, mark.getBegin(), mark.getEnd()); if(node.getNodeType().equals("JJ")){ while(node.getNodeType().equals("JJ")){ node = node.getParent(); if(node == null) break; } if(node != null){ mark.setBegin(node.getBegin()); mark.setEnd(node.getEnd()); } } } }
private static void elevateAdjectives(JCas jcas){ FSIterator<Annotation> markables = jcas.getAnnotationIndex(NEMarkable.type).iterator(); while(markables.hasNext()){ NEMarkable mark = (NEMarkable) markables.next(); TreebankNode node = MarkableTreeUtils.markableNode(jcas, mark.getBegin(), mark.getEnd()); if(node.getNodeType().equals("JJ")){ while(node.getNodeType().equals("JJ")){ node = node.getParent(); if(node == null) break; } if(node != null){ mark.setBegin(node.getBegin()); mark.setEnd(node.getEnd()); } } } }
public static void removeRightOfAnnotation(JCas jcas, TreebankNode node, Annotation annot) { // if the whole tree is to the left of the annotation then do nothing: if(node.getEnd() <= annot.getBegin() || node.getLeaf()) return; // if there is some overlap then iterate over trees, ignoring those to the left, recursing on those that overlap, and deleting those to the right for(int i = 0; i < node.getChildren().size(); i++){ TreebankNode child = node.getChildren(i); if(child.getEnd() <= annot.getBegin()){ // child is to the left of annotation completely continue; }else if(child.getBegin() > annot.getEnd()){ // child is to the right of annotation completely -- remove it and all to the right FSArray newChildren = new FSArray(jcas, i); for(int j = 0; j < i; j++){ newChildren.set(j, node.getChildren(j)); } node.setChildren(newChildren); break; }else{ removeRightOfAnnotation(jcas, child, annot); } } }
public static void removeRightOfAnnotation(JCas jcas, TreebankNode node, Annotation annot) { // if the whole tree is to the left of the annotation then do nothing: if(node.getEnd() <= annot.getBegin() || node.getLeaf()) return; // if there is some overlap then iterate over trees, ignoring those to the left, recursing on those that overlap, and deleting those to the right for(int i = 0; i < node.getChildren().size(); i++){ TreebankNode child = node.getChildren(i); if(child.getEnd() <= annot.getBegin()){ // child is to the left of annotation completely continue; }else if(child.getBegin() > annot.getEnd()){ // child is to the right of annotation completely -- remove it and all to the right FSArray newChildren = new FSArray(jcas, i); for(int j = 0; j < i; j++){ newChildren.set(j, node.getChildren(j)); } node.setChildren(newChildren); break; }else{ removeRightOfAnnotation(jcas, child, annot); } } }
public static TreebankNode getTreeCopy(JCas jcas, TreebankNode orig){ TreebankNode copy = null; if(orig instanceof TerminalTreebankNode){ copy = new TerminalTreebankNode(jcas); copy.setLeaf(true); copy.setChildren(null); }else{ copy = new TreebankNode(jcas); copy.setChildren(new FSArray(jcas, orig.getChildren().size())); for(int i = 0; i < orig.getChildren().size(); i++){ copy.setChildren(i, getTreeCopy(jcas, orig.getChildren(i))); copy.getChildren(i).setParent(copy); } } copy.setNodeType(orig.getNodeType()); copy.setNodeValue(orig.getNodeValue()); copy.setBegin(orig.getBegin()); copy.setEnd(orig.getEnd()); return copy; }
public static TreebankNode getTreeCopy(JCas jcas, TreebankNode orig){ TreebankNode copy = null; if(orig instanceof TerminalTreebankNode){ copy = new TerminalTreebankNode(jcas); copy.setLeaf(true); copy.setChildren(null); }else{ copy = new TreebankNode(jcas); copy.setChildren(new FSArray(jcas, orig.getChildren().size())); for(int i = 0; i < orig.getChildren().size(); i++){ copy.setChildren(i, getTreeCopy(jcas, orig.getChildren(i))); copy.getChildren(i).setParent(copy); } } copy.setNodeType(orig.getNodeType()); copy.setNodeValue(orig.getNodeValue()); copy.setBegin(orig.getBegin()); copy.setEnd(orig.getEnd()); return copy; }