Example #1
0
 def test_nice_date_time(self):
     for lang in self.test_config:
         set_default_lf_lang(lang)
         i = 1
         while (self.test_config[lang].get('test_nice_date_time') and
                self.test_config[lang]['test_nice_date_time'].get(str(i))):
             p = self.test_config[lang]['test_nice_date_time'][str(i)]
             dp = ast.literal_eval(p['datetime_param'])
             np = ast.literal_eval(p['now'])
             dt = datetime.datetime(dp[0], dp[1], dp[2], dp[3], dp[4],
                                    dp[5])
             now = None if not np else datetime.datetime(
                 np[0], np[1], np[2], np[3], np[4], np[5])
             print('Testing for ' + lang + ' that ' + str(dt) +
                   ' is date time ' + p['assertEqual'])
             self.assertEqual(
                 p['assertEqual'],
                 nice_date_time(dt,
                                lang=lang,
                                now=now,
                                use_24hour=ast.literal_eval(
                                    p['use_24hour']),
                                use_ampm=ast.literal_eval(p['use_ampm'])))
             i = i + 1
     set_default_lf_lang(default_lang)
Example #2
0
    def test_nice_year(self):
        for lang in self.test_config:
            set_default_lf_lang(lang)
            i = 1
            while (self.test_config[lang].get('test_nice_year')
                   and self.test_config[lang]['test_nice_year'].get(str(i))):
                p = self.test_config[lang]['test_nice_year'][str(i)]
                dp = ast.literal_eval(p['datetime_param'])
                dt = datetime.datetime(dp[0], dp[1], dp[2], dp[3], dp[4],
                                       dp[5])
                print('Testing for ' + lang + ' that ' + str(dt) +
                      ' is year ' + p['assertEqual'])
                self.assertEqual(
                    p['assertEqual'],
                    nice_year(dt, lang=lang, bc=ast.literal_eval(p['bc'])))
                i = i + 1
        set_default_lf_lang(default_lang)

        # Test all years from 0 to 9999 for all languages,
        # that some output is produced
        for lang in self.test_config:
            set_default_lf_lang(lang)
            print("Test all years in " + lang)
            for i in range(1, 9999):
                dt = datetime.datetime(i, 1, 31, 13, 2, 3)
                self.assertTrue(len(nice_year(dt, lang=lang)) > 0)
                # Looking through the date sequence can be helpful
                # print(nice_year(dt, lang=lang))
        set_default_lf_lang(default_lang)
Example #3
0
    def test_nice_date(self):
        for lang in self.test_config:
            set_default_lf_lang(lang)
            i = 1
            while (self.test_config[lang].get('test_nice_date')
                   and self.test_config[lang]['test_nice_date'].get(str(i))):
                p = self.test_config[lang]['test_nice_date'][str(i)]
                dp = ast.literal_eval(p['datetime_param'])
                np = ast.literal_eval(p['now'])
                dt = datetime.datetime(dp[0], dp[1], dp[2], dp[3], dp[4],
                                       dp[5])
                now = None if not np else datetime.datetime(
                    np[0], np[1], np[2], np[3], np[4], np[5])
                print('Testing for ' + lang + ' that ' + str(dt) +
                      ' is date ' + p['assertEqual'])
                self.assertEqual(p['assertEqual'],
                                 nice_date(dt, lang=lang, now=now))
                i = i + 1

        # test all days in a year for all languages,
        # that some output is produced
        for lang in self.test_config:
            set_default_lf_lang(lang)
            for dt in (datetime.datetime(2017, 12, 30, 0, 2, 3) +
                       datetime.timedelta(n) for n in range(368)):
                self.assertTrue(len(nice_date(dt, lang=lang)) > 0)
        set_default_lf_lang(default_lang)
Example #4
0
    def handle_utterance(self, message):
        """Main entrypoint for handling user utterances with Mycroft skills

        Monitor the messagebus for 'recognizer_loop:utterance', typically
        generated by a spoken interaction but potentially also from a CLI
        or other method of injecting a 'user utterance' into the system.

        Utterances then work through this sequence to be handled:
        1) Active skills attempt to handle using converse()
        2) Padatious high match intents (conf > 0.95)
        3) Adapt intent handlers
        5) High Priority Fallbacks
        6) Padatious near match intents (conf > 0.8)
        7) General Fallbacks
        8) Padatious loose match intents (conf > 0.5)
        9) Catch all fallbacks including Unknown intent handler

        If all these fail the complete_intent_failure message will be sent
        and a generic info of the failure will be spoken.

        Args:
            message (Message): The messagebus data
        """
        try:
            lang = _get_message_lang(message)
            set_default_lf_lang(lang)

            utterances = message.data.get('utterances', [])
            combined = _normalize_all_utterances(utterances)

            stopwatch = Stopwatch()

            # Create matchers
            padatious_matcher = PadatiousMatcher(self.padatious_service)

            # List of functions to use to match the utterance with intent.
            # These are listed in priority order.
            match_funcs = [
                self._converse, padatious_matcher.match_high,
                self.adapt_service.match_intent, self.fallback.high_prio,
                padatious_matcher.match_medium, self.fallback.medium_prio,
                padatious_matcher.match_low, self.fallback.low_prio
            ]

            match = None
            with stopwatch:
                # Loop through the matching functions until a match is found.
                for match_func in match_funcs:
                    match = match_func(combined, lang, message)
                    if match:
                        break
            if match:
                if match.skill_id:
                    self.add_active_skill(match.skill_id)
                    # If the service didn't report back the skill_id it
                    # takes on the responsibility of making the skill "active"

                # Launch skill if not handled by the match function
                if match.intent_type:
                    reply = message.reply(match.intent_type, match.intent_data)
                    # Add back original list of utterances for intent handlers
                    # match.intent_data only includes the utterance with the
                    # highest confidence.
                    reply.data["utterances"] = utterances
                    self.bus.emit(reply)

            else:
                # Nothing was able to handle the intent
                # Ask politely for forgiveness for failing in this vital task
                self.send_complete_intent_failure(message)
            self.send_metrics(match, message.context, stopwatch)
        except Exception as err:
            LOG.exception(err)
Example #5
0
 def reset_converse(self, message):
     """Let skills know there was a problem with speech recognition"""
     lang = _get_message_lang(message)
     set_default_lf_lang(lang)
     for skill in copy(self.active_skills):
         self.do_converse(None, skill[0], lang, message)