예제 #1
0
def train_test_metrics(train_dataset_path,
                       test_dataset_path,
                       output_path,
                       config_path=None,
                       exclude_slot_metrics=False,
                       include_errors=False,
                       verbose=False):
    if verbose:
        set_nlu_logger(logging.DEBUG)

    if config_path is not None:
        with Path(config_path).open("r", encoding="utf-8") as f:
            config = json.load(f)
        engine_cls = make_engine_cls(config)
    else:
        engine_cls = SnipsNLUEngine

    metrics_args = dict(train_dataset=train_dataset_path,
                        test_dataset=test_dataset_path,
                        engine_class=engine_cls,
                        include_slot_metrics=not exclude_slot_metrics)

    with Path(train_dataset_path).open("r", encoding="utf8") as f:
        load_resources(json.load(f)["language"])

    from snips_nlu_metrics import compute_train_test_metrics

    metrics = compute_train_test_metrics(**metrics_args)
    if not include_errors:
        metrics.pop("parsing_errors")

    with Path(output_path).open(mode="w", encoding="utf8") as f:
        f.write(json_string(metrics))
예제 #2
0
def parse(training_path, query, verbose=False, intents_filter=None):
    """Load a trained NLU engine and play with its parsing API interactively"""
    import csv
    import logging
    from builtins import input, str
    from snips_nlu import SnipsNLUEngine
    from snips_nlu.cli.utils import set_nlu_logger

    if verbose == 1:
        set_nlu_logger(logging.INFO)
    elif verbose >= 2:
        set_nlu_logger(logging.DEBUG)
    if intents_filter:
        # use csv in order to properly handle commas and other special
        # characters in intent names
        intents_filter = next(csv.reader([intents_filter]))
    else:
        intents_filter = None

    engine = SnipsNLUEngine.from_path(training_path)

    if query:
        print_parsing_result(engine, query, intents_filter)
        return

    while True:
        query = input("Enter a query (type 'q' to quit): ").strip()
        if not isinstance(query, str):
            query = query.decode("utf-8")
        if query == "q":
            break
        print_parsing_result(engine, query, intents_filter)
예제 #3
0
def parse(training_path, query, verbose=False):
    """Load a trained NLU engine and play with its parsing API interactively"""
    import logging
    from builtins import input, str
    from snips_nlu import SnipsNLUEngine
    from snips_nlu.cli.utils import set_nlu_logger

    if verbose == 1:
        set_nlu_logger(logging.INFO)
    elif verbose >= 2:
        set_nlu_logger(logging.DEBUG)

    engine = SnipsNLUEngine.from_path(training_path)

    if query:
        print_parsing_result(engine, query)
        return

    while True:
        query = input("Enter a query (type 'q' to quit): ").strip()
        if not isinstance(query, str):
            query = query.decode("utf-8")
        if query == "q":
            break
        print_parsing_result(engine, query)
예제 #4
0
def parse(training_path, query, verbose=False):
    """Load a trained NLU engine and play with its parsing API interactively"""
    if verbose:
        set_nlu_logger(logging.DEBUG)

    engine = SnipsNLUEngine.from_path(training_path)

    if query:
        print_parsing_result(engine, query)
        return

    while True:
        query = input("Enter a query (type 'q' to quit): ").strip()
        if query == "q":
            break
        print_parsing_result(engine, query)
예제 #5
0
def cross_val_metrics(dataset_path,
                      output_path,
                      config_path=None,
                      nb_folds=5,
                      train_size_ratio=1.0,
                      exclude_slot_metrics=False,
                      include_errors=False,
                      verbose=0):
    import json
    import logging
    from pathlib import Path
    from snips_nlu_metrics import compute_cross_val_metrics
    from snips_nlu import SnipsNLUEngine
    from snips_nlu.cli.utils import set_nlu_logger
    from snips_nlu.common.utils import json_string

    if verbose == 1:
        set_nlu_logger(logging.INFO)
    elif verbose >= 2:
        set_nlu_logger(logging.DEBUG)

    def progression_handler(progress):
        print("%d%%" % int(progress * 100))

    if config_path is not None:
        with Path(config_path).open("r", encoding="utf-8") as f:
            config = json.load(f)
        engine_cls = make_engine_cls(config)
    else:
        engine_cls = SnipsNLUEngine

    metrics_args = dict(dataset=dataset_path,
                        engine_class=engine_cls,
                        progression_handler=progression_handler,
                        nb_folds=nb_folds,
                        train_size_ratio=train_size_ratio,
                        include_slot_metrics=not exclude_slot_metrics,
                        slot_matching_lambda=_match_trimmed_values)

    metrics = compute_cross_val_metrics(**metrics_args)
    if not include_errors:
        metrics.pop("parsing_errors")

    with Path(output_path).open(mode="w", encoding="utf8") as f:
        f.write(json_string(metrics))
예제 #6
0
def train(dataset_path, output_path, config_path, verbose):
    """Train an NLU engine on the provided dataset"""
    if verbose:
        set_nlu_logger(logging.DEBUG)
    with Path(dataset_path).open("r", encoding="utf8") as f:
        dataset = json.load(f)

    config = None
    if config_path is not None:
        with Path(config_path).open("r", encoding="utf8") as f:
            config = json.load(f)

    print("Create and train the engine...")
    engine = SnipsNLUEngine(config).fit(dataset)

    print("Persisting the engine...")
    engine.persist(output_path)

    print("Saved the trained engine to %s" % output_path)
예제 #7
0
def cross_val_metrics(dataset_path,
                      output_path,
                      config_path=None,
                      nb_folds=5,
                      train_size_ratio=1.0,
                      exclude_slot_metrics=False,
                      include_errors=False,
                      verbose=False):
    if verbose:
        set_nlu_logger(logging.DEBUG)

    def progression_handler(progress):
        print("%d%%" % int(progress * 100))

    if config_path is not None:
        with Path(config_path).open("r", encoding="utf-8") as f:
            config = json.load(f)
        engine_cls = make_engine_cls(config)
    else:
        engine_cls = SnipsNLUEngine

    metrics_args = dict(
        dataset=dataset_path,
        engine_class=engine_cls,
        progression_handler=progression_handler,
        nb_folds=nb_folds,
        train_size_ratio=train_size_ratio,
        include_slot_metrics=not exclude_slot_metrics,
    )

    with Path(dataset_path).open("r", encoding="utf8") as f:
        load_resources(json.load(f)["language"])

    from snips_nlu_metrics import compute_cross_val_metrics

    metrics = compute_cross_val_metrics(**metrics_args)
    if not include_errors:
        metrics.pop("parsing_errors")

    with Path(output_path).open(mode="w", encoding="utf8") as f:
        f.write(json_string(metrics))
예제 #8
0
def train_test_metrics(train_dataset_path,
                       test_dataset_path,
                       output_path,
                       config_path=None,
                       exclude_slot_metrics=False,
                       include_errors=False,
                       verbosity=0):
    import json
    import logging
    from pathlib import Path
    from snips_nlu_metrics import compute_train_test_metrics
    from snips_nlu import SnipsNLUEngine
    from snips_nlu.cli.utils import set_nlu_logger
    from snips_nlu.common.utils import json_string

    if verbosity == 1:
        set_nlu_logger(logging.INFO)
    elif verbosity >= 2:
        set_nlu_logger(logging.DEBUG)

    if config_path is not None:
        with Path(config_path).open("r", encoding="utf-8") as f:
            config = json.load(f)
        engine_cls = make_engine_cls(config)
    else:
        engine_cls = SnipsNLUEngine

    metrics_args = dict(train_dataset=train_dataset_path,
                        test_dataset=test_dataset_path,
                        engine_class=engine_cls,
                        include_slot_metrics=not exclude_slot_metrics,
                        slot_matching_lambda=_match_trimmed_values)

    metrics = compute_train_test_metrics(**metrics_args)
    if not include_errors:
        metrics.pop("parsing_errors")

    with Path(output_path).open(mode="w", encoding="utf8") as f:
        f.write(json_string(metrics))
예제 #9
0
def train(dataset_path,
          output_path,
          config_path=None,
          verbose=False,
          random_seed=None):
    """Train an NLU engine on the provided dataset"""
    import json
    import logging
    from pathlib import Path

    from snips_nlu import SnipsNLUEngine
    from snips_nlu.cli.utils import set_nlu_logger
    from snips_nlu.common.utils import check_random_state

    if verbose == 1:
        set_nlu_logger(logging.INFO)
    elif verbose >= 2:
        set_nlu_logger(logging.DEBUG)

    with Path(dataset_path).open("r", encoding="utf8") as f:
        dataset = json.load(f)

    config = None
    if config_path is not None:
        with Path(config_path).open("r", encoding="utf8") as f:
            config = json.load(f)

    random_state = check_random_state(random_seed)

    print("Create and train the engine...")
    engine = SnipsNLUEngine(config, random_state=random_state).fit(dataset)

    print("Persisting the engine...")
    engine.persist(output_path)

    print("Saved the trained engine to %s" % output_path)