2016-03-05 103 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(); 
      } 
     } 
    } 
}