def _process(self, input_pack: DataPack):
     input_pack.set_text(input_pack.text + "\n" + self.instruction)
     u = Utterance(
         input_pack,
         len(input_pack.text) - len(self.instruction),
         len(input_pack.text),
     )
     u.speaker = "ai"
Beispiel #2
0
def new_utterance(input_pack: DataPack, text: str, speaker: str):
    input_pack.set_text(input_pack.text + '\n' + text)
    logging.info('The response is:')
    logging.info(text)

    u = Utterance(input_pack,
                  len(input_pack.text) - len(text),
                  len(input_pack.text))
    u.speaker = speaker
Beispiel #3
0
    def new_utternace(self, input_pack: DataPack, text: str, speaker: str):
        input_pack.set_text(input_pack.text + "\n" + text)
        logging.info("The response is:")
        logging.info(text)

        print("The response is:")
        print(text)

        u = Utterance(input_pack,
                      len(input_pack.text) - len(text), len(input_pack.text))
        u.speaker = speaker
Beispiel #4
0
    def _process(self, input_pack: MultiPack):
        query = input_pack.get_pack(self.in_pack_name).text
        params = "?" + urlencode(
            {
                "api-version": "3.0",
                "from": self.src_language,
                "to": [self.target_language],
            },
            doseq=True,
        )
        microsoft_constructed_url = self.microsoft_translate_url + params

        response = requests.post(
            microsoft_constructed_url,
            headers=self.microsoft_headers,
            json=[{
                "text": query
            }],
        )

        if response.status_code != 200:
            raise RuntimeError(response.json()["error"]["message"])

        text = response.json()[0]["translations"][0]["text"]
        pack: DataPack = input_pack.add_pack(self.out_pack_name)
        pack.set_text(text=text)

        Document(pack, 0, len(text))
        Utterance(pack, 0, len(text))
Beispiel #5
0
    def _parse_pack(self, data_source: str) -> Iterator[MultiPack]:
        r"""Takes a raw string and converts into a MultiPack.

        Args:
            data_source: str that contains text of a document.

        Returns: MultiPack containing a datapack for the current query.
        """
        multi_pack = MultiPack()

        # use context to build the query
        if self.resources is not None and self.resources.get("user_utterance"):
            multi_pack.add_pack_(
                self.resources.get("user_utterance")[-1], "user_utterance")

        if self.resources is not None and self.resources.get("bot_utterance"):
            multi_pack.add_pack_(
                self.resources.get("bot_utterance")[-1], "bot_utterance")

        pack = multi_pack.add_pack(self.configs.pack_name)
        pack.set_text(data_source, replace_func=self.text_replace_operation)

        Utterance(pack, 0, len(data_source))

        yield multi_pack
Beispiel #6
0
    def _parse_pack(self, data_source: str) -> Iterator[MultiPack]:
        """
        Takes a raw string and converts into a MultiPack

        Args:
            data_source: str that contains text of a document

        Returns: MultiPack containing a datapack for the current query

        """

        multi_pack = MultiPack()

        # use context to build the query
        if self.resource.get("user_utterance"):
            user_pack = self.resource.get("user_utterance")[-1]
            multi_pack.update_pack({"user_utterance": user_pack})

        if self.resource.get("bot_utterance"):
            bot_pack = self.resource.get("bot_utterance")[-1]
            multi_pack.update_pack({"bot_utterance": bot_pack})

        pack = DataPack()
        utterance = Utterance(pack, 0, len(data_source))
        pack.add_entry(utterance)

        pack.set_text(data_source, replace_func=self.text_replace_operation)
        multi_pack.update_pack({self.config.pack_name: pack})

        yield multi_pack
    def _process(self, input_pack: MultiPack):
        query = input_pack.get_pack(self.in_pack_name).text
        params = '?' + urlencode(
            {
                'api-version': '3.0',
                'from': self.src_language,
                'to': [self.target_language]
            },
            doseq=True)
        microsoft_constructed_url = self.microsoft_translate_url + params

        response = requests.post(microsoft_constructed_url,
                                 headers=self.microsoft_headers,
                                 json=[{
                                     "text": query
                                 }])

        if response.status_code != 200:
            raise RuntimeError(response.json()['error']['message'])

        text = response.json()[0]["translations"][0]["text"]
        pack = DataPack()

        document = Document(pack, 0, len(text))
        utterance = Utterance(pack, 0, len(text))
        pack.add_entry(document)
        pack.add_entry(utterance)

        pack.set_text(text=text)
        input_pack.update_pack({self.out_pack_name: pack})
Beispiel #8
0
def create_utterance(input_pack: DataPack, text: str, speaker: str):
    """
    Create an utterance in the datapack. This is composed of two steps:
     1. Append the utterance text to the data pack.
     2. Create :class:`~ft.onto.base_ontology.Utterance` entry on the text.
     3. Set the speaker of the utterance to the provided `speaker`.

    Args:
        input_pack: The data pack to add utterance into.
        text: The text of the utterance.
        speaker: The speaker name to be associated with the utterance.

    """
    input_pack.set_text(input_pack.text + '\n' + text)

    u = Utterance(input_pack,
                  len(input_pack.text) - len(text), len(input_pack.text))
    u.speaker = speaker
Beispiel #9
0
 def _parse_pack(self, data_source: str) -> Iterator[DataPack]:
     pack = DataPack()
     self.set_text(pack, data_source)
     Utterance(pack, 0, len(data_source))
     yield pack