0

Basically what I want is to replace all the pronouns from a text with the actual entity.

        // Path to the folder with models extracted from `stanford-corenlp-3.7.0-models.jar`
        var jarRoot = ...

        // Text for processing
        var text = "Kosgi Santosh sent an email to Stanford University. He didn't get a reply.";

        // Annotation pipeline configuration
        var props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        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 StanfordCoreNLP(props);
        Directory.SetCurrentDirectory(curDir);

        // Annotation
        var annotation = new Annotation(text);
        pipeline.annotate(annotation);

        var graph = annotation.get(new CorefChainAnnotation().getClass());
        Console.WriteLine(graph);

So far I could only find how to "pretty print" it, but I would like to further process the result from "graph", yet I do not know how to actually parse the result from "annotation.get(new CorefChainAnnotation().getClass())". In Java it is said that it would return a Map < Integer, CorefChain >, but I don't know how it is supposed to work in C#.

Do you have any ideas?

Kari
  • 201
  • 2
  • 5

1 Answers1

0

Once you have the annotation, you get the graph by casting it.

Map graph = (Map)document.get(new CorefCoreAnnotations.CorefChainAnnotation().getClass());
var entrySetValues = graph.entrySet();
Iterator it = entrySetValues.iterator();
while (it.hasNext())
{
     Map.Entry kvpair = (Map.Entry)it.next();
     CorefChain corefChain = (CorefChain)kvpair.getValue();
     var mentionsList = corefChain.getMentionsInTextualOrder() as ArrayList;
     foreach (CorefMention mention in mentionsList)
     {
          string noun = mention.mentionSpan;
          // do other stuff
     } 

     it.remove();
}

For C#, the idea is to first cast an object correctly, get the list from the cast object as an ArrayList, Loop on the arraylist and cast the objects correctly again.