예제 #1
0
    def test_http_speech_to_intent_hermes(self):
        response = requests.post(
            self.api_url("speech-to-intent"),
            data=self.wav_bytes,
            params={"outputFormat": "hermes"},
        )
        self.check_status(response)

        result = response.json()
        self.assertEqual(result["type"], "intent")

        nlu_intent = NluIntent.from_dict(result["value"])

        self.assertEqual(nlu_intent.raw_input, "turn on the living room lamp")
        self.assertEqual(nlu_intent.input, "turn on the living room lamp")

        # Intent name and slots
        self.assertEqual(nlu_intent.intent.intent_name, "ChangeLightState")

        slots_by_name = {slot.slot_name: slot for slot in nlu_intent.slots}
        self.assertIn("name", slots_by_name)
        self.assertEqual(slots_by_name["name"].value["value"],
                         "living room lamp")

        self.assertIn("state", slots_by_name)
        self.assertEqual(slots_by_name["state"].value["value"], "on")
예제 #2
0
    def test_http_text_to_intent_hermes(self):
        """Test text-to-intent HTTP endpoint (Hermes format)"""
        response = requests.post(
            self.api_url("text-to-intent"),
            data="set bedroom light to BLUE",
            params={"outputFormat": "hermes"},
        )
        self.check_status(response)

        result = response.json()
        self.assertEqual(result["type"], "intent")

        nlu_intent = NluIntent.from_dict(result["value"])

        # Original text with upper-case COLOR
        self.assertEqual(nlu_intent.raw_input, "set bedroom light to BLUE")

        # Case-corrected text
        self.assertEqual(nlu_intent.input, "set bedroom light to blue")

        # Intent name and slots
        self.assertEqual(nlu_intent.intent.intent_name, "ChangeLightColor")

        slots_by_name = {slot.slot_name: slot for slot in nlu_intent.slots}
        self.assertIn("name", slots_by_name)
        self.assertEqual(slots_by_name["name"].value["value"], "bedroom light")

        self.assertIn("color", slots_by_name)
        self.assertEqual(slots_by_name["color"].value["value"], "blue")
예제 #3
0
    def test_http_nlu_number_range(self):
        """Test recognition with a number range"""
        response = requests.post(
            self.api_url("text-to-intent"),
            data="set a timer for 10 minutes",
            params={"outputFormat": "hermes"},
        )
        self.check_status(response)

        # Shouldn't exist yet
        result = response.json()
        self.assertEqual(result["type"], "intentNotRecognized")

        # Add new intent
        response = requests.get(self.api_url("sentences"),
                                headers={"Accept": "application/json"})
        self.check_status(response)
        sentences = response.json()

        sentences[
            "intents/timer.ini"] = "[SetTimer]\nset a timer for (1..59){minute} minutes\n"

        # Save sentences
        response = requests.post(self.api_url("sentences"), json=sentences)
        self.check_status(response)

        try:
            # Re-train
            response = requests.post(self.api_url("train"))
            self.check_status(response)

            # Should work now
            response = requests.post(
                self.api_url("text-to-intent"),
                data="set a timer for 10 minutes",
                params={"outputFormat": "hermes"},
            )
            self.check_status(response)

            result = response.json()
            self.assertEqual(result["type"], "intent")

            nlu_intent = NluIntent.from_dict(result["value"])

            # Intent name and slots
            self.assertEqual(nlu_intent.intent.intent_name, "SetTimer")

            slots_by_name = {slot.slot_name: slot for slot in nlu_intent.slots}
            self.assertIn("minute", slots_by_name)
            self.assertEqual(slots_by_name["minute"].value["value"], 10)
        finally:
            # Remove sentences
            sentences["intents/timer.ini"] = ""
            response = requests.post(self.api_url("sentences"), json=sentences)
            self.check_status(response)

            # Re-train
            response = requests.post(self.api_url("train"))
            self.check_status(response)
예제 #4
0
    def test_http_nlu_new_slot_value(self):
        """Test recognition with a new slot value"""
        response = requests.post(
            self.api_url("text-to-intent"),
            data="set bedroom light to purple",
            params={"outputFormat": "hermes"},
        )
        self.check_status(response)

        # Shouldn't exist yet
        result = response.json()
        self.assertEqual(result["type"], "intentNotRecognized")

        response = requests.get(self.api_url("slots/color"))
        self.check_status(response)
        original_colors = response.json()

        # Add purple to color slot
        response = requests.post(self.api_url("slots/color"), json=["purple"])
        self.check_status(response)

        # Re-train
        response = requests.post(self.api_url("train"))
        self.check_status(response)

        # Try again
        response = requests.post(
            self.api_url("text-to-intent"),
            data="set bedroom light to purple",
            params={"outputFormat": "hermes"},
        )
        self.check_status(response)

        result = response.json()
        self.assertEqual(result["type"], "intent")

        nlu_intent = NluIntent.from_dict(result["value"])

        # Intent name and slots
        self.assertEqual(nlu_intent.intent.intent_name, "ChangeLightColor")

        slots_by_name = {slot.slot_name: slot for slot in nlu_intent.slots}
        self.assertIn("name", slots_by_name)
        self.assertEqual(slots_by_name["name"].value["value"], "bedroom light")

        self.assertIn("color", slots_by_name)
        self.assertEqual(slots_by_name["color"].value["value"], "purple")

        # Restore colors
        response = requests.post(
            self.api_url("slots/color"),
            json=original_colors,
            params={"overwriteAll": "true"},
        )
        self.check_status(response)

        # Re-train
        response = requests.post(self.api_url("train"))
        self.check_status(response)
예제 #5
0
    def test_http_nlu_new_slot(self):
        """Test recognition with a new slot"""
        response = requests.post(
            self.api_url("text-to-intent"),
            data="what is the weather like in Germany",
            params={"outputFormat": "hermes"},
        )
        self.check_status(response)

        # Shouldn't exist yet
        result = response.json()
        self.assertEqual(result["type"], "intentNotRecognized")

        # Get sentences
        response = requests.get(self.api_url("sentences"),
                                headers={"Accept": "application/json"})
        self.check_status(response)
        sentences = response.json()

        try:
            # Add new slot
            response = requests.post(self.api_url("slots/location"),
                                     json=["Germany", "France"])
            self.check_status(response)

            # Add new intent
            sentences[
                "intents/weather.ini"] = "[GetWeather]\nwhat is the weather like in ($location){location}\n"

            # Save sentences
            response = requests.post(self.api_url("sentences"), json=sentences)
            self.check_status(response)

            # Re-train
            response = requests.post(self.api_url("train"))
            self.check_status(response)

            # Should work now
            response = requests.post(
                self.api_url("text-to-intent"),
                data="what is the weather like in Germany",
                params={"outputFormat": "hermes"},
            )
            self.check_status(response)

            result = response.json()
            self.assertEqual(result["type"], "intent")

            nlu_intent = NluIntent.from_dict(result["value"])

            # Intent name and slots
            self.assertEqual(nlu_intent.intent.intent_name, "GetWeather")

            slots_by_name = {slot.slot_name: slot for slot in nlu_intent.slots}
            self.assertIn("location", slots_by_name)
            self.assertEqual(slots_by_name["location"].value["value"],
                             "Germany")
        finally:
            # Remove slot
            response = requests.post(
                self.api_url("slots/location"),
                json=[],
                params={"overwrite_all": "true"},
            )
            self.check_status(response)

            # Remove sentences
            sentences["intents/weather.ini"] = ""
            response = requests.post(self.api_url("sentences"), json=sentences)
            self.check_status(response)

            # Re-train
            response = requests.post(self.api_url("train"))
            self.check_status(response)