使用POS标签获取单词?

时间:2016-03-05 17:09:29

标签: stanford-nlp

我正在学习使用CoreNLP,我想知道,有没有办法使用POS标签获取单词。让我举个例子,

假设NLP被要求POS标签"这是基本测试。"结果是,

This_DT is_VBZ basic_JJ testing_NN ._. 

从中,有没有办法获得句子的DT等等?除了使用基本的字符串命令。

1 个答案:

答案 0 :(得分:1)

以下是一些演示访问注释的示例代码:

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();
            }
        }
    }
}