2016-01-23 51 views
4

我知道如何註釋一個句子並獲得每個單詞的詞條,但是我不知道該怎麼做,如果我只是想單詞單詞單詞。我試圖斯坦福大學NLP:如何解讀單個單詞?

Annotation tokenAnnotation = new Annotation("wedding"); 
List<CoreMap> list = tokenAnnotation.get(SentencesAnnotation.class); 

String tokenLemma = list 
         .get(0).get(TokensAnnotation.class) 
         .get(0).get(LemmaAnnotation.class); 

tokenAnnotation只有一個TextAnnotation關鍵,這意味着listnull這裏。

那麼,我該如何解釋一個單詞?

回答

3

有兩種選擇:你可以標註您通過StanfordCoreNLP管道Annotation對象:

StanfordCoreNLP pipeline = new StanfordCoreNLP(new Properties(){{ 
    setProperty("annotators", "tokenize,ssplit,pos,lemma"); 
}}); 

Annotation tokenAnnotation = new Annotation("wedding"); 
pipeline.annotate(tokenAnnotation); // necessary for the LemmaAnnotation to be set. 
List<CoreMap> list = tokenAnnotation.get(SentencesAnnotation.class); 
String tokenLemma = list 
         .get(0).get(TokensAnnotation.class) 
         .get(0).get(LemmaAnnotation.class); 

另一種選擇是使用SimpleCoreNLP API:

String tokenLemma = new Sentence("wedding").lemma(0); 
+0

OMG 10H的工作,我做垃圾。我完全忘記了調用'annotate()'O_O謝謝,那當然應該是^^ – displayname