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:MedArkRef.AnalysisUtilities.java

License:Open Source License

public static void downcaseFirstToken(Tree inputTree) {
    if (inputTree == null) {
        return;/* w w w. j  a  va 2 s  . c  om*/
    }
    Tree firstWordTree = inputTree.getLeaves().get(0);
    if (firstWordTree == null)
        return;
    Tree preterm = firstWordTree.parent(inputTree);
    String firstWord = firstWordTree.yield().toString();
    if (firstWord != null && preterm != null && preterm.label() != null
            && !preterm.label().toString().matches("^NNP.*") && !firstWord.equals("I")) {
        //if(firstWord.indexOf('-') == -1 && !firstWord.equals("I")){
        firstWord = firstWord.substring(0, 1).toLowerCase() + firstWord.substring(1);
        firstWordTree.label().setValue(firstWord);
    }

    //if(QuestionTransducer.DEBUG) System.err.println("downcaseFirstToken: "+inputTree.toString());
}

From source file:nlp.prototype.NewJFrame.java

public void setTree(Tree tree) {
    DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Hello");
    rootNode.add(new DefaultMutableTreeNode(tree.label().value()));
    model.setRoot(rootNode);/*from  ww  w .j  ava 2  s  . c o m*/
}

From source file:nlp.prototype.NewJFrame.java

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked

    DefaultTreeModel model2 = (DefaultTreeModel) jTree2.getModel();
    DefaultMutableTreeNode rootNode2 = new DefaultMutableTreeNode("top");
    model2.setRoot(rootNode2);//from  ww w . ja v a  2  s .  c  o  m

    /*TextCorpus textCorpus = processor.parseCorpus(jTextArea1.getText());
            
    for (SentenceToken token : textCorpus.getSentences()) {
    DefaultMutableTreeNode sentenceTokenNode = new DefaultMutableTreeNode();
    sentenceTokenNode.setUserObject(token);
    rootNode2.add(sentenceTokenNode);
    addNodes(token, sentenceTokenNode);
    }
            
    DefaultTokenSerializer serializer = new DefaultTokenSerializer();
    Document xmlDocument = serializer.serialize(textCorpus);
    jTextArea4.setText(serializer.transform(xmlDocument));
    jTextArea7.setText(serializer.transform(xmlDocument, this.jTextArea6.getText()));*/

    Annotation document = new Annotation(jTextArea1.getText());
    pipeline.annotate(document);
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    Map<Integer, CorefChain> corefMap = document.get(CorefChainAnnotation.class);
    List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class);

    DefaultListModel listModel = new DefaultListModel();

    for (Class key : document.keySet()) {
        Object value = document.get(key);

        if (value != null && value.toString() != null && !value.toString().isEmpty()) {
            listModel.addElement(key.toString() + " - [" + value.toString() + "]");
        }
    }

    DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("top");
    model.setRoot(rootNode);

    List<POSToken> tokenList = new ArrayList<>();

    jList1.setModel(listModel);

    for (CoreMap sentence : sentences) {
        Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
        SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
        String root = graph.getFirstRoot().originalText();

        MultiValuedMap<String, GrammarToken> map = new HashSetValuedHashMap<>();

        for (SemanticGraphEdge edge : graph.edgeIterable()) {
            GrammarToken grammarToken = new GrammarToken(edge);
            map.put(grammarToken.getTarget(), grammarToken);
        }

        DefaultMutableTreeNode node = new DefaultMutableTreeNode();
        POSToken token = new POSToken((CoreLabel) tree.label());
        token.setGrammar(graph.toString());
        node.setUserObject(token);
        rootNode.add(node);
        addNodes(tree, false, node, node, map, root, corefMap, tokens);
        tokenList.add(token);
    }

    setAdjacentNodes(tokenList);
}

From source file:nlp.prototype.NewJFrame.java

private void addNodes(Tree tree, boolean normalizeParent, DefaultMutableTreeNode grandParentNode,
        DefaultMutableTreeNode parentNode, MultiValuedMap<String, GrammarToken> map, String root,
        Map<Integer, CorefChain> corefMap, List<CoreLabel> tokens) {
    POSToken parentToken = (POSToken) parentNode.getUserObject();
    POSToken grandParentToken = (POSToken) grandParentNode.getUserObject();

    Tree[] children = tree.children();/*from   w  ww.ja v a  2 s. c  o m*/
    if (children != null && children.length > 0) {

        for (Tree child : children) {
            POSToken childToken = new POSToken((CoreLabel) child.label());
            DefaultMutableTreeNode node = new DefaultMutableTreeNode();
            node.setUserObject(childToken);

            if (children.length == 1 && normalizeParent) {
                grandParentNode.remove(parentNode);
                grandParentNode.add(node);

                grandParentToken.removeChild(parentToken);
                grandParentToken.addChild(childToken);

                addNodes(child, true, grandParentNode, node, map, root, corefMap, tokens);
            } else {
                parentNode.add(node);
                parentToken.addChild(childToken);
                addNodes(child, true, parentNode, node, map, root, corefMap, tokens);
            }
        }
    } else {

        POSToken token = (POSToken) parentNode.getUserObject();
        token.setGrammarToken(map.get(token.toString()));
        token.setIsRoot(token.toString().equals(root));

        if (token.getCoref() != -1) {
            CorefMention mention = corefMap.get(token.getCoref()).getRepresentativeMention();

            StringBuilder sb = new StringBuilder();
            for (int i = mention.startIndex - 1; i < mention.endIndex - 1; i++) {
                sb.append(tokens.get(i).value()).append(" ");
            }

            token.setReference(sb.toString().trim());
        }
    }
}

From source file:nlp.prototype.Prototype.java

public static void main(String[] args) throws IOException {
    Properties props = new Properties();
    props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    Annotation document = new Annotation("Adrian is a handsome boy. He is also good-looking.");
    pipeline.annotate(document);/*from  w  w  w  .j a  va  2s  . c om*/
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    for (CoreMap sentence : sentences) {

        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {

            String word = token.get(TextAnnotation.class);

            String pos = token.get(PartOfSpeechAnnotation.class);

            String ne = token.get(NamedEntityTagAnnotation.class);

            System.out.println("word: " + word + " pos: " + pos + " ne:" + ne);
        }

        Tree tree = sentence.get(TreeAnnotation.class);
        System.out.println("parse tree:\n" + tree);
        System.out.println("label: " + tree.label());
        System.out.println("label class: " + tree.label().getClass());

        SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
        System.out.println("dependency graph:\n" + dependencies);
    }

    /*NewJFrame frame = new NewJFrame();
    frame.pack();
    frame.setText("haha");
    frame.setVisible(true);*/

}

From source file:nlp.service.implementation.DefaultLanguageProcessor.java

@Override
public SentenceToken assembleToken(SemanticGraph semanticGraph, CoreMap sentence) {
    Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
    CoreLabel coreLabel = (CoreLabel) tree.label();
    SentenceToken token = new SentenceToken(coreLabel.get(BeginIndexAnnotation.class),
            coreLabel.get(EndIndexAnnotation.class));
    List<WordToken> wordTokens = new ArrayList<>();

    GrammarService grammarService = new DefaultGrammarService(semanticGraph);

    if (!tree.isLeaf()) {
        addChildNodes(grammarService, wordTokens, token, tree.children());
    }//w w  w . j a  v a2s . co m

    populateGrammarRelations(grammarService, token, wordTokens);

    return token;
}

From source file:nlp.service.implementation.DefaultLanguageProcessor.java

private void addChildNodes(GrammarService grammarService, List<WordToken> wordTokens, Token token,
        Tree[] children) {/*from  w w  w .  j a  va 2  s.  com*/
    for (Tree child : children) {
        Tree childToAdd = child;
        Tree grandChild = getLeaf(child);
        if (grandChild != null) {
            childToAdd = grandChild;
        }

        if (childToAdd.isLeaf()) {
            WordToken leafToken = createWordToken(grammarService, token, (CoreLabel) childToAdd.label());
            token.addToken(leafToken);
            wordTokens.add(leafToken);
        } else {

            Token childToken = createPhraseToken(token, (CoreLabel) childToAdd.label());
            if (childToken != null) {
                token.addToken(childToken);
                addChildNodes(grammarService, wordTokens, childToken, childToAdd.children());
            }
        }
    }
}

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

License:Apache License

public StringBuilder toStringBuilderExtenderByAnotherLinkedTree1(StringBuilder sb, Tree t, Tree treeToInsert,
        String[] corefWords) {/*from w  ww  .j a  v  a 2  s  .  c om*/
    if (t.isLeaf()) {
        if (t.label() != null) {
            sb.append(t.label().value());
        }
        return sb;
    } else {
        sb.append('(');
        if (t.label() != null) {
            if (t.value() != null) {
                sb.append(t.label().value());
            }
        }
        boolean bInsertNow = false;
        Tree[] kids = t.children();
        if (kids != null) {
            for (Tree kid : kids) {
                if (corefWords != null) {
                    String word = corefWords[corefWords.length - 1];
                    String phraseStr = kid.toString();
                    phraseStr = phraseStr.replace(")", "");
                    if (phraseStr.endsWith(word)) {
                        bInsertNow = true;
                    }
                }
            }
            if (bInsertNow) {
                for (Tree kid : kids) {
                    sb.append(' ');
                    toStringBuilderExtenderByAnotherLinkedTree1(sb, kid, null, null);
                }
                sb.append(' ');
                toStringBuilderExtenderByAnotherLinkedTree1(sb, treeToInsert, null, null);
            } else {
                for (Tree kid : kids) {
                    sb.append(' ');
                    toStringBuilderExtenderByAnotherLinkedTree1(sb, kid, treeToInsert, corefWords);
                }

            }
        }

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

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 a2  s.c  om
        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());
        }/*from   w w  w  . ja v  a  2s  . co  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(')');
    }
}