def check_game_name_with_user_response(vars): logger.info(f"check_game_name_with_user_response") igdb_game_description, _ = game_info.search_igdb_game_description_by_user_and_bot_phrases( vars) if igdb_game_description is not None: logger.info( f"(user_wants_to_talk_about_particular_game_request)saving candidate id to shared memory" ) state_utils.save_to_shared_memory( vars, candidate_game_id=igdb_game_description["id"]) shared_memory = state_utils.get_shared_memory(vars) logger.info( f"(check_game_name_with_user_response)shared_memory: {shared_memory.keys()}" ) response = f"Would you like to talk about the video game {igdb_game_description['name']}?" 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: response = "" state_utils.set_confidence(vars, confidence=common_nlg.CONF_0) state_utils.set_can_continue( vars, continue_flag=common_constants.CAN_NOT_CONTINUE) return response
def fast_food_response(vars): try: shared_memory = state_utils.get_shared_memory(vars) used_facts = shared_memory.get("fast_food_facts", []) unused_facts = [i for i in FAST_FOOD_FACTS if i not in used_facts] used_questions = shared_memory.get("fast_food_questions", []) unused_questions = [ i for i in FAST_FOOD_QUESTIONS if i not in used_questions ] fact = "" question = "" if unused_facts: fact = random.choice(unused_facts) state_utils.save_to_shared_memory(vars, fast_food_facts=used_facts + [fact]) if unused_questions: question = random.choice(unused_questions) state_utils.save_to_shared_memory( vars, fast_food_questions=used_questions + [question]) if fact and question: state_utils.set_confidence(vars, confidence=CONF_MIDDLE) state_utils.set_can_continue(vars, continue_flag=CAN_CONTINUE_PROMPT) return f"I just found out that {fact} {question}" else: state_utils.set_can_continue(vars, continue_flag=CAN_NOT_CONTINUE) return error_response(vars) except Exception as exc: logger.exception(exc) sentry_sdk.capture_exception(exc) return error_response(vars)
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 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 more_facts_request(ngrams, vars): shared_memory = state_utils.get_shared_memory(vars) used_facts = shared_memory.get("used_facts", []) flag = all([bool(len(used_facts)), condition_utils.no_special_switch_off_requests(vars), yes_request(ngrams, vars)]) logger.info(f"more_facts_request {flag}") return flag
def choose_pet_phrase(vars, found_users_pet): shared_memory = state_utils.get_shared_memory(vars) used_cats_dogs_phrases_num = shared_memory.get("used_cats_dogs_phrases", {}) new_used_cats_dog_phrases_num = used_cats_dogs_phrases_num cand_phrases = CATS_DOGS_PHRASES[found_users_pet] if used_cats_dogs_phrases_num and found_users_pet in used_cats_dogs_phrases_num: used_phrases_num = used_cats_dogs_phrases_num[found_users_pet] if len(used_phrases_num) < len(cand_phrases): while True: cand_phrase_num = random.randint(0, len(cand_phrases) - 1) if cand_phrase_num not in used_phrases_num: break else: cand_phrase_num = random.randint(0, len(cand_phrases) - 1) if cand_phrase_num not in used_phrases_num: new_used_cats_dog_phrases_num[found_users_pet].append( cand_phrase_num) else: cand_phrase_num = random.randint(0, len(cand_phrases) - 1) new_used_cats_dog_phrases_num[found_users_pet] = [cand_phrase_num] pet_phrase = cand_phrases[cand_phrase_num] state_utils.save_to_shared_memory( vars, used_cats_dogs_phrases=new_used_cats_dog_phrases_num) return pet_phrase
def bot_will_give_another_how_to_request(ngrams, vars): flag = (common_intents.user_says_yes_request( ngrams, vars) and len(MINECRAFT_HOW_TOS) - len( state_utils.get_shared_memory(vars).get("used_how_to_indices", [])) >= 1) logger.info(f"bot_will_give_another_how_to={flag}") return flag
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 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 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 my_pet_response(vars): shared_memory = state_utils.get_shared_memory(vars) my_pet = shared_memory.get("my_pet", "") make_my_pets_info(vars) response = "" continue_flag = common_constants.CAN_CONTINUE_SCENARIO conf = CONF_2 if my_pet: fact_dict = find_fact(vars, MY_PET_FACTS, my_pet) fact = fact_dict.get("statement", "") question = fact_dict.get("question", "") answer, _, conf, continue_flag = answer_users_question(vars) response = f"{answer} {fact} {question}".strip().replace(" ", " ") if my_pet == "cat": state_utils.save_to_shared_memory(vars, told_about_cat=True) state_utils.save_to_shared_memory(vars, cat=True) state_utils.save_to_shared_memory(vars, start_about_cat=False) if my_pet == "dog": state_utils.save_to_shared_memory(vars, told_about_dog=True) state_utils.save_to_shared_memory(vars, dog=True) state_utils.save_to_shared_memory(vars, start_about_dog=False) state_utils.save_to_shared_memory(vars, start=True) if response: state_utils.set_confidence(vars, confidence=conf) state_utils.set_can_continue(vars, continue_flag=continue_flag) else: state_utils.set_confidence(vars, confidence=CONF_4) state_utils.set_can_continue(vars, continue_flag=common_constants.CAN_NOT_CONTINUE) logger.info(f"my_pet_response: {response}") return response
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 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 stop_animals_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) shared_memory = state_utils.get_shared_memory(vars) if stop_about_animals(user_uttr, shared_memory): flag = True logger.info(f"stop_animals_request={flag}") return flag
def more_details_response(vars): shared_memory = state_utils.get_shared_memory(vars) used_titles = shared_memory.get("used_titles", []) mentions_list = shared_memory.get("mentions", []) curr_pages = shared_memory.get("curr_pages", []) mention_pages_list = shared_memory.get("mention_pages", []) mentions_dict = {} for mention, mention_page in zip(mentions_list, mention_pages_list): mentions_dict[mention] = mention_page user_uttr = state_utils.get_last_human_utterance(vars) annotations = user_uttr["annotations"] nounphrases = annotations.get("spacy_nounphrases", []) inters = list(set(nounphrases).intersection(set(mentions_list))) found_entity_substr_list = [] found_entity_substr = inters[0] found_entity_substr_list.append(found_entity_substr) found_entity_types = [] new_page = False curr_page = mentions_dict[found_entity_substr] if curr_page: curr_pages.append(curr_page) new_page = True logger.info( f"more_details_response, found_entity_substr {found_entity_substr} curr_pages {curr_pages}" ) page_content, main_pages = get_page_content(curr_page) first_pars = page_content["first_par"] facts_str, new_mentions_list, new_mention_pages_list = make_facts_str( first_pars) titles_q, titles_we_use, all_titles = get_titles(found_entity_substr, found_entity_types, page_content) if not titles_we_use: titles_we_use = list( set(page_content.keys()).difference({"first_par"})) logger.info( f"all_titles {all_titles} titles_q {titles_q} titles_we_use {titles_we_use}" ) chosen_title, chosen_page_title = choose_title(vars, all_titles, titles_we_use, "", [], curr_pages) question = make_question(chosen_title, titles_q, found_entity_substr, []) response = f"{facts_str} {question}" response = response.strip() if chosen_title: used_titles.append(chosen_title) save_wiki_vars(vars, found_entity_substr_list, curr_pages, chosen_title, chosen_page_title, used_titles, [[]], new_page) if response: state_utils.set_confidence(vars, confidence=CONF_DICT["IN_SCENARIO"]) state_utils.set_can_continue( vars, continue_flag=common_constants.CAN_CONTINUE_SCENARIO) else: state_utils.set_confidence(vars, confidence=CONF_DICT["UNDEFINED"]) state_utils.set_can_continue( vars, continue_flag=common_constants.CAN_NOT_CONTINUE) return response
def get_igdb_ids_for_games_user_wanted_to_discuss(vars, assert_not_empty=True): shared_memory = state_utils.get_shared_memory(vars) ids = shared_memory.get("igdb_ids_for_games_user_wanted_to_discuss", []) if assert_not_empty: assert ids, ( "Shared memory field 'igdb_game_ids_user_wanted_to_discuss' is empty. " "If dff_gaming_skill reached state SYS_USER_CONFIRMS_GAME 'games_user_wanted_do_discuss' cannot be empty " ) return ids
def to_travel_skill_request(ngrams, vars): flag = False shared_memory = state_utils.get_shared_memory(vars) cuisine_discussed = shared_memory.get("cuisine", "") if cuisine_discussed: if cuisine_discussed in CUISINES_COUNTRIES: flag = True logger.info(f"to_travel_skill_request {flag}") return flag
def genre_advice_response(vars): try: state_utils.set_confidence(vars, MUST_CONTINUE_CONFIDENCE) state_utils.set_can_continue(vars, continue_flag=MUST_CONTINUE) genre = state_utils.get_shared_memory(vars).get("genre", "pop") if genre in ["pop", "80s", "70s"]: return "Well, now you know it." elif genre == "jazz": return "Oh, you should definitely check them out, like, one hundred percent." elif genre == "classic" or genre == "contemporary": return "Well, I was worth trying. I guess it right most of the time." elif genre == "electronic" or genre == "trance" or genre == "techno" or genre == "dance" or genre == "house": return "What do you like then?" elif genre == "hip hop" or genre == "rap": return "That's ok, I can't do it either." elif genre == "rock" or genre == "metal" or genre == "hardcore": return "Well, now you know it." elif genre == "children": return "But do you like listening to it?" elif genre == "folk": return "Ok, I get it. I guess you have a more \ pretentious taste than I do." elif genre == "reggae": return "I've been confused for a long time, \ because they sound so alike to me!" elif genre == "country": return "Well, try to imagine Blue Ridge Mountains \ while listening to it. You will get what I mean." elif genre == "alternative": return "Oh, I really recommend you to know them better. \ That is some classic alternative in my opinion." elif genre == "indie": return "To me, that is crazy. Though they are not very popular in \ the United States, they even were an inspiration to Kurt Cobain." elif genre == "all" or genre == "everything": return 'You should check him out, especially "Space oddity". \ It is really something special. \ They even played it on a real Space Station!' elif genre == "nineties": return "Yea, I guess I share your opinion on that point." elif genre == "eighties": return "Yea, I guess I understand what you mean." else: return "Cool, I think I understand what you mean." except Exception as exc: logger.exception(exc) sentry_sdk.capture_exception(exc) state_utils.set_confidence(vars, CANNOT_CONTINUE_CONFIDENCE) return error_response(vars)
def get_page_info(vars, function_type, where_to_find="current"): shared_memory = state_utils.get_shared_memory(vars) curr_pages = shared_memory.get("curr_pages", []) found_entity_substr_list = shared_memory.get("found_entity_substr", []) prev_title = shared_memory.get("prev_title", "") prev_page_title = shared_memory.get("prev_page_title", "") used_titles = shared_memory.get("used_titles", []) found_entity_types_list = shared_memory.get("found_entity_types", []) started = shared_memory.get("start", False) was_prev_active = if_was_prev_active(vars) logger.info(f"started {started}") if function_type == "response" and curr_pages and found_entity_substr_list and found_entity_types_list: page_content, _ = get_page_content(curr_pages[-1]) all_titles = find_all_titles([], page_content) wants_more = if_wants_more(vars, all_titles) logger.info( f"deleting, function_type {function_type} wants_more {wants_more}") if not wants_more: curr_pages.pop() found_entity_substr_list.pop() found_entity_types_list.pop() if not started or not was_prev_active: curr_pages = [] found_entity_substr_list = [] found_entity_types_list = [] state_utils.save_to_shared_memory(vars, start=False) new_page = shared_memory.get("new_page", False) page_content_list = [] main_pages_list = [] if curr_pages: for page in curr_pages[-2:]: page_content, main_pages = get_page_content(page) page_content_list.append(page_content) main_pages_list.append(main_pages) else: found_entity_substr, _, found_entity_types = find_entity( vars, where_to_find) curr_page = get_page_title(vars, found_entity_substr) if curr_page: curr_pages.append(curr_page) found_entity_substr_list.append(found_entity_substr) found_entity_types_list.append(list(found_entity_types)) for page in curr_pages[-2:]: page_content, main_pages = get_page_content(page) page_content_list.append(page_content) main_pages_list.append(main_pages) return ( found_entity_substr_list, prev_title, prev_page_title, found_entity_types_list, used_titles, curr_pages, page_content_list, main_pages_list, new_page, )
def wikihow_step_response(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 = shared_memory.get("used_wikihow_titles", []) found_title = "" facts_str = "" question = "" 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 and title != "intro": found_title = title break if prev_wikihow_title: paragraphs = article_content[prev_wikihow_title] if paragraphs: paragraph = paragraphs[0] sentences = nltk.sent_tokenize(paragraph) sentences_list = [] cur_len = 0 max_len = 50 for sentence in sentences: words = nltk.word_tokenize(sentence) if cur_len + len(words) < max_len: sentences_list.append(sentence) cur_len += len(words) if sentences_list: facts_str = " ".join(sentences_list) logger.info( f"wikihow_step_response, sentences_list {sentences_list} facts_str {facts_str}" ) if found_title: question = f"Would you like to know about {found_title.lower()}?" logger.info( f"wikihow_step_response found_title {found_title} prev_wikihow_title {prev_wikihow_title}" ) response = f"{facts_str} {question}" response = response.strip() if found_title: state_utils.save_to_shared_memory(vars, prev_wikihow_title=found_title) used_wikihow_titles.append(found_title) state_utils.save_to_shared_memory( vars, used_wikihow_titles=used_wikihow_titles) if response: state_utils.set_confidence(vars, confidence=CONF_DICT["IN_SCENARIO"]) state_utils.set_can_continue( vars, continue_flag=common_constants.CAN_CONTINUE_SCENARIO) else: state_utils.set_confidence(vars, confidence=CONF_DICT["UNDEFINED"]) state_utils.set_can_continue( vars, continue_flag=common_constants.CAN_NOT_CONTINUE) return response
def my_fav_story_response(vars): try: utt = state_utils.get_last_human_utterance(vars)["text"].lower() shared_memory = state_utils.get_shared_memory(vars) used_topics = shared_memory.get("used_topics", []) name = "" story = "" question = "" response = "" for topic in FAV_STORIES_TOPICS: if topic in utt: name = FAV_STORIES_TOPICS.get(topic, "").get("name", "") story = FAV_STORIES_TOPICS.get(topic, "").get("story", "") question = FAV_STORIES_TOPICS.get(topic, "").get("question", "") if name and (topic not in used_topics): if topic == "weekdays": response = f"{story} {question}" else: response = f"My favorite {topic} is {name}. {story} {question}" if topic == "book": state_utils.set_confidence(vars, confidence=CONF_LOW) state_utils.set_can_continue(vars, continue_flag=CAN_CONTINUE_SCENARIO) elif topic == "music" and "what kind of music do you like" in utt: state_utils.set_confidence(vars, confidence=CONF_HIGH) state_utils.set_can_continue(vars, continue_flag=MUST_CONTINUE) elif topic == "music": state_utils.set_confidence(vars, confidence=CONF_MIDDLE) state_utils.set_can_continue(vars, continue_flag=CAN_CONTINUE_SCENARIO) elif (topic == "game") and ("to play with" in utt): state_utils.set_can_continue(vars, continue_flag=CAN_NOT_CONTINUE) return error_response(vars) else: state_utils.set_confidence(vars, confidence=CONF_HIGH) state_utils.set_can_continue(vars, continue_flag=MUST_CONTINUE) state_utils.save_to_shared_memory(vars, used_topics=used_topics + [topic]) return response else: state_utils.set_can_continue(vars, continue_flag=CAN_NOT_CONTINUE) return error_response(vars) if not name and any( [("my" not in utt) and ("favorite" in utt), re.search(YOUR_FAVORITE_COMPILED_PATTERN, utt)] ): response = "Oh, I don't have one. What about you?" state_utils.set_confidence(vars, confidence=CONF_MIDDLE) state_utils.set_can_continue(vars, continue_flag=CAN_CONTINUE_SCENARIO) else: response = "I've never heard about it. Could you please tell me more about it?" state_utils.set_confidence(vars, confidence=CONF_MIDDLE) state_utils.set_can_continue(vars, continue_flag=CAN_NOT_CONTINUE) return response except Exception as exc: logger.exception(exc) sentry_sdk.capture_exception(exc) state_utils.set_confidence(vars, 0) return error_response(vars)
def check_switch(vars, topic_config): 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) found_topic = shared_memory.get("special_topic", "") first_utt = False utt_can_continue = "can" utt_conf = 0.0 shared_memory = state_utils.get_shared_memory(vars) for topic in topic_config: linkto = topic_config[topic].get("linkto", []) for phrase in linkto: if phrase.lower() in bot_uttr["text"].lower(): found_topic = topic first_utt = True break pattern = topic_config[topic].get("pattern", "") if pattern: if if_chat_about_particular_topic(user_uttr, bot_uttr, compiled_pattern=pattern): utt_can_continue = "must" utt_conf = 1.0 found_topic = topic first_utt = True elif re.findall(pattern, user_uttr["text"]) and not found_topic: utt_can_continue = "prompt" utt_conf = 0.95 found_topic = topic first_utt = True switch_on = topic_config[topic].get("switch_on", []) for switch_elem in switch_on: condition = switch_elem["cond"] if check_condition(condition, user_uttr, bot_uttr, shared_memory): found_topic = topic utt_can_continue = switch_elem.get("can_continue", "can") utt_conf = switch_elem.get("conf", utt_conf) first_utt = True break if found_topic: break return found_topic, first_utt, utt_can_continue, utt_conf
def wikihow_question_request(ngrams, vars): flag = False shared_memory = state_utils.get_shared_memory(vars) curr_pages = shared_memory.get("curr_pages", []) if not curr_pages: found_entity_substr, _, found_entity_types = find_entity( vars, "current") if found_entity_substr and found_entity_substr in wikihowq_by_substr: flag = True logger.info(f"wikihow_question_request={flag}") return flag
def if_facts_agree(vars): 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: condition = fact["cond"] flag = check_condition(condition, user_uttr, bot_uttr, shared_memory) if flag: break return flag
def animal_facts_request(ngrams, vars): flag = False shared_memory = state_utils.get_shared_memory(vars) isno = is_no(state_utils.get_last_human_utterance(vars)) facts = shared_memory.get("wild_animal_facts", []) used_wild_animal_facts = shared_memory.get("used_wild_animal_facts", []) if facts and len(facts) > len(used_wild_animal_facts) and not isno: flag = True if len(used_wild_animal_facts) > 0 and isno: flag = False logger.info(f"animal_facts_request={flag}") return flag
def my_pets_request(ngrams, vars): flag = False bot_uttr = state_utils.get_last_bot_utterance(vars)["text"] isyes = is_yes(state_utils.get_last_human_utterance(vars)) shared_memory = state_utils.get_shared_memory(vars) told_about_cat = shared_memory.get("told_about_cat", False) told_about_dog = shared_memory.get("told_about_dog", False) if "Would you like to learn more about my pets?" in bot_uttr and isyes and not ( told_about_cat and told_about_dog): flag = True logger.info(f"my_pets_request={flag}") return flag
def is_wild_request(ngrams, vars): flag = False user_uttr = state_utils.get_last_human_utterance(vars) text = user_uttr["text"] annotations = user_uttr["annotations"] shared_memory = state_utils.get_shared_memory(vars) used_is_wild = shared_memory.get("is_wild", False) if (not re.search(PETS_TEMPLATE, text) and not used_is_wild and not if_linked_to_wiki_skill(annotations, "dff_animals_skill")): flag = True logger.info(f"is_wild_request={flag}") return flag
def do_you_have_pets_request(ngrams, vars): flag = False shared_memory = state_utils.get_shared_memory(vars) asked_have_pets = shared_memory.get("asked_have_pets", False) user_uttr = state_utils.get_last_human_utterance(vars) pet_in_uttr = re.findall(r"(\bpet\b|\bpets\b)", user_uttr["text"], re.IGNORECASE) found_badlist = re.findall(NOT_SWITCH_TEMPLATE, user_uttr["text"]) if (not found_badlist and pet_in_uttr and not asked_have_pets and ("you" not in user_uttr["text"] or "my" in user_uttr["text"])): flag = True return flag
def mention_pets_request(ngrams, vars): flag = False text = state_utils.get_last_human_utterance(vars)["text"] shared_memory = state_utils.get_shared_memory(vars) started = shared_memory.get("start", False) dont_like = re.findall(NOT_LIKE_PATTERN, text) found_badlist = re.findall(NOT_SWITCH_TEMPLATE, text) if re.search(PETS_TEMPLATE, text) and not started and not dont_like and not found_badlist: flag = True logger.info(f"mention_pets_request={flag}") return flag
def tell_how_to_and_ask_if_it_was_interesting_response(vars): how_to_index = state_utils.get_shared_memory(vars).get( "current_how_to_index") assert how_to_index is not None, ( "The shared memory field `current_how_to_index` should have been filled on one " "of previous turns") state_utils.set_confidence(vars, confidence=common_nlg.CONF_1) state_utils.set_can_continue(vars, continue_flag=common_constants.MUST_CONTINUE) shared_memory_ops.add_how_to_index_to_used_how_to_indices( vars, how_to_index) return MINECRAFT_HOW_TOS[how_to_index]["answer"] + " Was it interesting?"