Esempio n. 1
0
def link(origin, link_name, force=False, resources_path=None):
    """
    Create a symlink for language resources within the snips_nlu/data
    directory. Accepts either the name of a pip package, or the local path to
    the resources data directory.

    Linking resources allows loading them via
    snips_nlu.load_resources(link_name).
    """
    if is_package(origin):
        resources_path = get_package_path(origin)
    else:
        resources_path = Path(origin) if resources_path is None \
            else Path(resources_path)
    if not resources_path.exists():
        raise OSError("%s not found" % str(resources_path))
    link_path = DATA_PATH / link_name
    if link_path.is_symlink() and not force:
        raise OSError("Symlink already exists: %s" % str(link_path))
    elif link_path.is_symlink():
        link_path.unlink()
    elif link_path.exists():
        raise OSError("Symlink cannot be overwritten: %s" % str(link_path))
    resources_sub_dir = get_resources_sub_directory(resources_path)
    create_symlink(link_path, resources_sub_dir)
    pretty_print("%s --> %s" % (str(resources_sub_dir), str(link_path)),
                 "You can now load the resources via "
                 "snips_nlu.load_resources('%s')" % link_name,
                 title="Linking successful", level=PrettyPrintLevel.SUCCESS)
Esempio n. 2
0
def load_resources(name):
    """Load language specific resources

    Args:
        name (str): Resource name as in ``snips-nlu download <name>``. Can also
            be the name of a python package or a directory path.

    Note:
        Language resources must be loaded before fitting or parsing
    """
    if name in set(d.name for d in DATA_PATH.iterdir()):
        load_resources_from_dir(DATA_PATH / name)
    elif is_package(name):
        package_path = get_package_path(name)
        resources_sub_dir = get_resources_sub_directory(package_path)
        load_resources_from_dir(resources_sub_dir)
    elif Path(name).exists():
        path = Path(name)
        if (path / "__init__.py").exists():
            path = get_resources_sub_directory(path)
        load_resources_from_dir(path)
    else:
        raise MissingResource("Language resource '{r}' not found. This may be "
                              "solved by running "
                              "'python -m snips_nlu download {r}'"
                              .format(r=name))
Esempio n. 3
0
def link_resources(origin, link_name, force, resources_path):
    if is_package(origin):
        resources_path = get_package_path(origin)
    else:
        resources_path = Path(origin) if resources_path is None \
            else Path(resources_path)
    if not resources_path.exists():
        raise OSError("%s not found" % str(resources_path))
    link_path = DATA_PATH / link_name
    if link_path.is_symlink() and not force:
        raise OSError("Symlink already exists: %s" % str(link_path))
    elif link_path.is_symlink():
        link_path.unlink()
    elif link_path.exists():
        raise OSError("Symlink cannot be overwritten: %s" % str(link_path))
    resources_sub_dir = get_resources_sub_directory(resources_path)
    create_symlink(link_path, resources_sub_dir)
    return link_path, resources_sub_dir
Esempio n. 4
0
def load_resources(name):
    """Load language specific resources

    Args:
        name (str): resource name

    Note:
        Language resources must be loaded before fitting or parsing
    """
    if name in set(d.name for d in DATA_PATH.iterdir()):
        _load_resources_from_dir(DATA_PATH / name)
    elif is_package(name):
        package_path = get_package_path(name)
        _load_resources_from_dir(package_path)
    elif Path(name).exists():
        _load_resources_from_dir(Path(name))
    else:
        raise MissingResource(
            "Language resource '{r}' not found. This may be "
            "solved by running 'snips-nlu download {r}'".format(r=name))