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

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

Introduction

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

Prototype

public Tree headPreTerminal(HeadFinder hf) 

Source Link

Document

Returns the preterminal tree that is the head of the tree.

Usage

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

License:Open Source License

/**
 * e.g., John and Mary like Bill.  -> John LIKES Bill.  Mary LIKES Bill.
 * John and I like Bill -> John LIKES Bill.  I LIKE Bill.
 * John and I are old. -> I IS old. John IS old.
 *//*from w ww .ja v  a2  s .  c  o m*/
private void correctTense(Tree subject, Tree clause) {
    int tmpIndex;
    //correct verb tense when modifying subjects
    for (Tree uncle : clause.getChildrenAsList()) {
        String newVerbPOS = null;
        Tree verbPreterminal = null;
        boolean needToModifyVerb = false;
        //if the node is a subject (i.e., its uncle is a VP), then check
        //to see if its tense needs to be changed
        String headPOS = subject.headPreTerminal(this.hf).label().toString();
        if (uncle.label().toString().equals("VP") && !headPOS.endsWith("S")) {
            verbPreterminal = uncle.headPreTerminal(this.hf);
            //original main verb was plural but the conjoined subject word is singular
            //e.g., John (and Mary) like Bill.  -> John like Bill.
            if ((verbPreterminal.label().toString().equals("VB")
                    || verbPreterminal.label().toString().equals("VBP"))) { //the parser confuses VBP with VB
                if (subject.yield().toString().equals("I") || subject.yield().toString().equals("you")) {
                    newVerbPOS = "VBP";
                } else {
                    newVerbPOS = "VBZ";
                }
                needToModifyVerb = true;
            } else if (verbPreterminal.label().toString().equals("VBD")) {
                newVerbPOS = "VBD";
                needToModifyVerb = true;
            }
        }
        //if needed, change the tense of the verb
        if (needToModifyVerb) {
            String verbLemma = QuestionUtil.getLemma(verbPreterminal.getChild(0).label().toString(),
                    verbPreterminal.label().toString());
            String newVerb;
            //special cases
            if (verbLemma.equals("be") && newVerbPOS.equals("VBD")) {
                if (subject.label().toString().endsWith("S"))
                    newVerb = "were";
                else
                    newVerb = "was";
            } else if (verbLemma.equals("be") && subject.yield().toString().equals("I")
                    && newVerbPOS.equals("VBP")) {
                newVerb = "am";
            } else { //default
                newVerb = this.conjugator.getSurfaceForm(verbLemma, newVerbPOS);
            }
            tmpIndex = verbPreterminal.parent(uncle).objectIndexOf(verbPreterminal);
            Tree verbParent = verbPreterminal.parent(uncle);
            verbParent.removeChild(tmpIndex);
            verbParent.addChild(tmpIndex,
                    QuestionUtil.readTreeFromString("(" + newVerbPOS + " " + newVerb + ")"));
        }
    }
}

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

License:Open Source License

protected boolean isPlural(Tree nountree) {
    String headTerminalLabel = nountree.headPreTerminal(this.hf).label().toString();
    return (headTerminalLabel.equals("NNS") || headTerminalLabel.equals("NPS"));
}

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

License:Open Source License

/**
 * e.g., John and Mary like Bill.  -> John LIKES Bill.  Mary LIKES Bill.
 * John and I like Bill -> John LIKES Bill.  I LIKE Bill.
 * John and I are old. -> I IS old. John IS old.
 */// ww  w .j av  a  2  s  .co  m
private void correctTense(Tree subject, Tree clause) {
    int tmpIndex;
    //correct verb tense when modifying subjects
    for (Tree uncle : clause.getChildrenAsList()) {
        String newVerbPOS = null;
        Tree verbPreterminal = null;
        boolean needToModifyVerb = false;
        //if the node is a subject (i.e., its uncle is a VP), then check
        //to see if its tense needs to be changed
        String headPOS = subject.headPreTerminal(AnalysisUtilities.getInstance().getHeadFinder()).label()
                .toString();
        if (uncle.label().toString().equals("VP") && !headPOS.endsWith("S")) {
            verbPreterminal = uncle.headPreTerminal(AnalysisUtilities.getInstance().getHeadFinder());
            //original main verb was plural but the conjoined subject word is singular
            //e.g., John (and Mary) like Bill.  -> John like Bill.
            if ((verbPreterminal.label().toString().equals("VB")
                    || verbPreterminal.label().toString().equals("VBP"))) { //the parser confuses VBP with VB
                if (subject.yield().toString().equals("I") || subject.yield().toString().equals("you")) {
                    newVerbPOS = "VBP";
                } else {
                    newVerbPOS = "VBZ";
                }
                needToModifyVerb = true;
            } else if (verbPreterminal.label().toString().equals("VBD")) {
                newVerbPOS = "VBD";
                needToModifyVerb = true;
            }
        }
        //if needed, change the tense of the verb
        if (needToModifyVerb) {
            String verbLemma = AnalysisUtilities.getInstance().getLemma(
                    verbPreterminal.getChild(0).label().toString(), verbPreterminal.label().toString());
            String newVerb;
            //special cases
            if (verbLemma.equals("be") && newVerbPOS.equals("VBD")) {
                if (subject.label().toString().endsWith("S"))
                    newVerb = "were";
                else
                    newVerb = "was";
            } else if (verbLemma.equals("be") && subject.yield().toString().equals("I")
                    && newVerbPOS.equals("VBP")) {
                newVerb = "am";
            } else { //default
                newVerb = AnalysisUtilities.getInstance().getSurfaceForm(verbLemma, newVerbPOS);
            }
            tmpIndex = verbPreterminal.parent(uncle).indexOf(verbPreterminal);
            Tree verbParent = verbPreterminal.parent(uncle);
            verbParent.removeChild(tmpIndex);
            verbParent.addChild(tmpIndex,
                    AnalysisUtilities.getInstance().readTreeFromString("(" + newVerbPOS + " " + newVerb + ")"));
        }
    }
}

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

License:Open Source License

protected boolean isPlural(Tree nountree) {
    String headTerminalLabel = nountree.headPreTerminal(AnalysisUtilities.getInstance().getHeadFinder()).label()
            .toString();/*from  ww  w  .j a  v  a 2s.  c o  m*/
    return (headTerminalLabel.equals("NNS") || headTerminalLabel.equals("NPS"));
}

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

License:Apache License

public String computePredicationType(Mention np) {
    String predType = null;/*from   w  w w .j av a2  s.c om*/
    Tree mentionTree = np.getHighestProjection();
    Tree sentenceTree = np.getSentenceTree();
    Tree parentNode = null;
    if (mentionTree == null && ConfigProperties.getInstance().getDbgPrint()) {
        System.out.println("No mentionTree for " + np.toString());
    }
    if (mentionTree != null)
        parentNode = mentionTree.ancestor(1, sentenceTree);
    if (!(parentNode == null) && parentNode.children().length > 1
            && parentNode.children()[1].label().toString().equals("VP")
            && parentNode.children()[1].children().length > 1) {
        String hword10 = parentNode.children()[1].children()[0].headTerminal(new ModCollinsHeadFinder())
                .toString();
        if (hword10.equals("is") || hword10.equals("are") || hword10.equals("was") || hword10.equals("were")) {
            Tree pchild11 = parentNode.children()[1].children()[1];
            if (pchild11 != null) {// &&
                if (pchild11.label().toString().equals("NP")) {
                    String pchild11_headpos = pchild11.headPreTerminal(new ModCollinsHeadFinder()).label()
                            .toString();
                    if (!pchild11_headpos.equals("JJS") && !pchild11_headpos.equals("NNP")) {
                        predType = pchild11.headTerminal(new ModCollinsHeadFinder()).toString();
                    }
                }
            }
        }
    }
    return predType;
}

From source file:elkfed.expletives.EF_Tree.java

License:Apache License

public static Tree tree_pred(Tree node) {
    LabelFactory lf = new StringLabelFactory();
    Tree result = new LabeledScoredTreeNode();
    result.setLabel(lf.newLabel(node.value() + "-PRD"));
    if (node.value().equals("PP") && node.children().length == 2 && node.children()[0].value().equals("IN")) {
        Tree[] dtrs = new Tree[2];
        dtrs[0] = node.children()[0];/*from   www. j  av a  2  s . c o  m*/
        dtrs[1] = node.children()[1].headPreTerminal(new ModCollinsHeadFinder());
        result.setChildren(dtrs);
    }
    Tree[] dtrs = new Tree[1];
    dtrs[0] = node.headPreTerminal(new ModCollinsHeadFinder());
    result.setChildren(dtrs);
    return result;
}