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

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

Introduction

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

Prototype

public void addChild(Tree t) 

Source Link

Document

Adds the tree t at the last index position among the daughters.

Usage

From source file:coreferenceresolver.util.Util.java

public static void assignNounPhrases(List<NounPhrase> nounPhrases, List<Review> reviews) {
    CollinsHeadFinder headFinder = new CollinsHeadFinder();
    for (NounPhrase np : nounPhrases) {
        Review review = reviews.get(np.getReviewId());
        Sentence sentence = review.getSentences().get(np.getSentenceId());
        String npContent = "";
        for (CRFToken token : np.getCRFTokens()) {
            npContent += token.getWord() + " ";
        }//from w w w  .  ja  v a2s .com

        //Initiate a NP Tree
        Tree npNode = initNPTree();
        for (CRFToken cRFToken : np.getCRFTokens()) {
            Tree cRFTokenTree = sentence.getTokens().get(cRFToken.getIdInSentence()).getTokenTree();
            npNode.addChild(cRFTokenTree);
        }
        np.setNpNode(npNode);
        np.setHeadNode(npNode.headTerminal(headFinder));

        int npOffsetBegin = sentence.getTokens().get(np.getCRFTokens().get(0).getIdInSentence())
                .getOffsetBegin();
        np.setOffsetBegin(npOffsetBegin);
        int npOffsetEnd = sentence.getTokens()
                .get(np.getCRFTokens().get(np.getCRFTokens().size() - 1).getIdInSentence()).getOffsetEnd();
        np.setOffsetEnd(npOffsetEnd);

        review.addNounPhrase(np);
        sentence.addNounPhrase(np);
        sentence.setSentimentForNPs();
    }
}

From source file:edu.cmu.ark.nlp.sent.SentenceSimplifier.java

License:Open Source License

private void removeConjoinedSiblingsHelper(Tree copy, int childindex) {
    //if(GlobalProperties.getDebug()) System.err.println("removeConjoinedSiblingsHelper: "+copy.toString());
    Tree child = copy.getNodeNumber(childindex);
    Tree parent = child.parent(copy);// w  w  w  .  j av a 2s .c o  m
    Tree gparent = parent.parent(copy);

    int parentIdx = gparent.objectIndexOf(parent);

    //By an annoying PTB convention, some verb phrase conjunctions 
    //can conjoin two verb preterminals under a VP,
    //rather than only allowing VP nodes to be conjoined.
    //e.g., John walked and played.
    //So, we add an extra VP node in between if necessary
    if (child.label().toString().startsWith("VB")) {
        gparent.removeChild(parentIdx);
        Tree newTree = factory.newTreeNode("VP", new ArrayList<Tree>());
        newTree.addChild(child);
        gparent.addChild(parentIdx, newTree);
    } else {
        gparent.setChild(parentIdx, child);
    }
}

From source file:edu.cmu.ark.nlp.sent.SentenceSimplifier.java

License:Open Source License

/**
 * e.g., As John slept, I studied. ->  John slept.
 * //from w w  w  . ja  va2  s .  c  o m
 */
private void extractSubordinateClauses(Collection<Question> extracted, Question input) {
    Tree subord;
    String tregexOpStr;
    TregexPattern matchPattern;
    TregexMatcher matcher;

    tregexOpStr = " SBAR [ > VP < IN | > S|SINV ]  " + //not a complement
            " !< (IN < if|unless|that)" + //not a conditional antecedent
            " < (S=sub !< (VP < VBG)) " + //+ //not a participial phrase
            " >S|SINV|VP "; //not part of a noun phrase or PP (other methods for those)

    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(input.getIntermediateTree());
    while (matcher.find()) {
        Tree newTree = factory.newTreeNode("ROOT", new ArrayList<Tree>());
        subord = matcher.getNode("sub");
        newTree.addChild(subord.deepCopy());

        QuestionUtil.addPeriodIfNeeded(newTree);
        addQuotationMarksIfNeeded(newTree);
        Question newTreeWithFeatures = input.deeperCopy();
        newTreeWithFeatures.setIntermediateTree(newTree);
        if (this.getComputeFeatures)
            newTreeWithFeatures.setFeatureValue("extractedFromFiniteClause", 1.0); //old feature name
        if (this.getComputeFeatures)
            newTreeWithFeatures.setFeatureValue("extractedFromSubordinateClause", 1.0);
        //if(GlobalProperties.getDebug()) System.err.println("extractSubordinateClauses: "+newTree.toString());
        addIfNovel(extracted, newTreeWithFeatures);
    }
}

From source file:edu.cmu.ark.nlp.sent.SentenceSimplifier.java

License:Open Source License

private void extractComplementClauses(Collection<Question> extracted, Question input) {
    Tree subord;//from  ww  w .  j  av  a 2  s.  c o m
    String tregexOpStr;
    TregexPattern matchPattern;
    TregexMatcher matcher;

    //TODO should also address infinitive complements
    tregexOpStr = "SBAR " + " < (S=sub !< (VP < VBG)) " + //+ //not a participial phrase
            " !> NP|PP " + //not part of a noun phrase or PP (other methods for those)
            " [ $- /^VB.*/=verb | >+(SBAR) (SBAR $- /^VB.*/=verb) ] "; //complement of a VP (follows the verb)

    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(input.getIntermediateTree());
    while (matcher.find()) {
        Tree newTree = factory.newTreeNode("ROOT", new ArrayList<Tree>());
        subord = matcher.getNode("sub");
        Tree verb = matcher.getNode("verb");
        String verbLemma = QuestionUtil.getLemma(verb.yield().toString(), verb.label().toString());

        if (!verbImpliesComplement(verbLemma)) {
            continue;
        }
        newTree.addChild(subord.deepCopy());

        QuestionUtil.addPeriodIfNeeded(newTree);
        addQuotationMarksIfNeeded(newTree);
        Question newTreeWithFeatures = input.deeperCopy();
        newTreeWithFeatures.setIntermediateTree(newTree);
        if (this.getComputeFeatures)
            newTreeWithFeatures.setFeatureValue("extractedFromFiniteClause", 1.0); //old feature name
        if (this.getComputeFeatures)
            newTreeWithFeatures.setFeatureValue("extractedFromComplementClause", 1.0);
        //if(GlobalProperties.getDebug()) System.err.println("extractComplementClauses: "+newTree.toString());
        addIfNovel(extracted, newTreeWithFeatures);
    }
}

From source file:edu.cmu.ark.nlp.sent.SentenceSimplifier.java

License:Open Source License

/**
 * e.g., In January, John wore his winter coat. -> John wore his winter coat in January.
 * //from   w  w  w.  j  a v a  2s.c o  m
 * @param input
 * @return
 */
private void moveLeadingPPsAndQuotes(Question input) {
    String tregexOpStr;
    TregexPattern matchPattern;
    TregexMatcher matcher;
    Tree mainvp = null;
    Tree subj = null;

    tregexOpStr = "ROOT < (S|SINV=mainclause < (NP|SBAR=subj !$++ /,/) < VP=mainvp "
            + " [ < (PP=modifier < NP) " //must be a PP with an NP object
            + "| < (S=modifier < SBAR|NP <<# VB|VBD|VBP|VBZ) ] ) "; //OR: a quote, which is an S clause with a subject and finite main verb
    //the modifiers to move must be immediately followed by commas

    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(input.getIntermediateTree());

    List<Tree> modifiers = new ArrayList<Tree>();
    while (matcher.find()) {
        if (mainvp == null) {
            mainvp = matcher.getNode("mainvp").deepCopy();
            subj = matcher.getNode("subj").deepCopy();
        }
        Tree mainclause = matcher.getNode("mainclause");
        Tree modifier = matcher.getNode("modifier").deepCopy();
        int idx = mainclause.objectIndexOf(modifier);
        if (modifiers.contains(modifier))
            continue; //just in case the tregex expression catches duplicates
        //add commas and quotation marks if they appeared in the original
        if (idx > 0 && mainclause.getChild(idx - 1).label().toString().equals("``")) {
            modifiers.add(QuestionUtil.readTreeFromString("(, ,)"));
            modifiers.add(QuestionUtil.readTreeFromString("(`` ``)"));
            Tree sbar = factory.newTreeNode("SBAR", new ArrayList<Tree>());
            sbar.addChild(modifier);
            modifiers.add(sbar);
            modifiers.add(QuestionUtil.readTreeFromString("('' '')"));
        } else {
            modifiers.add(modifier);
        }
    }

    if (mainvp != null) { //any matches?
        for (Tree m : modifiers) {
            mainvp.addChild(m);
        }

        Tree newTree = factory.newTreeNode("ROOT", new ArrayList<Tree>());
        Tree clause = factory.newTreeNode("S", new ArrayList<Tree>());
        newTree.addChild(clause);
        clause.addChild(subj);
        clause.addChild(mainvp);

        QuestionUtil.addPeriodIfNeeded(newTree);
        addQuotationMarksIfNeeded(newTree);
        //if(GlobalProperties.getDebug()) System.err.println("moveLeadingModifiers: "+ newTree.toString());
        input.setIntermediateTree(newTree);
        if (this.getComputeFeatures)
            input.setFeatureValue("movedLeadingPPs", 1.0);
    }

}

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

License:Open Source License

private void removeConjoinedSiblingsHelper(Tree copy, int childindex) {
    if (GlobalProperties.getDebug())
        System.err.println("removeConjoinedSiblingsHelper: " + copy.toString());
    Tree child = copy.getNodeNumber(childindex);
    Tree parent = child.parent(copy);//from   w  ww. j  a  va 2 s . co m
    Tree gparent = parent.parent(copy);

    int parentIdx = gparent.indexOf(parent);

    //By an annoying PTB convention, some verb phrase conjunctions 
    //can conjoin two verb preterminals under a VP,
    //rather than only allowing VP nodes to be conjoined.
    //e.g., John walked and played.
    //So, we add an extra VP node in between if necessary
    if (child.label().toString().startsWith("VB")) {
        gparent.removeChild(parentIdx);
        Tree newTree = factory.newTreeNode("VP", new ArrayList<Tree>());
        newTree.addChild(child);
        gparent.addChild(parentIdx, newTree);
    } else {
        gparent.setChild(parentIdx, child);
    }
}

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

License:Open Source License

/**
 * e.g., As John slept, I studied. ->  John slept.
 * /*w  ww  .  ja v a  2 s.  c  o  m*/
 */
private void extractSubordinateClauses(Collection<Question> extracted, Question input) {
    Tree subord;
    String tregexOpStr;
    TregexPattern matchPattern;
    TregexMatcher matcher;

    tregexOpStr = " SBAR [ > VP < IN | > S|SINV ]  " + //not a complement
            " !< (IN < if|unless|that)" + //not a conditional antecedent
            " < (S=sub !< (VP < VBG)) " + //+ //not a participial phrase
            " >S|SINV|VP "; //not part of a noun phrase or PP (other methods for those)

    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(input.getIntermediateTree());
    while (matcher.find()) {
        Tree newTree = factory.newTreeNode("ROOT", new ArrayList<Tree>());
        subord = matcher.getNode("sub");
        newTree.addChild(subord.deeperCopy());

        AnalysisUtilities.addPeriodIfNeeded(newTree);
        addQuotationMarksIfNeeded(newTree);
        Question newTreeWithFeatures = input.deeperCopy();
        newTreeWithFeatures.setIntermediateTree(newTree);
        if (GlobalProperties.getComputeFeatures())
            newTreeWithFeatures.setFeatureValue("extractedFromFiniteClause", 1.0); //old feature name
        if (GlobalProperties.getComputeFeatures())
            newTreeWithFeatures.setFeatureValue("extractedFromSubordinateClause", 1.0);
        if (GlobalProperties.getDebug())
            System.err.println("extractSubordinateClauses: " + newTree.toString());
        addIfNovel(extracted, newTreeWithFeatures);
    }
}

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

License:Open Source License

private void extractComplementClauses(Collection<Question> extracted, Question input) {
    Tree subord;//from  w  w w . j  ava 2  s  .co m
    String tregexOpStr;
    TregexPattern matchPattern;
    TregexMatcher matcher;

    //TODO should also address infinitive complements
    tregexOpStr = "SBAR " + " < (S=sub !< (VP < VBG)) " + //+ //not a participial phrase
            " !> NP|PP " + //not part of a noun phrase or PP (other methods for those)
            " [ $- /^VB.*/=verb | >+(SBAR) (SBAR $- /^VB.*/=verb) ] "; //complement of a VP (follows the verb)

    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(input.getIntermediateTree());
    while (matcher.find()) {
        Tree newTree = factory.newTreeNode("ROOT", new ArrayList<Tree>());
        subord = matcher.getNode("sub");
        Tree verb = matcher.getNode("verb");
        String verbLemma = AnalysisUtilities.getInstance().getLemma(verb.yield().toString(),
                verb.label().toString());

        if (!verbImpliesComplement(verbLemma)) {
            continue;
        }
        newTree.addChild(subord.deeperCopy());

        AnalysisUtilities.addPeriodIfNeeded(newTree);
        addQuotationMarksIfNeeded(newTree);
        Question newTreeWithFeatures = input.deeperCopy();
        newTreeWithFeatures.setIntermediateTree(newTree);
        if (GlobalProperties.getComputeFeatures())
            newTreeWithFeatures.setFeatureValue("extractedFromFiniteClause", 1.0); //old feature name
        if (GlobalProperties.getComputeFeatures())
            newTreeWithFeatures.setFeatureValue("extractedFromComplementClause", 1.0);
        if (GlobalProperties.getDebug())
            System.err.println("extractComplementClauses: " + newTree.toString());
        addIfNovel(extracted, newTreeWithFeatures);
    }
}

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

License:Open Source License

/**
 * e.g., In January, John wore his winter coat. -> John wore his winter coat in January.
 * //from  w  w w  .  j a va2 s  .  c o  m
 * @param input
 * @return
 */
private void moveLeadingPPsAndQuotes(Question input) {
    String tregexOpStr;
    TregexPattern matchPattern;
    TregexMatcher matcher;
    Tree mainvp = null;
    Tree subj = null;

    tregexOpStr = "ROOT < (S|SINV=mainclause < (NP|SBAR=subj !$++ /,/) < VP=mainvp "
            + " [ < (PP=modifier < NP) " //must be a PP with an NP object
            + "| < (S=modifier < SBAR|NP <<# VB|VBD|VBP|VBZ) ] ) "; //OR: a quote, which is an S clause with a subject and finite main verb
    //the modifiers to move must be immediately followed by commas

    matchPattern = TregexPatternFactory.getPattern(tregexOpStr);
    matcher = matchPattern.matcher(input.getIntermediateTree());

    List<Tree> modifiers = new ArrayList<Tree>();
    while (matcher.find()) {
        if (mainvp == null) {
            mainvp = matcher.getNode("mainvp").deeperCopy();
            subj = matcher.getNode("subj").deeperCopy();
        }
        Tree mainclause = matcher.getNode("mainclause");
        Tree modifier = matcher.getNode("modifier").deeperCopy();
        int idx = mainclause.indexOf(modifier);
        if (modifiers.contains(modifier))
            continue; //just in case the tregex expression catches duplicates
        //add commas and quotation marks if they appeared in the original
        if (idx > 0 && mainclause.getChild(idx - 1).label().toString().equals("``")) {
            modifiers.add(AnalysisUtilities.getInstance().readTreeFromString("(, ,)"));
            modifiers.add(AnalysisUtilities.getInstance().readTreeFromString("(`` ``)"));
            Tree sbar = factory.newTreeNode("SBAR", new ArrayList<Tree>());
            sbar.addChild(modifier);
            modifiers.add(sbar);
            modifiers.add(AnalysisUtilities.getInstance().readTreeFromString("('' '')"));
        } else {
            modifiers.add(modifier);
        }
    }

    if (mainvp != null) { //any matches?
        for (Tree m : modifiers) {
            mainvp.addChild(m);
        }

        Tree newTree = factory.newTreeNode("ROOT", new ArrayList<Tree>());
        Tree clause = factory.newTreeNode("S", new ArrayList<Tree>());
        newTree.addChild(clause);
        clause.addChild(subj);
        clause.addChild(mainvp);

        AnalysisUtilities.addPeriodIfNeeded(newTree);
        addQuotationMarksIfNeeded(newTree);
        if (GlobalProperties.getDebug())
            System.err.println("moveLeadingModifiers: " + newTree.toString());
        input.setIntermediateTree(newTree);
        if (GlobalProperties.getComputeFeatures())
            input.setFeatureValue("movedLeadingPPs", 1.0);
    }

}

From source file:edu.nus.comp.nlp.stanford.UtilParser.java

License:Open Source License

private static Tree putOnBranch(TypedDependency dep, Tree tree) {
    /*/*from w  w  w.  ja va 2s .  c  o m*/
     * Each node is a tree with a single child
     */
    Tree mySubtree = lstf.newTreeNode(dep.gov().label(), new LinkedList<Tree>(dep.dep()));
    mySubtree.setValue("[<-" + dep.reln() + "-] " + dep.dep().value());//nudge in the dependency relation information

    if (tree.children().length == 0) {
        if (tree.label().value().toString().equals("DUMMYROOT")) {
            tree.addChild(mySubtree);
            return tree;
        } else {
            //Shouldn't happen
            System.err.println("Forgot to add a child earlier.");
            return null;
        }
    } else {
        //         System.err.println(dep.dep().label() +"\t[on]\t" + tree.label());
        for (Tree child : tree.children()) {
            //if dep is child's parent, insert dep between child and its parent
            if (((CoreLabel) child.label()).index() == dep.dep().label().index()) {
                tree.removeChild(tree.objectIndexOf(child));
                mySubtree.addChild(child);
            }
        }
        if (mySubtree.children().length > 1) {
            tree.addChild(mySubtree);
            return tree;
        }

        for (Tree child : tree.children()) {
            //if dep is Child's sibling, or child
            if (((CoreLabel) child.label()).index() == dep.gov().label().index()) {
                tree.addChild(mySubtree);
                return tree;
            }

            if (child.children().length > 0) {
                if (putOnBranch(dep, child) != null) {
                    return tree;
                }
            }
        }
    }
    //          tree.getLeaves() == null
    //check its childrens, recurisively.
    return null;
}