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

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

Introduction

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

Prototype

public abstract Tree[] children();

Source Link

Document

Returns an array of children for the current node.

Usage

From source file:CollapseUnaryTransformer.java

License:Apache License

public Tree transformTree(Tree tree) {
    if (tree.isPreTerminal() || tree.isLeaf()) {
        return tree.deepCopy();
    }/*from w w w  .j a v  a  2 s.c o  m*/

    Label label = tree.label().labelFactory().newLabel(tree.label());
    Tree[] children = tree.children();
    while (children.length == 1 && !children[0].isLeaf()) {
        children = children[0].children();
    }
    List<Tree> processedChildren = Generics.newArrayList();
    for (Tree child : children) {
        processedChildren.add(transformTree(child));
    }
    return tree.treeFactory().newTreeNode(label, processedChildren);
}

From source file:BuildBinarizedDataset.java

/**
 * Sets all of the labels on a tree to the given default value.
 *//*from  ww w  . j  av  a  2  s .  co m*/
public static void setUnknownLabels(Tree tree, Integer defaultLabel) {
    if (tree.isLeaf()) {
        return;
    }

    for (Tree child : tree.children()) {
        setUnknownLabels(child, defaultLabel);
    }

    tree.label().setValue(defaultLabel.toString());
}

From source file:BuildBinarizedDataset.java

public static void setPredictedLabels(Tree tree) {
    if (tree.isLeaf()) {
        return;//  w  ww .  ja va2 s  .  c om
    }

    for (Tree child : tree.children()) {
        setPredictedLabels(child);
    }

    tree.label().setValue(Integer.toString(RNNCoreAnnotations.getPredictedClass(tree)));
}

From source file:BuildBinarizedDataset.java

public static boolean setSpanLabel(Tree tree, Pair<Integer, Integer> span, String value) {
    if (!(tree.label() instanceof CoreLabel)) {
        throw new AssertionError("Expected CoreLabels");
    }/*  w ww. j  a  va 2  s.co m*/
    CoreLabel label = (CoreLabel) tree.label();
    if (label.get(CoreAnnotations.BeginIndexAnnotation.class).equals(span.first)
            && label.get(CoreAnnotations.EndIndexAnnotation.class).equals(span.second)) {
        label.setValue(value);
        return true;
    }
    if (label.get(CoreAnnotations.BeginIndexAnnotation.class) > span.first
            && label.get(CoreAnnotations.EndIndexAnnotation.class) < span.second) {
        return false;
    }
    for (Tree child : tree.children()) {
        if (setSpanLabel(child, span, value)) {
            return true;
        }
    }
    return false;
}

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);// w  w w .  j a  v a  2  s. 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

public static ArrayList<Tree> findPronouns(Tree t) {
    ArrayList<Tree> pronouns = new ArrayList<Tree>();
    if (t.label().value().equals("PRP") && !t.children()[0].label().value().equals("I")
            && !t.children()[0].label().value().equals("you")
            && !t.children()[0].label().value().equals("You")) {
        pronouns.add(t);//from  w  w  w .j a  va2 s  . c  o  m
    } else
        for (Tree child : t.children())
            pronouns.addAll(findPronouns(child));
    return pronouns;
}

From source file:cc.vidr.parseviz.ParseViz.java

License:Open Source License

public static void printTreeDot(Tree tree, StringBuilder sb, Tree root) {
    sb.append("n").append(tree.nodeNumber(root)).append("[label=\"").append(tree.label()).append("\"];\n");
    for (Tree child : tree.children()) {
        sb.append("n").append(tree.nodeNumber(root)).append("--n").append(child.nodeNumber(root)).append(";\n");
        printTreeDot(child, sb, root);//from  w  w  w. jav  a  2s  .co  m
    }
}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to get the negative characters from the sentence
 *
 * @return arrayList of negative words in a sentence which are denoted by RB
 * and CC//from ww  w  .j  a v  a2s .c o m
 */
public ArrayList NegativeSentenceDetection() {
    String phraseNotation = "RB|CC";//@" + phrase + "! << @" + phrase;
    TregexPattern VBpattern = TregexPattern.compile(phraseNotation);
    TregexMatcher matcher = VBpattern.matcher(sTree);
    ArrayList negativeLists = new ArrayList();
    while (matcher.findNextMatchingNode()) {
        Tree match = matcher.getMatch();
        Tree[] innerChild = match.children();
        for (Tree inChild : innerChild) {
            negativeLists.add(inChild.getLeaves().get(0).yieldWords().get(0).word());
        }
    }
    return negativeLists;
}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to extract the classes from a sentence
 *
 * @return ArrayList: arrayList of classes from a sentence
 *///from   w w  w.  j a v  a  2 s  .  co  m
public ArrayList getClassList() {
    nounList = new ArrayList();
    attributeLists = new ArrayList();
    int adjectiveExist = 0;
    int adjectiveNoun = 0;
    String adj = "";
    String storingClass = "";
    HashSet classWithAttr = new HashSet();
    storingClassWithAttr = new HashMap<String, HashSet>();

    List<Tree> leaves;
    String phraseNotation = "(NP([<NNS|NN|NNP]$VP))";//@" + phrase + "! << @" + phrase;

    /*For the single Tree */
    TregexPattern VBpattern = TregexPattern.compile(phraseNotation);
    TregexMatcher matcher = VBpattern.matcher(sTree);
    String tempClass = "";

    while (matcher.findNextMatchingNode()) {
        Tree match = matcher.getMatch();
        Tree[] innerChild = match.children();
        adjectiveExist = 0;
        adjectiveNoun = 0;
        int separator = 0;

        if (innerChild.length > 1) {
            int count = 1;
            int loopCount = 1;
            for (Tree inChild : innerChild) {
                if (inChild.value().equals("CC")) {
                    separator = 1;
                }
                if ((inChild.value().equals("JJ")) || (inChild.value().equals("VBG"))) {
                    adjectiveExist++;
                    leaves = inChild.getLeaves();
                    adj = leaves.get(0).yieldWords().get(0).word();
                    if (dictionaryForClassList.contains(adj)) {
                        adj = "";
                    }
                }
                //if adjective exist store the classes and attributes separately
                if (adjectiveExist == 1) {
                    storeClassesAndAttributesWhenAdjectiveExistToIdentifyClasses(inChild, adjectiveNoun, adj);
                } else {
                    //storeClassesAndAttributesWhenAdjectiveNotExistToIdentifyClasses(inChild, loopCount, innerChild, separator, tempClass, count);
                    if ((inChild.value().equals("NN"))
                            || (inChild.value().equals("NNS") || (inChild.value().equals("NNP")))) {
                        leaves = inChild.getLeaves(); //leaves correspond to the tokens
                        if (separator == 0) {
                            if (loopCount == innerChild.length) {
                                String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                                String word = "";
                                word = stemmingForAWord(identifiedWord);
                                if (!dictionaryForClassList.contains(word)) {
                                    nounList.remove(tempClass);
                                    nounList.add(word);
                                    attributeLists.add(tempClass);

                                }

                            } else if (count == 1) {
                                String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                                /*if the identified word is having underscore skips the stemming part . ex: user_id*/
                                String word = stemmingForAWord(identifiedWord);
                                nounList.add(word);
                                tempClass = word;
                                storingClass = word;

                            } else {
                                /*if the identified word is having underscore skips the stemming part . ex: user_id*/
                                if (tempClass.contains("_")) {
                                    nounList.remove(tempClass);
                                } else {
                                    nounList.remove(morphology.stem(tempClass));
                                    nounList.remove(tempClass);
                                }
                                String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());

                                tempClass += " " + identifiedWord;
                                nounList.add(tempClass);
                                storingClass = tempClass;
                            }

                            count++;
                        } else {
                            String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                            /*if the identified word is having underscore skips the stemming part . ex: user_id*/
                            String word = stemmingForAWord(identifiedWord);
                            nounList.add(word);
                            tempClass = word;
                            storingClass = word;
                        }
                    }

                }
                loopCount++;
            }
        } else {
            for (Tree inChild : innerChild) {
                if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS"))
                        || (inChild.value().equals("NNP"))) {
                    leaves = inChild.getLeaves(); //leaves correspond to the tokens
                    String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                    if (!identifiedWord.contains("_")) {
                        nounList.add(morphology.stem(identifiedWord));
                    } else {
                        nounList.add(identifiedWord);
                    }
                }
                if (inChild.value().equals("JJ")) {
                    //leaves correspond to the tokens
                    leaves = inChild.getLeaves();
                    nounList.add(((leaves.get(0).yieldWords()).get(0).word()));
                }
            }
        }
    }
    System.out.println("NOUN LIST :" + nounList);
    return nounList;
}

From source file:com.project.NLP.Requirement.PhrasesIdentification.java

/**
 * method to identify the attributes using the tokenization
 *
 * @return ArrayList: arrayList of attributes
 *///from  w  w  w.  j av  a 2  s. co m
public ArrayList getAttributeList() {
    nounList = new ArrayList();
    attributeLists = new ArrayList();
    ArrayList adjAtt = new ArrayList();
    int separator = 0;
    List<Tree> leaves;
    String phraseNotation = "NP([<NNS|NN|NNP]![<JJ|VBG])!$VP";// !<VBG";//@" + phrase + "! << @" + phrase;

    TregexPattern VBpattern = TregexPattern.compile(phraseNotation);
    TregexMatcher matcher = VBpattern.matcher(sTree);

    while (matcher.findNextMatchingNode()) {
        Tree match = matcher.getMatch();
        Tree[] innerChild = match.children();
        int adjectiveExist = 0;
        String adj = "";
        String attribute = "";
        String b = "";

        if (innerChild.length > 1) {
            int count = 1;

            for (Tree inChild : innerChild) {
                if (inChild.value().equals("CC") || inChild.value().equals(",")) {
                    separator = 1;
                }
                if ((inChild.value().equals("JJ")) || (inChild.value().equals("VBG"))) {
                    adjectiveExist++;
                    leaves = inChild.getLeaves();
                    adj = leaves.get(0).toString();
                    if (designEleList.contains(adj)) {
                        adj = "";
                    }
                }
                if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS"))
                        || (inChild.value().equals("NNP"))) {
                    leaves = inChild.getLeaves(); //leaves correspond to the tokens
                    if (count == 1) {
                        if (adjectiveExist == 1) {
                            attribute = adj + " " + leaves.get(0).yieldWords().get(0).word();
                        } else {
                            attribute = leaves.get(0).yieldWords().get(0).word();
                        }
                        if (!designEleList.contains(attribute)) {
                            String identifiedWord = attribute;
                            if (!identifiedWord.contains("_")) {
                                attributeLists.add(morphology.stem(identifiedWord));
                            } else {
                                attributeLists.add(identifiedWord);
                            }
                        }

                    } else if (count >= 2 && separator == 0) {
                        if (!attribute.contains("_")) {

                            attributeLists.remove(morphology.stem(attribute));
                            attributeLists.remove(attribute);
                        } else {
                            attributeLists.remove(attribute);
                        }

                        attribute += " " + (leaves.get(0).yieldWords()).get(0).word();
                        attributeLists.add(attribute);
                    } else if (count >= 2 && separator == 1) {
                        attribute = (leaves.get(0).yieldWords()).get(0).word();
                        if (!attribute.contains("_")) {
                            attributeLists.add(morphology.stem(attribute));
                        } else {
                            attributeLists.add(attribute);
                        }
                        separator = 0;
                    }
                    count++;
                }
            }
        } else {
            for (Tree inChild : innerChild) {
                if ((inChild.value().equals("NN")) || (inChild.value().equals("NNS"))) {
                    leaves = inChild.getLeaves(); //leaves correspond to the tokens
                    String identifiedWord = ((leaves.get(0).yieldWords()).get(0).word());
                    if (!identifiedWord.contains("_")) {
                        attributeLists.add(morphology.stem(identifiedWord));
                    } else {
                        attributeLists.add(identifiedWord);
                    }
                }
            }
        }
    }
    adjAtt = getAdjectiveAttribute();
    if (!adjAtt.isEmpty()) {
        String att = "";
        for (int i = 0; i < adjAtt.size(); i++) {
            att = adjAtt.get(i).toString();
            if (!att.isEmpty() || !att.equals("") || !(att.equals(" "))) {
                attributeLists.add(att.trim());
            }
        }
    }

    System.out.println("ATTRIBUTE LIST :" + attributeLists);
    return attributeLists;

}