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

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

Introduction

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

Prototype

@Override
    public String value() 

Source Link

Usage

From source file:elkfed.expletives.EF_Tree.java

License:Apache License

/** constructs a marked subtree for parts which are
 * outside the path to the pronoun//w  w w .  j ava 2 s.  co  m
 * @param node the starting point
 * @return a marked subtree for the tree starting with node
 */
public static Tree tree_outside(Tree node) {
    LabelFactory lf = new StringLabelFactory();
    // verbs and modals are copied verbatim
    if (node.value().matches("VB[DZPNG]?")) {
        return tagged_word(Morphology.stemStatic(node.children()[0].value(), node.value()).value(), "VBX");
        //return node;
    } else if (node.value().matches("TO|MD|IN|RB")) {
        return node;
    }
    Tree result = new LabeledScoredTreeNode();
    result.setLabel(lf.newLabel(node.value()));
    if (node.value().matches("VP")) {
        List<Tree> dtrs = new ArrayList<Tree>();
        dtrs_inside(node, dtrs);
        result.setChildren(dtrs);
    } else {
        List<Tree> dtrs = null;
        result.setChildren(dtrs);
    }
    return result;
}

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/*from   w w w.  j  a v  a 2 s .  c om*/
 * @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);/* ww 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.lang.EnglishLanguagePlugin.java

License:Apache License

private Boolean iscoordnp(Tree np) {
    // helper -- checks that a parse np-tree is in fact coordination (contains CC on the highest level)
    if (np == null)
        return false;
    if (!np.value().equalsIgnoreCase("NP"))
        return false;
    Tree[] chlds = np.children();// w  w  w  .  jav  a  2 s .c  o m
    for (int i = 0; i < chlds.length; i++) {
        if (chlds[i].value().equalsIgnoreCase("CC"))
            return true;
    }
    return false;
}

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.com

    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./*w  w  w .  j a  v  a 2s .c om*/
*/
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  a  v  a2 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 List<Tree>[] calcParseInfo(Tree sentTree, int startWord, int endWord, MentionType markableType) {
    /* 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//  ww w .j  a v  a  2  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);
    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;
}

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  ww w  .  ja v  a 2 s  .  c om*/
     * (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;
}

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 w  ww  . j a  va  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;

}