def test_fitting_should_be_reproducible_after_serialization(self): # Given dataset = BEVERAGE_DATASET validated_dataset = validate_and_format_dataset(dataset) seed1 = 666 seed2 = 42 config = ProbabilisticIntentParserConfig( intent_classifier_config=LogRegIntentClassifierConfig( random_seed=seed1), slot_filler_config=CRFSlotFillerConfig(random_seed=seed2)) parser = ProbabilisticIntentParser(config) parser_dict = parser.to_dict() # When fitted_parser_1 = ProbabilisticIntentParser.from_dict(parser_dict).fit( validated_dataset) fitted_parser_2 = ProbabilisticIntentParser.from_dict(parser_dict).fit( validated_dataset) # Then feature_weights_1 = fitted_parser_1.slot_fillers[ "MakeTea"].crf_model.state_features_ feature_weights_2 = fitted_parser_2.slot_fillers[ "MakeTea"].crf_model.state_features_ self.assertEqual(feature_weights_1, feature_weights_2)
def test_should_be_serializable_before_fitting(self): # Given parser = ProbabilisticIntentParser() # When actual_parser_dict = parser.to_dict() # Then expected_parser_dict = { "unit_name": "probabilistic_intent_parser", "config": { "unit_name": "probabilistic_intent_parser", "slot_filler_config": CRFSlotFillerConfig().to_dict(), "intent_classifier_config": LogRegIntentClassifierConfig().to_dict() }, "intent_classifier": None, "slot_fillers": dict(), } self.assertDictEqual(actual_parser_dict, expected_parser_dict)
def test_should_be_serializable(self): # Given class TestIntentClassifierConfig(ProcessingUnitConfig): unit_name = "test_intent_classifier" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestIntentClassifierConfig() class TestIntentClassifier(IntentClassifier): unit_name = "test_intent_classifier" config_type = TestIntentClassifierConfig def get_intent(self, text, intents_filter): return None def fit(self, dataset): return self def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): config = cls.config_type() return TestIntentClassifier(config) class TestSlotFillerConfig(ProcessingUnitConfig): unit_name = "test_slot_filler" def to_dict(self): return {"unit_name": self.unit_name} @classmethod def from_dict(cls, obj_dict): return TestSlotFillerConfig() class TestSlotFiller(SlotFiller): unit_name = "test_slot_filler" config_type = TestSlotFillerConfig def get_slots(self, text): return [] def fit(self, dataset, intent): return self def to_dict(self): return { "unit_name": self.unit_name, } @classmethod def from_dict(cls, unit_dict): config = cls.config_type() return TestSlotFiller(config) register_processing_unit(TestIntentClassifier) register_processing_unit(TestSlotFiller) parser_config = ProbabilisticIntentParserConfig( intent_classifier_config=TestIntentClassifierConfig(), slot_filler_config=TestSlotFillerConfig()) parser = ProbabilisticIntentParser(parser_config) parser.fit(validate_and_format_dataset(BEVERAGE_DATASET)) # When actual_parser_dict = parser.to_dict() # Then expected_parser_config = { "unit_name": "probabilistic_intent_parser", "slot_filler_config": { "unit_name": "test_slot_filler" }, "intent_classifier_config": { "unit_name": "test_intent_classifier" } } expected_parser_dict = { "unit_name": "probabilistic_intent_parser", "config": expected_parser_config, "intent_classifier": { "unit_name": "test_intent_classifier" }, "slot_fillers": { "MakeCoffee": { "unit_name": "test_slot_filler" }, "MakeTea": { "unit_name": "test_slot_filler" }, }, } self.assertDictEqual(actual_parser_dict, expected_parser_dict)