Merge pull request #5481 from explosion/feature/blank-shortcut-v2

Add blank:{lang} shortcut support to util.load_model
This commit is contained in:
Matthew Honnibal 2020-05-22 00:08:23 +02:00 committed by GitHub
commit e1cb7e838b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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