Add blank:{lang} shortcut to util.load_mode

This commit is contained in:
Ines Montani 2020-05-21 20:24:07 +02:00
parent 0f1beb5ff2
commit cb02bff0eb
2 changed files with 13 additions and 0 deletions

View File

@ -135,3 +135,14 @@ def test_ascii_filenames():
root = Path(__file__).parent.parent root = Path(__file__).parent.parent
for path in root.glob("**/*"): for path in root.glob("**/*"):
assert all(ord(c) < 128 for c in path.name), path.name assert all(ord(c) < 128 for c in path.name), path.name
def test_load_model_blank_shortcut():
"""Test that using a model name like "blank:en" works as a shortcut for
spacy.blank("en").
"""
nlp = util.load_model("blank:en")
assert nlp.lang == "en"
assert nlp.pipeline == []
with pytest.raises(ImportError):
util.load_model("blank:fjsfijsdof")

View File

@ -161,6 +161,8 @@ def load_model(name, **overrides):
if not data_path or not data_path.exists(): if not data_path or not data_path.exists():
raise IOError(Errors.E049.format(path=path2str(data_path))) raise IOError(Errors.E049.format(path=path2str(data_path)))
if isinstance(name, basestring_): # in data dir / shortcut if isinstance(name, basestring_): # in data dir / shortcut
if name.startswith("blank:"): # shortcut for blank model
return get_lang_class(name.replace("blank:", ""))()
if name in set([d.name for d in data_path.iterdir()]): if name in set([d.name for d in data_path.iterdir()]):
return load_model_from_link(name, **overrides) return load_model_from_link(name, **overrides)
if is_package(name): # installed as package if is_package(name): # installed as package