def test_blank_config(): file_config = {} cmdline_args = {} env_vars = {} f = write_file_config(file_config) final_config = MyNLUConfig(f.name, env_vars, cmdline_args) assert final_config.as_dict() == defaults
def test_cmdline_overrides_envvar(): file_config = {"path": "/path/to/dir"} cmdline_args = {"path": "/another/path"} env_vars = {"MYNLU_PATH": "/alternate/path"} f = write_file_config(file_config) final_config = MyNLUConfig(f.name, env_vars, cmdline_args) assert final_config['path'] == "/another/path"
def test_file_config_unchanged(): file_config = {"path": "/path/to/dir"} cmdline_args = {} env_vars = {} f = write_file_config(file_config) final_config = MyNLUConfig(f.name, env_vars, cmdline_args) assert final_config['path'] == "/path/to/dir"
def test_pipeline_splits_list(): file_config = {} cmdline_args = {"pipeline": "nlp_spacy,ner_spacy"} env_vars = {} f = write_file_config(file_config) final_config = MyNLUConfig(f.name, env_vars, cmdline_args) assert final_config['pipeline'] == ["nlp_spacy", "ner_spacy"]
def app(component_builder): """ This fixture makes use of the IResource interface of the Klein application to mock HTTP server. :param component_builder: :return: """ if "TRAVIS_BUILD_DIR" in os.environ: root_dir = os.environ["TRAVIS_BUILD_DIR"] else: root_dir = os.getcwd() _, nlu_log_file = tempfile.mkstemp(suffix="_mynlu_logs.json") _config = { 'write': nlu_log_file, 'port': -1, # unused in test app "pipeline": "keyword", "path": os.path.join(root_dir, "test_models"), "data": os.path.join(root_dir, "data/demo-restaurants.json"), "server_model_dirs": { "one": "test_model_mitie", "two": "test_model_mitie_sklearn", "three": "test_model_spacy_sklearn", }, "max_training_processes": 1 } config = MyNLUConfig(cmdline_args=_config) mynlu = MyNLU(config, component_builder, True) return StubTreq(mynlu.app.resource())
def test_invalid_pipeline_template(): file_config = {} cmdline_args = {"pipeline": "my_made_up_name"} env_vars = {} f = write_file_config(file_config) with pytest.raises(InvalidConfigError) as execinfo: MyNLUConfig(f.name, env_vars, cmdline_args) assert "unknown pipeline template" in str(execinfo.value)
def base_test_conf(pipeline_template): return MyNLUConfig(cmdline_args={ 'response_log': temp_log_file_dir(), 'port': 5022, "pipeline": registry.registered_pipeline_templates.get(pipeline_template, []), "path": tempfile.mkdtemp(), "data": "./data/test/demo-mynlu-small.json" })
def test_pipeline_looksup_registry(): pipeline_template = list(registered_pipeline_templates)[0] file_config = {} cmdline_args = {"pipeline": pipeline_template} env_vars = {} f = write_file_config(file_config) final_config = MyNLUConfig(f.name, env_vars, cmdline_args) assert final_config['pipeline'] == registered_pipeline_templates[ pipeline_template]
def test_invalid_config_json(): file_config = """{"pipeline": [mitie]}""" # invalid json cmdline_args = {} env_vars = {} with tempfile.NamedTemporaryFile("w+", suffix="_tmp_config_file.json") as f: f.write(file_config) f.flush() with pytest.raises(mynlu.config.InvalidConfigError): MyNLUConfig(f.name, env_vars, cmdline_args)
def train(cfg_name, model_name): from mynlu.cli.train import create_persistor from mynlu.converters import load_data config = MyNLUConfig(cfg_name) trainer = Trainer(config) training_data = load_data(config['data']) trainer.train(training_data) persistor = create_persistor(config) trainer.persist("test_models", persistor, model_name=model_name)
def test_automatically_converts_to_unicode(): env_args = { "some_test_key_u": str("some test value"), str("some_test_key_str"): str("some test value") } cmd_args = { "some_other_test_key_str": str("some test value"), str("some_other_test_key_u"): str("some test value") } final_config = MyNLUConfig(CONFIG_DEFAULTS_PATH, env_args, cmd_args) assert type(final_config["some_test_key_u"]) is Text assert type(final_config["some_test_key_str"]) is Text assert type(final_config["some_other_test_key_str"]) is Text assert type(final_config["some_other_test_key_u"]) is Text
def zh_test_conf_nlp_mitie(): return MyNLUConfig(cmdline_args={ 'response_log': temp_log_file_dir(), 'port': 5023, "pipeline": ["nlp_mitie", "tokenizer_jieba", "ner_mitie", "ner_synonyms", "intent_entity_featurizer_regex", "intent_featurizer_mitie", "intent_classifier_sklearn"], "path": tempfile.mkdtemp(), "language": "zh", "mitie_file": "./data/total_word_feature_extractor_zh.dat", "data": "./data/test/demo-mynlu-small.json" })
def test_default_config_file(): final_config = MyNLUConfig() assert len(final_config) > 1
def default_config(): return MyNLUConfig(CONFIG_DEFAULTS_PATH)