def test_should_cleanup(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: MakeTea utterances: - make me a [beverage_temperature:Temperature](hot) cup of tea - make me [number_of_cups:snips/number](five) tea cups""") dataset = Dataset.from_yaml_files("en", [dataset_stream]) slot_filler = CRFSlotFiller().fit(dataset, "MakeTea") crf_file = Path(slot_filler.crf_model.modelfile.name) self.assertTrue(crf_file.exists()) # When slot_filler._cleanup() # pylint:disable=protected-access # Then self.assertFalse(crf_file.exists())
def test_should_be_deserializable_before_fit(self, mock_deserialize_crf_model): # Given mock_deserialize_crf_model.return_value = None features_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1 }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] slot_filler_config = CRFSlotFillerConfig( feature_factory_configs=features_factories) slot_filler_dict = { "unit_name": "crf_slot_filler", "crf_model_data": None, "language_code": None, "intent": None, "slot_name_mapping": None, "config": slot_filler_config.to_dict() } # When slot_filler = CRFSlotFiller.from_dict(slot_filler_dict) # Then expected_features_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1 }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] expected_language = None expected_config = CRFSlotFillerConfig( feature_factory_configs=expected_features_factories) expected_intent = None expected_slot_name_mapping = None expected_crf_model = None self.assertEqual(slot_filler.crf_model, expected_crf_model) self.assertEqual(slot_filler.language, expected_language) self.assertEqual(slot_filler.intent, expected_intent) self.assertEqual(slot_filler.slot_name_mapping, expected_slot_name_mapping) self.assertDictEqual(expected_config.to_dict(), slot_filler.config.to_dict())
def test_should_compute_features(self): # Given features_factories = [ { "factory_name": NgramFactory.name, "args": { "n": 1, "use_stemming": False, "common_words_gazetteer_name": None }, "offsets": [0], "drop_out": 0.3 }, ] slot_filler_config = CRFSlotFillerConfig( feature_factory_configs=features_factories, random_seed=40) tokens = tokenize("foo hello world bar", LANGUAGE_EN) dataset_stream = io.StringIO(""" --- type: intent name: my_intent utterances: - this is [slot1:entity1](my first entity) - this is [slot2:entity2](second_entity)""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json shared = self.get_shared_data( dataset, CustomEntityParserUsage.WITHOUT_STEMS) slot_filler = CRFSlotFiller(slot_filler_config, **shared) slot_filler.fit(dataset, intent="my_intent") # When features_with_drop_out = slot_filler.compute_features(tokens, True) # Then expected_features = [ {"ngram_1": "foo"}, {}, {"ngram_1": "world"}, {}, ] self.assertListEqual(expected_features, features_with_drop_out)
def test_should_get_sub_builtin_slots(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: PlanBreak utterances: - 'I want to leave from [start:snips/datetime](tomorrow) until [end:snips/datetime](next thursday)' - find me something from [start](9am) to [end](12pm) - I need a break from [start](2pm) until [end](4pm) - Can you suggest something from [start](april 4th) until [end](april 6th) ? - find an activity from [start](6pm) to [end](8pm) - Book me a trip from [start](this friday) to [end](next tuesday)""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json intent = "PlanBreak" shared = self.get_shared_data(dataset) shared[RANDOM_STATE] = 42 slot_filler = CRFSlotFiller(**shared) slot_filler.fit(dataset, intent) # When slots = slot_filler.get_slots("Find me a plan from 5pm to 6pm") # Then expected_slots = [ unresolved_slot(match_range={ START: 20, END: 23 }, value="5pm", entity="snips/datetime", slot_name="start"), unresolved_slot(match_range={ START: 27, END: 30 }, value="6pm", entity="snips/datetime", slot_name="end") ] self.assertListEqual(expected_slots, slots)
def test_should_parse_naughty_strings(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: my_intent utterances: - this is [entity1](my first entity)""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json naughty_strings_path = TEST_PATH / "resources" / "naughty_strings.txt" with naughty_strings_path.open(encoding='utf8') as f: naughty_strings = [line.strip("\n") for line in f.readlines()] # When shared = self.get_shared_data(dataset) slot_filler = CRFSlotFiller(**shared).fit(dataset, "my_intent") # Then for s in naughty_strings: with self.fail_if_exception("Naughty string crashes"): slot_filler.get_slots(s)
def test_should_be_serializable_into_bytearray(self): # Given dataset = BEVERAGE_DATASET slot_filler = CRFSlotFiller().fit(dataset, "MakeTea") # When slot_filler_bytes = slot_filler.to_byte_array() loaded_slot_filler = CRFSlotFiller.from_byte_array(slot_filler_bytes) slots = loaded_slot_filler.get_slots("make me two cups of tea") # Then expected_slots = [ unresolved_slot(match_range={ START: 8, END: 11 }, value='two', entity='snips/number', slot_name='number_of_cups') ] self.assertListEqual(expected_slots, slots)
def test_should_fit_and_parse_empty_intent(self): # Given dataset = { "intents": { "dummy_intent": { "utterances": [ { "data": [ { "text": " " } ] } ] } }, "language": "en", "entities": dict() } slot_filler = CRFSlotFiller(**self.get_shared_data(dataset)) # When slot_filler.fit(dataset, "dummy_intent") slot_filler.get_slots("ya")
def test_should_get_builtin_slots(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: GetWeather utterances: - what is the weather [datetime:snips/datetime](at 9pm) - what's the weather in [location:weather_location](berlin) - What's the weather in [location](tokyo) [datetime](this weekend)? - Can you tell me the weather [datetime] please ? - what is the weather forecast [datetime] in [location](paris)""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json intent = "GetWeather" shared = self.get_shared_data(dataset) shared[RANDOM_STATE] = 42 slot_filler = CRFSlotFiller(**shared) slot_filler.fit(dataset, intent) # When slots = slot_filler.get_slots("Give me the weather at 9pm in Paris") # Then expected_slots = [ unresolved_slot(match_range={ START: 20, END: 26 }, value='at 9pm', entity='snips/datetime', slot_name='datetime'), unresolved_slot(match_range={ START: 30, END: 35 }, value='Paris', entity='weather_location', slot_name='location') ] self.assertListEqual(expected_slots, slots)
def test_should_be_serializable(self): # Given features_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1 }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] config = CRFSlotFillerConfig( tagging_scheme=TaggingScheme.BILOU, feature_factory_configs=features_factories) dataset = SAMPLE_DATASET slot_filler = CRFSlotFiller(config) intent = "dummy_intent_1" slot_filler.fit(dataset, intent=intent) # When slot_filler.persist(self.tmp_file_path) # Then metadata_path = self.tmp_file_path / "metadata.json" self.assertJsonContent(metadata_path, {"unit_name": "crf_slot_filler"}) expected_crf_file = Path(slot_filler.crf_model.modelfile.name).name self.assertTrue((self.tmp_file_path / expected_crf_file).exists()) expected_feature_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1, "language_code": "en" }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] expected_config = CRFSlotFillerConfig( tagging_scheme=TaggingScheme.BILOU, feature_factory_configs=expected_feature_factories) expected_slot_filler_dict = { "crf_model_file": expected_crf_file, "language_code": "en", "config": expected_config.to_dict(), "intent": intent, "slot_name_mapping": { "dummy_slot_name": "dummy_entity_1", "dummy_slot_name2": "dummy_entity_2", "dummy_slot_name3": "dummy_entity_2", } } slot_filler_path = self.tmp_file_path / "slot_filler.json" self.assertJsonContent(slot_filler_path, expected_slot_filler_dict)
def test_should_not_use_crf_when_dataset_with_no_slots(self): # Given dataset = { "language": "en", "intents": { "intent1": { "utterances": [{ "data": [{ "text": "This is an utterance without " "slots" }] }] } }, "entities": {} } slot_filler = CRFSlotFiller(**self.get_shared_data(dataset)) mock_compute_features = MagicMock() slot_filler.compute_features = mock_compute_features # When slot_filler.fit(dataset, "intent1") slots = slot_filler.get_slots("This is an utterance without slots") # Then mock_compute_features.assert_not_called() self.assertListEqual([], slots)
def test_should_get_slots(self): # Given dataset = validate_and_format_dataset(BEVERAGE_DATASET) config = CRFSlotFillerConfig(random_seed=42) intent = "MakeTea" slot_filler = CRFSlotFiller(config) slot_filler.fit(dataset, intent) # When slots = slot_filler.get_slots("make me two cups of tea") # Then expected_slots = [ unresolved_slot(match_range={ START: 8, END: 11 }, value='two', entity='snips/number', slot_name='number_of_cups') ] self.assertListEqual(slots, expected_slots)
def test_should_get_builtin_slots(self): # Given dataset = validate_and_format_dataset(WEATHER_DATASET) config = CRFSlotFillerConfig(random_seed=42) intent = "SearchWeatherForecast" slot_filler = CRFSlotFiller(config) slot_filler.fit(dataset, intent) # When slots = slot_filler.get_slots("Give me the weather at 9p.m. in Paris") # Then expected_slots = [ unresolved_slot(match_range={START: 20, END: 28}, value='at 9p.m.', entity='snips/datetime', slot_name='datetime'), unresolved_slot(match_range={START: 32, END: 37}, value='Paris', entity='weather_location', slot_name='location') ] self.assertListEqual(expected_slots, slots)
def test_should_compute_features(self): # Given features_factories = [ { "factory_name": NgramFactory.name, "args": { "n": 1, "use_stemming": False, "common_words_gazetteer_name": None }, "offsets": [0], "drop_out": 0.3 }, ] slot_filler_config = CRFSlotFillerConfig( feature_factory_configs=features_factories, random_seed=40) slot_filler = CRFSlotFiller(slot_filler_config) tokens = tokenize("foo hello world bar", LANGUAGE_EN) dataset = validate_and_format_dataset(SAMPLE_DATASET) slot_filler.fit(dataset, intent="dummy_intent_1") # When features_with_drop_out = slot_filler.compute_features(tokens, True) # Then expected_features = [ { "ngram_1": "foo" }, {}, { "ngram_1": "world" }, {}, ] self.assertListEqual(expected_features, features_with_drop_out)
def test_refit(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: my_intent utterances: - this is [entity1](my first entity)""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json updated_dataset_stream = io.StringIO(""" --- type: intent name: my_intent utterances: - this is [entity1](my first entity) - this is [entity1](my first entity) again""") updated_dataset = Dataset.from_yaml_files( "en", [updated_dataset_stream]).json config = CRFSlotFillerConfig(feature_factory_configs=[ { "args": { "common_words_gazetteer_name": "top_10000_words_stemmed", "use_stemming": True, "n": 1 }, "factory_name": "ngram", "offsets": [-2, -1, 0, 1, 2] }, ]) # When slot_filler = CRFSlotFiller(config).fit(dataset, "my_intent") # Then slot_filler.fit(updated_dataset, "my_intent")
def test_should_be_serializable_before_fit(self): # Given features_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1 }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] config = CRFSlotFillerConfig( tagging_scheme=TaggingScheme.BILOU, feature_factory_configs=features_factories) slot_filler = CRFSlotFiller(config) # When slot_filler.persist(self.tmp_file_path) # Then metadata_path = self.tmp_file_path / "metadata.json" self.assertJsonContent(metadata_path, {"unit_name": "crf_slot_filler"}) expected_slot_filler_dict = { "unit_name": "crf_slot_filler", "crf_model_file": None, "language_code": None, "config": config.to_dict(), "intent": None, "slot_name_mapping": None, } slot_filler_path = self.tmp_file_path / "slot_filler.json" self.assertJsonContent(slot_filler_path, expected_slot_filler_dict)
def test_training_should_be_reproducible(self): # Given random_state = 42 dataset_stream = io.StringIO(""" --- type: intent name: MakeTea utterances: - make me a [beverage_temperature:Temperature](hot) cup of tea - make me [number_of_cups:snips/number](five) tea cups""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json # When slot_filler1 = CRFSlotFiller(random_state=random_state) slot_filler1.fit(dataset, "MakeTea") slot_filler2 = CRFSlotFiller(random_state=random_state) slot_filler2.fit(dataset, "MakeTea") # Then self.assertDictEqual(slot_filler1.crf_model.state_features_, slot_filler2.crf_model.state_features_) self.assertDictEqual(slot_filler1.crf_model.transition_features_, slot_filler2.crf_model.transition_features_)
def test_should_be_serializable_when_fitted_without_slots(self): # Given features_factories = [ { "factory_name": ShapeNgramFactory.name, "args": {"n": 1}, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] } ] config = CRFSlotFillerConfig( tagging_scheme=TaggingScheme.BILOU, feature_factory_configs=features_factories) dataset = { "language": "en", "intents": { "intent1": { "utterances": [ { "data": [ { "text": "This is an utterance without " "slots" } ] } ] } }, "entities": {} } slot_filler = CRFSlotFiller(config, **self.get_shared_data(dataset)) slot_filler.fit(dataset, intent="intent1") # When slot_filler.persist(self.tmp_file_path) # Then metadata_path = self.tmp_file_path / "metadata.json" self.assertJsonContent(metadata_path, {"unit_name": "crf_slot_filler"}) self.assertIsNone(slot_filler.crf_model)
def test_should_fit_with_naughty_strings_no_tags(self): # Given naughty_strings_path = TEST_PATH / "resources" / "naughty_strings.txt" with naughty_strings_path.open(encoding='utf8') as f: naughty_strings = [line.strip("\n") for line in f.readlines()] utterances = [{DATA: [{TEXT: naughty_string}]} for naughty_string in naughty_strings] # When naughty_dataset = { "intents": { "naughty_intent": { "utterances": utterances } }, "entities": dict(), "language": "en", } # Then with self.fail_if_exception("Naughty string crashes"): shared = self.get_shared_data(naughty_dataset) CRFSlotFiller(**shared).fit(naughty_dataset, "naughty_intent")
def test_should_be_deserializable(self): # Given language = LANGUAGE_EN feature_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1, "language_code": language }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] slot_filler_config = CRFSlotFillerConfig( feature_factory_configs=feature_factories) slot_filler_dict = { "unit_name": "crf_slot_filler", "crf_model_file": "foobar.crfsuite", "language_code": "en", "intent": "dummy_intent_1", "slot_name_mapping": { "dummy_intent_1": { "dummy_slot_name": "dummy_entity_1", } }, "config": slot_filler_config.to_dict() } metadata = {"unit_name": "crf_slot_filler"} self.tmp_file_path.mkdir() self.writeJsonContent(self.tmp_file_path / "metadata.json", metadata) self.writeJsonContent(self.tmp_file_path / "slot_filler.json", slot_filler_dict) self.writeFileContent(self.tmp_file_path / "foobar.crfsuite", "foo bar") # When slot_filler = CRFSlotFiller.from_path(self.tmp_file_path) # Then expected_language = LANGUAGE_EN expected_feature_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1, "language_code": language }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] expected_config = CRFSlotFillerConfig( feature_factory_configs=expected_feature_factories) expected_intent = "dummy_intent_1" expected_slot_name_mapping = { "dummy_intent_1": { "dummy_slot_name": "dummy_entity_1", } } self.assertEqual(slot_filler.language, expected_language) self.assertEqual(slot_filler.intent, expected_intent) self.assertEqual(slot_filler.slot_name_mapping, expected_slot_name_mapping) self.assertDictEqual(expected_config.to_dict(), slot_filler.config.to_dict()) crf_path = Path(slot_filler.crf_model.modelfile.name) self.assertFileContent(crf_path, "foo bar")
def test_should_be_serializable(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: my_intent utterances: - this is [slot1:entity1](my first entity) - this is [slot2:entity2](second_entity)""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json features_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1 }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] config = CRFSlotFillerConfig( tagging_scheme=TaggingScheme.BILOU, feature_factory_configs=features_factories) shared = self.get_shared_data(dataset) slot_filler = CRFSlotFiller(config, **shared) intent = "my_intent" slot_filler.fit(dataset, intent=intent) # When slot_filler.persist(self.tmp_file_path) # Then metadata_path = self.tmp_file_path / "metadata.json" self.assertJsonContent(metadata_path, {"unit_name": "crf_slot_filler"}) self.assertTrue((self.tmp_file_path / CRF_MODEL_FILENAME).exists()) expected_feature_factories = [{ "factory_name": ShapeNgramFactory.name, "args": { "n": 1, "language_code": "en" }, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] }] expected_config = CRFSlotFillerConfig( tagging_scheme=TaggingScheme.BILOU, feature_factory_configs=expected_feature_factories) expected_slot_filler_dict = { "crf_model_file": CRF_MODEL_FILENAME, "language_code": "en", "config": expected_config.to_dict(), "intent": intent, "slot_name_mapping": { "slot1": "entity1", "slot2": "entity2", } } slot_filler_path = self.tmp_file_path / "slot_filler.json" self.assertJsonContent(slot_filler_path, expected_slot_filler_dict)
def test_should_be_deserializable(self, mock_deserialize_crf_model): # Given language = LANGUAGE_EN mock_deserialize_crf_model.return_value = None feature_factories = [ { "factory_name": ShapeNgramFactory.name, "args": {"n": 1, "language_code": language}, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] } ] slot_filler_config = CRFSlotFillerConfig( feature_factory_configs=feature_factories) slot_filler_dict = { "unit_name": "crf_slot_filler", "crf_model_data": "mocked_crf_model_data", "language_code": "en", "intent": "dummy_intent_1", "slot_name_mapping": { "dummy_intent_1": { "dummy_slot_name": "dummy_entity_1", } }, "config": slot_filler_config.to_dict() } # When slot_filler = CRFSlotFiller.from_dict(slot_filler_dict) # Then mock_deserialize_crf_model.assert_called_once_with( "mocked_crf_model_data") expected_language = LANGUAGE_EN expected_feature_factories = [ { "factory_name": ShapeNgramFactory.name, "args": {"n": 1, "language_code": language}, "offsets": [0] }, { "factory_name": IsDigitFactory.name, "args": {}, "offsets": [-1, 0] } ] expected_config = CRFSlotFillerConfig( feature_factory_configs=expected_feature_factories) expected_intent = "dummy_intent_1" expected_slot_name_mapping = { "dummy_intent_1": { "dummy_slot_name": "dummy_entity_1", } } self.assertEqual(slot_filler.language, expected_language) self.assertEqual(slot_filler.intent, expected_intent) self.assertEqual(slot_filler.slot_name_mapping, expected_slot_name_mapping) self.assertDictEqual(expected_config.to_dict(), slot_filler.config.to_dict())
def test_augment_slots(self): # Given language = LANGUAGE_EN text = "Find me a flight before 10pm and after 8pm" tokens = tokenize(text, language) missing_slots = {"start_date", "end_date"} tags = ['O' for _ in tokens] def mocked_sequence_probability(_, tags_): tags_1 = [ 'O', 'O', 'O', 'O', '%sstart_date' % BEGINNING_PREFIX, '%sstart_date' % INSIDE_PREFIX, 'O', '%send_date' % BEGINNING_PREFIX, '%send_date' % INSIDE_PREFIX ] tags_2 = [ 'O', 'O', 'O', 'O', '%send_date' % BEGINNING_PREFIX, '%send_date' % INSIDE_PREFIX, 'O', '%sstart_date' % BEGINNING_PREFIX, '%sstart_date' % INSIDE_PREFIX ] tags_3 = ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'] tags_4 = [ 'O', 'O', 'O', 'O', 'O', 'O', 'O', '%sstart_date' % BEGINNING_PREFIX, '%sstart_date' % INSIDE_PREFIX ] tags_5 = [ 'O', 'O', 'O', 'O', 'O', 'O', 'O', '%send_date' % BEGINNING_PREFIX, '%send_date' % INSIDE_PREFIX ] tags_6 = [ 'O', 'O', 'O', 'O', '%sstart_date' % BEGINNING_PREFIX, '%sstart_date' % INSIDE_PREFIX, 'O', 'O', 'O' ] tags_7 = [ 'O', 'O', 'O', 'O', '%send_date' % BEGINNING_PREFIX, '%send_date' % INSIDE_PREFIX, 'O', 'O', 'O' ] tags_8 = [ 'O', 'O', 'O', 'O', '%sstart_date' % BEGINNING_PREFIX, '%sstart_date' % INSIDE_PREFIX, 'O', '%sstart_date' % BEGINNING_PREFIX, '%sstart_date' % INSIDE_PREFIX ] tags_9 = [ 'O', 'O', 'O', 'O', '%send_date' % BEGINNING_PREFIX, '%send_date' % INSIDE_PREFIX, 'O', '%send_date' % BEGINNING_PREFIX, '%send_date' % INSIDE_PREFIX ] if tags_ == tags_1: return 0.6 elif tags_ == tags_2: return 0.8 elif tags_ == tags_3: return 0.2 elif tags_ == tags_4: return 0.2 elif tags_ == tags_5: return 0.99 elif tags_ == tags_6: return 0.0 elif tags_ == tags_7: return 0.0 elif tags_ == tags_8: return 0.5 elif tags_ == tags_9: return 0.5 else: raise ValueError("Unexpected tag sequence: %s" % tags_) slot_filler_config = CRFSlotFillerConfig(random_seed=42) slot_filler = CRFSlotFiller(config=slot_filler_config) slot_filler.language = LANGUAGE_EN slot_filler.intent = "intent1" slot_filler.slot_name_mapping = { "start_date": "snips/datetime", "end_date": "snips/datetime", } # pylint:disable=protected-access slot_filler._get_sequence_probability = MagicMock( side_effect=mocked_sequence_probability) # pylint:enable=protected-access slot_filler.compute_features = MagicMock(return_value=None) # When # pylint: disable=protected-access augmented_slots = slot_filler._augment_slots(text, tokens, tags, missing_slots) # pylint: enable=protected-access # Then expected_slots = [ unresolved_slot(value='after 8pm', match_range={ START: 33, END: 42 }, entity='snips/datetime', slot_name='end_date') ] self.assertListEqual(augmented_slots, expected_slots)