2016-03-24 13:19:43 +03:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import sys
|
2017-01-19 14:03:52 +03:00
|
|
|
import shutil
|
2016-03-24 13:19:43 +03:00
|
|
|
|
|
|
|
import sputnik
|
|
|
|
from sputnik.package_list import (PackageNotFoundException,
|
|
|
|
CompatiblePackageNotFoundException)
|
|
|
|
|
|
|
|
from . import about
|
2016-10-23 20:46:44 +03:00
|
|
|
from . import util
|
2016-03-24 13:19:43 +03:00
|
|
|
|
|
|
|
|
2016-11-20 18:48:04 +03:00
|
|
|
def download(lang, force=False, fail_on_exist=True, data_path=None):
|
|
|
|
if not data_path:
|
2017-01-10 01:40:26 +03:00
|
|
|
data_path = util.get_data_path(require_exists=False)
|
2016-11-20 18:48:04 +03:00
|
|
|
|
|
|
|
# spaCy uses pathlib, and util.get_data_path returns a pathlib.Path object,
|
|
|
|
# but sputnik (which we're using below) doesn't use pathlib and requires
|
|
|
|
# its data_path parameters to be strings, so we coerce the data_path to a
|
|
|
|
# str here.
|
|
|
|
data_path = str(data_path)
|
|
|
|
|
2016-03-24 13:19:43 +03:00
|
|
|
try:
|
2016-11-04 12:44:11 +03:00
|
|
|
pkg = sputnik.package(about.__title__, about.__version__,
|
2016-11-20 18:48:04 +03:00
|
|
|
about.__models__.get(lang, lang), data_path)
|
2016-11-04 12:44:11 +03:00
|
|
|
if force:
|
|
|
|
shutil.rmtree(pkg.path)
|
|
|
|
elif fail_on_exist:
|
2016-09-14 17:04:09 +03:00
|
|
|
print("Model already installed. Please run 'python -m "
|
|
|
|
"spacy.%s.download --force' to reinstall." % lang, file=sys.stderr)
|
|
|
|
sys.exit(0)
|
2016-03-24 13:19:43 +03:00
|
|
|
except (PackageNotFoundException, CompatiblePackageNotFoundException):
|
|
|
|
pass
|
|
|
|
|
2016-10-19 01:47:44 +03:00
|
|
|
package = sputnik.install(about.__title__, about.__version__,
|
2016-11-20 18:48:04 +03:00
|
|
|
about.__models__.get(lang, lang), data_path)
|
2016-03-24 13:19:43 +03:00
|
|
|
|
|
|
|
try:
|
2016-10-19 01:47:44 +03:00
|
|
|
sputnik.package(about.__title__, about.__version__,
|
2016-11-20 18:48:04 +03:00
|
|
|
about.__models__.get(lang, lang), data_path)
|
2016-03-24 13:19:43 +03:00
|
|
|
except (PackageNotFoundException, CompatiblePackageNotFoundException):
|
|
|
|
print("Model failed to install. Please run 'python -m "
|
|
|
|
"spacy.%s.download --force'." % lang, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2016-10-23 20:46:44 +03:00
|
|
|
print("Model successfully installed to %s" % data_path, file=sys.stderr)
|