| 1 |
import logging |
| 2 |
import os |
| 3 |
import urllib.request |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
from .exceptions import FastTextModelNotFoundException |
| 7 |
from .utils import create_data_model_home, dateparser_model_home |
| 8 |
|
| 9 |
|
| 10 |
def fasttext_downloader(model_name): |
| 11 |
create_data_model_home() |
| 12 |
models = { |
| 13 |
"small": "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.ftz", |
| 14 |
"large": "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin", |
| 15 |
} |
| 16 |
if model_name not in models: |
| 17 |
message = 'dateparser-download: Couldn\'t find a model called "{}". Supported models are: {}'.format( |
| 18 |
model_name, ", ".join(models.keys()) |
| 19 |
) |
| 20 |
raise FastTextModelNotFoundException(message) |
| 21 |
|
| 22 |
models_directory_path = os.path.join(dateparser_model_home, (model_name + ".bin")) |
| 23 |
|
| 24 |
if not Path(models_directory_path).is_file(): |
| 25 |
model_url = models[model_name] |
| 26 |
logging.info( |
| 27 |
'dateparser-download: Downloading model "{}" from "{}"...'.format( |
| 28 |
model_name, model_url |
| 29 |
) |
| 30 |
) |
| 31 |
try: |
| 32 |
urllib.request.urlretrieve(model_url, models_directory_path) |
| 33 |
except urllib.error.HTTPError as e: |
| 34 |
raise Exception( |
| 35 |
"dateparser-download: Fasttext model cannot be downloaded due to HTTP error" |
| 36 |
) from e |
| 37 |
else: |
| 38 |
logging.info( |
| 39 |
'dateparser-download: The model "{}" is already downloaded'.format( |
| 40 |
model_name |
| 41 |
) |
| 42 |
) |