public static JObject ProcessText(string text) { var annotation = new Annotation(text); using (java.io.StringWriter writer = new java.io.StringWriter()) { pipeline.annotate(annotation); pipeline.jsonPrint(annotation, writer); return(JObject.Parse(writer.toString())); } }
public static AnnotationObject Annotate(string content) { // Annotation var annotation = new Annotation(content); Pipeline.annotate(annotation); // Result - Print using var stream = new ByteArrayOutputStream(); Pipeline.jsonPrint(annotation, new PrintWriter(stream)); //----- string serialized = stream.toString().Replace("\n", ""); var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject <AnnotationObject>(serialized); //----- stream.close(); return(deserialized); }
public NlpResult DeserializeInput(StanfordCoreNLP pipeline, NlpResult nlpResult, string stringInput) { // Annotation var annotation = new Annotation(stringInput); pipeline.annotate(annotation); // Result - Pretty Print using (var stream = new ByteArrayOutputStream()) { pipeline.jsonPrint(annotation, new PrintWriter(stream)); _jsonContentProvider.PopulateFromString(nlpResult, stream.toString()); Debug.WriteLine(stream.toString()); stream.close(); } return(nlpResult); }
/****************************************/ /**** Public Methods ****/ /****************************************/ /// <summary> /// Analyse a paragraph of text looking for semantic relationships between words and sentences. /// </summary> /// <param name="text">Paragraph to analyse as string</param> /// <param name="annotators">Annotators to extract from the analysis as list of strings. See <see cref="https://stanfordnlp.github.io/CoreNLP/annotators.html"/> for a complete list.</param> /// <returns>A json string containing annotated text</returns> public static string Semantics(string text, IEnumerable <string> annotators = null) { Properties props = new Properties(); string finalAnnotators = ""; if (annotators != null) { finalAnnotators = String.Join(", ", annotators.ToArray()); } finalAnnotators += "tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment"; props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment"); props.setProperty("ner.useSUTime", "0"); props.setProperty("thread", Environment.ProcessorCount.ToString()); string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1]; string jarRoot = "C:\\Users\\" + username + "\\AppData\\Roaming\\BHoM\\stanford-corenlp-3.8.0"; if (!System.IO.Directory.Exists(jarRoot)) { throw new System.IO.FileNotFoundException("Please download stanford-corenlp-3.8.0 from https://stanfordnlp.github.io/CoreNLP/index.html#download and place it in" + jarRoot); } string curDir = Environment.CurrentDirectory; Directory.SetCurrentDirectory(jarRoot); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Directory.SetCurrentDirectory(curDir); Annotation annotation = new Annotation(text); pipeline.annotate(annotation); ByteArrayOutputStream stream = new ByteArrayOutputStream(); pipeline.jsonPrint(annotation, new PrintWriter(stream)); return(stream.ToString()); }
public void ProcessText(string text, string outputPath) { var props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse"); 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(stanfordJarRoot); var pipeline = new StanfordCoreNLP(props); Directory.SetCurrentDirectory(curDir); Console.WriteLine("Starting to parse."); // Annotation var annotation = new Annotation(text); pipeline.annotate(annotation); // Result - Pretty Print Console.WriteLine("Parsing complete.. writing to file."); string jsonOutput; using (var stream = new ByteArrayOutputStream()) { pipeline.jsonPrint(annotation, new PrintWriter(stream)); jsonOutput = stream.toString(); stream.close(); } using (var file = new StreamWriter(outputPath)) { file.WriteLine(jsonOutput); } Console.WriteLine("Processing complete."); }