List of usage examples for edu.stanford.nlp.trees Tree dominates
public boolean dominates(Tree t)
From source file:edu.cmu.ark.AnalysisUtilities.java
License:Open Source License
public static boolean cCommands(Tree root, Tree n1, Tree n2) { if (n1.dominates(n2)) return false; Tree n1Parent = n1.parent(root);/*from ww w . ja va 2s.com*/ while (n1Parent != null && n1Parent.numChildren() == 1) { n1Parent = n1Parent.parent(root); } if (n1Parent != null && n1Parent.dominates(n2)) return true; return false; }
From source file:elkfed.coref.features.pairs.FE_CCommand.java
License:Apache License
public static Boolean getCCommand(PairInstance inst) { // should be in the same sentence if (inst.getAnaphor().getSentId() != inst.getAntecedent().getSentId()) return false; //Ana should not be reflexive or reciprocal pronoun if (inst.getAnaphor().getReflPronoun()) return false; // should have not-null maxnp-trees (otherwise -- problematic mentions) Tree sentenceTree = inst.getAnaphor().getSentenceTree(); Tree AnaTree = inst.getAnaphor().getMaxNPParseTree(); Tree AnteTree = inst.getAntecedent().getMaxNPParseTree(); if (sentenceTree == null) return false; if (AnaTree == null) return false; if (AnteTree == null) return false; // should not dominate each other if (AnaTree.dominates(AnteTree)) return false; if (AnteTree.dominates(AnaTree)) return false; //the first branching node for ante should dominate ana (but not via S-node) AnteTree = AnteTree.parent(sentenceTree); while (AnteTree != null) { if (AnteTree.children().length > 1) { if (!AnteTree.dominates(AnaTree)) return false; while (AnaTree != null && AnaTree != AnteTree) { if (AnaTree.value().toLowerCase().startsWith("s")) return false; AnaTree = AnaTree.parent(sentenceTree); }/*from w ww. j a v a 2 s . c om*/ return true; } AnteTree = AnteTree.parent(sentenceTree); } return false; }
From source file:elkfed.coref.mentions.Mention.java
License:Apache License
/** * @author samuel// ww w . j ava 2 s . c o m * @param leftMostDiscursePos * left most discourse position * @param rightMostDiscursePos * most discourse position * @return The subtree inside a span from the current mentions * sentenceTreeWithDiscIds */ public Tree getMarkableTree(int leftMostDiscursePos, int rightMostDiscursePos) { Tree sentenceTree = this.getSentenceTreeWithDiscIds(); List<Tree> leaves = sentenceTree.getLeaves(); int start = leftMostDiscursePos - this.getSentenceStart(); int end = rightMostDiscursePos - this.getSentenceStart(); Tree startNode = leaves.get(start); Tree endNode = leaves.get(end); Tree parentNode = startNode; while (parentNode != null && !parentNode.dominates(endNode)) { parentNode = parentNode.parent(sentenceTree); } return parentNode; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
/** * constructs a marked subtree for the part where the * pronoun is <i>inside</i> the subtree * @param node the starting point// w w w . j av a 2 s . co m * @param pron our pronoun * @return a marked subtree for the tree starting with node */ public static Tree tree_inside(Tree node, Tree pron) { LabelFactory lf = new StringLabelFactory(); int pron_left = pron.leftCharEdge(node); int pron_right = pron.rightCharEdge(node); List<Tree> dtrs = new ArrayList<Tree>(node.children().length); boolean node_seen = false; for (Tree t : node.children()) { if (t == pron) { dtrs.add(t); node_seen = true; } else if (t.dominates(pron)) { dtrs.add(tree_inside(t, pron)); node_seen = true; } else { String cat = t.value(); if (cat.matches("S|SBAR")) { dtrs.add(tree_pruned(t)); } else { dtrs.add(tree_outside(t)); } } } Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value() + "-I")); result.setChildren(dtrs); return result; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
public static Tree tree_markonly(Tree node, Tree pron) { LabelFactory lf = new StringLabelFactory(); List<Tree> dtrs = new ArrayList<Tree>(node.children().length); for (Tree t : node.children()) { if (t == pron) { dtrs.add(t);/*from w w w.ja v a2s . c o m*/ } else if (t.dominates(pron)) { dtrs.add(tree_markonly(t, pron)); } else { dtrs.add(t); } } Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value() + "-I")); result.setChildren(dtrs); return result; }
From source file:elkfed.expletives.ExpletiveInstance.java
License:Apache License
public ExpletiveInstance(Tree root, Tree pronoun, String id) { _root = root;/*w w w .ja v a 2 s. c o m*/ _pronoun = pronoun; _id = id; List<Tree> wordsT = root.getLeaves(); List<Label> posT = root.preTerminalYield(); // get words and POS into an array so that // we get an idea of the pronoun's surrounding String[] words = new String[wordsT.size()]; String[] pos = new String[wordsT.size()]; if (!root.dominates(pronoun)) { System.err.format("%s does not dominate %s. WTF?", root, pronoun); } for (int here = 0; here < wordsT.size(); here++) { Tree w1 = wordsT.get(here); Label p1 = posT.get(here); words[here] = w1.toString(); pos[here] = p1.value(); if (w1 == pronoun) { _idx = here; } else if (pronoun.dominates(w1)) { _idx = here; pronoun = w1; } } assert _idx >= 0 : String.format("wanted %s in %s", pronoun, root); assert pos[_idx].equals("PRP") : String.format("wanted PRP got '%s'", pos[_idx]); _words = words; _pos = pos; }
From source file:elkfed.lang.AbstractLanguagePlugin.java
License:Apache License
protected Tree calcLowestProjection(Tree sentenceTree, int startWord, int endWord) { List<Tree> Leaves = sentenceTree.getLeaves(); Tree startNode = Leaves.get(startWord); Tree endNode = Leaves.get(endWord);// w w w. j a v a 2s . com Tree parentNode = startNode; while (parentNode != null && !parentNode.dominates(endNode)) { parentNode = parentNode.parent(sentenceTree); } if (parentNode == null) { return startNode; } return parentNode; }
From source file:elkfed.lang.EnglishLanguagePlugin.java
License:Apache License
public Tree[] calcParseExtra(Tree sentenceTree, int startWord, int endWord, Tree prsHead, HeadFinder StHeadFinder) {//w w w. j av a 2 s.c o m List<Tree> Leaves = sentenceTree.getLeaves(); Tree startNode = Leaves.get(startWord); Tree endNode = null; if (endWord >= Leaves.size()) { // for marks that do not respect sentence boundaries endNode = Leaves.get(Leaves.size() - 1); } else { endNode = Leaves.get(endWord); } Tree prevNode = null; if (startWord > 0) prevNode = Leaves.get(startWord - 1); Tree nextNode = null; if (endWord < Leaves.size() - 1) nextNode = Leaves.get(endWord + 1); Tree[] result = new Tree[3]; //---------- calculate minimal np-like subtree, containing the head and included in the mention Tree HeadNode = prsHead; if (prsHead == null) { // todo: this should be fixed somehow though // todo (ctd): use getHeadIndex from NPHeadFinder, but need to reconstruct the markable // todo (ctd): mind marks spanning over sentene boundaries result[0] = null; result[1] = null; result[2] = null; return result; } Tree mincand = prsHead; Tree t = mincand; Tree minnp = null; Tree maxnp = null; while (t != null && (prevNode == null || !t.dominates(prevNode)) && (nextNode == null || !t.dominates(nextNode))) { if (t.value().equalsIgnoreCase("NP")) { mincand = t; t = null; } if (t != null) t = t.parent(sentenceTree); } result[0] = mincand; t = mincand; while (t != null && (t == mincand || !iscoordnp(t))) { if (t.value().equalsIgnoreCase("NP")) { if (t.headTerminal(StHeadFinder) == HeadNode) { maxnp = t; if (minnp == null) minnp = t; } else { t = null; } } if (t != null) t = t.parent(sentenceTree); } result[1] = minnp; result[2] = maxnp; return result; }
From source file:elkfed.lang.EnglishLanguagePlugin.java
License:Apache License
/** determines baseNP node and highest projection. * This code comes from Xiaofeng's SyntaxTreeFeature.alignToTree method, * but was moved here so that (i) everyone profits from it and * (ii) everyone (including Xiaofeng) profits from eventual bugfixes * that are applied to this central place. *//*from w w w . j a va2 s. c om*/ protected Tree calcLowestProjection(Tree sentenceTree, int startWord, int endWord) { List<Tree> Leaves = sentenceTree.getLeaves(); Tree startNode = Leaves.get(startWord); Tree endNode = null; if (endWord >= Leaves.size()) { // for marks that do not respect sentence boundaries endNode = Leaves.get(Leaves.size() - 1); } else { endNode = Leaves.get(endWord); } Tree parentNode = startNode; while (parentNode != null && !parentNode.dominates(endNode)) { parentNode = parentNode.parent(sentenceTree); } if (parentNode == null) { return startNode; } //to deal with the embeded NPs //like "(NP (dt the) [(nnp iditarod) (nnp trail)] [ (nnp sled) (nnp dog) (nn race)] )" if (parentNode.value().charAt(0) == 'N' || parentNode.value().charAt(0) == 'n') { int LastNodeIndex = parentNode.children().length - 1; Tree lastNode = parentNode.children()[LastNodeIndex]; if ((lastNode.value().equalsIgnoreCase("NP") || lastNode.value().equalsIgnoreCase("NNP") || lastNode.value().equalsIgnoreCase("NNPS") || lastNode.value().equalsIgnoreCase("NNS") || lastNode.value().equalsIgnoreCase("NN")) && !lastNode.dominates(endNode) && lastNode != endNode) { return endNode.parent(parentNode); } } return parentNode; }
From source file:elkfed.lang.ItalianLanguagePlugin.java
License:Apache License
@Override public Tree[] calcParseExtra(Tree sentenceTree, int startWord, int endWord, Tree prsHead, HeadFinder StHeadFinder) {/*from ww w . j a v a 2 s .co m*/ List<Tree> Leaves = sentenceTree.getLeaves(); Tree startNode = Leaves.get(startWord); Tree endNode = null; if (endWord >= Leaves.size()) { // for marks that do not respect sentence boundaries endNode = Leaves.get(Leaves.size() - 1); } else { endNode = Leaves.get(endWord); } Tree prevNode = null; if (startWord > 0) prevNode = Leaves.get(startWord - 1); Tree nextNode = null; if (endWord < Leaves.size() - 1) nextNode = Leaves.get(endWord + 1); Tree[] result = new Tree[3]; //---------- calculate minimal np-like subtree, containing the head and included in the mention Tree HeadNode = prsHead; if (prsHead == null) { // todo: this should be fixed somehow though // todo (ctd): use getHeadIndex from NPHeadFinder, but need to reconstruct the markable // todo (ctd): mind marks spanning over sentene boundaries result[0] = null; result[1] = null; result[2] = null; return result; } Tree mincand = prsHead; Tree t = mincand; Tree minnp = null; Tree maxnp = null; while (t != null && (prevNode == null || !t.dominates(prevNode)) && (nextNode == null || !t.dominates(nextNode))) { if (t.value().equalsIgnoreCase("NP")) { mincand = t; t = null; } if (t != null) t = t.parent(sentenceTree); } result[0] = mincand; t = mincand; while (t != null && (t == mincand || !iscoordnp(t))) { if (t.value().equalsIgnoreCase("NP")) { if (t.headTerminal(StHeadFinder) == HeadNode) { maxnp = t; if (minnp == null) minnp = t; } else { t = null; } } if (t != null) t = t.parent(sentenceTree); } result[1] = minnp; result[2] = maxnp; return result; }