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; }
private static String calcNPunderVP(TreebankNode n){ if(n != null && n.getParent() != null && n.getParent().getNodeType().equals("VP")){ return "Y"; } return "N"; }
/** @generated * @param jcas JCas to which this Feature Structure belongs * @param begin offset to the begin spot in the SofA * @param end offset to the end spot in the SofA */ public TreebankNode(JCas jcas, int begin, int end) { super(jcas); setBegin(begin); setEnd(end); readObject(); }
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; }
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); }
typeParts = parse.getType().split("-"); parent.setNodeType(typeParts[0]); parent.setNodeValue(null); parent.setLeaf(parse.getChildCount() == 0); StringArray tags = new StringArray(jcas, typeParts.length-1); for(int i = 1; i < typeParts.length; i++){ tags.set(i-1, typeParts[i]); parent.setNodeTags(tags); parent.setHeadIndex(parse.getHeadIndex()); }else{ try{ TreebankNode child = new TreebankNode(jcas); child.setParent(parent); children.set(i, child); recursivelyCreateStructure(jcas, child, subtree, root); child.addToIndexes(); }catch(NullPointerException e){ System.err.println("MaxentParserWrapper Error: " + e); parent.setBegin(((TreebankNode)children.get(0)).getBegin()); parent.setEnd(((TreebankNode)children.get(subtrees.length-1)).getEnd()); parent.setChildren(children);
if(!tree.getLeaf()){ for(int i = 0; i < tree.getChildren().size(); i++){ TreebankNode child = tree.getChildren(i); if(child.getBegin() <= arg1.getBegin() && child.getEnd() >= arg1.getEnd()){ tree = child; break; // break out of inner for-loop if(tree.getBegin() == arg1.getBegin() && tree.getEnd() == arg1.getEnd()){ while(tree.getParent() != null && tree.getParent().getBegin() == arg1.getBegin() && tree.getParent().getEnd() == arg1.getEnd()){ tree = tree.getParent(); newTree = new TreebankNode(jcas, tree.getBegin(), tree.getEnd()); newTree.setNodeType(nodeType); newTree.setChildren(new FSArray(jcas, 1)); newTree.setChildren(0, tree); newTree.setParent(tree.getParent()); TreeUtils.replaceChild(tree.getParent(), tree, newTree); tree.setParent(newTree); if(!tree.getLeaf()){ for(int i = 0; i < tree.getChildren().size(); i++){ if(startChild == -1){ if(tree.getChildren(i).getBegin() == arg1.getBegin()){ startChild = i; }else if(tree.getChildren(i).getEnd() == arg1.getEnd()){ endChild = i; break;
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 elevateListConcepts(JCas jcas, TreebankNode tree) { if(tree.getLeaf()) return; for(int i = 0; i < tree.getChildren().size(); i++){ if(tree.getChildren(i).getNodeType().equals("CONCEPT")){ conceptIndex = i; break; for(int i = 0; i < tree.getChildren().size(); i++){ elevateListConcepts(jcas, tree.getChildren(i)); if(conceptIndex == 0 && tree.getChildren().size() > 1 && tree.getChildren(1).getNodeType().matches("CC|,") || conceptIndex == tree.getChildren().size()-1 && tree.getChildren().size() > 1 && tree.getChildren(conceptIndex-1).getNodeType().matches("CC|,") || conceptIndex > 0 && conceptIndex < tree.getChildren().size()-1 && tree.getChildren().size() > 2 && tree.getChildren(conceptIndex-1).getNodeType().equals(",") && tree.getChildren(conceptIndex+1).getNodeType().matches("CC|,")){ TreebankNode entityRoot = tree.getChildren(conceptIndex).getChildren(0); tree.setChildren(conceptIndex, entityRoot); entityRoot.setParent(tree); TreebankNode replacementNode = new TreebankNode(jcas); replacementNode.setNodeType(tree.getNodeType()); replacementNode.setChildren(tree.getChildren()); for(int i = 0; i < replacementNode.getChildren().size(); i++){ replacementNode.getChildren(i).setParent(replacementNode); replacementNode.setParent(tree); tree.setNodeType("CONCEPT"); tree.setChildren(children);
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; }
if(! treebankNode.getNodeType().startsWith("N")) { return originalSpan; if(treebankNode.getNodeType().equals("NP")) { treebankNodeSizes.put(treebankNode, treebankNode.getCoveredText().length()); identifiedAnnotation.setBegin(sortedTreebankNodes.get(0).getBegin()); identifiedAnnotation.setEnd(sortedTreebankNodes.get(0).getEnd());
public static String tree2str(TreebankNode pathTree){ StringBuffer buff = new StringBuffer(); buff.append("("); try{ buff.append(pathTree.getNodeType()); }catch(Exception e){ System.err.println("Caught NPE"); } if(pathTree.getLeaf()){ //pathTree.getChildren().size() == 1 && pathTree.getChildren(0).getLeaf()){ buff.append(" "); buff.append(pathTree.getNodeValue()); // buff.append(")"); }else{ for(int i = 0; i < pathTree.getChildren().size(); i++){ buff.append(" "); buff.append(tree2str(pathTree.getChildren(i))); } } buff.append(")"); return buff.toString(); }
public static SimpleTree extractPathEnclosedTree(TreebankNode t1, TreebankNode t2, JCas jcas){ SimpleTree node = null; if(t1.getBegin() > t2.getBegin()){ TreebankNode temp = t1; t1 = t2; t2 = temp; if(t1 == t2 || (t1.getBegin() >= t2.getBegin() && t1.getEnd() <= t2.getEnd()) || (t2.getBegin() >= t1.getBegin() && t2.getEnd() <= t1.getEnd())){ node = sameTree(t1,t2); }else{ if(lca == null){ lca = new TopTreebankNode(jcas); lca.setNodeType("TOP"); lca.setChildren(new FSArray(jcas,2)); if(l1.size()==0){ l1.add(t1); l2.add(t2); lca.setChildren(0, l1.get(0)); lca.setChildren(1, l2.get(0));
public static TopTreebankNode getTopNode(TreebankNode inTree) { TreebankNode cur = inTree; TopTreebankNode top = null; while(!(cur instanceof TopTreebankNode)){ cur = cur.getParent(); } top = (TopTreebankNode) cur; return top; }
/** Internal - constructor used by generator * @generated * @param addr low level Feature Structure reference * @param type the type of this Feature Structure */ public TreebankNode(int addr, TOP_Type type) { super(addr, type); readObject(); }
public static SimpleTree getSimpleClone(TreebankNode node) { SimpleTree t = new SimpleTree(node.getNodeType()); if(node instanceof TerminalTreebankNode){ t.addChild(new SimpleTree(node.getNodeValue())); }else{ for(int i = 0; i < node.getChildren().size(); i++){ t.addChild(getSimpleClone(node.getChildren(i))); } } return t; }
@Override public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs( JCas identifiedAnnotationView, Annotation sentence) { List<IdentifiedAnnotationPair> pairs = super.getCandidateRelationArgumentPairs(identifiedAnnotationView, sentence); // find pairs enclosed inside a noun phrase List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>(); for(IdentifiedAnnotationPair pair : pairs) { if(Utils.validateDegreeOfArgumentTypes(pair)) { for(TreebankNode nounPhrase : Utils.getNounPhrases(identifiedAnnotationView, (Sentence) sentence)) { if(Utils.isEnclosed(pair, nounPhrase)) { IdentifiedAnnotation arg1 = pair.getArg1(); IdentifiedAnnotation arg2 = pair.getArg2(); result.add(new IdentifiedAnnotationPair(arg1, arg2)); System.out.println("NP: " + nounPhrase.getCoveredText() + ", " + nounPhrase.getBegin() + ", " + nounPhrase.getEnd()); System.out.println("arg1: " + arg1.getCoveredText() + ", " + arg1.getBegin() + ", " + arg1.getEnd()); System.out.println("arg2: " + arg2.getCoveredText() + ", " + arg2.getBegin() + ", " + arg2.getEnd()); System.out.println(); break; // don't check other NPs } } } } return result; } }
public static Queue<TreebankNode> initializeQueue(TreebankNode X, TreebankNode Y){ Queue<TreebankNode> q = new LinkedList<TreebankNode>(); while(Y.getParent() != X){ Y = Y.getParent(); } for(int i = 0; i < X.getChildren().size(); i++){ TreebankNode n = X.getChildren(i); q.add(n); if(n == Y) break; } return q; }