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

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

Introduction

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

Prototype

@Override
public Label label() 

Source Link

Document

Returns the label associated with the current node, or null if there is no label.

Usage

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

License:Apache License

private void navigateR(Tree t, List<ParseTreeNode> sentence, List<List<ParseTreeNode>> phrases) {
    if (!t.isPreTerminal()) {
        if (t.label() != null) {
            if (t.value() != null) {
                // if ROOT or S, returns empty
                List<ParseTreeNode> nodes = parsePhrase(t.label().value(), t.toString());
                nodes = assignIndexToNodes(nodes, sentence);
                if (!nodes.isEmpty())
                    phrases.add(nodes);//w ww .  j  a v a  2  s.  c om
                if (nodes.size() > 0 && nodes.get(0).getId() == null) {
                    if (nodes.size() > 1 && nodes.get(1) != null && nodes.get(1).getId() != null) {
                        try {
                            ParseTreeNode n = nodes.get(0);
                            n.setId(nodes.get(1).getId() - 1);
                            nodes.set(0, n);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        log.severe("Failed alignment:" + nodes);
                    }
                }
            }
        }
        Tree[] kids = t.children();
        if (kids != null) {
            for (Tree kid : kids) {
                navigateR(kid, sentence, phrases);
            }
        }
        return;
    }
}

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

License:Apache License

private void navigateR1(Tree t, List<ParseTreeNode> sentence, int l, List<List<ParseTreeNode>> phrases) {
    if (t.isPreTerminal()) {
        if (t.label() != null) {
            List<ParseTreeNode> node = parsePhrase(t.toString());
            if (!node.isEmpty())
                phrases.add(node);//from   w  w  w . ja v  a2s . com
        }
        return;
    } else {
        if (t.label() != null) {
            if (t.value() != null) {
                List<ParseTreeNode> node = parsePhrase(t.label().value());
                if (!node.isEmpty())
                    phrases.add(node);
            }
        }
        Tree[] kids = t.children();
        if (kids != null) {
            for (Tree kid : kids) {
                navigateR1(kid, sentence, l, phrases);
            }
        }
        return;
    }
}

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  .  java 2 s . 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.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()/*  w  w  w  . j  av 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  .j av  a2s  .  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.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 w  w. j  ava2  s .  c  o m*/

    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.devboost.stanford.language.LanguageCreator.java

License:Open Source License

private static List<Word> createWords(Tree root, Tree tree) {
    List<Word> words = new ArrayList<Word>();
    Label label = tree.label();
    if (label instanceof CoreLabel) {
        CoreLabel coreLabel = (CoreLabel) label;
        int beginPosition = coreLabel.beginPosition();
        int endPosition = coreLabel.endPosition();
        String originalText = coreLabel.word();
        if (originalText != null) {
            Tree parent = tree.ancestor(1, root);
            Label parentLabel = parent.label();
            if (parentLabel instanceof CoreLabel) {
                String parentCategory = ((CoreLabel) parentLabel).category();
                parentCategory = parentCategory.replaceAll("\\$", "S");
                if (parentCategory != null) {
                    EClassifier classifier = LanguagePackage.eINSTANCE.getEClassifier(parentCategory);
                    if (classifier instanceof EClass) {
                        Word word = (Word) LanguageFactory.eINSTANCE.create((EClass) classifier);
                        word.setText(originalText);
                        word.setBegin(beginPosition);
                        word.setEnd(endPosition);
                        words.add(word);
                    }//from   www  . j a  va 2  s . c  o  m
                }
            }
        }
    }
    List<Tree> children = tree.getChildrenAsList();
    for (Tree child : children) {
        words.addAll(createWords(root, child));
    }
    return words;
}

From source file:org.nlp2rdf.implementation.stanfordcore.StanfordCoreNLPWrapper.java

License:Open Source License

public void processTree(Tree currentNode, URIGenerator uriGenerator, String prefix, String text,
        OntModel model) {/*w  ww.j ava 2s.  c o  m*/
    // String tag = currentNode.label().value();
    //log.info("Current Node :" + currentNode);
    //log.info("Label: " + currentNode.label() + "");
    //log.info("Label Value: " + currentNode.label().value() + "");
    //log.info("Preterminal: " + currentNode.isPreTerminal() + "");
    //log.info("Index: " + ((CoreLabel) currentNode.label()).get(CharacterOffsetBeginAnnotation.class) + "");

    if (currentNode.isLeaf()) {
        //the node is a leaf and belongs in the realm of pos tagging
    } else {
        Phrase p = new Text2RDF().createStringAnnotationForClass(Phrase.class, prefix, text,
                getSpan(currentNode), uriGenerator, model);
        List<Tree> children = currentNode.getChildrenAsList();
        for (Tree child : children) {

            /* if (false && child.isPreTerminal()) {
            //skip preterminals
            log.debug("skipping preterminal: "+currentNode);
            log.debug("label: "+currentNode.label());
            child = child.getChild(0);
            Word childTerminal = new Text2RDF().createStringAnnotationForClass(Word.class, prefix, text, getSpan(child), uriGenerator, model);
            p.addChild(childTerminal);
             */
            Phrase childPhrase = new Text2RDF().createStringAnnotationForClass(Phrase.class, prefix, text,
                    getSpan(child), uriGenerator, model);
            p.addChild(childPhrase);
            processTree(child, uriGenerator, prefix, text, model);

            log.info("Current Node :" + currentNode);
            log.info("Label: " + currentNode.label() + "");
            log.info("Label Value: " + currentNode.label().value() + "");
            log.info("Preterminal: " + currentNode.isPreTerminal() + "");
            //log.info("Index: " + ((CoreLabel) currentNode.label()).get(CharacterOffsetBeginAnnotation.class) + "");

            //adding syntax classes from olia and olia-top
            String tag = ((CoreLabel) currentNode.label()).get(CategoryAnnotation.class);
            Set<String> classes = penn_syntax.getClassURIsForTag(tag);
            for (String classUri : classes) {
                log.info("found: " + classUri + " for: " + tag);
                OntModel hierarchy = penn_syntax.getHierarchy(classUri);
                for (ExtendedIterator<OntClass> it = hierarchy.listClasses(); it.hasNext();) {
                    OntClass oc = it.next();
                    p.addOntClass(model.createResource(oc.getURI()));
                }
                //Copy the hierarchy
                model.add(hierarchy);
            }
        }
    }
}

From source file:pltag.util.Utils.java

License:Open Source License

public static Tree removeEmptyNodes(Tree tree) {
    Tree[] children = tree.children();/*  w w  w .j a  v  a2s  . co m*/
    for (int i = 0; i < children.length; i++) {
        Tree child = children[i];
        // heuristic: all leaf nodes have a special anchor symbol '<>' in the end.
        // If we don't find this, then we should remove the node.
        if (child.numChildren() == 0 && !child.label().value().contains("<>")) {
            //                System.out.println("node " + child);                
            tree.removeChild(i);
        } else {
            removeEmptyNodes(child);
        }
    }
    return tree;
}

From source file:qmul.corpus.DCPSECorpus.java

License:Open Source License

/**
 * @param dialogueName//  w ww. j ava 2 s.  co m
 * @param genre
 * @param reader
 * @return whether to carry on or not
 */
private boolean getSentences(String dialogueName, String genre, BufferedReader reader) {
    Pattern p = Pattern.compile("<#(\\d+):(\\d+):(\\w+)>\\s+<sent>");
    try {
        Dialogue dialogue = null;
        DialogueSpeaker lastSpeaker = null;
        DialogueTurn currentTurn = null;
        int currentSubdialogue = -1;
        Filter<Tree> nodeFilter = new NodeFilter();
        String line = reader.readLine();
        while (line != null) {
            Matcher m = p.matcher(line);
            if (m.find()) {
                // get the metadata
                int sentNum = Integer.parseInt(m.group(1));
                int subDialogue = Integer.parseInt(m.group(2));
                String spk = m.group(3).toUpperCase();
                // start new dialogue if subdialogue changed
                if (subDialogue != currentSubdialogue) {
                    if (dialogue != null) {
                        if (!checkDialogue(dialogue)) {
                            return false;
                        }
                    }
                    dialogue = addDialogue(dialogueName + ":" + subDialogue, genre);
                }
                currentSubdialogue = subDialogue;
                // set up speaker
                String spkId = dialogue.getId() + ":" + spk;
                DialogueSpeaker speaker = getSpeakerMap().get(spkId);
                // System.out.println("Getting tree for sent " + sentNum + " spk [" + spkId + "]=[" + speaker + "] "
                // + line);
                // get the tree and extract the transcription
                Tree tree = CreateTreeFromDCPSE.makeTree(reader);
                String trans = "";
                if (tree != null) {
                    tree = tree.prune(nodeFilter);
                    if (tree != null) {
                        for (Tree leaf : tree.getLeaves()) {
                            String label = leaf.label().toString();
                            label = label.replaceAll("^\\s*\\{(.*)\\}\\s*$", "$1");
                            label = label.replaceAll("^\\s*<([,.:;?!]+)>\\s*$", "$1");
                            trans += label + " ";
                        }
                        trans = trans.substring(0, trans.length() - 1);
                        // start new turn if speaker has changed
                        if ((lastSpeaker == null) || !speaker.equals(lastSpeaker) || (currentTurn == null)) {
                            currentTurn = dialogue.addTurn(-1, speaker);
                            // System.out.println(currentTurn);
                        }
                        // add sentence
                        dialogue.addSent(sentNum, currentTurn, trans, tree);
                        // System.out.println(sent);
                        lastSpeaker = speaker;
                    }
                }
            }
            line = reader.readLine();
        }
        return checkDialogue(dialogue);
    } catch (IOException e) {
        System.err.println("Error reading sentence line" + e.getMessage());
        return false;
    }
}