def run(self, payload, user): callback_id = user.message.callback_id if user.behaviors.has_callback(callback_id): params = {log_const.KEY_NAME: "handling_respond"} log("HandlerRespond started", user, params) action_params = user.behaviors.get_callback_action_params( callback_id) if action_params: app_info = None for original_message_name in [ MESSAGE_TO_SKILL, SERVER_ACTION, RUN_APP ]: if original_message_name in action_params: app_info = AppInfo( action_params[original_message_name].get( APP_INFO, {})) break smart_kit_metrics.counter_incoming(self.app_name, user.message.message_name, self.__class__.__name__, user, app_info=app_info) text_preprocessing_result = TextPreprocessingResult( payload.get("message", {})) params = { log_const.KEY_NAME: log_const.NORMALIZED_TEXT_VALUE, "normalized_text": str(text_preprocessing_result.raw), } log("text preprocessing result: '%(normalized_text)s'", user, params) action_name = self.get_action_name(payload, user) action = user.descriptions["external_actions"][action_name] action_params = self.get_action_params(payload) return action.run(user, text_preprocessing_result, action_params)
def run(self, payload, user): callback_id = user.message.callback_id action_params = self.get_action_params(payload, user) action_name = self.get_action_name(payload, user) params = { log_const.KEY_NAME: "process_time", "callback_id": str(callback_id), "process_time": self.get_processing_time(user), "action_name": action_name } if user.behaviors.has_callback(callback_id): action_params = self.get_action_params(payload, user) params["to_message_name"] = action_params.get(TO_MESSAGE_NAME) log("HandlerRespond with action %(action_name)s started respond on %(to_message_name)s", user, params) else: log("HandlerRespond with action %(action_name)s started without callback", user, params) smart_kit_metrics.counter_incoming(self.app_name, user.message.message_name, self.__class__.__name__, user) text_preprocessing_result = TextPreprocessingResult(payload.get("message", {})) if payload.get("message"): params = { log_const.KEY_NAME: log_const.NORMALIZED_TEXT_VALUE, "normalized_text": str(text_preprocessing_result.raw), } log("text preprocessing result: '%(normalized_text)s'", user, params, level="DEBUG") action = user.descriptions["external_actions"][action_name] return action.run(user, text_preprocessing_result, action_params)
def success(self, callback_id: str): log(f"behavior.success started: got callback %({log_const.BEHAVIOR_CALLBACK_ID_VALUE})s.", self._user, params={ log_const.KEY_NAME: log_const.BEHAVIOR_SUCCESS_VALUE, log_const.BEHAVIOR_CALLBACK_ID_VALUE: callback_id }) callback = self._get_callback(callback_id) result = None if callback: self._check_hostname(callback_id, callback) self._add_returned_callback(callback_id) behavior = self.descriptions[callback.behavior_id] callback_action_params = callback.action_params self._log_callback( callback_id, "behavior_success", smart_kit_metrics.counter_behavior_success, "success", callback_action_params, ) text_preprocessing_result = TextPreprocessingResult( callback.text_preprocessing_result) result = behavior.success_action.run(self._user, text_preprocessing_result, callback_action_params) self._delete(callback_id) return result
def _try_extract_last_messages(self, user, params): processed_items = user.preprocessing_messages_for_scenarios.processed_items count = self.count - 1 if self.count else len(processed_items) for preprocessing_result_raw in islice(processed_items, 0, count): preprocessing_result = TextPreprocessingResult( preprocessing_result_raw) result = self.filler.extract(preprocessing_result, user, params) if result is not None: return result
def run(self, payload, user): super().run(payload, user) text_preprocessing_result = TextPreprocessingResult( payload.get("message", {})) params = {log_const.KEY_NAME: "HandlerCloseApp"} self._clear_current_scenario.run(user, text_preprocessing_result) if payload.get("message"): params["tpr_str"] = str(text_preprocessing_result.raw) log("HandlerCloseApp with text preprocessing result", user, params)
def classifiers_initial_launch(classifiers: Dict[str, Any]) -> None: # external классификаторы должны запускаться последними type_order = [_type for _type in CLASSIFIER_TYPES_MAP] sorted_cls_by_type_order = OrderedDict( sorted(classifiers.items(), key=lambda i: type_order.index(i[1]["type"]))) text_preprocessing_result = TextPreprocessingResult({}) for cls_name, cls_settings in sorted_cls_by_type_order.items(): cls = CLASSIFIER_TYPES_MAP[cls_settings["type"]](cls_settings) cls.initial_launch(text_preprocessing_result, sorted_cls_by_type_order) sorted_cls_by_type_order[cls_name] = cls
def run(self, payload, user): super().run(payload, user) text_preprocessing_result = TextPreprocessingResult( payload.get("message", {})) log( "text preprocessing result: '{}'".format( str(text_preprocessing_result.raw)), user, {log_const.KEY_NAME: log_const.NORMALIZED_TEXT_VALUE}) answer = self._handle_base(text_preprocessing_result, user) return answer
def __init__(self, items: Optional[Dict[str, Any]], id: Optional[str] = None) -> None: super(ApproveFiller, self).__init__(items, id) from smart_kit.configs import get_app_config app_config = get_app_config() self.yes_words = items.get("yes_words") self.no_words = items.get("no_words") self.set_yes_words: Set = set(self.yes_words or []) self.set_no_words: Set = set(self.no_words or []) self.yes_words_normalized: Set = { TextPreprocessingResult(result).tokenized_string for result in app_config.NORMALIZER.normalize_sequence( self.set_yes_words) } self.no_words_normalized: Set = { TextPreprocessingResult(result).tokenized_string for result in app_config.NORMALIZER.normalize_sequence( self.set_no_words) }
def run(self, payload, user): super().run(payload, user) text_preprocessing_result = TextPreprocessingResult(payload["message"]) params = { log_const.KEY_NAME: log_const.NORMALIZED_TEXT_VALUE, "normalized_text": str(text_preprocessing_result.raw), } log("text preprocessing result: '%(normalized_text)s'", user, params) answer = self._handle_base(text_preprocessing_result, user) return answer
def test_1(self, mock_get_app_config): patch_get_app_config(mock_get_app_config) items = { 'yes_words': ['да', 'конечно', 'давай', 'ага', 'хорошо', 'именно', 'можно'], 'no_words': ['нет', 'не', 'ни', 'нельзя', 'отнюдь', 'нету'] } filler = ApproveRawTextFiller(items) normalizer = LocalTextNormalizer() user_phrase = 'конечно' text_pre_result = TextPreprocessingResult(normalizer(user_phrase)) result = filler.extract(text_pre_result, None) self.assertTrue(result) user_phrase = 'да нет' text_pre_result = TextPreprocessingResult(normalizer(user_phrase)) result = filler.extract(text_pre_result, None) self.assertFalse(result) user_phrase = 'даю' text_pre_result = TextPreprocessingResult(normalizer(user_phrase)) result = filler.extract(text_pre_result, None) self.assertIsNone(result)
def misstate(self, callback_id: str): log(f"behaviors.misstate started: got callback %({log_const.BEHAVIOUR_CALLBACK_ID_VALUE})s.", self._user, params={log_const.KEY_NAME: log_const.BEHAVIOUR_MISSTATE_VALUE, log_const.BEHAVIOUR_CALLBACK_ID_VALUE: callback_id}) callback = self._get_callback(callback_id) result = None if callback: self._add_returned_callback(callback_id) behavior = self.descriptions[callback.behavior_id] callback_action_params = callback.action_params self._log_callback(callback_id, "SmartKitBehaviors_misstate", smart_kit_metrics.counter_behavior_misstate, "misstate", callback_action_params) text_preprocessing_result = TextPreprocessingResult(callback.text_preprocessing_result) result = behavior.misstate_action.run(self._user, text_preprocessing_result, callback_action_params) self._delete(callback_id) return result
def run(self, payload, user): action_params = pickle_deepcopy(self.get_action_params(payload)) params = { log_const.KEY_NAME: "handling_server_action", "server_action_params": str(action_params), "server_action_id": self.get_action_name(payload, user) } log("HandlerServerAction %(server_action_id)s started", user, params) app_info = user.message.app_info smart_kit_metrics.counter_incoming(self.app_name, user.message.message_name, self.__class__.__name__, user, app_info=app_info) action_id = self.get_action_name(payload, user) action = user.descriptions["external_actions"][action_id] return action.run(user, TextPreprocessingResult({}), action_params)
def timeout(self, callback_id: str): log(f"behavior.timeout started: got callback %({log_const.BEHAVIOR_CALLBACK_ID_VALUE})s.", self._user, params={ log_const.KEY_NAME: log_const.BEHAVIOR_TIMEOUT_VALUE, log_const.BEHAVIOR_CALLBACK_ID_VALUE: callback_id }) callback = self._get_callback(callback_id) result = None if callback: self._add_returned_callback(callback_id) behavior = self.descriptions[callback.behavior_id] callback_action_params = callback.action_params self._log_callback(callback_id, "behavior_timeout", smart_kit_metrics.counter_behavior_timeout, "timeout", callback_action_params) text_preprocessing_result = TextPreprocessingResult( callback.text_preprocessing_result) result = behavior.timeout_action.run(self._user, text_preprocessing_result, callback_action_params) self._delete(callback_id) return result
def _handle_base(self, user): answer, is_answer_found = self.dialogue_manager.run( TextPreprocessingResult({}), user) return answer or []