From 326746eb158646f69bf143ef5f6bc67d9370a05b Mon Sep 17 00:00:00 2001 From: ines Date: Mon, 8 May 2017 15:29:47 +0200 Subject: [PATCH] Add util function to resolve arg to model path 1. check if in data dir or shortcut link 2. check if installed as a pip package 3. check if string is path to model 4. check if Path or Path-like object --- spacy/util.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spacy/util.py b/spacy/util.py index ba1655347..0790cc869 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -108,6 +108,22 @@ def read_json(location): return ujson.load(f) +def resolve_model_path(name): + data_path = get_data_path() + if not data_path or not data_path.exists(): + raise IOError("Can't find spaCy data path: %s" % path2str(data_path)) + if isinstance(name, basestring_): + if (data_path / name).exists(): # in data dir or shortcut link + return (data_path / name) + if is_package(name): # installed as a package + return get_model_package_path(name) + if Path(name).exists(): # path to model + return Path(name) + elif hasattr(name, 'exists'): # Path or Path-like object + return name + raise IOError("Can't find model '%s'" % name) + + def is_package(origin): """ Check if string maps to a package installed via pip.