RinW
RinW

Reputation: 553

Obtain word using the POS tags?

I'm learning to use the CoreNLP and I'd like to know, is there a way to obtain the word using the POS tag. Let me give an example,

Assume the NLP is asked to POS tag "This is basic testing." and the result is,

This_DT is_VBZ basic_JJ testing_NN ._. 

From it, is there way to obtain just the DT's of the sentence and so on? Other than using basic string commands.

Upvotes: 0

Views: 102

Answers (1)

StanfordNLPHelp
StanfordNLPHelp

Reputation: 8739

Here is some sample code demonstrating accessing annotations:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.trees.TreeCoreAnnotations.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.ling.CoreAnnotations.*;
import edu.stanford.nlp.util.*;


public class PipelineExample {

    public static void main (String[] args) throws IOException {
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        String text = "This is basic testing.";
        Annotation annotation = new Annotation(text);
        pipeline.annotate(annotation);
        System.out.println("---");
        System.out.println("text: "+text);
        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                System.out.print("["+token.word()+" "+token.get(CoreAnnotations.PartOfSpeechAnnotation.class)+"]");
                System.out.println();
            }
        }
    }
}

Upvotes: 1

Related Questions