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

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

Introduction

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

Prototype

public List<Tree> preOrderNodeList() 

Source Link

Usage

From source file:knu.univ.lingvo.coref.MentionExtractor.java

License:Open Source License

/**
 * Post-processes the extracted mentions. Here we set the Mention fields required for coref and order mentions by tree-traversal order.
 * @param words List of words in each sentence, in textual order
 * @param trees List of trees, one per sentence
 * @param unorderedMentions List of unordered, unprocessed mentions
 *                 Each mention MUST have startIndex and endIndex set!
 *                 Optionally, if scoring is desired, mentions must have mentionID and originalRef set.
 *                 All the other Mention fields are set here.
 * @return List of mentions ordered according to the tree traversal
 * @throws Exception/*from   w  ww  .j a  v  a 2s . c  o m*/
 */
public List<List<Mention>> arrange(Annotation anno, List<List<CoreLabel>> words, List<Tree> trees,
        List<List<Mention>> unorderedMentions, boolean doMergeLabels) throws Exception {

    List<List<Mention>> orderedMentionsBySentence = new ArrayList<List<Mention>>();

    //
    // traverse all sentences and process each individual one
    //
    int mentionNumber = 0;
    for (int sent = 0, sz = words.size(); sent < sz; sent++) {
        List<CoreLabel> sentence = words.get(sent);
        Tree tree = trees.get(sent);
        List<Mention> mentions = unorderedMentions.get(sent);
        Map<String, List<Mention>> mentionsToTrees = Generics.newHashMap();

        // merge the parse tree of the entire sentence with the sentence words
        if (doMergeLabels)
            mergeLabels(tree, sentence);

        //
        // set the surface information and the syntactic info in each mention
        // startIndex and endIndex MUST be set before!
        //
        for (Mention mention : mentions) {
            mention.sentenceNumber = sent;
            mention.mentionNumber = mentionNumber++;
            mention.contextParseTree = tree;
            mention.sentenceWords = sentence;
            mention.originalSpan = new ArrayList<CoreLabel>(
                    mention.sentenceWords.subList(mention.startIndex, mention.endIndex));
            if (!((CoreLabel) tree.label()).has(CoreAnnotations.BeginIndexAnnotation.class))
                tree.indexSpans(0);
            if (mention.headWord == null) {
                Tree headTree = ((RuleBasedCorefMentionFinder) mentionFinder).findSyntacticHead(mention, tree,
                        sentence);
                mention.headWord = (CoreLabel) headTree.label();
                mention.headIndex = mention.headWord.get(CoreAnnotations.IndexAnnotation.class) - 1;
            }
            if (mention.mentionSubTree == null) {
                // mentionSubTree = highest NP that has the same head
                Tree headTree = tree.getLeaves().get(mention.headIndex);
                if (headTree == null) {
                    throw new RuntimeException("Missing head tree for a mention!");
                }
                Tree t = headTree;
                while ((t = t.parent(tree)) != null) {
                    if (t.headTerminal(headFinder) == headTree && t.value().equals("NP")) {
                        mention.mentionSubTree = t;
                    } else if (mention.mentionSubTree != null) {
                        break;
                    }
                }
                if (mention.mentionSubTree == null) {
                    mention.mentionSubTree = headTree;
                }
            }

            List<Mention> mentionsForTree = mentionsToTrees.get(treeToKey(mention.mentionSubTree));
            if (mentionsForTree == null) {
                mentionsForTree = new ArrayList<Mention>();
                mentionsToTrees.put(treeToKey(mention.mentionSubTree), mentionsForTree);
            }
            mentionsForTree.add(mention);

            // generates all fields required for coref, such as gender, number, etc.
            mention.process(dictionaries, semantics, this, singletonPredictor);
        }

        //
        // Order all mentions in tree-traversal order
        //
        List<Mention> orderedMentions = new ArrayList<Mention>();
        orderedMentionsBySentence.add(orderedMentions);

        // extract all mentions in tree traversal order (alternative: tree.postOrderNodeList())
        for (Tree t : tree.preOrderNodeList()) {
            List<Mention> lm = mentionsToTrees.get(treeToKey(t));
            if (lm != null) {
                for (Mention m : lm) {
                    orderedMentions.add(m);
                }
            }
        }

        //
        // find appositions, predicate nominatives, relative pronouns in this sentence
        //
        findSyntacticRelations(tree, orderedMentions);
        assert (mentions.size() == orderedMentions.size());
    }
    return orderedMentionsBySentence;
}

From source file:wtute.parser.EssayParser.java

public void commaSplice(List<HasWord> sentence) {
    System.out.println("SPLICEr");
    Tree sentenceTree = lp.parse(sentence);
    Iterator<Tree> iter = sentenceTree.iterator();
    TregexPattern pattern = TregexPattern
            .compile("@S < (@S $+ (/,/ $+ (@NP $+ @VP))) | < (@NP $+ (@VP $+ (/,/ $+ @VP)))");
    TregexPattern patternTwo = TregexPattern.compile("@VP < (@VP $+ (/,/ $+ @VP))");
    TregexPattern patternThree = TregexPattern.compile("@S < (@S $+ (/,/ $+ @S))");
    TregexMatcher matcher = pattern.matcher(sentenceTree);
    TregexMatcher matcherTwo = patternTwo.matcher(sentenceTree);
    TregexMatcher matcherThree = patternThree.matcher(sentenceTree);
    boolean oneFound, twoFound = false, threeFound = false;
    while ((oneFound = matcher.findNextMatchingNode()) || (twoFound = matcherTwo.findNextMatchingNode())
            || (threeFound = matcherThree.findNextMatchingNode())) {
        Tree match;
        if (oneFound) {
            System.out.println("1");
            match = matcher.getMatch();//from w  w w  .  j  a  va2s  .  c  o m
        } else if (twoFound) {
            System.out.println("2");
            match = matcherTwo.getMatch();
        } else {
            System.out.println("3");
            match = matcherThree.getMatch();
        }

        System.out.println(Sentence.listToString(match.yield()));
        List<Tree> tl = match.preOrderNodeList();

        String errorSect = tl.get(tl.indexOf(Tree.valueOf("(, ,)")) - 1) + ", "
                + Sentence.listToString(tl.get(tl.indexOf(Tree.valueOf("(, ,)")) + 2).yieldWords());
        System.out.println(errorSect);
        xmlc.addError(errorSect, "Comma splice: You have used a comma to separate two individual sentences",
                null, new String[] { "or", "and", "but", "so", " ; ", " . " }, "grammar", "Consider using a "
                        + "conjunction after the comma, a semi-colon or a full-stop to break the sentence.");

    }
}