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

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

Introduction

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

Prototype

@Override
public Iterator<Tree> iterator() 

Source Link

Document

Returns an iterator over all the nodes of the tree.

Usage

From source file:KleinBilingualParser.java

private static HashMap<Tree, Tree> getSampleNodeAlignment(Tree eParseTree, Tree fParseTree) {
    HashMap<Tree, Tree> alignment = new HashMap<>();

    Iterator<Tree> eSubtrees = eParseTree.iterator();
    for (Tree fSubTree : fParseTree) {
        if (!eSubtrees.hasNext()) {
            break;
        } else {/*from  ww w.ja va2 s .  c  o m*/
            Tree eSubTree = eSubtrees.next();
            if (!fSubTree.isLeaf() && !eSubTree.isLeaf()) {
                alignment.put(fSubTree, eSubTree);
            }
        }
    }

    return alignment;
}

From source file:elkfed.mmax.pipeline.P2Chunker.java

License:Apache License

/** Add parser, part of speech, and chunk markables */
protected void addMarkables() {

    final StringBuffer markableBuffer = new StringBuffer();
    List<Markable> sentences = null;

    for (Markable parseMarkable : DiscourseUtils.getMarkables(currentDocument, DEFAULT_PARSE_LEVEL)) {

        int start = parseMarkable.getLeftmostDiscoursePosition();
        int end = parseMarkable.getRightmostDiscoursePosition();

        /** Retrieve chunk tags from the parse tree and add chunk markables */

        /* traverse parse-tree (real tree, not string), extract basic NPs and poss */

        Tree pTree = null;
        pTree = Tree.valueOf(parseMarkable.getAttributeValue(PipelineComponent.TAG_ATTRIBUTE));
        normalizeTree(pTree);/*from   ww w . j a v a  2 s  .  co m*/

        if (pTree == null)
            continue;

        //add all basic nps
        for (Iterator<Tree> treeIt = pTree.iterator(); treeIt.hasNext();) {
            Tree nod = treeIt.next();
            if (nod.value().equals("NP" + NPSTATUS_SEPARATOR + "1")
                    || nod.value().equals("NP" + NPSTATUS_SEPARATOR + "2")) {
                markableBuffer.setLength(0);
                addChunkMarkable(nod, pTree, start, false);
            }
        }

        List<Tree> Leaves = pTree.getLeaves();

        // add NPs embedding possessives
        for (Tree l : Leaves) {
            if (l.value().toLowerCase().startsWith("'s")) {

                if (l.parent(pTree) != null && l.parent(pTree).value().equals("POS")
                        && l.parent(pTree).parent(pTree) != null
                        && l.parent(pTree).parent(pTree).value().startsWith("NP")
                        && l.parent(pTree).parent(pTree).parent(pTree) != null && l.parent(pTree).parent(pTree)
                                .parent(pTree).value().equals("NP" + NPSTATUS_SEPARATOR + "0")) {
                    Tree nod = l.parent(pTree).parent(pTree).parent(pTree);
                    markableBuffer.setLength(0);
                    addChunkMarkable(nod, pTree, start, true);

                }

            }

        }
    }
}

From source file:elkfed.mmax.pipeline.P2Chunker.java

License:Apache License

private void normalizeTree(Tree tree) {
    // for leaves -- add positions
    // for nps -- add whether they are basic or not

    int leaveIndex = 0;
    for (Iterator<Tree> treeIt = tree.iterator(); treeIt.hasNext();) {
        Tree currentTree = treeIt.next();
        Label nodeLabel = currentTree.label();
        if (currentTree.isLeaf()) {
            nodeLabel.setValue(nodeLabel.value() + INDEX_SEPARATOR + leaveIndex);
            leaveIndex++;/*  w  w w.ja va2  s  .co m*/
        } else {

            if (currentTree.value().toLowerCase().startsWith("np")) {

                Boolean found = false;

                //adjust this np for keeping (if not already discarded
                if (!currentTree.value().endsWith("0") && !currentTree.value().endsWith("2"))
                    currentTree.label().setValue("NP" + NPSTATUS_SEPARATOR + "1");

                //adjust upper np for discarding

                Tree p = currentTree;
                Tree head = p.headTerminal(getHeadFinder());
                while (p != null && !found) {
                    p = p.parent(tree);
                    if (p != null && p.value().toLowerCase().startsWith("np")
                            && p.headTerminal(getHeadFinder()) == head && (!iscoordnp(p))) {
                        found = true;
                        p.label().setValue("NP" + NPSTATUS_SEPARATOR + "0");
                        currentTree.label().setValue("NP" + NPSTATUS_SEPARATOR + "2");
                    }
                }

            } else {
                nodeLabel.setValue(nodeLabel.value().toUpperCase());
            }
        }
    }
}

From source file:elkfed.mmax.pipeline.SemTagger.java

License:Apache License

/** Given a tree, uppercases all its non-terminal label (so that the
 *  CollinsHeadFinder can be applied to it) and add a positional index to
 *  the terminal nodes.//from   w w w  .  j  a v a 2  s .co m
 * 
 * @param tree the tree whose labels are to be uppercased
 */
private void normalizeTree(Tree tree) {
    int leaveIndex = 0;
    for (Iterator<Tree> treeIt = tree.iterator(); treeIt.hasNext();) {
        Tree currentTree = treeIt.next();
        Label nodeLabel = currentTree.label();
        if (currentTree.isLeaf()) {
            nodeLabel.setValue(nodeLabel.value() + INDEX_SEPARATOR + leaveIndex);
            leaveIndex++;
        } else {
            nodeLabel.setValue(nodeLabel.value().toUpperCase());
        }
    }
}

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;//from  w ww . j a  v  a 2 s  .c  o m
        if (oneFound) {
            System.out.println("1");
            match = matcher.getMatch();
        } 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.");

    }
}