Example usage for edu.stanford.nlp.trees Tree parent

List of usage examples for edu.stanford.nlp.trees Tree parent

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees Tree parent.

Prototype

public Tree parent(Tree root) 

Source Link

Document

Return the parent of the tree node.

Usage

From source file:elkfed.coref.features.pairs.FE_Span.java

License:Apache License

public static Boolean getSpanEmbed(PairInstance inst) {

    // should be in the same sentence
    if (inst.getAnaphor().getSentId() != inst.getAntecedent().getSentId())
        return false;

    // should not be in apposition
    if (FE_AppositiveParse.getAppositivePrs(inst))
        return false;

    // should not be adjacent (should take from appo_icab, but doesn't work for some reason)

    Markable m1 = inst.getAntecedent().getMarkable();
    Markable m2 = inst.getAnaphor().getMarkable();

    int sana = m2.getLeftmostDiscoursePosition();
    int sante = m1.getLeftmostDiscoursePosition();
    int eana = m2.getRightmostDiscoursePosition();
    int eante = m1.getRightmostDiscoursePosition();

    MiniDiscourse doc = m1.getMarkableLevel().getDocument();

    if (m1.getAttributeValue("min_ids") != null) {
        String[] spans = MarkableHelper.parseRanges(m1.getAttributeValue("min_ids"));
        sante = doc.DiscoursePositionFromDiscourseElementID(spans[0]);
        eante = doc.DiscoursePositionFromDiscourseElementID(spans[spans.length - 1]);
    }//from w  w w. jav  a  2s. co  m
    if (m2.getAttributeValue("min_ids") != null) {
        String[] spans = MarkableHelper.parseRanges(m2.getAttributeValue("min_ids"));
        sana = doc.DiscoursePositionFromDiscourseElementID(spans[0]);
        eana = doc.DiscoursePositionFromDiscourseElementID(spans[spans.length - 1]);
    }

    if (eana == sante - 1)
        return false;
    if (eante == sana - 1)
        return false;

    // check for trivial embedding, if maxnps are missing
    if (inst.getAntecedent().getMaxNPParseTree() == null && sana >= sante && eana <= eante)
        return true;
    if (inst.getAnaphor().getMaxNPParseTree() == null && sana <= sante && eana >= eante)
        return true;

    // check for maximal np embedding

    sana -= inst.getAnaphor().getSentenceStart();
    sante -= inst.getAntecedent().getSentenceStart();
    eana -= inst.getAnaphor().getSentenceStart();
    eante -= inst.getAntecedent().getSentenceStart();

    // if anaphor does have MaxNPParseTree -- check that it does not span over the antecedent
    // if it does -- check whether there is an s-node in between

    if (inst.getAnaphor().getMaxNPParseTree() != null) {
        Tree sentTree = inst.getAntecedent().getSentenceTree();
        List<Tree> Leaves = sentTree.getLeaves();
        Tree startNode = Leaves.get(sante);
        if (eante < Leaves.size()) {
            // check <leaves.size for markables spanning over sentence boundaries
            Tree endNode = Leaves.get(eante);
            if (inst.getAnaphor().getMaxNPParseTree().dominates(endNode)
                    && inst.getAnaphor().getMaxNPParseTree().dominates(startNode)) {
                Boolean sfound = false;
                Tree t = startNode;
                while (t != null && t != inst.getAnaphor().getMaxNPParseTree() && !sfound) {
                    if (t.value().toLowerCase().startsWith("s"))
                        sfound = true;
                    t = t.parent(sentTree);
                }
                if (!sfound)
                    return true;
            }
        }

    }

    // same for antecedent
    if (inst.getAntecedent().getMaxNPParseTree() != null) {
        Tree sentTree = inst.getAntecedent().getSentenceTree();
        List<Tree> Leaves = sentTree.getLeaves();
        Tree startNode = Leaves.get(sana);
        if (eana < Leaves.size()) {
            Tree endNode = Leaves.get(eana);

            if (inst.getAntecedent().getMaxNPParseTree().dominates(endNode)
                    && inst.getAntecedent().getMaxNPParseTree().dominates(startNode)) {
                Boolean sfound = false;
                Tree t = startNode;
                while (t != null && t != inst.getAntecedent().getMaxNPParseTree() && !sfound) {
                    if (t.value().toLowerCase().startsWith("s"))
                        sfound = true;
                    t = t.parent(sentTree);
                }
                if (!sfound)
                    return true;

            }
        }
    }

    return false;

}

From source file:elkfed.coref.mentions.Mention.java

License:Apache License

public String getRootPath() {
    Tree top = getSentenceTree();// ww w  . ja v  a2 s  .co m
    Tree here = getHighestProjection();
    StringBuffer sb = new StringBuffer();
    String lastValue = null;
    while (here != top) {
        here = here.parent(top);
        String val = here.value();
        if (!val.equals(lastValue))
            sb.append(here.value()).append(".");
        lastValue = val;
    }
    return sb.toString();
}

From source file:elkfed.coref.mentions.Mention.java

License:Apache License

/**
 * @author samuel/*w  w  w .ja  v a 2  s .  co  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

public void extractFeatures(ExpletiveInstance inst) {
    Tree t_root = inst.getRoot();//from  www .  ja v a2 s .c o  m
    Tree t_pron = inst.getPronoun();
    int idx;

    LabeledScoredTreeNode t2 = (LabeledScoredTreeNode) t_pron.parent(t_root);
    while (t2 != t_root && !t2.label().value().matches("(S|RRC|FRAG|VP)")) {
        t2 = (LabeledScoredTreeNode) t2.parent(t_root);
    }
    inst.setFeature(FD_EXPL_TREE, tree_inside(t2, t_pron));
    //inst.setFeature(FD_EXPL_TREE2, tree_markonly(t2,t_pron));
    inst.setFeature(FD_EXPL_TREE2, t2);
}

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);//from  ww  w.j  a va 2s.  c o m

    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) {/*from   w w w.  j  av a2s . 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

/** finds the highest projection of a node and
*  collects all the postmodifiers.//from  ww  w  . ja v  a  2  s  .  co  m
*/
private void calcHighestProjection(List<Tree>[] result, Tree sentenceTree) {
    Tree lowestProjection = result[0].get(0);
    Tree highestProjection = lowestProjection;
    List<Tree> projections = result[0];
    List<Tree> postmodifiers = result[2];
    if (!lowestProjection.value().equalsIgnoreCase("NP")) {
        return;
    }
    Tree parent;
    parentloop: while ((parent = highestProjection.parent(sentenceTree)) != null) {
        if (!parent.value().equalsIgnoreCase("NP"))
            break;
        // NN[P][S] and CC are a safe sign that this is not
        // a projection of our original NP
        for (Tree chld : parent.children()) {
            if (chld.value().toUpperCase().startsWith("NN") || chld.value().equalsIgnoreCase("CC"))
                break parentloop;
        }
        boolean chldSeen = false;
        for (Tree chld : parent.children()) {
            if (chld == highestProjection)
                chldSeen = true;
            else if (chldSeen && !chld.value().equals(",") && !chld.value().equals(":"))
                postmodifiers.add(chld);
        }
        projections.add(parent);
        highestProjection = parent;
    }
}

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.
 *//*  w  w  w . j av  a 2 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.GermanLanguagePlugin.java

License:Apache License

@Override
public String getHeadGF(Mention mention) {
    Markable markable = mention.getMarkable();

    String markableGF = "*NULL*";

    MiniDiscourse doc = markable.getMarkableLevel().getDocument();
    MarkableLevel deprel = doc.getMarkableLevelByName(DEFAULT_DEPREL_LEVEL);

    String head_pos = markable.getAttributeValue(AbstractLanguagePlugin.HEAD_POS);

    int head_disc_pos = doc.getDiscoursePositionFromDiscourseElementID(head_pos);

    try {/*from  w w w .j a v a 2s  . c  o  m*/
        markableGF = deprel.getMarkablesAtDiscoursePosition(head_disc_pos).get(0).getAttributeValue("tag");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
    //FIXME: mmax conversion bug?
    if (markableGF.equals("KON") || markableGF.equals("cj")) {
        Tree sentenceTree = mention.getSentenceTreeWithDiscIds();
        Tree conjuntion = mention.getMarkableSubTree();
        while (conjuntion.parent(sentenceTree) != null
                && conjuntion.parent(sentenceTree).value().toLowerCase().matches("(nx|en-add)")) {
            conjuntion = conjuntion.parent(sentenceTree);
        }
        ArrayList<String> phrase = mention.getHighestProjectingPhraseWithPOS(conjuntion, "NN");
        if (phrase == null) {
            phrase = mention.getHighestProjectingPhraseWithPOS(conjuntion, "NE");
        }
        if (phrase != null) {
            head_disc_pos = doc.getDiscoursePositionFromDiscourseElementID(phrase.get(0));
            markableGF = deprel.getMarkablesAtDiscoursePosition(head_disc_pos).get(0).getAttributeValue("tag");
        }
    }
    return markableGF;
}

From source file:elkfed.lang.ItalianLanguagePlugin.java

License:Apache License

@Override
public List<Tree>[] calcParseInfo(Tree sentTree, int startWord, int endWord, MentionType mentionType) {
    /* now, the *proper* way to do this would be to find the
     * head(s) and look for everything non-head. Instead,
     * we just look for children that look like modifiers,
     * which means that we get weird results
     * (i) for appositions/*from www  . j av  a2  s  . co m*/
     * (ii) for elliptic NPs (e.g., 'la gialla'),
     *      where the head 'gialla' also gets recruited as a modifier
     */
    List<Tree>[] result = new List[3];
    List<Tree> projections = new ArrayList<Tree>();
    List<Tree> premod = new ArrayList<Tree>();
    List<Tree> postmod = new ArrayList<Tree>();
    result[0] = projections;
    result[1] = premod;
    result[2] = postmod;
    Tree node = calcLowestProjection(sentTree, startWord, endWord);
    NodeCategory ncat = labelCat(node.label().value());
    if (startWord == endWord
            && (ncat == NodeCategory.CN || ncat == NodeCategory.PRO || ncat == NodeCategory.PN)) {
        node = node.parent(sentTree);
    }
    projections.add(node);
    for (Tree n : node.children()) {
        String cat = n.value();
        if (attr_node.matcher(cat).matches()) {
            premod.add(n);
        } else if (rel_node.matcher(cat).matches()) {
            postmod.add(n);
        }
    }
    return result;
}