public static void FindCoreferenceResolution(string text) { //var text = "Barack Obama nominated Hillary Rodham Clinton as his secretary of state on Monday. He chose her because she had foreign affairs experience as a former First Lady."; var jarRoot = @"../../../data/paket-files/stanford-corenlp-3.9.1-models/"; var propsFile = Path.Combine(jarRoot, "StanfordCoreNLP.properties"); var props = new Properties(); //props.setProperty("annotators", "coref"); props.load(new FileReader(propsFile)); props.setProperty("ner.useSUTime", "0"); // We should change current directory, so StanfordCoreNLP could find all the model files automatically var curDir = Environment.CurrentDirectory; Directory.SetCurrentDirectory(jarRoot); var pipeline = new StanfordNLP.StanfordCoreNLP(props); Directory.SetCurrentDirectory(curDir); // Annotation var annotation = new Annotation(text); pipeline.annotate(annotation); // Result (detail) //using (var stream = new ByteArrayOutputStream()) //{ // pipeline.prettyPrint(annotation, new PrintWriter(stream)); // Console.WriteLine(stream.toString()); // stream.close(); //} // Result var corefChainAnnotation = new CorefCoreAnnotations.CorefChainAnnotation().getClass(); Console.WriteLine("---"); Console.WriteLine("coref chains"); var corefChain = annotation.get(corefChainAnnotation) as Map; foreach (CorefChain cc in corefChain.values().toArray()) { Console.WriteLine($"\t{cc}"); } }
// Sample from https://stanfordnlp.github.io/CoreNLP/coref.html static void Main() { var jarRoot = @"..\..\..\..\data\paket-files\nlp.stanford.edu\stanford-corenlp-full-2018-02-27\models"; Annotation document = new Annotation("Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008."); Properties props = new Properties(); props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref"); props.setProperty("ner.useSUTime", "0"); var curDir = Environment.CurrentDirectory; Directory.SetCurrentDirectory(jarRoot); var pipeline = new StanfordCoreNLP(props); Directory.SetCurrentDirectory(curDir); pipeline.annotate(document); var corefChainAnnotation = new CorefCoreAnnotations.CorefChainAnnotation().getClass(); var sentencesAnnotation = new CoreAnnotations.SentencesAnnotation().getClass(); var corefMentionsAnnotation = new CorefCoreAnnotations.CorefMentionsAnnotation().getClass(); Console.WriteLine("---"); Console.WriteLine("coref chains"); var corefChain = document.get(corefChainAnnotation) as Map; foreach (CorefChain cc in corefChain.values().toArray()) { Console.WriteLine($"\t{cc}"); } var sentences = document.get(sentencesAnnotation) as ArrayList; foreach (CoreMap sentence in sentences.toArray()) { Console.WriteLine("---"); Console.WriteLine("mentions"); var corefMentions = sentence.get(corefMentionsAnnotation) as ArrayList; foreach (Mention m in corefMentions) { Console.WriteLine("\t" + m); } } }
public void CorefTest() { Annotation document = new Annotation("Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008."); Properties props = new Properties(); props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,coref"); props.setProperty("ner.useSUTime", "false"); var curDir = Environment.CurrentDirectory; Directory.SetCurrentDirectory(Files.CoreNlp.JarRoot); var pipeline = new StanfordCoreNLP(props); Directory.SetCurrentDirectory(curDir); pipeline.annotate(document); var corefChainAnnotation = new CorefCoreAnnotations.CorefChainAnnotation().getClass(); var sentencesAnnotation = new CoreAnnotations.SentencesAnnotation().getClass(); var corefMentionsAnnotation = new CorefCoreAnnotations.CorefMentionsAnnotation().getClass(); Console.WriteLine("---"); Console.WriteLine("coref chains"); var corefChain = (Map)document.get(corefChainAnnotation); foreach (CorefChain cc in corefChain.values().toArray()) { Console.WriteLine($"\t{cc}"); } var sentences = (ArrayList)document.get(sentencesAnnotation); foreach (CoreMap sentence in sentences.toArray()) { Console.WriteLine("---"); Console.WriteLine("mentions"); var corefMentions = (ArrayList)sentence.get(corefMentionsAnnotation); foreach (Mention m in corefMentions) { Console.WriteLine("\t" + m); } } }