public void StanfordCoreNlpDemoThatChangeCurrentDirectory() { const string 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("sutime.binders", "0"); // we should change current directory so StanfordCoreNLP could find all the model files var curDir = Environment.CurrentDirectory; Directory.SetCurrentDirectory(Config.JarRoot); var pipeline = new edu.stanford.nlp.pipeline.StanfordCoreNLP(props); Directory.SetCurrentDirectory(curDir); // Annotation var annotation = new Annotation(Text); pipeline.annotate(annotation); // Result - Pretty Print using (var stream = new ByteArrayOutputStream()) { pipeline.prettyPrint(annotation, new PrintWriter(stream)); Console.WriteLine(stream.toString()); } this.CustomAnnotationPrint(annotation); }
public static void Execute(string option, string text, bool disableLogging = true) { if (disableLogging) { RedwoodConfiguration.current().clear().apply(); } var jarRoot = @"../../../data/paket-files/stanford-corenlp-3.9.1-models/"; var props = new Properties(); props.setProperty("annotators", option); 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 StanfordNLP.Annotation(text); pipeline.annotate(annotation); //get sentencesAnnotation to get sentences var sentencesAnnotation = new CoreAnnotations.SentencesAnnotation().getClass(); //get tokensAnnotaion to get tokens in each sentence var tokensAnnotaion = new CoreAnnotations.TokensAnnotation().getClass(); //get posAnnotation to get POS result of each token var posAnnotation = new CoreAnnotations.PartOfSpeechAnnotation().getClass(); //get nerAnnotation to get NER result of each token var nerAnnotaion = new CoreAnnotations.NamedEntityTagAnnotation().getClass(); var deparseAnnotation = new TreeCoreAnnotations.TreeAnnotation().getClass(); //deparseAnnotation = new TypedDependency().getClass(); var sentences = annotation.get(sentencesAnnotation) as ArrayList; foreach (CoreMap sentence in sentences.toArray()) { var tokens = (ArrayList)sentence.get(tokensAnnotaion); Console.WriteLine("Token-POS-NER: "); foreach (CoreLabel token in tokens) { Console.Write($"{token.value()}-{token.get(posAnnotation)}-{token.get(nerAnnotaion)} "); } Console.WriteLine("\n\n\n"); var parsedText = (Tree)sentence.get(deparseAnnotation); if (parsedText != null) { Console.WriteLine("Parsed Text: "); new TreePrint("penn,typedDependenciesCollapsed").printTree(parsedText); } } }
public void StanfordCoreNlpDemoManualConfiguration() { Console.WriteLine(Environment.CurrentDirectory); const string 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("pos.model", Config.GetModel(@"pos-tagger\english-bidirectional\english-bidirectional-distsim.tagger")); props.setProperty("ner.model", Config.GetModel(@"ner\english.all.3class.distsim.crf.ser.gz")); props.setProperty("parse.model", Config.GetModel(@"lexparser\englishPCFG.ser.gz")); props.setProperty("dcoref.demonym", Config.GetModel(@"dcoref\demonyms.txt")); props.setProperty("dcoref.states", Config.GetModel(@"dcoref\state-abbreviations.txt")); props.setProperty("dcoref.animate", Config.GetModel(@"dcoref\animate.unigrams.txt")); props.setProperty("dcoref.inanimate", Config.GetModel(@"dcoref\inanimate.unigrams.txt")); props.setProperty("dcoref.male", Config.GetModel(@"dcoref\male.unigrams.txt")); props.setProperty("dcoref.neutral", Config.GetModel(@"dcoref\neutral.unigrams.txt")); props.setProperty("dcoref.female", Config.GetModel(@"dcoref\female.unigrams.txt")); props.setProperty("dcoref.plural", Config.GetModel(@"dcoref\plural.unigrams.txt")); props.setProperty("dcoref.singular", Config.GetModel(@"dcoref\singular.unigrams.txt")); props.setProperty("dcoref.countries", Config.GetModel(@"dcoref\countries")); props.setProperty("dcoref.extra.gender", Config.GetModel(@"dcoref\namegender.combine.txt")); props.setProperty("dcoref.states.provinces", Config.GetModel(@"dcoref\statesandprovinces")); props.setProperty("dcoref.singleton.predictor", Config.GetModel(@"dcoref\singleton.predictor.ser")); props.setProperty("dcoref.big.gender.number", Config.GetModel(@"dcoref\gender.data.gz")); var sutimeRules = new[] { Config.GetModel(@"sutime\defs.sutime.txt"), Config.GetModel(@"sutime\english.holidays.sutime.txt"), Config.GetModel(@"sutime\english.sutime.txt") }; props.setProperty("sutime.rules", String.Join(",", sutimeRules)); props.setProperty("sutime.binders", "0"); var pipeline = new edu.stanford.nlp.pipeline.StanfordCoreNLP(props); // Annotation var annotation = new Annotation(Text); pipeline.annotate(annotation); // Result - Pretty Print using (var stream = new ByteArrayOutputStream()) { pipeline.prettyPrint(annotation, new PrintWriter(stream)); Console.WriteLine(stream.toString()); } this.CustomAnnotationPrint(annotation); }
private void button1_Click(object sender, EventArgs e) { text = InputText.Text.ToString(); option = OutputType.Text.ToString(); if (text != "") { PWF = new PleaseWaitForm(); PWF.Show(); this.Visible = false; var annotation = new Annotation(text); pipeline.annotate(annotation); using (var stream = new ByteArrayOutputStream()) { if (option == "Pretty") { pipeline.prettyPrint(annotation, new PrintWriter(stream)); } else if (option == "Json") { pipeline.jsonPrint(annotation, new PrintWriter(stream)); } else if (option == "XML") { pipeline.xmlPrint(annotation, new PrintWriter(stream)); } else if (option == "Conll") { pipeline.conllPrint(annotation, new PrintWriter(stream)); } else { pipeline.conllPrint(annotation, new PrintWriter(stream)); } OutputText.Text = stream.ToString(); stream.close(); } PWF.Close(); this.Visible = true; } }
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}"); } }