Update spacy.load()

path argument is now deprecated and name can either take a model name
or path. Implement lazy loading by importing module and read Language
class name off __all__.
This commit is contained in:
ines 2017-05-08 15:27:25 +02:00
parent 94697e9afc
commit a7801e7342
1 changed files with 11 additions and 25 deletions

View File

@ -1,37 +1,23 @@
# coding: utf8 # coding: utf8
from __future__ import unicode_literals from __future__ import unicode_literals
from . import util
from .util import prints
from .deprecated import resolve_model_name
import importlib import importlib
from .compat import basestring_
from .cli.info import info from .cli.info import info
from .glossary import explain from .glossary import explain
from . import util
_languages_name = set(["en", "de", "es", "pt", "fr",
"it", "hu", "zh", "nl", "sv",
"fi", "bn", "he", "nb", "ja"])
def load(name, **overrides): def load(name, **overrides):
if overrides.get('path') in (None, False, True): if overrides.get('path') not in (None, False, True):
data_path = util.get_data_path() name = overrides.get('path')
model_name = resolve_model_name(name) model_path = util.resolve_model_path(name)
model_path = data_path / model_name meta = util.parse_package_meta(model_path)
if not model_path.exists(): if 'lang' not in meta:
lang_name = util.get_lang_class(name).lang raise IOError('No language setting found in model meta.')
model_path = None module = importlib.import_module('.%s' % meta['lang'], 'spacy')
prints("Only loading the '%s' tokenizer." % lang_name, cls = getattr(module, module.__all__[0])
title="Warning: no model found for '%s'" % name)
else:
model_path = util.ensure_path(overrides['path'])
data_path = model_path.parent
model_name = ''
meta = util.parse_package_meta(data_path, model_name, require=False)
lang = meta['lang'] if meta and 'lang' in meta else name
module = importlib.import_module("."+lang, "spacy")
cls = module.EXPORT
overrides['meta'] = meta overrides['meta'] = meta
overrides['path'] = model_path overrides['path'] = model_path
return cls(**overrides) return cls(**overrides)