2016-03-05 99 views
0

我正在学习使用CoreNLP,我想知道,有没有一种方法可以使用POS标签来获取单词。我举一个例子,使用POS标签获取单词?

假设NLP被要求给POS标签“这是基本测试”。其结果是,

This_DT is_VBZ basic_JJ testing_NN ._. 

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

回答

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