java - Upgrading Stanford Coreference Resolution 3.5.2 code to work with version 3.7.0 -
i using multiple projects. main project uses stanfordnlp version 3.7.0 , given working coreference code version 3.5.2 attached below. code not work version 3.7.0. happens coreference working code find coreferenced word in next sentence automatically replaced. https://github.com/sunil3590/artificial-guy
i sure don't want use both versions in program loading both parsers , models overkill. , both may not work together.
i wondering how go updating code below can work on version 3.7.0.
package edu.ncsu.artificialguy; import java.util.arraylist; import java.util.list; import java.util.map; import java.util.properties; //import org.apache.commons.lang3.text.wordutils; import edu.stanford.nlp.dcoref.corefchain; import edu.stanford.nlp.dcoref.corefchain.corefmention; import edu.stanford.nlp.dcoref.corefcoreannotations.corefchainannotation; import edu.stanford.nlp.dcoref.corefcoreannotations.corefclusteridannotation; import edu.stanford.nlp.ling.coreannotations.lemmaannotation; import edu.stanford.nlp.ling.coreannotations.namedentitytagannotation; import edu.stanford.nlp.ling.coreannotations.partofspeechannotation; import edu.stanford.nlp.ling.coreannotations.sentenceindexannotation; import edu.stanford.nlp.ling.coreannotations.sentencesannotation; import edu.stanford.nlp.ling.coreannotations.textannotation; import edu.stanford.nlp.ling.coreannotations.tokensannotation; import edu.stanford.nlp.ling.corelabel; import edu.stanford.nlp.pipeline.annotation; import edu.stanford.nlp.pipeline.stanfordcorenlp; import edu.stanford.nlp.semgraph.semanticgraph; import edu.stanford.nlp.semgraph.semanticgraphcoreannotations.collapsedccprocesseddependenciesannotation; import edu.stanford.nlp.util.coremap; public class stanfordcoreference { // singleton instance private static stanfordcoreference nlp = null; private static stanfordcorenlp pipeline; public stanfordcoreference() { // load required models loadmodels(); } public static void initcoreference(){ // perform nlp operations required stanfordcoreference nlp = new stanfordcoreference(); } public static string returncoreferencedtext(string text){ // perform nlp operations required stanfordcoreference nlp = stanfordcoreference.getinstance(); // resolve co reference //string coreftext = nlp.resolvecoref(story.gettext()); return nlp.resolvecoref(text); } public static void main(string[] args) throws exception { // perform nlp operations required stanfordcoreference nlp = stanfordcoreference.getinstance(); // resolve co reference string text = "john drove judy’s house. made dinner."; //string coreftext = nlp.resolvecoref(story.gettext()); string coreftext = nlp.resolvecoref(text); // story after co reference resolution system.out.println("\n################# after coreference resolution ###################"); system.out.println(coreftext.replace(". ", ".\n")); system.out.println("##################################################################\n"); } public static stanfordcoreference getinstance() { if (nlp == null) { nlp = new stanfordcoreference(); } return nlp; } private static void loadmodels() { properties props = new properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, ner, depparse, parse, dcoref"); props.put("enforcerequirements", false); pipeline = new stanfordcorenlp(props); } private annotation runpipeline(string text) { // create empty annotation given text annotation document = new annotation(text); // run pipeline on text pipeline.annotate(document); return document; } public string resolvecoref(string text) { // hold resolved string string resolved = new string(); // run pipeline annotation document = runpipeline(text); // coref chains , sentences map<integer, corefchain> corefs = document.get(corefchainannotation.class); list<coremap> sentences = document.get(sentencesannotation.class); // process each sentence (coremap sentence : sentences) { int cursentidx = sentence.get(sentenceindexannotation.class); list<corelabel> tokens = sentence.get(tokensannotation.class); boolean ispronoun = false; (corelabel token : tokens) { // process pronouns ispronoun = false; string pos = token.get(partofspeechannotation.class); if (pos.equals("prp") || pos.equals("pp$")) { ispronoun = true; } integer corefclustid = token.get(corefclusteridannotation.class); corefchain chain = corefs.get(corefclustid); // if there no chain replace if (chain == null || chain.getmentionsintextualorder().size() == 1 || ispronoun == false) { resolved += token.word() + token.after(); } else { int sentindx = chain.getrepresentativemention().sentnum - 1; corefmention reprment = chain.getrepresentativemention(); string rootword = sentences.get(sentindx) .get(tokensannotation.class) .get(reprment.headindex - 1) .originaltext(); if (cursentidx != sentindx || token.index() < reprment.startindex || token.index() > reprment.endindex) { if (character.isuppercase(token.originaltext().charat(0))) { // rootword = wordutils.capitalize(rootword); } resolved += rootword + token.after(); } else { resolved += token.word() + token.after(); } } } } return resolved; } }
Comments
Post a Comment