def user_maybe_wants_to_talk_about_particular_game_request(ngrams, vars): user_uttr = state_utils.get_last_human_utterance(vars) user_text = user_uttr.get("text", "").lower() bot_text = state_utils.get_last_bot_utterance(vars).get("text", "").lower() game_names_from_local_list_of_games = find_games_in_text( user_text) + find_games_in_text(bot_text) if game_names_from_local_list_of_games: if does_text_contain_link_to_gaming(bot_text): logger.info("performing additional check") flag = False elif common_intents.switch_to_particular_game_discussion(vars): assert ( game_names_from_local_list_of_games ), "At least one game should have been found in function `switch_to_particular_game_discussion()`" possible_game_name = game_names_from_local_list_of_games[0][0] flag = (not any([ n.lower() in possible_game_name.lower() for n in WORDS_THAT_ARE_DEFINITELY_GAME_NAMES ]) and not does_text_contain_video_game_words( user_text ) and not does_text_contain_link_to_gaming(bot_text) and ( not was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars) or was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars) and if_chat_about_particular_topic( user_uttr, compiled_pattern= GAMES_WITH_AT_LEAST_1M_COPIES_SOLD_COMPILED_PATTERN))) else: flag = False else: flag = False logger.info( f"user_maybe_wants_to_talk_about_particular_game_request={flag}") return flag
def switch_to_particular_game_discussion(vars): user_uttr = state_utils.get_last_human_utterance(vars) user_text = user_uttr.get("text", "").lower() prev_bot_uttr = state_utils.get_last_bot_utterance(vars) prev_bot_text = prev_bot_uttr.get("text", "") found_video_game_in_user_uttr = find_games_in_text(user_text) logger.info( f"(switch_to_particular_game_discussion)found_video_game_in_user_uttr: {found_video_game_in_user_uttr}" ) found_video_game_in_user_uttr = bool(found_video_game_in_user_uttr) found_video_game_in_bot_uttr = find_games_in_text(prev_bot_text) logger.info( f"(switch_to_particular_game_discussion)found_video_game_in_bot_uttr: {found_video_game_in_bot_uttr}" ) found_video_game_in_bot_uttr = bool(found_video_game_in_bot_uttr) choose_particular_game = if_choose_topic( user_uttr, prev_bot_uttr) and found_video_game_in_user_uttr question_answer_contains_video_game = ("?" not in user_text and "?" in prev_bot_text and found_video_game_in_user_uttr) bot_asked_about_game_and_user_answered_yes = (found_video_game_in_bot_uttr and "?" in prev_bot_text and is_yes(user_uttr)) flag = (lets_talk_about( vars, GAMES_WITH_AT_LEAST_1M_COPIES_SOLD_COMPILED_PATTERN) or choose_particular_game or question_answer_contains_video_game or bot_asked_about_game_and_user_answered_yes) logger.info(f"switch_to_particular_game_discussion={flag}") return flag
def user_mentioned_games_as_his_interest_request(ngrams, vars, first_time=True): user_uttr = state_utils.get_last_human_utterance(vars) user_text = user_uttr.get("text", "").lower() bot_text = state_utils.get_last_bot_utterance(vars).get("text", "").lower() game_names_from_local_list_of_games = find_games_in_text( user_text) + find_games_in_text(bot_text) flag = ( not game_names_from_local_list_of_games and common_intents.switch_to_general_gaming_discussion(vars) and not user_doesnt_like_gaming_request(ngrams, vars) and not user_didnt_name_game_after_question_about_games_and_didnt_refuse_to_discuss_request( ngrams, vars) and (first_time and ANSWER_TO_GENERAL_WISH_TO_DISCUSS_VIDEO_GAMES_AND_QUESTION_WHAT_GAME_YOU_PLAY not in bot_text or not first_time and ANSWER_TO_GENERAL_WISH_TO_DISCUSS_VIDEO_GAMES_AND_QUESTION_WHAT_GAME_YOU_PLAY in bot_text) and (not was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars) or was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars) and if_chat_about_particular_topic( user_uttr, compiled_pattern=VIDEO_GAME_WORDS_COMPILED_PATTERN))) logger.info(f"user_mentioned_games_as_his_interest_request={flag}") return flag
def user_doesnt_like_gaming_request(ngrams, vars): user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) bot_text = bot_uttr.get("text", "").lower() found_game_name = bool(find_games_in_text(user_uttr.get("text", ""))) flag = ( is_no(user_uttr) and not found_game_name and (does_text_contain_link_to_gaming(bot_text) or common_intents.is_question_about_games(bot_text)) and not was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars)) logger.info(f"user_doesnt_like_gaming_request={flag}") return flag
def user_wants_to_talk_about_minecraft_request(ngrams, vars): if common_intents.switch_to_particular_game_discussion(vars): user_uttr = state_utils.get_last_human_utterance(vars) prev_bot_uttr = state_utils.get_last_bot_utterance(vars) game_names_from_local_list_of_games = find_games_in_text( user_uttr.get("text", "")) if is_yes(user_uttr): game_names_from_local_list_of_games += find_games_in_text( prev_bot_uttr.get("text", "")) logger.info( f"(user_wants_to_talk_about_minecraft_request)game_names_from_local_list_of_games: " f"{game_names_from_local_list_of_games}") assert ( game_names_from_local_list_of_games ), "At least one game should have been found in function `switch_to_particular_game_discussion()`" flag = any([ "minecraft" in gn[0].lower() for gn in game_names_from_local_list_of_games ]) else: flag = False logger.info(f"user_wants_to_talk_about_minecraft_request={flag}") return flag
def user_definitely_wants_to_talk_about_game_that_user_played_request( ngrams, vars, additional_check=None): user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) game_names_from_local_list_of_games = find_games_in_text( user_uttr.get("text", "")) flag = ( bool(game_names_from_local_list_of_games) and does_text_contain_link_to_gaming(bot_uttr.get("text", "")) and additional_check(ngrams, vars) and (not was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars) or was_link_from_gaming_to_other_skill_made_in_previous_bot_utterance( vars) and if_chat_about_particular_topic( user_uttr, compiled_pattern= GAMES_WITH_AT_LEAST_1M_COPIES_SOLD_COMPILED_PATTERN))) logger.info( f"user_definitely_wants_to_talk_about_game_that_user_played_and_bot_didnt_play with additional check " f"{common_intents.get_additional_check_description(additional_check)}: {flag}" ) return flag
def respond(): dialogs_batch = [None] st_time = time.time() dialogs_batch = request.json["dialogs"] rand_seed = request.json.get("rand_seed") responses = [] for dialog in dialogs_batch: prev_skill_outputs = get_skill_outputs_from_dialog( dialog["utterances"][-MEMORY_LENGTH:], "game_cooperative_skill", activated=True ) is_active_last_answer = bool(prev_skill_outputs) human_attr = dialog["human"]["attributes"] prev_state = human_attr.get("game_cooperative_skill", {}).get("state", {}) try: state = copy.deepcopy(prev_state) if state and not is_active_last_answer: state["messages"] = [] # pre_len = len(state.get("messages", [])) last_utter = dialog["human_utterances"][-1] last_utter_text = last_utter["text"].lower() agent_intents = get_agent_intents(last_utter) # for tests attr = {} if rand_seed: random.seed(int(rand_seed)) response, state = skill([last_utter_text], state, agent_intents) # logger.info(f"state = {state}") # logger.info(f"last_utter_text = {last_utter_text}") # logger.info(f"response = {response}") bot_utterance = dialog["bot_utterances"][-1] if dialog["bot_utterances"] else {} text = response.get("text", "Sorry") if not response.get("confidence"): confidence = 0 elif ( not is_active_last_answer and if_chat_about_particular_topic( dialog["human_utterances"][-1], bot_utterance, compiled_pattern=GAMES_COMPILED_PATTERN, ) and find_games_in_text(last_utter_text) ): confidence = 0 elif not is_active_last_answer and if_chat_about_particular_topic( dialog["human_utterances"][-1], bot_utterance, compiled_pattern=GAMES_COMPILED_PATTERN, ): confidence = 1 elif is_active_last_answer: confidence = 1 elif is_yes(dialog["human_utterances"][-1]) and game_skill_was_proposed(bot_utterance): confidence = 1 elif not is_yes(dialog["human_utterances"][-1]) and game_skill_was_proposed(bot_utterance): confidence = 0.95 text = FALLBACK_ACKN_TEXT state = prev_state elif GAMES_COMPILED_PATTERN.search(last_utter_text) and not is_active_last_answer: confidence = 0.98 else: confidence = 0 curr_text = clean_text(text.lower()) last_text = clean_text(bot_utterance.get("text", "").lower()) ratio = difflib.SequenceMatcher(None, curr_text.split(), last_text.split()).ratio() if ratio > 0.95: confidence = 0 if confidence == 1: can_continue = MUST_CONTINUE elif confidence > 0.95: can_continue = CAN_CONTINUE_SCENARIO else: can_continue = CAN_NOT_CONTINUE human_attr["game_cooperative_skill"] = {"state": state} attr["can_continue"] = can_continue except Exception as exc: sentry_sdk.capture_exception(exc) logger.exception(exc) text = "" confidence = 0.0 human_attr["game_cooperative_skill"] = {"state": prev_state} attr = {} bot_attr = {} responses.append((text, confidence, human_attr, bot_attr, attr)) total_time = time.time() - st_time logger.info(f"game_cooperative_skill exec time = {total_time:.3f}s") return jsonify(responses)