Разрешите Coreference с помощью Stanford Parser в.NET
По сути, я хочу заменить все местоимения из текста на реальную сущность.
// 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);
До сих пор я мог только найти, как "красиво напечатать" его, но я хотел бы дополнительно обработать результат из "графика", но я не знаю, как на самом деле анализировать результат из "annotation.get(new CorefChainAnnotation()"). GetClass())". В Java сказано, что он вернет Map
Есть ли у вас какие-либо идеи?
1 ответ
Получив аннотацию, вы получите график, приведя его.
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();
}
Для C# идея состоит в том, чтобы сначала правильно привести объект к объекту, получить список из объекта приведения в виде ArrayList, Loop для Arraylist и снова правильно привести объект.