def test_train_load_predict_loop( default_model_storage: ModelStorage, default_execution_context: ExecutionContext, mitie_model: MitieModel, mitie_tokenizer: MitieTokenizer, ): resource = Resource("mitie_classifier") component = MitieIntentClassifier.create( MitieIntentClassifier.get_default_config(), default_model_storage, resource, default_execution_context, ) training_data = rasa.shared.nlu.training_data.loading.load_data( "data/examples/rasa/demo-rasa.yml") # Tokenize message as classifier needs that mitie_tokenizer.process_training_data(training_data) component.train(training_data, mitie_model) component = MitieIntentClassifier.load( MitieIntentClassifier.get_default_config(), default_model_storage, resource, default_execution_context, ) test_message = Message({TEXT: "hi"}) mitie_tokenizer.process([test_message]) component.process([test_message], mitie_model) assert test_message.data[INTENT][INTENT_NAME_KEY] == "greet" assert test_message.data[INTENT][PREDICTED_CONFIDENCE_KEY] > 0
def test_mitie_featurizer( create: Callable[[Dict[Text, Any]], MitieFeaturizer], mitie_model: MitieModel, mitie_tokenizer: MitieTokenizer, ): featurizer = create({"alias": "mitie_featurizer"}) sentence = "Hey how are you today" message = Message(data={TEXT: sentence}) mitie_tokenizer.process([message]) tokens = message.get(TOKENS_NAMES[TEXT]) seq_vec, sen_vec = featurizer.features_for_tokens( tokens, mitie_model.word_feature_extractor) expected = np.array([ 0.00000000e00, -5.12735510e00, 4.39929873e-01, -5.60760403e00, -8.26445103e00 ]) expected_cls = np.array( [0.0, -4.4551446, 0.26073121, -1.46632245, -1.84205751]) assert 6 == len(seq_vec) + len(sen_vec) assert np.allclose(seq_vec[0][:5], expected, atol=1e-5) assert np.allclose(sen_vec[-1][:5], expected_cls, atol=1e-5)
def test_load_from_untrained( default_model_storage: ModelStorage, default_execution_context: ExecutionContext, mitie_model: MitieModel, mitie_tokenizer: MitieTokenizer, ): resource = Resource("some_resource") component = MitieIntentClassifier.load( MitieIntentClassifier.get_default_config(), default_model_storage, resource, default_execution_context, ) test_message = Message({TEXT: "hi"}) mitie_tokenizer.process([test_message]) component.process([test_message], mitie_model) assert test_message.data[INTENT] == {"name": None, "confidence": 0.0}
def test_load_from_untrained_but_with_resource_existing( default_model_storage: ModelStorage, default_execution_context: ExecutionContext, mitie_model: MitieModel, mitie_tokenizer: MitieTokenizer, ): resource = Resource("some_resource") with default_model_storage.write_to(resource): # This makes sure the directory exists but the model file itself doesn't pass component = MitieIntentClassifier.load( MitieIntentClassifier.get_default_config(), default_model_storage, resource, default_execution_context, ) test_message = Message({TEXT: "hi"}) mitie_tokenizer.process([test_message]) component.process([test_message], mitie_model) assert test_message.data[INTENT] == {"name": None, "confidence": 0.0}