def submit(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict]:

        temp = {"his": [], "her": []}
        refer = ""

        # get slots
        topic = tracker.slots["topic"]
        pronoun = tracker.slots["pronoun"]
        locat = tracker.slots["location"]
        role = tracker.slots["role"]

        # get topic if exists
        if topic:
            temp = topic
        """
        {
            'intent': {
                'name': 'query',
                'confidence': 0.9659770727157593
            },
            'entities': ,
            'intent_ranking': ,
            'text': 'who is the prime minister of Japan'
        }
        """
        print(str(temp))
        if pronoun and temp:
            if (pronoun == "her" and temp["her"]) or (pronoun == "his"
                                                      and temp["his"]):
                # change name from pronoun in "topic" slot
                # if len(temp[pronoun]) > 1:
                #     dispatcher.utter_message("Did you mean :")
                #     for target in temp[pronoun][-3:]:
                #         dispatcher.utter_message(target)
                #     return [
                #         SlotSet("pronoun", None),
                #         FollowupAction("confirm_name"),
                #         SlotSet("listed", temp[pronoun][-3]),
                #     ]
                # else:
                refer = temp[pronoun][-1]
            else:
                dispatcher.utter_message("I don't know the context of " +
                                         pronoun)
                return []

        elif not role and not locat:
            dispatcher.utter_message("I don't know the context of " + pronoun)
            return []

        if refer and pronoun:
            msg = str(refer).strip() + "+" + str(role).strip()
            temp1, temp2 = role, refer
        else:
            msg = str(role).strip() + "+" + str(locat).strip()
            temp1, temp2 = role, locat
        # dispatcher.utter_message(txt)

        # get data from Wolfram Alpha
        qr = self.get_wolfram_data(msg)

        # Extract data
        try:
            result = self.extract_values(qr, "plaintext")[0]
            if result == "(data not available)":
                raise Exception
        except Exception as _:
            dispatcher.utter_message("Sorry, I can't find " + temp1 + " of " +
                                     temp2)
            return []

        # Send image
        img = self.extract_values(qr, "src")
        if len(img) > 1:
            dispatcher.utter_image_url(img[-1])

        # Split multiple data separated by | and get the last
        res_list = []
        name_list = []
        if "|" in str(result):
            if str(role) == "child":
                for datum in str(result).split("|"):
                    res_list.append(datum.strip())
                    name_list.append(datum.strip())
            else:
                res = str(result).split("|")[-1].strip()
                res_list.append(res)
                name = re.sub(r"\(.*", "", res).strip()
                name_list.append(name)
        else:
            res_list.append(str(result).strip())
            name = re.sub(r"\(.*", "", result).strip()
            name_list.append(name)

        for name in name_list:
            # get gender so we can overwrite the topic
            gender = self.get_gender(name)

            if not re.match(r"\d", name):
                if gender == "male":
                    pronoun = "his"
                else:
                    pronoun = "her"
                if name not in temp[pronoun]:
                    temp[pronoun].append(name)

                if len(temp[pronoun]) > 3:
                    temp[pronoun].pop(0)

        # print(str(qr))
        for res in res_list:
            dispatcher.utter_message(res)

        return [SlotSet("topic", temp), SlotSet("pronoun", None)]
Ejemplo n.º 2
0
    def run(self, dispatcher: CollectingDispatcher, tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        dispatcher.utter_image_url("https://i.imgur.com/nGF1K8f.jpg")

        return []