Esempio n. 1
0
def _download_and_link_entity(long_resource_name, entity_name, language,
                              compatibility, pip_args):
    full_resource_name = long_resource_name + "_" + language
    version = get_resources_version(full_resource_name, entity_name,
                                    compatibility)
    entity_alias = get_builtin_entity_shortname(entity_name).lower()
    entity_base_url = _get_entity_base_url(language, entity_alias, version)
    latest = get_json(entity_base_url + "/latest",
                      "Latest entity resources version")
    latest_url = "{b}/{n}#egg={r}=={v}".format(
        b=entity_base_url, n=latest["filename"], r=full_resource_name,
        v=latest["version"])
    exit_code = install_remote_package(latest_url, pip_args)
    if exit_code != 0:
        sys.exit(exit_code)
    try:
        # Get package path here because link uses
        # pip.get_installed_distributions() to check if the resource is a
        # package, which fails if the resource was just installed via
        # subprocess
        package_path = get_package_path(full_resource_name)
        link_alias = entity_alias + "_" + language
        link_path, resources_dir = link_resources(
            full_resource_name, link_alias, force=True,
            resources_path=package_path)
        pretty_print("%s --> %s" % (str(resources_dir), str(link_path)),
                     "You can now use the '%s' builtin entity" % entity_name,
                     title="Linking successful",
                     level=PrettyPrintLevel.SUCCESS)
    except:  # pylint:disable=bare-except
        pretty_print(
            "Creating a shortcut link for '%s' didn't work." % entity_name,
            title="The builtin entity resources were successfully downloaded, "
                  "however linking failed.",
            level=PrettyPrintLevel.ERROR)
Esempio n. 2
0
def download(resource_name, *pip_args):
    """Download compatible resources for the specified language"""
    resource_name = resource_name.lower()
    shortcuts = get_json(__about__.__shortcuts__, "Resource shortcuts")
    full_resource_name = shortcuts.get(resource_name, resource_name)
    compatibility = _get_compatibility()
    version = _get_resources_version(full_resource_name, compatibility)
    dl = _download_model(
        '{r}-{v}/{r}-{v}.tar.gz#egg={r}=={v}'.format(r=full_resource_name,
                                                     v=version), pip_args)
    if dl != 0:
        sys.exit(dl)
    try:
        # Get package path here because link uses
        # pip.get_installed_distributions() to check if model is a
        # package, which fails if model was just installed via
        # subprocess
        package_path = get_package_path(full_resource_name)
        link(full_resource_name,
             resource_name,
             force=True,
             resources_path=package_path)
    except:  # pylint:disable=bare-except
        prints(
            "Creating a shortcut link for '{r}' didn't work, but you can "
            "still load the resources via its full package name: "
            "snips_nlu.load_resources('{n}')".format(r=resource_name,
                                                     n=full_resource_name),
            title="Language resources were successfully downloaded, however "
            "linking failed.")
Esempio n. 3
0
def download_from_resource_name(resource_name, pip_args, verbose=True):
    shortcuts = get_json(__about__.__shortcuts__, "Resource shortcuts")
    check_resources_alias(resource_name, shortcuts)
    compatibility = get_compatibility()
    resource_name = resource_name.lower()
    full_resource_name = shortcuts.get(resource_name, resource_name)
    _download_and_link(resource_name, full_resource_name, compatibility,
                       pip_args, verbose)
Esempio n. 4
0
def _get_compatibility():
    version = __about__.__version__
    table = get_json(__about__.__compatibility__, "Compatibility table")
    compatibility = table["snips-nlu"]
    if version not in compatibility:
        prints("No compatible resources found for version %s" % version,
               title="Resources compatibility error",
               exits=1)
    return compatibility[version]
Esempio n. 5
0
def download_builtin_entity(entity_name, language, *pip_args):
    """Download compatible language or gazetteer entity resources"""
    download_from_resource_name(language, pip_args, verbose=False)

    shortcuts = get_json(__about__.__shortcuts__, "Resource shortcuts")
    check_resources_alias(entity_name, shortcuts)

    compatibility = get_compatibility()
    resource_name_lower = entity_name.lower()
    long_resource_name = shortcuts.get(resource_name_lower,
                                       resource_name_lower)

    _download_and_link_entity(long_resource_name, entity_name, language,
                              compatibility, pip_args)
Esempio n. 6
0
def download_language_builtin_entities(language, *pip_args):
    """Download all gazetteer entity resources for a given language as well as
    basic language resources for this language"""
    download_from_resource_name(language, pip_args, verbose=False)

    shortcuts = get_json(__about__.__shortcuts__, "Resource shortcuts")
    for entity_name in get_supported_gazetteer_entities(language):
        check_resources_alias(entity_name, shortcuts)

        compatibility = get_compatibility()
        resource_name_lower = entity_name.lower()
        long_resource_name = shortcuts.get(resource_name_lower,
                                           resource_name_lower)

        _download_and_link_entity(long_resource_name, entity_name, language,
                                  compatibility, pip_args)
Esempio n. 7
0
def download_language_builtin_entities(language, *pip_args):
    """Download all gazetteer entity resources for a given language as well as
    basic language resources for this language"""
    from builtins import str
    from snips_nlu_parsers import get_supported_gazetteer_entities
    from snips_nlu import __about__
    from snips_nlu.cli.download import download_from_resource_name
    from snips_nlu.cli.utils import (check_resources_alias, get_compatibility,
                                     get_json)

    download_from_resource_name(language, pip_args, verbose=False)

    shortcuts = get_json(__about__.__shortcuts__, "Resource shortcuts")
    for entity_name in get_supported_gazetteer_entities(str(language)):
        check_resources_alias(entity_name, shortcuts)

        compatibility = get_compatibility()
        resource_name_lower = entity_name.lower()
        long_resource_name = shortcuts.get(resource_name_lower,
                                           resource_name_lower)

        _download_and_link_entity(long_resource_name, entity_name, language,
                                  compatibility, pip_args)