Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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