Esempio n. 1
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
Esempio n. 2
0
    def _handle_answer_whose_song(self, state, triggered_answers):
        question_name = 'QuestionWhoseSong'
        musician_wiki_entity = self.state_manager.current_state.entity_linker.best_ent_of_type(
            WikiEntityInterface.EntityGroup.MUSICIAN)
        if not musician_wiki_entity or musician_wiki_entity.name is WikiEntityInterface.PageName.MUSICIAN:
            if question_name in triggered_answers and 'answer' in triggered_answers[
                    question_name]:
                musician_name = triggered_answers[question_name]['answer']
                musician_wiki_entity = WikiEntityInterface.link_span(
                    musician_name)

        rg_result = None
        if musician_wiki_entity and WikiEntityInterface.is_in_entity_group(
                musician_wiki_entity,
                WikiEntityInterface.EntityGroup.MUSICIAN):
            musician_entity = MusicEntity(
                pref_label=musician_wiki_entity.common_name,
                wiki_entity=musician_wiki_entity)
            rg_result = self.create_rg_result_ask_another_song_of_musician(
                state, musician_entity)
            if rg_result:
                response = self.choose(ACKNOWLEDGE_WHOSE_SONG)
                rg_result.text = f"{response} {rg_result.text}"

        if not rg_result:
            response = self.choose(ACKNOWLEDGE_COULDNT_GET_MUSICIAN)
            rg_result = self.prepare_rg_result(response, state)
            rg_result.conditional_state.needs_internal_prompt = False

        return rg_result
Esempio n. 3
0
    def _handle_answer_favorite_question(self, state, triggered_answers):
        question_name = 'QuestionFavoriteSongTemplate'

        # Prepare responses
        if self.is_no_answer(
        ) or 'NegativeTemplate' in triggered_answers or 'DontKnowTemplate' in triggered_answers:
            response = self.choose(ACKNOWLEDGE_FAVORITE_SONG_NEGATIVE)
            rg_result = self.prepare_rg_result(response, state)
            rg_result.conditional_state.needs_internal_prompt = False
            rg_result.needs_prompt = True
        else:
            # Check if the user mentioned a song.
            song_wiki_entity = None

            if not song_wiki_entity and question_name in triggered_answers and 'answer' in triggered_answers[
                    question_name]:
                song_name = triggered_answers[question_name]['answer']
                song_wiki_entity = WikiEntityInterface.link_span(song_name)

            # If the user mentioned a song, ask a follow up question.
            if song_wiki_entity and WikiEntityInterface.is_in_entity_group(
                    song_wiki_entity, WikiEntityInterface.EntityGroup.SONG):
                response = self.choose(ACKNOWLEDGE_FAVORITE_SONG)
                followup = self.choose(QUESTION_WHY_DO_YOU_LIKE)
                response = "{} {}".format(response, followup)
                rg_result = self.prepare_rg_result(response, state)

                song_entity = MusicEntity(
                    pref_label=song_wiki_entity.common_name,
                    wiki_entity=song_wiki_entity)
                musician_entity = MusicianTreelet.get_musician(
                    song_wiki_entity.name)

                rg_result.conditional_state.song_entity = song_entity
                rg_result.conditional_state.musician_entity = musician_entity
                rg_result.text = response.replace(
                    WikiEntityInterface.ENTITY_PLACEHOLDER,
                    song_wiki_entity.common_name)
                rg_result.cur_entity = song_wiki_entity
                rg_result.conditional_state.discussed_entities.append(
                    song_wiki_entity)
                rg_result.conditional_state.needs_internal_prompt = False
                rg_result.conditional_state.next_treelet = self.name
                rg_result.conditional_state.asked_question = 'QuestionWhyDoYouLikeSong'
            # We don't know which song the user mentioned.
            elif state.num_repeats < REPEAT_THRESHOLD:
                response = self.choose(QUESTION_COULD_YOU_REPEAT)
                rg_result = self.prepare_rg_result(response, state)
                rg_result.conditional_state.repeated_question = True
                rg_result.conditional_state.needs_internal_prompt = False
                rg_result.conditional_state.next_treelet = self.name
                rg_result.conditional_state.asked_question = 'QuestionWhyDoYouLikeSong'
            else:
                response = self.choose(ACKNOWLEDGE_COULDNT_GET_SONG)
                rg_result = self.prepare_rg_result(response, state)
                rg_result.conditional_state.needs_internal_prompt = False

        return rg_result
Esempio n. 4
0
    def _handle_answer_like_question(self, state, triggered_answers):
        question_name = 'QuestionLikedMusicianTemplate'

        if 'NegativeTemplate' in triggered_answers or 'DontKnowTemplate' in triggered_answers:
            response = self.choose(ACKNOWLEDGE_LIKED_MUSICIAN_NEGATIVE)
            rg_result = self.prepare_rg_result(response, state)
            rg_result.conditional_state.needs_internal_prompt = False
            rg_result.needs_prompt = True
        else:
            # Check if the user mentioned a musician.
            musician_wiki_entity = self.state_manager.current_state.entity_linker.best_ent_of_type(
                WikiEntityInterface.EntityGroup.MUSICIAN)
            if state.musician_entity: musician_wiki_entity = state.musician_entity.wiki_entity

            if not musician_wiki_entity and question_name in triggered_answers and 'answer' in triggered_answers[question_name]:
                musician_name = triggered_answers[question_name]['answer']
                musician_wiki_entity = WikiEntityInterface.link_span(musician_name)

            # If we know the musician.
            if musician_wiki_entity and WikiEntityInterface.is_in_entity_group(musician_wiki_entity, WikiEntityInterface.EntityGroup.MUSICIAN):
                if state.acknowledged_entity:
                    response = self.choose(ACKNOWLEDGE_LIKED_MUSICIAN)
                else:
                    response = self.choose(ACKNOWLEDGE_MENTIONED_MUSICIAN)
                response = response.replace(WikiEntityInterface.ENTITY_PLACEHOLDER, musician_wiki_entity.common_name)
                rg_result = self.prepare_rg_result(response, state)
                rg_result.conditional_state.discussed_entities.append(musician_wiki_entity)
                musician_entity = MusicEntity(pref_label=musician_wiki_entity.common_name, wiki_entity=musician_wiki_entity)
                song_entity = self.get_song_of_musician(musician_entity, state.discussed_entities)
                if song_entity:
                    expression = self.choose(EXPRESS_INTEREST_OTHER_SONG)
                    expression = expression.replace(f"SONG_{WikiEntityInterface.ENTITY_PLACEHOLDER}", song_entity.pref_label)
                    expression = expression.replace(f"MUSICIAN_{WikiEntityInterface.ENTITY_PLACEHOLDER}", musician_entity.pref_label)
                    question = self.choose(QUESTION_OTHER_SONGS)
                    question = question.replace(WikiEntityInterface.ENTITY_PLACEHOLDER, musician_entity.pref_label)
                    rg_result.text = f"{response} {expression} {question}"
                    rg_result.cur_entity = song_entity.wiki_entity
                    rg_result.conditional_state.needs_internal_prompt = False
                    rg_result.conditional_state.next_treelet = self.name
                    rg_result.conditional_state.asked_question = 'QuestionWhichOfTheirSongsDoYouLike'
            elif state.num_repeats < REPEAT_THRESHOLD:
                response = self.choose(QUESTION_COULD_YOU_REPEAT)
                rg_result = self.prepare_rg_result(response, state)
                rg_result.conditional_state.repeated_question = True
                rg_result.conditional_state.needs_internal_prompt = False
                rg_result.conditional_state.next_treelet = self.name
                rg_result.conditional_state.asked_question = 'QuestionLikedMusicianTemplate'
            else:
                response = self.choose(ACKNOWLEDGE_COULDNT_GET_MUSICIAN)
                rg_result = self.prepare_rg_result(response, state)
                rg_result.priority = ResponsePriority.WEAK_CONTINUE
                rg_result.conditional_state.needs_internal_prompt = False

        return rg_result
Esempio n. 5
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
Esempio n. 6
0
    def get_musician(song_name):

        # Try to find the musician from the Wiki overview.
        musician_name = WikiEntityInterface.extract_musician_name_from_song_overview(song_name)

        if musician_name:
            wiki_entity = WikiEntityInterface.link_span(musician_name)
            if wiki_entity and WikiEntityInterface.is_in_entity_group(wiki_entity, WikiEntityInterface.EntityGroup.MUSICIAN):
                musician_entity = MusicEntity(pref_label=wiki_entity.common_name, wiki_entity=wiki_entity)
                return musician_entity

        # Try to find in Knowledge graph
        musician_entity = KnowledgeInterface().get_musician_entity_by_song(song_name)
        return musician_entity
    def _handle_answer_favorite_question(self, state, triggered_answers):
        question_name = 'QuestionFavoriteInstrumentTemplate'

        # Check if the user mentioned an instrument.
        cur_entity = self.state_manager.current_state.entity_tracker.cur_entity
        instrument_entity = None
        if cur_entity and WikiEntityInterface.is_in_entity_group(
                cur_entity, WikiEntityInterface.EntityGroup.INSTRUMENT):
            instrument_entity = cur_entity
        elif question_name in triggered_answers and 'answer' in triggered_answers[
                question_name]:
            instrument_name = triggered_answers[question_name]['answer']
            instrument_entity = WikiEntityInterface.link_span(
                instrument_name.capitalize())

        # If the user mentioned an instrument, ask a follow up question.
        if instrument_entity:
            response = self.choose(ACKNOWLEDGE_FAVORITE_INSTRUMENT)
            followup = self.choose(FAVORITE_INSTRUMENT_FOLLOWUP_QUESTIONS)
            response = "{} {}".format(response, followup)
            response = response.replace(WikiEntityInterface.ENTITY_PLACEHOLDER,
                                        instrument_entity.common_name)
            rg_result = self.prepare_rg_result(response, state)
            rg_result.cur_entity = instrument_entity
            rg_result.conditional_state.asked_question = 'QuestionFavoriteInstrumentTemplate Followup 1'
            rg_result.conditional_state.needs_internal_prompt = False
            rg_result.conditional_state.next_treelet = self.name
        # If not, acknowledge the user response and exit the Music RG.
        elif self.is_no_answer(
        ) or 'NegativeTemplate' in triggered_answers or 'DontKnowTemplate' in triggered_answers:
            response = self.choose(ACKNOWLEDGE_FAVORITE_INSTRUMENT_NEGATIVE)
            rg_result = self.prepare_rg_result(response, state)
            rg_result.conditional_state.needs_internal_prompt = False
            rg_result.needs_prompt = True
        else:
            response = self.choose(ACKNOWLEDGE_FAVORITE_INSTRUMENT_NEVER_HEARD)
            rg_result = self.prepare_rg_result(response, state)
            rg_result.conditional_state.needs_internal_prompt = False
            rg_result.needs_prompt = True

        return rg_result