/// <summary> /// Setup extended tagger that includes POS, lemma and entity analysis /// </summary> private void SetupExtendedTagger() { PerformanceTester.StartMET("NLP"); // Get path to Stanford NLP models var jarRoot = Path.Combine(Utility.GetResourcesFolder(), @"stanford-corenlp-3.9.2-models"); // Turn off logging RedwoodConfiguration.current().clear().apply(); var props = new java.util.Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner"); // Makes Named Entity Recognition work in the library props.setProperty("ner.useSUTime", "0"); props.put("ner.applyFineGrained", "0"); props.put("ner.fine.regexner.mapping", jarRoot + @"\edu\stanford\nlp\models\kbp\english\"); // Set current directory var curDir = Environment.CurrentDirectory; var modelsDirectory = curDir + "\\" + jarRoot + @"\edu\stanford\nlp\models"; Directory.SetCurrentDirectory(jarRoot); // Load Stanford NLP Tagger = new StanfordCoreNLP(props); PerformanceTester.StopMET("NLP"); }
/// <summary> /// Get sentences from the given message text /// </summary> /// <param name="text">The message text</param> /// <returns>A list with the sentences</returns> public List <object> GetSentences(string text) { PerformanceTester.StartMET("GetSentences"); var annotation = new Annotation(text); Tagger.annotate(annotation); Sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList; PerformanceTester.StopMET("GetSentences"); return(new List <object>(Sentences.toArray())); }
/// <summary> /// Tag the given message text /// </summary> /// <param name="text">The message text</param> /// <returns>A list with the tagged words</returns> public List <TaggedWord> Tag(string text, bool useSavedSentences = false) { PerformanceTester.StartMET("Tagging"); var list = new List <TaggedWord>(); ArrayList sentences = null; if (useSavedSentences && Sentences != null) { sentences = Sentences; } else { var annotation = new Annotation(text); Tagger.annotate(annotation); sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList; } foreach (CoreMap sentence in sentences) { var tokens = sentence.get(new CoreAnnotations.TokensAnnotation().getClass()) as ArrayList; foreach (CoreLabel token in tokens) { var original = token.get(new CoreAnnotations.OriginalTextAnnotation().getClass()); var after = token.get(new CoreAnnotations.AfterAnnotation().getClass()); var before = token.get(new CoreAnnotations.BeforeAnnotation().getClass()); var word = token.get(new CoreAnnotations.TextAnnotation().getClass()); var pos = token.get(new CoreAnnotations.PartOfSpeechAnnotation().getClass()); //var ner = token.get(new CoreAnnotations.NamedEntityTagAnnotation().getClass()); //var lemma = token.get(new CoreAnnotations.LemmaAnnotation().getClass()); var taggedWord = new TaggedWord() { Word = word.ToString(), Original = original.ToString(), WhiteSpaceCharacterAfter = after.ToString(), WhiteSpaceCharacterBefore = before.ToString(), POSStringIdentifier = pos.ToString(), //Lemma = lemma.ToString(), //NERStringIdentifier = ner.ToString() }; list.Add(taggedWord); } } PerformanceTester.StopMET("Tagging"); return(list); }
//*************************************************/ // METHODS //*************************************************/ #region Methods /// <summary> /// Setup tagger including POS /// </summary> private void SetupTagger() { PerformanceTester.StartMET("NLP"); // Get path to Stanford NLP models var jarRoot = Path.Combine(Utility.GetResourcesFolder(), @"stanford-corenlp-3.9.2-models"); // Turn off logging RedwoodConfiguration.current().clear().apply(); // Set properties var props = new java.util.Properties(); props.setProperty("annotators", "tokenize, ssplit, pos"); // Set current directory var curDir = Environment.CurrentDirectory; var modelsDirectory = curDir + "\\" + jarRoot + @"\edu\stanford\nlp\models"; Directory.SetCurrentDirectory(jarRoot); // Load Stanford NLP Tagger = new StanfordCoreNLP(props); PerformanceTester.StopMET("NLP"); }
/// <summary> /// Analyze a text message from the user /// </summary> /// <param name="text"></param> /// <returns>A message result</returns> private MessageResult AnalyzeText(string text) { try { var tempText = text.ToLower(); // Get sentences that has been written var sentences = NLPHelper.GetSentences(tempText); // Does not support more than one sentence if (sentences.Count > 1) { return(new MessageResult(Properties.Resources.i_cannot_process_more_than_one_sentence)); } // Tagging on the given words List <TaggedWord> wordList = null; // Function for tagging Action <bool> tagging = (useSavedSentences) => { // Insert a question mark at the end of a text if (!tempText.EndsWith("?")) { tempText += "?"; } // Tag the words in the text wordList = NLPHelper.Tag(tempText, useSavedSentences); }; PerformanceTester.StartMET("Use calculator"); // If text has any numbers use the calculator string output; if (SimpleCalculatorHelper.CheckInput(ref tempText, out output)) { // If text is not math command if (output == null) { var input = tempText; tagging(false); if (wordList.Any(x => x.IsWHWord || x.IsVerb)) { // Skip the WH-words and verbs in the beginning when using calculator var context = wordList .SkipWhile(x => x.IsWHWord || x.IsVerb) .Where(x => x.POSStringIdentifier != ".") .ToList(); // Get text var joined = context.ListToString(); // If sentence starts with a WH word insert an equal sign if (wordList.FirstOrDefault().IsWHWord) { input = joined.Insert(0, "="); } } // Use calculator output = SimpleCalculatorHelper.UseCalculator(input); PerformanceTester.StopMET("Use calculator"); } // If output is not null write the calculator result to the user if (output != null) { return(new MessageResult() { Messages = new MessageObject[] { new MessageObject(output) } }); } } PerformanceTester.StopMET("Use calculator"); tagging(true); PerformanceTester.StartMET("Analyze word list"); // Analyze the input var analyzeResult = AnalyzeWordList(wordList); PerformanceTester.StopMET("Analyze word list"); // If success if (analyzeResult.IsSuccess) { // Analyze the search strings return(AnalyzeSearchStrings(analyzeResult.SearchStrings)); } // If error else { return(new MessageResult(analyzeResult.ErrorMessage)); } } catch (Exception mes) { return(new MessageResult(mes.Message)); } }
/// <summary> /// Analyze a given list of search strings /// </summary> /// <param name="list">The list with the search strings</param> /// <returns>A message result</returns> private MessageResult AnalyzeSearchStrings(List <string> list) { List <MessageObject> messages = new List <MessageObject>(); List <Material> materials = null; try { PerformanceTester.StartMET("Analyze search strings"); foreach (var str in list) { // Checking for bot command if (RunCommand(str)) { return(new MessageResult()); } // Search for term and topic Term term = null; Topic topic = null; Entity.GetTermAndTopic(out term, out topic, str); if (topic == null && term == null) { continue; } // Get materials // A term and a topic has the same name if (term != null && topic != null) { PerformanceTester.StopMET("Analyze search strings"); messages.Add(new MessageObject(term, topic)); return(new MessageResult(messages.ToArray())); } // Get materials for a topic else if (topic != null) { materials = Entity.GetMaterials(topicId: topic.Id); } // Get materials for a term else { materials = Entity.GetMaterials(term.Id); } PerformanceTester.StopMET("Analyze search strings"); if (materials.Count == 0) { return(new MessageResult(Properties.Resources.no_materials_found)); } // Add bot message messages.Add(new MessageObject(string.Format(Properties.Resources.this_is_what_i_found_about, term != null ? term.Name.ToLower() : topic.Name.ToLower()))); // Add materials foreach (var material in materials) { messages.Add(new MessageObject(material)); } return(new MessageResult(messages.ToArray())); } PerformanceTester.StopMET("Analyze search strings"); // One noun if (list.Count == 1) { return(new MessageResult(string.Format(Properties.Resources.i_do_not_know_anything_about_that_noun, list[0], GetTopics()))); } // More nouns else { return(new MessageResult(string.Format(Properties.Resources.i_do_not_know_anything_about_those_nouns, GetTopics()))); } } catch (Exception mes) { return(new MessageResult(mes.Message)); } }