CustomLemmaAnnotator implements Annotator { public void annotate(Annotation annotation) { for (CoreLabel token : annotation.get(CoreAnnotations.TokensAnnotation.class)) { String lemma = wordToLemma.getOrDefault(token.word(), token.word()); token.set(CoreAnnotations.LemmaAnnotation.class, lemma); } } CustomLemmaAnnotator(String name, Properties props); void annotate(Annotation annotation); @Override Set<Class<? extends CoreAnnotation>> requires(); @Override Set<Class<? extends CoreAnnotation>> requirementsSatisfied();  }
@Test public void test() { Properties props = new Properties(); props.setProperty("annotators", "tokenize,ssplit,pos,custom.lemma"); props.setProperty("customAnnotatorClass.custom.lemma", "com.fancyerii.blog.stanfordnlp.CustomLemmaAnnotator"); props.setProperty("custom.lemma.lemmaFile", "custom-lemmas.txt"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); CoreDocument exampleDocument = new CoreDocument("Some many goods there."); pipeline.annotate(exampleDocument); List<CoreLabel> firstSentenceTokens = exampleDocument.sentences().get(0).tokens(); for (CoreLabel token : firstSentenceTokens) { System.out.println(token.word()+"/"+token.getString(LemmaAnnotation.class) + "\t" + token.beginPosition() + "\t" + token.endPosition()); } }