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

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

Introduction

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

Prototype

public boolean isLeaf() 

Source Link

Document

Says whether a node is a leaf.

Usage

From source file:opennlp.tools.parse_thicket.kernel_interface.TreeExtenderByAnotherLinkedTree.java

License:Apache License

public StringBuilder toStringBuilderExtenderByAnotherLinkedTree(StringBuilder sb, Tree t, Tree treeToInsert) {
    if (t.isLeaf()) {
        if (t.label() != null) {
            sb.append(t.label().value());
        }/*from  w  w  w.  j a v  a 2 s.  c o m*/
        return sb;
    } else {
        sb.append('(');
        if (t.label() != null) {
            if (t.value() != null) {
                sb.append(t.label().value());
            }
        }

        boolean bInsertNow = false;
        // we try match trees to find out if we are at the insertion
        // position
        if (treeToInsert != null) {
            List<ParseTreeNode> bigTreeNodes = parsePhrase(t.label().value());
            List<ParseTreeNode> smallTreeNodes = parsePhrase(
                    treeToInsert.getChild(0).getChild(0).getChild(0).label().value());

            System.out.println(t + " \n " + treeToInsert + "\n");

            if (smallTreeNodes.size() > 0 && bigTreeNodes.size() > 0)
                for (ParseTreeNode smallNode : smallTreeNodes) {
                    if (!bigTreeNodes.get(0).getWord().equals("")
                            && bigTreeNodes.get(0).getWord().equalsIgnoreCase(smallNode.getWord()))
                        bInsertNow = true;
                }
        }

        if (bInsertNow) {
            Tree[] kids = t.children();
            if (kids != null) {
                for (Tree kid : kids) {
                    sb.append(' ');
                    toStringBuilderExtenderByAnotherLinkedTree(sb, kid, null);
                }
                sb.append(' ');
                toStringBuilderExtenderByAnotherLinkedTree(sb, treeToInsert.getChild(0).getChild(1), null);
                int z = 0;
                z++;
            }
        } else {
            Tree[] kids = t.children();
            if (kids != null) {
                for (Tree kid : kids) {
                    sb.append(' ');
                    toStringBuilderExtenderByAnotherLinkedTree(sb, kid, treeToInsert);
                }

            }
        }
        return sb.append(')');
    }
}

From source file:opennlp.tools.parse_thicket.kernel_interface.TreeExtenderByAnotherLinkedTree.java

License:Apache License

public StringBuilder toStringBuilder(StringBuilder sb, Tree t) {
    if (t.isLeaf()) {
        if (t.label() != null) {
            sb.append(t.label().value());
        }/*  w w w. j  a  v  a 2s.c  o m*/
        return sb;
    } else {
        sb.append('(');
        if (t.label() != null) {
            if (t.value() != null) {
                sb.append(t.label().value());
            }
        }
        Tree[] kids = t.children();
        if (kids != null) {
            for (Tree kid : kids) {
                sb.append(' ');
                toStringBuilder(sb, kid);
            }
        }
        return sb.append(')');
    }
}

From source file:opennlp.tools.parse_thicket.matching.PT2ThicketPhraseBuilder.java

License:Apache License

private StringBuilder toStringBuilder(StringBuilder sb, Tree t) {
    if (t.isLeaf()) {
        if (t.label() != null) {
            sb.append(t.label().value());
        }//from w  w  w . ja  v  a2  s .c  om
        return sb;
    } else {
        sb.append('(');
        if (t.label() != null) {
            if (t.value() != null) {
                sb.append(t.label().value());
            }
        }
        Tree[] kids = t.children();
        if (kids != null) {
            for (Tree kid : kids) {
                sb.append(' ');
                toStringBuilder(sb, kid);
            }
        }
        return sb.append(')');
    }
}

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Sets the labels on the tree (except the leaves) to be the integer value
 * of the sentiment prediction. Makes it easy to print out with
 * Tree.toString()//from   ww w. ja  v a 2  s  .c o  m
 */
static void setSentimentLabels(Tree tree) {
    if (tree.isLeaf()) {
        return;
    }

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

    Label label = tree.label();
    if (!(label instanceof CoreLabel)) {
        throw new IllegalArgumentException("Required a tree with CoreLabels");
    }
    CoreLabel cl = (CoreLabel) label;
    cl.setValue(Integer.toString(RNNCoreAnnotations.getPredictedClass(tree)));
}

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Sets the labels on the tree to be the indices of the nodes. Starts
 * counting at the root and does a postorder traversal.
 *///  w  w w .java2s  . c  o  m
static int setIndexLabels(Tree tree, int index) {
    if (tree.isLeaf()) {
        return index;
    }

    tree.label().setValue(Integer.toString(index));
    index++;
    for (Tree child : tree.children()) {
        index = setIndexLabels(child, index);
    }
    return index;
}

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Outputs the vectors from the tree. Counts the tree nodes the same as
 * setIndexLabels.//  w ww  .  j  a v a2 s.c  o m
 */
static int outputTreeVectors(PrintStream out, Tree tree, int index) {
    if (tree.isLeaf()) {
        return index;
    }

    out.print("  " + index + ":");
    SimpleMatrix vector = RNNCoreAnnotations.getNodeVector(tree);
    for (int i = 0; i < vector.getNumElements(); ++i) {
        out.print("  " + NF.format(vector.get(i)));
    }
    out.println();
    index++;
    for (Tree child : tree.children()) {
        index = outputTreeVectors(out, child, index);
    }
    return index;
}

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Outputs the scores from the tree. Counts the tree nodes the same as
 * setIndexLabels./* www.j  a v  a  2 s.co  m*/
 */
static int outputTreeScores(PrintStream out, Tree tree, int index) {
    if (tree.isLeaf()) {
        return index;
    }

    out.print("  " + index + ":");
    SimpleMatrix vector = RNNCoreAnnotations.getPredictions(tree);
    for (int i = 0; i < vector.getNumElements(); ++i) {
        out.print("  " + NF.format(vector.get(i)));
    }
    out.println();
    index++;
    for (Tree child : tree.children()) {
        index = outputTreeScores(out, child, index);
    }
    return index;
}

From source file:opennlp.tools.parse_thicket.parse_thicket2graph.GraphFromPTreeBuilder.java

License:Apache License

private void navigate(Tree tree, Graph<ParseGraphNode, DefaultEdge> g, int l, ParseGraphNode currParent) {
    // String currParent = tree.label().value()+" $"+Integer.toString(l);
    // g.addVertex(currParent);
    if (tree.getChildrenAsList().size() == 1)
        navigate(tree.getChildrenAsList().get(0), g, l + 1, currParent);
    else if (tree.getChildrenAsList().size() == 0)
        return;/*from   w ww. j a va 2  s  . com*/

    for (Tree child : tree.getChildrenAsList()) {
        String currChild = null;
        ParseGraphNode currChildNode = null;
        try {
            if (child.isLeaf())
                continue;
            if (child.label().value().startsWith("S"))
                navigate(child.getChildrenAsList().get(0), g, l + 1, currParent);

            if (!child.isPhrasal() || child.isPreTerminal())
                currChild = child.toString() + " #" + Integer.toString(l);
            else
                currChild = child.label().value() + " #" + Integer.toString(l);
            currChildNode = new ParseGraphNode(child, currChild);
            g.addVertex(currChildNode);
            g.addEdge(currParent, currChildNode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        navigate(child, g, l + 1, currChildNode);
    }
}

From source file:org.lambda3.text.simplification.discourse.utils.parseTree.ParseTreeExtractionUtils.java

License:Open Source License

public static Tree getFirstLeaf(Tree tree) {
    if (tree.isLeaf()) {
        return tree;
    } else {//from www. ja v  a 2s .  c  om
        return getFirstLeaf(tree.firstChild());
    }
}

From source file:org.lambda3.text.simplification.discourse.utils.parseTree.ParseTreeExtractionUtils.java

License:Open Source License

public static Tree getLastLeaf(Tree tree) {
    if (tree.isLeaf()) {
        return tree;
    } else {/*ww  w .j  a  v  a  2s  .co  m*/
        return getLastLeaf(tree.lastChild());
    }
}