def test_should_get_intent_after_deserialization(self): # Given dataset_stream = io.StringIO(""" --- type: intent name: MakeTea utterances: - make me a cup of tea - i want two cups of tea please - can you prepare one cup of tea ? --- type: intent name: MakeCoffee utterances: - make me a cup of coffee please - brew two cups of coffee - can you prepare one cup of coffee""") dataset = Dataset.from_yaml_files("en", [dataset_stream]).json shared = self.get_shared_data(dataset) classifier = LogRegIntentClassifier(**shared).fit(dataset) classifier.persist(self.tmp_file_path) # When loaded_classifier = LogRegIntentClassifier.from_path( self.tmp_file_path, **shared) result = loaded_classifier.get_intent("Make me two cups of tea") # Then expected_intent = "MakeTea" self.assertEqual(expected_intent, result[RES_INTENT_NAME])
def test_should_get_intent_after_deserialization(self): # Given dataset = validate_and_format_dataset(BEVERAGE_DATASET) classifier = LogRegIntentClassifier().fit(dataset) classifier.persist(self.tmp_file_path) # When loaded_classifier = LogRegIntentClassifier.from_path( self.tmp_file_path) result = loaded_classifier.get_intent("Make me two cups of tea") # Then expected_intent = "MakeTea" self.assertEqual(expected_intent, result[RES_INTENT_NAME])
def test_should_be_deserializable(self, mock_from_dict): # Given mocked_featurizer = Featurizer(LANGUAGE_EN, None) mock_from_dict.return_value = mocked_featurizer intent_list = ["MakeCoffee", "MakeTea", None] coeffs = [ [1.23, 4.5], [6.7, 8.90], [1.01, 2.345], ] intercept = [ 0.34, 0.41, -0.98 ] t_ = 701. config = LogRegIntentClassifierConfig().to_dict() classifier_dict = { "coeffs": coeffs, "intercept": intercept, "t_": t_, "intent_list": intent_list, "config": config, "featurizer": mocked_featurizer.to_dict(), } self.tmp_file_path.mkdir() metadata = {"unit_name": "log_reg_intent_classifier"} self.writeJsonContent(self.tmp_file_path / "metadata.json", metadata) self.writeJsonContent(self.tmp_file_path / "intent_classifier.json", classifier_dict) # When classifier = LogRegIntentClassifier.from_path(self.tmp_file_path) # Then self.assertEqual(classifier.intent_list, intent_list) self.assertIsNotNone(classifier.featurizer) self.assertListEqual(classifier.classifier.coef_.tolist(), coeffs) self.assertListEqual(classifier.classifier.intercept_.tolist(), intercept) self.assertDictEqual(classifier.config.to_dict(), config)
def test_should_get_intent_after_deserialization(self): # Given dataset = validate_and_format_dataset(BEVERAGE_DATASET) classifier = LogRegIntentClassifier().fit(dataset) classifier.persist(self.tmp_file_path) # When builtin_entity_parser = BuiltinEntityParser.build(language="en") custom_entity_parser = CustomEntityParser.build( dataset, CustomEntityParserUsage.WITHOUT_STEMS) loaded_classifier = LogRegIntentClassifier.from_path( self.tmp_file_path, builtin_entity_parser=builtin_entity_parser, custom_entity_parser=custom_entity_parser) result = loaded_classifier.get_intent("Make me two cups of tea") # Then expected_intent = "MakeTea" self.assertEqual(expected_intent, result[RES_INTENT_NAME])