def test_should_persist_parser(self):
        # Given
        parser = BuiltinEntityParser.build("en")

        # When
        with temp_dir() as tmpdir:
            persisted_path = str(tmpdir / "persisted_builtin_parser")
            parser.persist(persisted_path)
            loaded_parser = BuiltinEntityParser.from_path(persisted_path)
        res = loaded_parser.parse("Raise the temperature to 9 degrees", None)

        # Then
        expected_result = [
            {
                "value": "9 degrees",
                "entity": {
                    "kind": "Temperature",
                    "unit": "degree",
                    "value": 9.0
                },
                "range": {"start": 25, "end": 34},
                "entity_kind": "snips/temperature"
            }
        ]
        self.assertListEqual(expected_result, res)
Esempio n. 2
0
    def test_should_not_accept_bytes_in_scope(self):
        # Given
        scope = [b"snips/number", b"snips/datetime"]
        parser = BuiltinEntityParser("en")

        # When/Then
        with self.assertRaises(TypeError):
            parser.parse("Raise to sixty", scope)
Esempio n. 3
0
    def test_should_not_accept_bytes_in_text(self):
        # Given
        parser = BuiltinEntityParser("en")
        bytes_text = b"Raise to sixty"

        # When/Then
        with self.assertRaises(TypeError):
            parser.parse(bytes_text)
Esempio n. 4
0
    def test_should_parse_in_all_languages(self):
        # Given
        all_languages = get_all_languages()
        text = "1234"

        # When / Then
        for language in all_languages:
            parser = BuiltinEntityParser(language)
            parser.parse(text)
    def test_should_parse_without_scope(self):
        # Given
        parser = BuiltinEntityParser("en")

        # When
        res = parser.parse("Raise to sixty two degrees celsius")

        # Then
        expected_result = [
            {
                "entity": {
                    "kind": "Temperature",
                    "unit": "celsius",
                    "value": 62.0
                },
                "entity_kind": "snips/temperature",
                "range": {"end": 34, "start": 9},
                "value": "sixty two degrees celsius"
            }
        ]

        self.assertListEqual(expected_result, res)
    def test_should_parse_with_scope(self):
        # Given
        parser = BuiltinEntityParser("en")
        scope = ["snips/temperature", "snips/number"]

        # When
        res = parser.parse("Raise to sixty two", scope)

        # Then
        expected_result = [
            {
                "entity": {
                    "kind": "Temperature",
                    "unit": None,
                    "value": 62.0
                },
                "entity_kind": "snips/temperature",
                "range": {"end": 18, "start": 9},
                "value": "sixty two"
            }
        ]

        self.assertListEqual(expected_result, res)
    def test_should_persist_parser_with_gazetteer_entities(self):
        # Given
        parser = BuiltinEntityParser.from_path(BUILTIN_PARSER_PATH)

        # When
        with temp_dir() as tmpdir:
            persisted_path = str(tmpdir / "persisted_builtin_parser")
            parser.persist(persisted_path)
            loaded_parser = BuiltinEntityParser.from_path(persisted_path)
        res = loaded_parser.parse("I want to listen to the stones", None)

        # Then
        expected_result = [
            {
                "value": "the stones",
                "entity": {
                    "kind": "MusicArtist",
                    "value": "The Rolling Stones"
                },
                "range": {"start": 20, "end": 30},
                "entity_kind": "snips/musicArtist"
            }
        ]
        self.assertListEqual(expected_result, res)
Esempio n. 8
0
def _build_builtin_parser(language, gazetteer_entities):
    with temp_dir() as serialization_dir:
        gazetteer_entity_parser = None
        if gazetteer_entities:
            gazetteer_entity_parser = _build_gazetteer_parser(
                serialization_dir, gazetteer_entities, language)

        metadata = {
            "language": language.upper(),
            "gazetteer_parser": gazetteer_entity_parser
        }
        metadata_path = serialization_dir / "metadata.json"
        with metadata_path.open("w", encoding="utf-8") as f:
            f.write(json_string(metadata))
        parser = _BuiltinEntityParser.from_path(serialization_dir)
        return BuiltinEntityParser(parser)
    def test_should_load_parser_with_gazetteer_entities_from_path(self):
        # Given
        parser = BuiltinEntityParser.from_path(BUILTIN_PARSER_PATH)

        # When
        res = parser.parse("I want to listen to the stones", None)

        # Then
        expected_result = [
            {
                "value": "the stones",
                "entity": {
                    "kind": "MusicArtist",
                    "value": "The Rolling Stones"
                },
                "range": {"start": 20, "end": 30},
                "entity_kind": "snips/musicArtist"
            }
        ]
        self.assertListEqual(expected_result, res)
    def test_should_load_parser_from_path(self):
        # Given
        parser = BuiltinEntityParser.from_path(
            BUILTIN_PARSER_NO_GAZETTEER_PATH)

        # When
        res = parser.parse("Raise the temperature to 9 degrees", None)

        # Then
        expected_result = [
            {
                "value": "9 degrees",
                "entity": {
                    "kind": "Temperature",
                    "unit": "degree",
                    "value": 9.0
                },
                "range": {"start": 25, "end": 34},
                "entity_kind": "snips/temperature"
            }
        ]

        self.assertListEqual(expected_result, res)
    def test_should_parse_with_gazetteer_entity(self):
        # Given
        gazetteer_parser_path = ROOT_DIR / "data" / "tests" / \
                                "builtin_gazetteer_parser"
        parser = BuiltinEntityParser.build("en", gazetteer_parser_path)
        scope = ["snips/musicArtist"]

        # When
        res = parser.parse("I want to listen to the stones please!", scope)

        # Then
        expected_result = [
            {
                "entity": {
                    "kind": "MusicArtist",
                    "value": "The Rolling Stones"
                },
                "entity_kind": "snips/musicArtist",
                "range": {"end": 30, "start": 20},
                "value": "the stones"
            }
        ]

        self.assertListEqual(expected_result, res)
Esempio n. 12
0
 def test_should_not_accept_bytes_as_language(self):
     with self.assertRaises(TypeError):
         BuiltinEntityParser(b"en")
Esempio n. 13
0
 def from_path(cls, path):
     parser = _BuiltinEntityParser.from_path(path)
     return cls(parser)