Search code examples
javamavennlpstanford-nlp

stanford nlp pos tagging


Im doing my POS (part of speech tagging) from this tagger. But when i combine that part to my maven project it doesnt work. Is there a way where i can user stanford directly to do pos without using a seperate tagger? I want the output as same as this.

 MaxentTagger tagger = new MaxentTagger("taggers/left3words-wsj-0-18.tagger");
        String sample = "Im so happy about my marks";
        String tagged = tagger.tagString(sample);
        System.out.println(tagged);

output:Im/NNP so/RB happy/JJ about/IN my/PRP$ marks/NNS


Solution

  • Of course Stanford CoreNLP can do tagging directly. The following lines of code tag your example, and give you the desired output.

    Properties props = new Properties();
    
    props.setProperty("annotators","tokenize, ssplit, pos");
    
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    Annotation annotation = new Annotation("I'm so happy about my marks");
    pipeline.annotate(annotation);
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        for (CoreLabel token: sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            String word = token.get(CoreAnnotations.TextAnnotation.class);
            // this is the POS tag of the token
            String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
            System.out.println(word + "/" + pos);
        }
    }