Пример #1
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is True:
            return EqualsMatch(False, word_no)

        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        if client_context.brain.dynamics.is_dynamic_set(
                self._set_name) is True:
            result = client_context.brain.dynamics.dynamic_set(
                client_context, self._set_name, word)
            return EqualsMatch(result, word_no, word)
        else:
            if self.set_is_known(client_context):
                match = self.words_in_set(client_context, words, word_no)
                if match.matched is True:
                    YLogger.debug(client_context,
                                  "Found word [%s] in set [%s]", word,
                                  self.set_name)
                    return match
                else:
                    YLogger.debug(client_context,
                                  "No word [%s] found in set [%s]", word,
                                  self.set_name)
                    return EqualsMatch(False, word_no)
            else:
                YLogger.debug(client_context,
                              "No set named [%s] in sets collection",
                              self.set_name)
                return EqualsMatch(False, word_no)
Пример #2
0
 def test_equals_match(self):
     equals_match = EqualsMatch(True, 1, "Hello World")
     self.assertIsNotNone(equals_match)
     self.assertTrue(equals_match.matched)
     self.assertEquals(1, equals_match.word_no)
     self.assertEquals("Hello World", equals_match.matched_phrase)
     self.assertEquals("True, 1, Hello World", equals_match.to_string())
Пример #3
0
    def words_in_set(self, client_context, words, word_no):

        word = words.word(word_no).upper()
        set_words = client_context.brain.sets.set(self.set_name)
        if not set_words:
            YLogger.error(self, "No set with name [%s]", self.set_name)

        elif word in set_words:
            phrases = set_words[word]
            phrases = sorted(phrases, key=len, reverse=True)
            for phrase in phrases:
                phrase_word_no = 0
                words_word_no = word_no
                while phrase_word_no < len(
                        phrase) and words_word_no < words.num_words():
                    phrase_word = phrase[phrase_word_no].upper()
                    word = words.word(words_word_no).upper()
                    if phrase_word == word:
                        if phrase_word_no + 1 == len(phrase):
                            return EqualsMatch(True, words_word_no,
                                               " ".join(phrase))
                    phrase_word_no += 1
                    words_word_no += 1

        return EqualsMatch(False, word_no)
Пример #4
0
 def equals(self, bot, client, words, word_no):
     word = words.word(word_no)
     if self._pattern_template is not None:
         template = bot.brain.regex_templates[self._pattern_template]
         if template is not None:
             result = template.match(word)
             if result is not None:
                 if logging.getLogger().isEnabledFor(logging.DEBUG):
                     logging.debug("Match word [%s] regex" % (word))
                 return EqualsMatch(True, word_no, word)
             else:
                 if logging.getLogger().isEnabledFor(logging.ERROR):
                     logging.error("No word [%s] matched refex" % (word))
                 return EqualsMatch(False, word_no)
         else:
             return EqualsMatch(False, word_no)
     else:
         result = self.pattern.match(word)
         if result is not None:
             if logging.getLogger().isEnabledFor(logging.DEBUG):
                 logging.debug("Match word [%s] regex" % (word))
             return EqualsMatch(True, word_no, word)
         else:
             if logging.getLogger().isEnabledFor(logging.ERROR):
                 logging.error("No word [%s] matched refex" % (word))
             return EqualsMatch(False, word_no)
Пример #5
0
 def equals(self, bot, clientid, words, word_no):
     word = words.word(word_no)
     if bot.brain.properties.has_property(self.property):
         if word == bot.brain.properties.property(self.property):
             if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Found word [%s] as bot property"%(word))
             return EqualsMatch(True, word_no, word)
     return EqualsMatch(False, word_no)
Пример #6
0
 def test_equals_match(self):
     equals_match = EqualsMatch(True, 1, "Hello World")
     self.assertIsNotNone(equals_match)
     self.assertTrue(equals_match.matched)
     self.assertEquals(1, equals_match.word_no)
     self.assertEquals("Hello World", equals_match.matched_phrase)
     self.assertEquals("True, 1, Hello World", equals_match.to_string())
Пример #7
0
    def equals(self, bot, client, words, word_no):
        word = words.word(word_no)

        if bot.brain.dynamics.is_dynamic_set(self._set_name) is True:
            result = bot.brain.dynamics.dynamic_set(bot, client,
                                                    self._set_name, word)
            return EqualsMatch(result, word_no, word)
        else:
            if self.set_is_known(bot):
                match = self.words_in_set(bot, words, word_no)
                if match.matched is True:
                    if logging.getLogger().isEnabledFor(logging.DEBUG):
                        logging.debug("Found word [%s] in set [%s]" %
                                      (word, self.set_name))
                    return match
                else:
                    if logging.getLogger().isEnabledFor(logging.ERROR):
                        logging.error("No word [%s] found in set [%s]" %
                                      (word, self.set_name))
                    return EqualsMatch(False, word_no)
            else:
                if logging.getLogger().isEnabledFor(logging.ERROR):
                    logging.error("No set named [%s] in sets collection" %
                                  (self.set_name))
                return EqualsMatch(False, word_no)
Пример #8
0
 def equals(self, client_context, words, word_no):
     word = words.word(word_no)
     if self._pattern_template is not None:
         template = client_context.brain.regex_templates[
             self._pattern_template]
         if template is not None:
             result = template.match(word)
             if result is not None:
                 YLogger.debug(client_context, "Match word [%s] regex",
                               word)
                 return EqualsMatch(True, word_no, word)
             else:
                 YLogger.error(client_context, "No word [%s] matched regex",
                               word)
                 return EqualsMatch(False, word_no)
         else:
             return EqualsMatch(False, word_no)
     else:
         result = self.pattern.match(word)
         if result is not None:
             YLogger.debug(client_context, "Match word [%s] regex", word)
             return EqualsMatch(True, word_no, word)
         else:
             YLogger.error(client_context, "No word [%s] matched regex",
                           word)
             return EqualsMatch(False, word_no)
Пример #9
0
 def equals(self, bot, client, words, word_no):
     word = words.word(word_no)
     for set_word in self._words:
         if word == set_word:
             logging.debug("Found word [%s] in iset" % (word))
             return EqualsMatch(True, word_no, word)
     logging.error("No word [%s] found in iset" % (word))
     return EqualsMatch(False, word_no)
Пример #10
0
    def equals(self, client_context, words, word_no):
        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        return EqualsMatch(self.equals_ignore_case(self._word, word), word_no, word)
Пример #11
0
 def equals(self, client_context, words, word_no):
     word = words.word(word_no)
     if client_context.brain.properties.has_property(self.property):
         if word == client_context.brain.properties.property(self.property):
             YLogger.debug(client_context,
                           "Found word [%s] as bot property", word)
             return EqualsMatch(True, word_no, word)
     return EqualsMatch(False, word_no)
Пример #12
0
    def words_in_set(self, client_context, words, word_no):
        set_words = client_context.brain.sets.set_list(self.set_name)
        if not set_words:
            YLogger.debug(self, "No set with name [%s]", self.set_name)
            return EqualsMatch(False, word_no)
        is_CJK = client_context.brain.sets.is_cjk(self.set_name)
        set_values = client_context.brain.sets.values(self.set_name)

        word = words.word(word_no)
        check_word = JapaneseLanguage.zenhan_normalize(word)
        word = check_word.upper()
        if is_CJK is True:
            keyword = word[0]
        else:
            keyword = word

        if keyword in set_words:
            phrases = set_words[keyword]
            phrases = sorted(phrases, key=len, reverse=True)
            for phrase in phrases:
                if is_CJK is True:
                    phrase_words = client_context.brain.tokenizer.texts_to_words(
                        phrase)
                    phrase = "".join(phrase_words)
                    phrase_text = phrase
                else:
                    phrase_text = " ".join(phrase)
                phrase_word_no = 0
                words_word_no = word_no
                while phrase_word_no < len(
                        phrase) and words_word_no < words.num_words():
                    word = words.word(words_word_no)
                    check_word = JapaneseLanguage.zenhan_normalize(word)
                    word = check_word.upper()
                    if is_CJK is True:
                        phrase_word = phrase[phrase_word_no:(phrase_word_no +
                                                             len(word))]
                        if phrase_word == word:
                            if (phrase_word_no + len(word)) == len(phrase):
                                return EqualsMatch(True, words_word_no,
                                                   set_values[phrase_text])
                        else:
                            break
                        phrase_word_no += len(word)
                    else:
                        phrase_word = phrase[phrase_word_no]
                        if phrase_word == word:
                            if phrase_word_no + 1 == len(phrase):
                                return EqualsMatch(True, words_word_no,
                                                   set_values[phrase_text])
                        else:
                            break
                        phrase_word_no += 1
                    words_word_no += 1

        return EqualsMatch(False, word_no)
Пример #13
0
 def equals(self, bot, client, words, word_no):
     word = words.word(word_no)
     for set_word in self._words:
         if word == set_word:
             if logging.getLogger().isEnabledFor(logging.DEBUG):
                 logging.debug("Found word [%s] in iset" % (word))
             return EqualsMatch(True, word_no, word)
     if logging.getLogger().isEnabledFor(logging.ERROR):
         logging.error("No word [%s] found in iset" % (word))
     return EqualsMatch(False, word_no)
Пример #14
0
    def equals(self, client_context, words, word_no):
        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        if self.priority_word == word:
            return EqualsMatch(True, word_no, word)

        return EqualsMatch(False, word_no)
Пример #15
0
    def equals(self, client_context, words, word_no):
        word = words.word(word_no)
        if word is not None:
            word = word.upper()
            for set_word in self._words:
                if word == set_word:
                    YLogger.debug(client_context, "Found word [%s] in iset",
                                  word)
                    return EqualsMatch(True, word_no, word)

        YLogger.error(client_context, "No word [%s] found in iset", word)
        return EqualsMatch(False, word_no)
Пример #16
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is True and client_context.match_type == Match.WORD:
            return EqualsMatch(False, word_no)

        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        return EqualsMatch(self.equals_ignore_case(self._word, word), word_no,
                           word)
Пример #17
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is True:
            return EqualsMatch(False, word_no)

        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        if self.equals_ignore_case(self.priority_word, word):
            return EqualsMatch(True, word_no, word)

        return EqualsMatch(False, word_no)
Пример #18
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is True:
            return EqualsMatch(False, word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        word = words.word(word_no)
        if word is not None:
            match = self.words_in_set(client_context, words, word_no)
            if match.matched is True:
                YLogger.debug(client_context, "Found word [%s] in iset", word)
                return match

        YLogger.debug(client_context, "No word [%s] found in iset", word)
        return EqualsMatch(False, word_no)
Пример #19
0
    def equals(self, bot, client, words, word_no):
        word = words.word(word_no)

        if self.set_is_numeric():
            return EqualsMatch(word.isnumeric(), word_no, word)
        elif self.set_is_known(bot):
            match = self.words_in_set(bot, words, word_no)
            if match.matched is True:
                logging.debug("Found word [%s] in set [%s]" %
                              (word, self.set_name))
                return match
            else:
                logging.error("No word [%s] found in set [%s]" %
                              (word, self.set_name))
                return EqualsMatch(False, word_no)
        else:
            logging.error("No set named [%s] in sets collection" %
                          (self.set_name))
            return EqualsMatch(False, word_no)
Пример #20
0
    def words_in_set(self, bot, words, word_no):

        word = words.word(word_no).upper()
        set_words = bot.brain.sets.set(self.set_name)

        if word in set_words:
            phrases = set_words[word]
            phrases = sorted(phrases, key=len, reverse=True)
            for phrase in phrases:
                phrase_word_no = 0
                words_word_no = word_no
                while phrase_word_no < len(phrase) and words_word_no < words.num_words():
                    phrase_word = phrase[phrase_word_no].upper()
                    word = words.word(words_word_no).upper()
                    if phrase_word == word:
                        if phrase_word_no+1 == len(phrase):
                            return EqualsMatch(True, words_word_no, " ".join(phrase))
                    phrase_word_no += 1
                    words_word_no += 1

        return EqualsMatch(False, word_no)
Пример #21
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is True:
            return EqualsMatch(False, word_no)

        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        if client_context.brain.properties.has_property(self.property):
            value = client_context.brain.properties.property(self.property)
            value_len = len(value)
            word_len = len(word)
            texts = word
            add_count = 0
            if value_len > word_len:
                texts_len = word_len
                check_index = 0
                for word in words.words:
                    if check_index <= word_no:
                        check_index += 1
                        continue
                    if word == '__TOPIC__':
                        break
                    texts_len += len(word)
                    texts += word
                    add_count += 1
                    if texts_len >= value_len:
                        break

            check_texts = JapaneseLanguage.zenhan_normalize(texts)
            value_texts = JapaneseLanguage.zenhan_normalize(value)
            if check_texts.upper() == value_texts.upper():
                word_no += add_count
                YLogger.debug(client_context,
                              "Found words [%s] as bot property", value)
                return EqualsMatch(True, word_no, value)

        return EqualsMatch(False, word_no)
Пример #22
0
 def equals(self, client_context, words, word_no):
     word = words.word(word_no)
     return EqualsMatch(
         self.equals_ignore_case(client_context, self._word, word), word_no,
         word)
Пример #23
0
 def equals(self, bot, clientid, words, word_no):
     word = words.word(word_no)
     if self.priority_word == word:
         return EqualsMatch(True, word_no, word)
     return EqualsMatch(False, word_no)
Пример #24
0
 def equals(self, client_context, words, word_no):
     return EqualsMatch(False, word_no)
Пример #25
0
 def equals(self, bot, clientid, words, word_no):
     return EqualsMatch(False, word_no)
Пример #26
0
 def equals(self, bot, clientid, words, word_no):
     word = words.word(word_no)
     return EqualsMatch(
         self.equals_ignore_case(bot, clientid, self._word, word), word_no,
         word)
Пример #27
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is False:
            return EqualsMatch(False, word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        conversation = client_context.bot.get_conversation(client_context)
        try:
            quetion = conversation.current_question()
            if quetion is None:
                return EqualsMatch(False, word_no)

            json_dict = quetion.property("__SYSTEM_NLUDATA__")
            if json_dict is None or json_dict == '':
                return EqualsMatch(False, word_no)
            else:
                json_dict = json.loads(json_dict)
        except Exception as e:
            YLogger.exception(self, "PatternNluNode failed to load keys", e)
            return EqualsMatch(False, word_no)

        if self._intent == "*":
            return EqualsMatch(True, word_no, self._intent)

        try:
            intents = json_dict["intents"]
        except Exception as e:
            YLogger.exception(self, "PatternNluNode failed to load JSON", e)
            return EqualsMatch(False, word_no)

        score = None
        try:
            intentList = [intent.get("intent") for intent in intents]
            scoreList = [score.get("score") for score in intents]

            matchIntentIndex = intentList.index(self._intent)
            maxScore = max(scoreList)

            if scoreList[matchIntentIndex] == maxScore:
                score = scoreList[matchIntentIndex]
            elif self._maxLikelihood is not True:
                score = scoreList[matchIntentIndex]

            if score is not None:
                if self._score is None:
                    YLogger.debug(client_context, "PatternNluNode [%s] resolved", self._intent)
                    return EqualsMatch(True, word_no, self._intent)

                if self._scoreOperator(score, self._score):
                    YLogger.debug(client_context, "PatternNluNode [%s] resolved to [%f] %s [%f]", self._intent, score, self.scoreName(), self._score)
                    return EqualsMatch(True, word_no, "__INTENT__"+self._intent)
        except Exception:
            YLogger.debug(client_context, "PatternNluNode failed to get intent[%s]", self._intent)
            return EqualsMatch(False, word_no)

        return EqualsMatch(False, word_no)
Пример #28
0
    def equals(self, client_context, words, word_no):
        if client_context.match_nlu is True:
            return EqualsMatch(False, word_no)

        word = words.word(word_no)

        if self.userid != '*':
            if self.userid != client_context.userid:
                return EqualsMatch(False, word_no)

        if self._pattern_template is not None:
            template = client_context.brain.regex_templates.regex(self._pattern_template)
            if template is not None:
                result = template.match(word)
                if result is not None:
                    YLogger.debug(client_context, "Match word [%s] regex", word)
                    return EqualsMatch(True, word_no, word)
                else:
                    YLogger.debug(client_context, "No word [%s] matched regex", word)
                    return EqualsMatch(False, word_no)
            else:
                return EqualsMatch(False, word_no)
        else:
            if self._is_form is False:
                result = self.pattern.match(word)
                if result is not None:
                    YLogger.debug(client_context, "Match word [%s] regex", word)
                    return EqualsMatch(True, word_no, word)
                else:
                    YLogger.debug(client_context, "No word [%s] matched regex", word)
                    return EqualsMatch(False, word_no)
            else:
                check_words = []
                check_index = 0
                for word in words.words:
                    if word_no > check_index:
                        check_index += 1
                        continue
                    if word == '__TOPIC__':
                        break
                    check_words.append(word)
                if len(check_words) > 0:
                    words_text = client_context.brain.tokenizer.words_to_texts(check_words)
                    result = self.pattern.match(words_text)
                    if result is not None:
                        if self._is_NotOption is False:
                            matchlen = result.end()
                        else:
                            matchlen = result.endpos
                        checklen = 0
                        matchwords = []
                        check_no = word_no
                        for word in check_words:
                            checklen += len(word)
                            if checklen >= matchlen:
                                overlen = checklen - matchlen
                                if overlen == 0:
                                    matchwords.append(word)
                                else:
                                    words.divide_word(check_no, (len(word) - overlen))
                                    matchwords.append(words.words[check_no])
                                break
                            matchwords.append(word)
                            check_no += 1
                            if words_text[checklen] == ' ':
                                checklen += 1
                        words_text = client_context.brain.tokenizer.words_to_texts(matchwords)
                        YLogger.debug(client_context, "Match words [%s] regex", words_text)
                        return EqualsMatch(True, check_no, words_text)

                YLogger.debug(client_context, "No words [%s] matched regex", word)
                return EqualsMatch(False, word_no)