def get_prompt(self, state, conditional_state=None, trigger_entity = None): question_candidates = [q for q in list(QUESTION_TEMPLATES.keys()) if q not in state.asked_questions] is_correct_entity_group = trigger_entity and WikiEntityInterface.is_in_entity_group(trigger_entity, WikiEntityInterface.EntityGroup.MUSICIAN) undiscussed_titles = [title for title in EXAMPLE_MUSICIAN_WIKI_PAGES.keys() if not WikiEntityInterface.is_title_in_entities(title, state.discussed_entities)] # Prepare prompt. prompt = self.prepare_prompt_result('Placeholder', state, priority=PromptType.GENERIC, cur_entity=None, conditional_state=conditional_state) prompt.conditional_state.turn_treelet_history.append(self.name) prompt.conditional_state.next_treelet = self.name prompt.conditional_state.needs_internal_prompt = False # Give current topic response if possible. if trigger_entity and is_correct_entity_group: prompt.priority = PromptType.CURRENT_TOPIC prompt.cur_entity = trigger_entity prompt.conditional_state.musician_entity = MusicEntity(pref_label=trigger_entity.common_name, wiki_entity=trigger_entity) prompt.conditional_state.discussed_entities.append(trigger_entity) prompt.conditional_state.asked_question = 'QuestionLikedMusicianTemplate' # This is the handler function for the question we are asking. prompt.conditional_state.acknowledged_entity = True acknowledgment = random.choice(['', self.choose(ACKNOWLEDGE_MENTIONED_MUSICIAN)]) question = self.choose(QUESTION_DO_YOU_LIKE) response = "{} {}".format(acknowledgment, question) prompt.text = response.replace(WikiEntityInterface.ENTITY_PLACEHOLDER, trigger_entity.common_name) return prompt # Generic response. if question_candidates: prompt.priority = PromptType.GENERIC prompt.cur_entity = WikiEntityInterface.get_by_name(WikiEntityInterface.PageName.MUSICIAN) prompt.expected_type = WikiEntityInterface.EntityGroup.MUSICIAN question_name = random.choice(question_candidates) prompt.conditional_state.asked_question = question_name question = self.construct_response_from_templates(QUESTION_TEMPLATES, question_name, question=True) transition = random.choice(NO_ENTITY_TRANSITIONS) prompt.text = " ".join((transition, question)) return prompt # Ask a question about a musician we know about. if undiscussed_titles: selected_title = undiscussed_titles[0] cur_entity = WikiEntityInterface.get_by_name(selected_title) if cur_entity: prompt.priority = PromptType.GENERIC prompt.cur_entity = cur_entity prompt.conditional_state.musician_entity = MusicEntity(pref_label=cur_entity.common_name, wiki_entity=cur_entity) prompt.conditional_state.discussed_entities.append(cur_entity) prompt.conditional_state.asked_question = 'QuestionLikedMusicianTemplate' # This is the handler function for the question we are asking. question = self.choose(QUESTION_DO_YOU_LIKE_POPULAR) question = Pronoun.replace_pronouns(question, EXAMPLE_MUSICIAN_WIKI_PAGES[selected_title]) question = question.replace(WikiEntityInterface.ENTITY_PLACEHOLDER, cur_entity.common_name) transition = random.choice(ENTITY_TRANSITIONS) prompt.text = " ".join((transition, question)) return prompt # We ran out of prompts. return None
def _handle_expression_chat_song(self, state, triggered_templates): expression_name = 'ChatSong' response = self.choose(CHAT_SONG_EXPRESSIONS) rg_result = self.prepare_rg_result(response, state) rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.SONG) return rg_result
def get_response_trigger_phrase(self, state, trigger_phrase = None): utterance = self.state_manager.current_state.text triggered_templates = self.process_templates(QUESTION_TEMPLATES, utterance) sentiment_answers = self.process_templates(SENTIMENT_TEMPLATES, utterance) triggered_templates = {**triggered_templates, **sentiment_answers} undiscussed_titles = [title for title in EXAMPLE_MUSICIAN_WIKI_PAGES.keys() if not WikiEntityInterface.is_title_in_entities(title, state.discussed_entities)] trigger_entity = None if trigger_phrase: trigger_entity = WikiEntityInterface.link_span(trigger_phrase) if trigger_entity and not WikiEntityInterface.is_in_entity_group(trigger_entity, WikiEntityInterface.EntityGroup.MUSICIAN): trigger_entity = None if 'QuestionLikedMusicianTemplate' in triggered_templates: response = self._handle_question_like(state, triggered_templates) elif trigger_entity: response = self.get_response_trigger_entity(state, trigger_entity) elif undiscussed_titles: selected_title = undiscussed_titles[0] cur_entity = WikiEntityInterface.get_by_name(selected_title) question = self.choose(QUESTION_DO_YOU_LIKE_POPULAR) question = Pronoun.replace_pronouns(question, EXAMPLE_MUSICIAN_WIKI_PAGES[selected_title]) question = question.replace(WikiEntityInterface.ENTITY_PLACEHOLDER, cur_entity.common_name) response = self.prepare_rg_result(question, state, cur_entity=cur_entity) response.conditional_state.musician_entity = MusicEntity(pref_label=cur_entity.common_name, wiki_entity=cur_entity) response.conditional_state.discussed_entities.append(cur_entity) response.conditional_state.asked_question = 'QuestionLikedMusicianTemplate' # This is the handler function for the question we are asking. response.conditional_state.next_treelet = self.name response.conditional_state.needs_internal_prompt = False else: response = self._handle_expression_chat_musician(state, None) return response
def _handle_answer_feelings_about_music_question(self, state, triggered_answers): question_name = 'QuestionFeelingsAboutMusicTemplate' # Determine which response we should give. is_negative_response = False if question_name in triggered_answers and 'negative_feeling' in triggered_answers[ question_name]: is_negative_response = True if self.is_no_answer( ) or 'NegativeTemplate' in triggered_answers or 'DontKnowTemplate' in triggered_answers: is_negative_response = True # Construct the response if is_negative_response: dry_expression = self.choose(DRY_EXPRESSIONS) positive_response = self.choose(ANSWER_FEELINGS_POSITIVE) response = "{} {}".format(dry_expression, positive_response) else: surprise_expression = random.choice(SURPRISE_EXPRESSIONS) positive_response = self.choose(ANSWER_FEELINGS_POSITIVE) response = "{} {}".format(surprise_expression, positive_response) # Prepare and return the RG result rg_result = self.prepare_rg_result(response, state) # Set the needs_prompt to True if we are not engaging the user if is_negative_response: rg_result.conditional_state.needs_internal_prompt = False rg_result.needs_prompt = True else: rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.MUSIC) return rg_result
def get_prompt(self, state, conditional_state=None, trigger_entity=None): # Instrument treelet only has generic prompts. question_candidates = [ q for q in list(QUESTION_TEMPLATES.keys()) if q not in state.asked_questions ] if question_candidates: question_name = self.choose(question_candidates) question = self.construct_response_from_templates( QUESTION_TEMPLATES, question_name, question=True) transition = random.choice(TRANSITIONS) prompt = self.prepare_prompt_result( " ".join((transition, question)), state, priority=PromptType.GENERIC, cur_entity=WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.INSTRUMENT), conditional_state=conditional_state) prompt.conditional_state.turn_treelet_history.append(self.name) prompt.conditional_state.asked_question = question_name prompt.conditional_state.next_treelet = self.name prompt.conditional_state.needs_internal_prompt = False prompt.expected_type = WikiEntityInterface.EntityGroup.INSTRUMENT return prompt return None
def _handle_answer_listening_conditions_question(self, state, triggered_answers): question_name = 'QuestionListeningConditionsTemplate' # Determine which response we should give. is_rare_response = False if question_name in triggered_answers and 'rare_words' in triggered_answers[ question_name]: is_rare_response = True if self.is_no_answer( ) or 'NegativeTemplate' in triggered_answers or 'DontKnowTemplate' in triggered_answers: is_rare_response = True # Construct the response if is_rare_response: response = self.choose(ANSWER_LISTENING_FREQUENCY_RARE) else: surprise_expression = random.choice(SURPRISE_EXPRESSIONS) frequent_response = self.choose( ANSWER_LISTENING_CONDITIONS_FREQUENT) response = "{} {}".format(surprise_expression, frequent_response) # Prepare and return the RG result rg_result = self.prepare_rg_result(response, state) # Set the needs_prompt to True if we are not engaging the user if is_rare_response: rg_result.conditional_state.needs_internal_prompt = False rg_result.needs_prompt = True else: rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.MUSIC) return rg_result
def _handle_question_feelings_about_music(self, state, triggered_templates): question_name = 'QuestionFeelingsAboutMusicTemplate' response = self.choose(ANSWER_FEELINGS_POSITIVE) rg_result = self.prepare_rg_result(response, state) rg_result = self._update_remaining_prompts(rg_result, question_name) rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.MUSIC) return rg_result
def _handle_question_listening_frequency(self, state, triggered_templates): question_name = 'QuestionListeningFrequencyTemplate' # Construct our answer from the example answers response = self.choose(ANSWER_LISTENING_FREQUENCY_FREQUENT) rg_result = self.prepare_rg_result(response, state) rg_result = self._update_remaining_prompts(rg_result, question_name) rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.MUSIC) return rg_result
def _handle_expression_chat_music(self, state, triggered_templates): expression_name = 'ChatMusicTemplate' like_reason = self.choose(LIKE_REASONS) chatting_expression = "" # self.choose(CHATTING_EXPRESSIONS) response = "{} {}".format(like_reason, chatting_expression) rg_result = self.prepare_rg_result(response, state) rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.MUSIC) return rg_result
def _handle_expression_like_music(self, state, triggered_templates): expression_name = 'LikeMusicTemplate' like_reason = self.choose(LIKE_REASONS) surprise_expression = random.choice(SURPRISE_EXPRESSIONS) response = "{} {}".format(surprise_expression, like_reason) rg_result = self.prepare_rg_result(response, state) rg_result.cur_entity = WikiEntityInterface.get_by_name( WikiEntityInterface.PageName.MUSIC) return rg_result
def get_response_trigger_phrase(self, state, trigger_phrase=None): utterance = self.state_manager.current_state.text triggered_templates = self.process_templates(QUESTION_TEMPLATES, utterance) sentiment_answers = self.process_templates(SENTIMENT_TEMPLATES, utterance) triggered_templates = {**triggered_templates, **sentiment_answers} undiscussed_items = [ title for title in EXAMPLE_SONG_WIKI_PAGES if not WikiEntityInterface.is_title_in_entities( title, state.discussed_entities) ] trigger_entity = None if trigger_phrase: trigger_entity = WikiEntityInterface.link_span(trigger_phrase) if trigger_entity and not WikiEntityInterface.is_in_entity_group( trigger_entity, WikiEntityInterface.EntityGroup.SONG): trigger_entity = None response = None if 'QuestionFavoriteSongTemplate' in triggered_templates: response = self._handle_question_favorite(state, triggered_templates) elif trigger_entity: response = self.get_response_trigger_entity(state, trigger_entity) elif undiscussed_items: song_title, song_kid = undiscussed_items[0] cur_entity = WikiEntityInterface.get_by_name(song_title) if cur_entity: question = self.choose(QUESTION_DO_YOU_LIKE_POPULAR) question = question.replace( WikiEntityInterface.ENTITY_PLACEHOLDER, cur_entity.common_name) response = self.prepare_rg_result(question, state, cur_entity=cur_entity) response.conditional_state.song_entity = MusicEntity( pref_label=cur_entity.common_name, wiki_entity=cur_entity, kid=song_kid) response.conditional_state.discussed_entities.append( cur_entity) response.conditional_state.asked_question = 'QuestionWhyDoYouLikeSong' # This is the handler function for the question we are asking. response.conditional_state.next_treelet = self.name response.conditional_state.needs_internal_prompt = False if not response: response = self._handle_expression_chat_song(state, None) return response