Stanford NLP: How to lemmatize single word?

烈酒焚心 提交于 2019-12-23 11:52:15

问题


I know how I can annotate a sentence and get the lemma of each word but I don't know how to do it if I just want to lemmatize a single word. I tried

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

but the tokenAnnotation has only one TextAnnotation key which means list will be nullhere.

So how can I lemmatize a single word?


回答1:


There are two options: you can either annotate you Annotation object through a StanfordCoreNLP pipeline:

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

The other option is to use the SimpleCoreNLP API:

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


来源:https://stackoverflow.com/questions/34963203/stanford-nlp-how-to-lemmatize-single-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!