Skip to content

Commit

Permalink
Replace model with full name when spacy load is used (#1140)
Browse files Browse the repository at this point in the history
Reviewed By: zhangguanheng66

Differential Revision: D26369005

fbshipit-source-id: b1e6b5d77810bb8f67d14b8a1c7ec0a9f4831cab
  • Loading branch information
datumbox authored and facebook-github-bot committed Feb 15, 2021
1 parent 310eac1 commit 35a8720
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion torchtext/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ def get_tokenizer(tokenizer, language='en'):
if tokenizer == "spacy":
try:
import spacy
spacy = spacy.load(language)
try:
spacy = spacy.load(language)
except IOError:
# Model shortcuts no longer work in spaCy 3.0+, try using fullnames
# List is from https://github.com/explosion/spaCy/blob/b903de3fcb56df2f7247e5b6cfa6b66f4ff02b62/spacy/errors.py#L789
OLD_MODEL_SHORTCUTS = spacy.errors.OLD_MODEL_SHORTCUTS if hasattr(spacy.errors, 'OLD_MODEL_SHORTCUTS') else {}
if language not in OLD_MODEL_SHORTCUTS:
raise
import warnings
warnings.warn(f'Spacy model "{language}" could not be loaded, trying "{OLD_MODEL_SHORTCUTS[language]}" instead')
spacy = spacy.load(OLD_MODEL_SHORTCUTS[language])
return partial(_spacy_tokenize, spacy=spacy)
except ImportError:
print("Please install SpaCy. "
Expand Down

0 comments on commit 35a8720

Please sign in to comment.