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)
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)
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 from_path(cls, path): from snips_nlu_parsers import (BuiltinEntityParser as _BuiltinEntityParser) parser = _BuiltinEntityParser.from_path(path) return cls(parser)
def from_path(cls, path): parser = _BuiltinEntityParser.from_path(path) return cls(parser)