def start_talk_request(ngrams, vars): flag = False dialog = vars["agent"]["dialog"] chosen_title, chosen_page_title = "", "" all_titles = [] bot_uttr = state_utils.get_last_bot_utterance(vars) prev_skill = bot_uttr.get("active_skill", "") if prev_skill != "dff_wiki_skill": found_entity_substr, found_entity_id, found_entity_types, found_page_title, _ = continue_after_topic_skill( dialog) if found_entity_substr and found_page_title: page_content, _ = get_page_content(found_page_title) chosen_title, chosen_page_title = get_title_info( vars, found_entity_substr, found_entity_types, "", [], page_content) _, _, all_titles = get_titles(found_entity_substr, found_entity_types, page_content) logger.info( f"start_talk_request, found_entity_substr {found_entity_substr} found_entity_id {found_entity_id} " f"found_entity_types {found_entity_types} found_page_title {found_page_title} " f"chosen_title {chosen_title}") user_uttr = state_utils.get_last_human_utterance(vars) isno = is_no(state_utils.get_last_human_utterance(vars)) if chosen_title: flag = True if (user_uttr["text"].endswith("?") and another_topic_question(vars, all_titles)) or isno: flag = False logger.info(f"start_talk_request={flag}") return flag
def wikihow_step_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) isyes = is_yes(state_utils.get_last_human_utterance(vars)) shared_memory = state_utils.get_shared_memory(vars) wikihow_article = shared_memory.get("wikihow_article", "") prev_wikihow_title = shared_memory.get("prev_wikihow_title", "") used_wikihow_titles = set(shared_memory.get("used_wikihow_titles", [])) logger.info( f"wikihow_step_request, prev_wikihow_title {prev_wikihow_title} used_wikihow_titles " f"{used_wikihow_titles}") found_title = "" if wikihow_article: article_content = get_wikihow_content(wikihow_article) if article_content: all_page_titles = article_content.keys() for title in all_page_titles: if title not in used_wikihow_titles: found_title = title break further = re.findall(r"(more|further|continue|follow)", user_uttr["text"], re.IGNORECASE) if found_title or prev_wikihow_title: flag = True if prev_wikihow_title and not (isyes or further): flag = False logger.info(f"wikihow_step_request={flag}") return flag
def animal_questions_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) annotations = state_utils.get_last_human_utterance(vars)["annotations"] found_animal = find_entity_by_types(annotations, {"Q55983715", "Q16521"}) found_animal_cnet = find_entity_conceptnet(annotations, ["animal"]) found_animal_in_list = find_in_animals_list(annotations) shared_memory = state_utils.get_shared_memory(vars) users_wild_animal = shared_memory.get("users_wild_animal", "") found_pet = re.findall(PETS_TEMPLATE, user_uttr["text"]) found_bird = re.findall(r"(\bbird\b|\bbirds\b)", user_uttr["text"]) used_wild_q = shared_memory.get("used_wild_q", []) all_facts_used = len(used_wild_q) == len(WILD_ANIMALS_Q) if ( not found_pet and ( found_bird or users_wild_animal or (found_animal and found_animal not in ANIMAL_BADLIST) or found_animal_in_list or found_animal_cnet ) and not all_facts_used ): flag = True logger.info(f"animal_questions_request, found_animal {found_animal} users_wild_animal {users_wild_animal}") logger.info(f"animal_questions_request={flag}") return flag
def no_requests(vars): """Function to determine if - user didn't asked to switch topic, - user didn't ask to talk about something particular, - user didn't requested high priority intents (like what_is_your_name) - user didn't requested any special intents - user didn't ask questions """ contain_no_special_requests = no_special_switch_off_requests(vars) request_intents = [ "opinion_request", "topic_switching", "lets_chat_about", "what_are_you_talking_about", "Information_RequestIntent", "Topic_SwitchIntent", "Opinion_RequestIntent", ] intents = common_utils.get_intents( state_utils.get_last_human_utterance(vars), which="all") is_not_request_intent = all( [intent not in request_intents for intent in intents]) is_no_question = "?" not in state_utils.get_last_human_utterance( vars)["text"] if contain_no_special_requests and is_not_request_intent and is_no_question: return True return False
def news_step_request(ngrams, vars): flag = False shared_memory = state_utils.get_shared_memory(vars) started_news = shared_memory.get("started_news", "") user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) isno = is_no(state_utils.get_last_human_utterance(vars)) if_switch = switch_wiki_skill_on_news(user_uttr, bot_uttr) news_memory = shared_memory.get("news_memory", []) cur_news_title = shared_memory.get("news_title", "") found_not_used_content = False if cur_news_title: title_num = -1 for n, elem in enumerate(news_memory): if elem["title"] == cur_news_title: for sentence_num, (sentence, used_sent) in enumerate(elem["content"]): if not used_sent: found_not_used_content = True title_num = n if not found_not_used_content and -1 < title_num < len(news_memory) - 1: found_not_used_content = True logger.info( f"news_step_request, started_news {started_news} if_switch {if_switch} " f"cur_news_title {cur_news_title} found_not_used_content {found_not_used_content}" ) if (not started_news and if_switch) or (started_news and cur_news_title and found_not_used_content): flag = True if isno or "?" in user_uttr["text"]: flag = False logger.info(f"news_step_request={flag}") return flag
def retrieve_and_save_name(vars): user_text = state_utils.get_last_human_utterance(vars)["text"] shared_memory = state_utils.get_shared_memory(vars) annotations = state_utils.get_last_human_utterance(vars)["annotations"] ner = annotations.get("ner", []) users_pet_breed = shared_memory.get("users_pet_breed", "") found_name = "" for entities in ner: if entities: for entity in entities: if entity.get("type", "") == "PER": found_name = entity["text"] if not found_name: fnd = re.findall( r"(name is |named |called |call him |call her )([a-z]+)\b", user_text) if fnd: found_name = fnd[0][1] if (found_name and not shared_memory.get("users_pet_name", "") and found_name not in {"black", "white", "grey", "brown", "yellow", "cat", "dog"} and found_name not in users_pet_breed): state_utils.save_to_shared_memory(vars, users_pet_name=found_name) return found_name
def user_has_not_pets_request(ngrams, vars): flag = False isno = is_no(state_utils.get_last_human_utterance(vars)) text = state_utils.get_last_human_utterance(vars)["text"] if not re.search(PETS_TEMPLATE, text) or isno: flag = True logger.info(f"user_has_not_pets_request={flag}") return flag
def what_cuisine_request(ngrams, vars): linkto_food_skill_agreed = any( [req.lower() in state_utils.get_last_bot_utterance(vars)["text"].lower() for req in TRIGGER_PHRASES] ) and any( [is_yes(state_utils.get_last_human_utterance(vars)), not is_no(state_utils.get_last_human_utterance(vars))] ) flag = (bool(lets_talk_about_check(vars)) or linkto_food_skill_agreed) and (not dont_want_talk(vars)) logger.info(f"what_cuisine_request {flag}") return flag
def if_tell_fact(vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) isno = is_no(state_utils.get_last_human_utterance(vars)) ( found_entity_substr_list, prev_title, prev_page_title, found_entity_types_list, used_titles, _, page_content_list, main_pages_list, page, ) = get_page_info(vars, "request") logger.info( f"request, found_entity_substr {found_entity_substr_list} prev_title {prev_title} " f"found_entity_types {found_entity_types_list} used_titles {used_titles}" ) shared_memory = state_utils.get_shared_memory(vars) started = shared_memory.get("start", False) shared_state = vars["agent"]["dff_shared_state"] logger.info(f"shared_state {shared_state}") if found_entity_substr_list and found_entity_types_list and page_content_list: chosen_title, chosen_page_title = get_title_info( vars, found_entity_substr_list[-1], found_entity_types_list[-1], prev_title, used_titles, page_content_list[-1], ) _, _, all_titles = get_titles(found_entity_substr_list[-1], found_entity_types_list[-1], page_content_list[-1]) logger.info( f"request, chosen_title {chosen_title} chosen_page_title {chosen_page_title}" ) wants_more = if_wants_more(vars, all_titles) not_want = re.findall(COMPILE_NOT_WANT_TO_TALK_ABOUT_IT, user_uttr["text"]) if (chosen_title or prev_title) and ( (wants_more and not not_want) or not started or len(found_entity_substr_list) > 1): flag = True if user_uttr["text"].endswith("?") and another_topic_question( vars, all_titles): flag = False if isno: flag = False return flag
def user_has_pets_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars)["text"] bot_uttr = state_utils.get_last_bot_utterance(vars)["text"].lower() isno = is_no(state_utils.get_last_human_utterance(vars)) isyes = is_yes(state_utils.get_last_human_utterance(vars)) user_has = re.findall("i (have|had)", user_uttr) bot_asked_like = "do you like animals" in bot_uttr bot_asked_have = "do you have pets" in bot_uttr if ((re.search(PETS_TEMPLATE_EXT, user_uttr) and not isno) or (re.search(PETS_TEMPLATE_EXT, bot_uttr) and (isyes or user_has)) or (bot_asked_like and user_has) or (bot_asked_have and isyes)): flag = True logger.info(f"user_has_pets_request={flag}") return flag
def is_not_wild_request(ngrams, vars): flag = False text = state_utils.get_last_human_utterance(vars)["text"] if re.search(PETS_TEMPLATE, text): flag = True logger.info(f"is_not_wild_request={flag}") return flag
def lets_talk_about_request(vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) have_pets = re.search(HAVE_LIKE_PETS_TEMPLATE, user_uttr["text"]) found_prompt = any([ phrase.lower() in bot_uttr["text"].lower() for phrase in TRIGGER_PHRASES ]) isno = is_no(user_uttr) is_stop = re.findall(r"(stop|shut|something else|change|don't want)", user_uttr["text"]) chat_about = if_chat_about_particular_topic( user_uttr, bot_uttr, compiled_pattern=ANIMALS_FIND_TEMPLATE) find_pattern = re.findall(ANIMALS_FIND_TEMPLATE, user_uttr["text"]) dont_like = re.findall(NOT_LIKE_PATTERN, user_uttr["text"]) was_prev_active = if_was_prev_active(vars) if chat_about and find_pattern: flag = True if not dont_like and (have_pets or (find_pattern and (not is_last_state(vars, "SYS_WHAT_ANIMALS") or not was_prev_active)) or (found_prompt and not isno and not is_stop)): flag = True if re.findall(NOT_SWITCH_TEMPLATE, user_uttr["text"]): flag = False logger.info(f"lets_talk_about_request={flag}") return flag
def music_request(ngrams, vars): # "Alexa, music" flag = bool( music_request_re.fullmatch( state_utils.get_last_human_utterance(vars)["text"])) logger.info(f"music_request {flag}") return flag
def is_no_human_abandon(vars): """Is dialog breakdown in human utterance or no. Uses MIDAS hold/abandon classes.""" midas_classes = common_utils.get_intents( state_utils.get_last_human_utterance(vars), which="midas") if "abandon" not in midas_classes: return True return False
def my_pet_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) prev_active_skill = bot_uttr.get("active_skill", "") dontlike = re.findall(r"(do not like |don't like |hate )(cat|dog)", user_uttr["text"]) isno = is_no(user_uttr) shared_memory = state_utils.get_shared_memory(vars) my_pet = shared_memory.get("my_pet", "") all_facts_used = False start_using_facts = False if my_pet: used_facts = shared_memory.get("used_facts", {}).get(my_pet, []) all_facts = MY_PET_FACTS[my_pet] if len(all_facts) == len(used_facts): all_facts_used = True if len(used_facts) > 0: start_using_facts = True about_users_pet = if_about_users_pet(ngrams, vars) if not about_users_pet and my_pet and not dontlike and not all_facts_used: flag = True if start_using_facts and prev_active_skill != "dff_animals_skill": flag = False if re.findall(r"(would you like|can tell you more)", bot_uttr["text"], re.IGNORECASE) and isno: flag = False logger.info(f"my_pet_request={flag}") return flag
def ask_if_user_thinks_that_gaming_is_unhealthy_response(vars): response = ( "It is known that people who play computer games too much can have health problems, " "both physical and emotional. Do you agree?") human_uttr = state_utils.get_last_human_utterance(vars) entities = get_entities(human_uttr, only_named=True) logger.info( f"(ask_if_user_thinks_that_gaming_is_unhealthy_response)entities: {entities}" ) bot_text = state_utils.get_last_bot_utterance(vars).get("text", "").lower() flags_set = False if not if_chat_about_particular_topic( human_uttr, compiled_pattern=VIDEO_GAME_WORDS_COMPILED_PATTERN): flags_set, response = common_nlg.maybe_set_confidence_and_continue_based_on_previous_bot_phrase( vars, bot_text, response) if not flags_set: if entities: state_utils.set_confidence( vars, confidence=common_nlg.CONF_092_CAN_CONTINUE) state_utils.set_can_continue( vars, continue_flag=common_constants.CAN_CONTINUE_SCENARIO) else: state_utils.set_confidence(vars, confidence=common_nlg.CONF_1) state_utils.set_can_continue( vars, continue_flag=common_constants.MUST_CONTINUE) return response
def find_entity(vars, where_to_find="current"): bot_uttr = state_utils.get_last_bot_utterance(vars) if where_to_find == "current": annotations = state_utils.get_last_human_utterance(vars)["annotations"] found_entity_substr, found_entity_id, found_entity_types, _ = find_entity_wp( annotations, bot_uttr) if not found_entity_substr: found_entity_substr, _ = find_entity_nounphr(annotations) else: all_user_uttr = vars["agent"]["dialog"]["human_utterances"] utt_num = len(all_user_uttr) found_entity_substr = "" found_entity_types = [] found_entity_id = "" if utt_num > 1: for i in range(utt_num - 2, 0, -1): annotations = all_user_uttr[i]["annotations"] found_entity_substr, found_entity_id, found_entity_types, _ = find_entity_wp( annotations, bot_uttr) if not found_entity_substr: found_entity_substr, _ = find_entity_nounphr(annotations) if found_entity_substr: break logger.info( f"find_entity, substr {found_entity_substr} types {found_entity_types}" ) return found_entity_substr, found_entity_id, found_entity_types
def not_wants_more_request(ngrams, vars): flag = False isno = is_no(state_utils.get_last_human_utterance(vars)) if isno: flag = True logger.info(f"not_wants_more_request={flag}") return flag
def why_fav_request(ngrams, vars): flag = False utt = state_utils.get_last_human_utterance(vars)["text"].lower() if "why" in utt: flag = True logger.info(f"why_fav_request {flag}") return flag
def factoid_q_request(ngrams, vars): flag = False shared_memory = state_utils.get_shared_memory(vars) user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) user_more_details = re.findall(COMPILE_LETS_TALK, user_uttr["text"]) user_annotations = user_uttr["annotations"] is_factoid = False factoid_cl = user_annotations.get("factoid_classification", {}) if factoid_cl and factoid_cl["factoid"] > factoid_cl["conversational"]: is_factoid = True bot_text = bot_uttr["text"].lower() sentences = nltk.sent_tokenize(bot_text) if len(sentences) > 1: sentences = [ sentence for sentence in sentences if not sentence.endswith("?") ] bot_text = " ".join(sentences) nounphrases = user_annotations.get("spacy_nounphrases", []) found_nounphr = any([nounphrase in bot_text for nounphrase in nounphrases]) logger.info( f"factoid_q_request, is_factoid {is_factoid} user_more_details {user_more_details} " f"nounphrases {nounphrases} bot_text {bot_text}") started = shared_memory.get("start", False) if is_factoid and not user_more_details and found_nounphr and started: flag = True logger.info(f"factoid_q_request={flag}") return flag
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 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 sys_response_to_speech_function_request(vars): flag = False # added check for introvert/extravert try: dialog = state_utils.get_dialog(vars) human_uttr_idx = len(dialog["human_utterances"]) logger.info(f"human dialog length: {human_uttr_idx}") if len(dialog["human_utterances"]) > 4: logger.info("human utterances number: at least 5") if is_introvert(dialog) is True: logger.info("user is: introvert") return False else: logger.info("user is: extravert") human_utterance = state_utils.get_last_human_utterance(vars) bot_utterance = state_utils.get_last_bot_utterance(vars) flag = is_supported_speech_function(human_utterance, bot_utterance) logger.info(f"sys_response_to_speech_function_request: {flag}") except Exception as exc: logger.exception(exc) logger.info( f"sys_response_to_speech_function_request: Exception: {exc}") sentry_sdk.capture_exception(exc) logger.info(f"sys_response_to_speech_function_request: {flag}") return flag
def is_minecraft_mentioned_in_user_or_bot_uttr(ngrams, vars): user_uttr_text = state_utils.get_last_human_utterance(vars).get("text", "") bot_uttr_text = state_utils.get_last_bot_utterance(vars).get("text", "") result = "minecraft" in user_uttr_text.lower( ) or "minecraft" in bot_uttr_text.lower() logger.info(f"is_minecraft_mentioned_in_user_or_bot_uttr={result}") return result
def user_likes_request(ngrams, vars): flag = False isno = is_no(state_utils.get_last_human_utterance(vars)) if not isno or if_link_from_wiki_skill(vars): flag = True logger.info(f"user_likes_request={flag}") return flag
def not_have_pets_request(ngrams, vars): flag = False text = state_utils.get_last_human_utterance(vars)["text"] if re.findall(r"(\bno\b|\bnot\b|don't)", text): flag = True logger.info(f"do_not_have_pets_request={flag}") return flag
def stop_animals_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_human_utterance(vars) found_prompt = any([ phrase.lower() in bot_uttr["text"].lower() for phrase in TRIGGER_PHRASES ]) isno = is_no(user_uttr) shared_memory = state_utils.get_shared_memory(vars) if stop_about_animals(user_uttr, shared_memory): flag = True if found_prompt and not isno: flag = False logger.info(f"stop_animals_request={flag}") return flag
def extract_and_save_wikipage(vars, save=False): flag = False user_uttr = state_utils.get_last_human_utterance(vars) bot_uttr = state_utils.get_last_bot_utterance(vars) shared_memory = state_utils.get_shared_memory(vars) cur_facts = shared_memory.get("cur_facts", {}) for fact in cur_facts: wikihow_page = fact.get("wikihow_page", "") condition = fact["cond"] checked = check_condition(condition, user_uttr, bot_uttr, shared_memory) if checked and wikihow_page: flag = True if save: state_utils.save_to_shared_memory( vars, cur_wikihow_page=wikihow_page) state_utils.save_to_shared_memory(vars, cur_facts={}) break wikipedia_page = fact.get("wikipedia_page", "") condition = fact["cond"] checked = check_condition(condition, user_uttr, bot_uttr, shared_memory) if checked and wikipedia_page: flag = True if save: state_utils.save_to_shared_memory( vars, cur_wikipedia_page=wikipedia_page) state_utils.save_to_shared_memory(vars, cur_facts={}) break 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 if_wants_more(vars, all_titles): flag = False isyes = is_yes(state_utils.get_last_human_utterance(vars)) isno = is_no(state_utils.get_last_human_utterance(vars)) user_uttr = state_utils.get_last_human_utterance(vars) further = re.findall(r"(more|further|continue|follow)", user_uttr["text"], re.IGNORECASE) another_topic = another_topic_question(vars, all_titles) if isyes or (further and not isno): flag = True if another_topic and not isyes: flag = False if isno: flag = False logger.info(f"wants_more={flag}") return flag