2016-01-23 48 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