Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Automatically install spacy models, if missing (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmmd authored Mar 1, 2018
1 parent cdfe9ae commit c12d435
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion allennlp/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import torch
import numpy
import spacy
from spacy.cli.download import download as spacy_download
from spacy.language import Language as SpacyModelType

from allennlp.common.checks import log_pytorch_version_info
Expand Down Expand Up @@ -189,6 +190,7 @@ def get_spacy_model(spacy_model_name: str, pos_tags: bool, parse: bool, ner: boo
keyed by the options we used to create the spacy model, so any particular configuration only
gets loaded once.
"""

options = (spacy_model_name, pos_tags, parse, ner)
if options not in LOADED_SPACY_MODELS:
disable = ['vectors', 'textcat']
Expand All @@ -198,7 +200,13 @@ def get_spacy_model(spacy_model_name: str, pos_tags: bool, parse: bool, ner: boo
disable.append('parser')
if not ner:
disable.append('ner')
spacy_model = spacy.load(spacy_model_name, disable=disable)
try:
spacy_model = spacy.load(spacy_model_name, disable=disable)
except OSError:
logger.warning(f"Spacy models '{spacy_model_name}' not found. Downloading and installing.")
spacy_download(spacy_model_name)
spacy_model = spacy.load(spacy_model_name, disable=disable)

LOADED_SPACY_MODELS[options] = spacy_model
return LOADED_SPACY_MODELS[options]

Expand Down

0 comments on commit c12d435

Please sign in to comment.