def add_that_to_node(self, that_element, base_node, userid="*"): try: current_node = self._pattern_factory.new_node_class('that')(userid) current_node = base_node.add_that(current_node) head_text = self.get_text_from_element(that_element) if head_text is not None: current_node = self._parse_text( TextUtils.strip_all_punctuation(head_text), current_node) added_child = False for sub_element in that_element: new_node = self.node_from_element(sub_element) current_node = current_node.add_child(new_node) tail_text = self.get_tail_from_element(sub_element) if tail_text is not None: current_node = self._parse_text(tail_text, current_node) added_child = True if head_text is None: if added_child is False: raise ParserException("That node text is empty", xml_element=that_element) return current_node except ParserException as parser_excep: parser_excep.xml_element = that_element raise parser_excep
def parse_last_sentences_from_response(self, response): response = re.sub(r'<\s*br\s*/>\s*', ".", response) response = re.sub(r'<br></br>*', ".", response) sentences = response.split(".") sentences = [x for x in sentences if x] last_sentence = sentences[-1] that_pattern = TextUtils.strip_all_punctuation(last_sentence) that_pattern = that_pattern.strip() return that_pattern
def ask_question(self, bot, clientid, sentence) -> str: if self.authentication is not None: if self.authentication.authenticate(clientid) is False: logging.error("[%s] failed authentication!") return self.authentication.configuration.denied_srai conversation = bot.get_conversation(clientid) topic_pattern = conversation.predicate("topic") if topic_pattern is None: logging.info("No Topic pattern default to [*]") topic_pattern = "*" else: logging.info("Topic pattern = [%s]", topic_pattern) try: that_question = conversation.nth_question(2) that_sentence = that_question.current_sentence() # If the last response was valid, i.e not none and not empty string, then use # that as the that_pattern, otherwise we default to '*' as pattern if that_sentence.response is not None and that_sentence.response != '': that_pattern = TextUtils.strip_all_punctuation( that_sentence.response) logging.info("That pattern = [%s]", that_pattern) else: logging.info("That pattern, no response, default to [*]") that_pattern = "*" except Exception: logging.info("No That pattern default to [*]") that_pattern = "*" match_context = self._aiml_parser.match_sentence( bot, clientid, sentence, topic_pattern=topic_pattern, that_pattern=that_pattern) if match_context is not None: template_node = match_context.template_node() logging.debug("AIML Parser evaluating template [%s]", template_node.to_string()) response = template_node.template.resolve(bot, clientid) if "<oob>" in response: response, oob = self.strip_oob(response) if oob is not None: oob_response = self.process_oob(bot, clientid, oob) response = response + " " + oob_response return response return None
def parse_last_sentences_from_response(self, response): # TODO Issue here when the response is more than just a simple sentence # If the response contains punctuation such as "Hello. There" then THAT is none response = re.sub(r'<\s*br\s*/>\s*', ".", response) response = re.sub(r'<br></br>*', ".", response) sentences = response.split(".") sentences = [x for x in sentences if x] last_sentence = sentences[-1] that_pattern = TextUtils.strip_all_punctuation(last_sentence) that_pattern = that_pattern.strip() # TODO Added this to catch a failed sentence if that_pattern == "": that_pattern = '*' return that_pattern
def parse_last_sentences_from_response(self, response): # TODO Issue here when the response is more than just a simple sentence # If the response contains punctuation such as "Hello. There" then THAT is none response = re.sub(r'<\s*br\s*/>\s*', ".", response) response = re.sub(r'<br></br>*', ".", response) sentences = response.split(".") sentences = [x for x in sentences if x] last_sentence = sentences[-1] that_pattern = TextUtils.strip_all_punctuation(last_sentence) that_pattern = that_pattern.strip() # TODO Added this to catch a failed sentence if that_pattern == "": that_pattern = '*' return that_pattern
def parse_last_sentences_from_response(self, response): # If the response contains punctuation such as "Hello. There" then THAT is none response = re.sub(r'<\s*br\s*/>\s*', ".", response) response = re.sub(r'<br></br>*', ".", response) sentences = response.split(".") sentences = [x for x in sentences if x] last_sentence = sentences[-1] that_pattern = TextUtils.strip_all_punctuation(last_sentence) that_pattern = that_pattern.strip() if that_pattern == "": that_pattern = '*' return that_pattern
def ask_question(self, bot, clientid, sentence) -> str: conversation = bot.get_conversation(clientid) topic_pattern = conversation.predicate("topic") if topic_pattern is None: logging.debug("No Topic pattern default to [*]") topic_pattern = "*" else: logging.debug("Topic pattern = [%s]", topic_pattern) try: that_question = conversation.nth_question(2) that_sentence = that_question.current_sentence() # If the last response was valid, i.e not none and not empty string, then use # that as the that_pattern, otherwise we default to '*' as pattern if that_sentence.response is not None and that_sentence.response != '': that_pattern = TextUtils.strip_all_punctuation( that_sentence.response) logging.debug("That pattern = [%s]", that_pattern) else: logging.debug("That pattern, no response, default to [*]") that_pattern = "*" except Exception: logging.debug("No That pattern default to [*]") that_pattern = "*" match_context = self._aiml_parser.match_sentence( bot, clientid, sentence, topic_pattern=topic_pattern, that_pattern=that_pattern) if match_context is not None: template_node = match_context.template_node() logging.debug("AIML Parser evaluating template [%s]", template_node.to_string()) #template_node.template.dump(tabs="", output_func=print) response = template_node.template.resolve(bot, clientid) return response return None
def ask_question(self, bot, clientid, sentence) -> str: conversation = bot.get_conversation(clientid) topic_pattern = conversation.predicate("topic") if topic_pattern is None: logging.info("No Topic pattern default to [*]") topic_pattern = "*" else: logging.info("Topic pattern = [%s]", topic_pattern) try: that_question = conversation.nth_question(2) that_sentence = that_question.current_sentence() # If the last response was valid, i.e not none and not empty string, then use # that as the that_pattern, otherwise we default to '*' as pattern if that_sentence.response is not None and that_sentence.response != '': that_pattern = TextUtils.strip_all_punctuation(that_sentence.response) logging.info("That pattern = [%s]", that_pattern) else: logging.info("That pattern, no response, default to [*]") that_pattern = "*" except Exception: logging.info("No That pattern default to [*]") that_pattern = "*" match_context = self._aiml_parser.match_sentence(bot, clientid, sentence, topic_pattern=topic_pattern, that_pattern=that_pattern) if match_context is not None: template_node = match_context.template_node() logging.debug("AIML Parser evaluating template [%s]", template_node.to_string()) return template_node.template.resolve(bot, clientid) return None
def process(self, context, word_string): YLogger.debug(context, "Removing punctuation...") return TextUtils.strip_all_punctuation(word_string)
def test_strip_all_punctuation(self): self.assertEquals("", TextUtils.strip_all_punctuation("")) self.assertEquals(" ", TextUtils.strip_all_punctuation(" ")) self.assertEquals("x y z", TextUtils.strip_all_punctuation("x! y? z.")) self.assertEquals("a b c", TextUtils.strip_all_punctuation("!a b c?"))
def process(self, bot, clientid, word_string): if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Removing punctuation...") return TextUtils.strip_all_punctuation(word_string)
def process(self, bot, clientid, string): logging.debug("Removing punctuation...") return TextUtils.strip_all_punctuation(string)
def process(self, context, word_string): YLogger.debug(context, "Removing punctuation...") return TextUtils.strip_all_punctuation(word_string)
def test_strip_all_punctuation(self): self.assertEquals("", TextUtils.strip_all_punctuation("")) self.assertEquals("", TextUtils.strip_all_punctuation(" ")) self.assertEquals("x y z", TextUtils.strip_all_punctuation("x! y? z.")) self.assertEquals("a b c", TextUtils.strip_all_punctuation("!a b c?"))