stanford nlp pos tagging

时间:2015-04-11 17:28:39

标签: java maven nlp stanford-nlp

我正在使用此标记器进行POS(词性标注)。但是,当我将该部分与我的maven项目结合起来时,它不起作用。有没有办法让我可以直接使用stanford做pos而不使用单独的标记器?我希望输出与此相同。

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

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

1 个答案:

答案 0 :(得分:7)

当然,斯坦福CoreNLP可以直接进行标记。以下代码行标记您的示例,并为您提供所需的输出。

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