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:ConstituencyParse.java

License:Apache License

public int[] constTreeParents(Tree tree) {
    Tree binarized = binarizer.transformTree(tree);
    Tree collapsedUnary = transformer.transformTree(binarized);
    Trees.convertToCoreLabels(collapsedUnary);
    collapsedUnary.indexSpans();/*from w ww.jav  a2  s  . c  o  m*/
    List<Tree> leaves = collapsedUnary.getLeaves();
    int size = collapsedUnary.size() - leaves.size();
    int[] parents = new int[size];
    HashMap<Integer, Integer> index = new HashMap<Integer, Integer>();

    int idx = leaves.size();
    int leafIdx = 0;
    for (Tree leaf : leaves) {
        Tree cur = leaf.parent(collapsedUnary); // go to preterminal
        int curIdx = leafIdx++;
        boolean done = false;
        while (!done) {
            Tree parent = cur.parent(collapsedUnary);
            if (parent == null) {
                parents[curIdx] = 0;
                break;
            }

            int parentIdx;
            int parentNumber = parent.nodeNumber(collapsedUnary);
            if (!index.containsKey(parentNumber)) {
                parentIdx = idx++;
                index.put(parentNumber, parentIdx);
            } else {
                parentIdx = index.get(parentNumber);
                done = true;
            }

            parents[curIdx] = parentIdx + 1;
            cur = parent;
            curIdx = parentIdx;
        }
    }

    return parents;
}

From source file:Anaphora_Resolution.ParseAllXMLDocuments.java

public static Tree HobbsResolve(Tree pronoun, ArrayList<Tree> forest) {
    Tree wholetree = forest.get(forest.size() - 1); // The last one is the one I am going to start from
    ArrayList<Tree> candidates = new ArrayList<Tree>();
    List<Tree> path = wholetree.pathNodeToNode(wholetree, pronoun);
    System.out.println(path);//from  ww w. j  a  va  2s.  co  m
    // Step 1
    Tree ancestor = pronoun.parent(wholetree); // This one locates the NP the pronoun is in, therefore we need one more "parenting" !
    // Step 2
    ancestor = ancestor.parent(wholetree);
    //System.out.println("LABEL: "+pronoun.label().value() + "\n\tVALUE: "+pronoun.firstChild());
    while (!ancestor.label().value().equals("NP") && !ancestor.label().value().equals("S"))
        ancestor = ancestor.parent(wholetree);
    Tree X = ancestor;
    path = X.pathNodeToNode(wholetree, pronoun);
    System.out.println(path);
    // Step 3
    for (Tree relative : X.children()) {
        for (Tree candidate : relative) {
            if (candidate.contains(pronoun))
                break; // I am looking to all the nodes to the LEFT (i.e. coming before) the path leading to X. contain <-> in the path
            //System.out.println("LABEL: "+relative.label().value() + "\n\tVALUE: "+relative.firstChild());
            if ((candidate.parent(wholetree) != X) && (candidate.parent(wholetree).label().value().equals("NP")
                    || candidate.parent(wholetree).label().value().equals("S")))
                if (candidate.label().value().equals("NP")) // "Propose as the antecedent any NP node that is encountered which has an NP or S node between it and X"
                    candidates.add(candidate);
        }
    }
    // Step 9 is a GOTO step 4, hence I will envelope steps 4 to 8 inside a while statement.
    while (true) { // It is NOT an infinite loop. 
        // Step 4
        if (X.parent(wholetree) == wholetree) {
            for (int q = 1; q < MAXPREVSENTENCES; ++q) {// I am looking for the prev sentence (hence we start with 1)
                if (forest.size() - 1 < q)
                    break; // If I don't have it, break
                Tree prevTree = forest.get(forest.size() - 1 - q); // go to previous tree
                // Now we look for each S subtree, in order of recency (hence right-to-left, hence opposite order of that of .children() ).
                ArrayList<Tree> backlist = new ArrayList<Tree>();
                for (Tree child : prevTree.children()) {
                    for (Tree subtree : child) {
                        if (subtree.label().value().equals("S")) {
                            backlist.add(child);
                            break;
                        }
                    }
                }
                for (int i = backlist.size() - 1; i >= 0; --i) {
                    Tree Treetovisit = backlist.get(i);
                    for (Tree relative : Treetovisit.children()) {
                        for (Tree candidate : relative) {
                            if (candidate.contains(pronoun))
                                continue; // I am looking to all the nodes to the LEFT (i.e. coming before) the path leading to X. contain <-> in the path
                            //System.out.println("LABEL: "+relative.label().value() + "\n\tVALUE: "+relative.firstChild());
                            if (candidate.label().value().equals("NP")) { // "Propose as the antecedent any NP node that you find"
                                if (!candidates.contains(candidate))
                                    candidates.add(candidate);
                            }
                        }
                    }
                }
            }
            break; // It will always come here eventually
        }
        // Step 5
        ancestor = X.parent(wholetree);
        //System.out.println("LABEL: "+pronoun.label().value() + "\n\tVALUE: "+pronoun.firstChild());
        while (!ancestor.label().value().equals("NP") && !ancestor.label().value().equals("S"))
            ancestor = ancestor.parent(wholetree);
        X = ancestor;
        // Step 6
        if (X.label().value().equals("NP")) { // If X is an NP
            for (Tree child : X.children()) { // Find the nominal nodes that X directly dominates
                if (child.label().value().equals("NN") || child.label().value().equals("NNS")
                        || child.label().value().equals("NNP") || child.label().value().equals("NNPS"))
                    if (!child.contains(pronoun))
                        candidates.add(X); // If one of them is not in the path between X and the pronoun, add X to the antecedents
            }
        }
        // Step SETTE
        for (Tree relative : X.children()) {
            for (Tree candidate : relative) {
                if (candidate.contains(pronoun))
                    continue; // I am looking to all the nodes to the LEFT (i.e. coming before) the path leading to X. contain <-> in the path
                //System.out.println("LABEL: "+relative.label().value() + "\n\tVALUE: "+relative.firstChild());
                if (candidate.label().value().equals("NP")) { // "Propose as the antecedent any NP node that you find"
                    boolean contains = false;
                    for (Tree oldercandidate : candidates) {
                        if (oldercandidate.contains(candidate)) {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains)
                        candidates.add(candidate);
                }
            }
        }
        // Step 8
        if (X.label().value().equals("S")) {
            boolean right = false;
            // Now we want all branches to the RIGHT of the path pronoun -> X.
            for (Tree relative : X.children()) {
                if (relative.contains(pronoun)) {
                    right = true;
                    continue;
                }
                if (!right)
                    continue;
                for (Tree child : relative) { // Go in but do not go below any NP or S node. Go below the rest
                    if (child.label().value().equals("NP")) {
                        candidates.add(child);
                        break; // not sure if this means avoid going below NP but continuing with the rest of non-NP children. Should be since its DFS.
                    }
                    if (child.label().value().equals("S"))
                        break; // Same
                }
            }
        }
    }

    // Step 9 is a GOTO, so we use a while.

    System.out.println(pronoun + ": CHAIN IS " + candidates.toString());
    ArrayList<Integer> scores = new ArrayList<Integer>();

    for (int j = 0; j < candidates.size(); ++j) {
        Tree candidate = candidates.get(j);
        Tree parent = null;
        int parent_index = 0;
        for (Tree tree : forest) {
            if (tree.contains(candidate)) {
                parent = tree;
                break;
            }
            ++parent_index;
        }
        scores.add(0);
        if (parent_index == 0)
            scores.set(j, scores.get(j) + 100); // If in the last sentence, +100 points
        scores.set(j, scores.get(j) + syntacticScore(candidate, parent));

        if (existentialEmphasis(candidate)) // Example: "There was a dog standing outside"
            scores.set(j, scores.get(j) + 70);
        if (!adverbialEmphasis(candidate, parent))
            scores.set(j, scores.get(j) + 50);
        if (headNounEmphasis(candidate, parent))
            scores.set(j, scores.get(j) + 80);

        int sz = forest.size() - 1;
        //          System.out.println("pronoun in sentence " + sz + "(sz). Candidate in sentence "+parent_index+" (parent_index)");
        int dividend = 1;
        for (int u = 0; u < sz - parent_index; ++u)
            dividend *= 2;
        //System.out.println("\t"+dividend);
        scores.set(j, scores.get(j) / dividend);
        System.out.println(candidate + " -> " + scores.get(j));
    }
    int max = -1;
    int max_index = -1;
    for (int i = 0; i < scores.size(); ++i) {
        if (scores.get(i) > max) {
            max_index = i;
            max = scores.get(i);
        }
    }
    Tree final_candidate = candidates.get(max_index);
    System.out.println("My decision for " + pronoun + " is: " + final_candidate);
    // Decide what candidate, with both gender resolution and Lappin and Leass ranking.

    Tree pronounparent = pronoun.parent(wholetree).parent(wholetree); // 1 parent gives me the NP of the pronoun
    int pos = 0;
    for (Tree sibling : pronounparent.children()) {
        System.out.println("Sibling " + pos + ": " + sibling);
        if (sibling.contains(pronoun))
            break;
        ++pos;
    }
    System.out.println("Before setchild: " + pronounparent);
    @SuppressWarnings("unused")
    Tree returnval = pronounparent.setChild(pos, final_candidate);
    System.out.println("After setchild: " + pronounparent);

    return wholetree; // wholetree is already modified, since it contains pronounparent
}

From source file:Anaphora_Resolution.ParseAllXMLDocuments.java

private static int syntacticScore(Tree candidate, Tree root) {
    // We will check whether the NP is inside an S (hence it would be a subject)
    // a VP (direct object)
    // a PP inside a VP (an indirect obj)
    Tree parent = candidate;
    while (!parent.label().value().equals("S")) {
        if (parent.label().value().equals("VP"))
            return 50; // direct obj
        if (parent.label().value().equals("PP")) {
            Tree grandparent = parent.parent(root);
            while (!grandparent.label().value().equals("S")) {
                if (parent.label().value().equals("VP")) // indirect obj is a PP inside a VP
                    return 40;
                parent = grandparent;//from   www  .  j a  v a 2  s  .  c o m
                grandparent = grandparent.parent(root);
            }
        }
        parent = parent.parent(root);
    }
    return 80; // If nothing remains, it must be the subject
}

From source file:Anaphora_Resolution.ParseAllXMLDocuments.java

private static boolean headNounEmphasis(Tree candidate, Tree root) {
    Tree parent = candidate.parent(root);
    while (!parent.label().value().equals("S")) { // If it is the head NP, it is not contained in another NP (that's exactly how the original algorithm does it)
        if (parent.label().value().equals("NP"))
            return false;
        parent = parent.parent(root);/*  ww  w  .j  av a2 s .  com*/
    }
    return true;
}

From source file:Anaphora_Resolution.ParseAllXMLDocuments.java

private static boolean adverbialEmphasis(Tree candidate, Tree root) { // Like in "Inside the castle, King Arthur was invincible". "Castle" has the adv emph.
    Tree parent = candidate;
    while (!parent.label().value().equals("S")) {
        if (parent.label().value().equals("PP")) {
            for (Tree sibling : parent.siblings(root)) {
                if ((sibling.label().value().equals(","))) {
                    //System.out.println("adv Emph!");
                    return true;
                }/*from   w w  w.  j  a  va2 s .  com*/
            }
        }
        parent = parent.parent(root);
    }
    return false;
}

From source file:artinex.TypDep.java

public static void main(String[] args) {
    String str = "What is index in array";
    TypDep parser = new TypDep();
    Tree tree = parser.parse(str);//from   w w w . j  a v  a  2 s .  c  om

    List<Tree> leaves = tree.getLeaves();
    // Print words and Pos Tags
    for (Tree leaf : leaves) {
        Tree parent = leaf.parent(tree);
        System.out.print(leaf.label().value() + "-" + parent.label().value() + " ");
    }
    System.out.println();

    //Type dependencies

    // Tree tree1 = str.get(TreeAnnotation.class);
    // Get dependency tree
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    Collection<TypedDependency> td = gs.typedDependenciesCollapsed();
    System.out.println(td);

}

From source file:Ceist.CeistView.java

License:Open Source License

/**
 * Displays the match results in a table with the matched parts
 * formatted./*www  .  ja  v a  2s  .co  m*/
 *
 * @param m the matcher containing the match results
 * @param matchedTree the tree which was matched
 * @param showTagged whether to show POS tags or not
 * @return the HTML to be displayed in the table row
 */
private String[] getMatcherTableRow(TregexMatcher m, Tree matchedTree, boolean showTagged) {
    //List<Tree> allMatches = new ArrayList<Tree>();

    // Find matches for templates
    String strQuestion = QuestionTemplate.getQuestionString(m, txtQuestionTemplate.getText());
    String strAnswer = AnswerTemplate.getAnswerString(m, txtAnswerTemplate.getText());

    // Display the full tree in which the match was found
    String strMatchAll = "<html>";
    String lastRef = "";

    for (Tree t : matchedTree.getLeaves()) {
        String nodeValue = t.nodeString();

        if (nodeValue.startsWith("{Q")) { // This is a match for the question string
            String ref = nodeValue.substring(2, nodeValue.indexOf("}"));
            nodeValue = nodeValue.substring(nodeValue.indexOf("}") + 1);
            t.setValue(nodeValue);

            if (!ref.equals(lastRef))
                lastRef = ref;
            else
                ref = "";

            if (!showTagged)
                strMatchAll += "<sup>" + ref + "</sup><b><font color=green>" + nodeValue + "</font></b> ";
            else
                strMatchAll += "<sup>" + ref + "</sup><b><font color=green>" + nodeValue
                        + "</font><font color=gray>/" + t.parent(matchedTree).nodeString() + "</font></b> ";

        } else if (nodeValue.startsWith("{A")) { // This is a match for the answer string
            String ref = nodeValue.substring(2, nodeValue.indexOf("}"));
            nodeValue = nodeValue.substring(nodeValue.indexOf("}") + 1);
            t.setValue(nodeValue);

            if (!ref.equals(lastRef))
                lastRef = ref;
            else
                ref = "";

            if (!showTagged)
                strMatchAll += "<sup>" + ref + "</sup><b>" + nodeValue + "</b> ";
            else
                strMatchAll += "<sup>" + ref + "</sup><b>" + nodeValue + "<font color=gray>/"
                        + t.parent(matchedTree).nodeString() + "</font></b> ";
        } else { // Normal unmatched text
            if (!showTagged)
                strMatchAll += nodeValue + " ";
            else
                strMatchAll += nodeValue + "<font color=gray>/" + t.parent(matchedTree).nodeString()
                        + "</font> ";
        }
    }

    strMatchAll += "</html>";

    return new String[] { strMatchAll, strQuestion, strAnswer };

}

From source file:edu.cmu.ark.AnalysisUtilities.java

License:Open Source License

public static void downcaseFirstToken(Tree inputTree) {
    Tree firstWordTree = inputTree.getLeaves().get(0);
    if (firstWordTree == null)
        return;/*from  w  w w  . jav a  2s  .  c om*/
    Tree preterm = firstWordTree.parent(inputTree);
    String firstWord = firstWordTree.yield().toString();
    if (!preterm.label().toString().matches("^NNP.*") && !firstWord.equals("I")) {
        //if(firstWord.indexOf('-') == -1 && !firstWord.equals("I")){
        firstWord = firstWord.substring(0, 1).toLowerCase() + firstWord.substring(1);
        firstWordTree.label().setValue(firstWord);
    }

    //if(QuestionTransducer.DEBUG) System.err.println("downcaseFirstToken: "+inputTree.toString());
}

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);
    while (n1Parent != null && n1Parent.numChildren() == 1) {
        n1Parent = n1Parent.parent(root);
    }/* w w  w . j  av a  2s  .c o  m*/

    if (n1Parent != null && n1Parent.dominates(n2))
        return true;

    return false;
}

From source file:edu.cmu.ark.nlp.question.Question.java

License:Open Source License

public List<Tree> findLogicalWordsAboveIntermediateTree() {
    List<Tree> res = new ArrayList<Tree>();

    Tree pred = intermediateTree.getChild(0).headPreTerminal(this.hf);
    String lemma = QuestionUtil.getLemma(pred.yield().toString(), pred.label().toString());

    String tregexOpStr;/*from  ww w  .j  a  v a  2 s  .  co  m*/
    TregexPattern matchPattern;
    TregexMatcher matcher;

    Tree sourcePred = null;
    for (Tree leaf : sourceTree.getLeaves()) {
        Tree tmp = leaf.parent(sourceTree);
        String sourceLemma = QuestionUtil.getLemma(leaf.label().toString(), tmp.label().toString());
        if (sourceLemma.equals(lemma)) {
            sourcePred = tmp;
            break;
        }
    }

    tregexOpStr = "RB|VB|VBD|VBP|VBZ|IN|MD|WRB|WDT|CC=command";
    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(sourceTree);

    Tree command;
    while (matcher.find() && sourcePred != null) {
        command = matcher.getNode("command");
        if (QuestionUtil.cCommands(sourceTree, command, sourcePred)
                && command.parent(sourceTree) != sourcePred.parent(sourceTree)) {
            res.add(command);
        }
    }

    return res;
}