Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Created June 8, 2023 22:40
Show Gist options
  • Save chapmanjacobd/a5fae99c19c6af16dc8234e18b30bf02 to your computer and use it in GitHub Desktop.
Save chapmanjacobd/a5fae99c19c6af16dc8234e18b30bf02 to your computer and use it in GitHub Desktop.
def load_spacy_model(model=None):
try:
import spacy
except ModuleNotFoundError:
log.error("Install spaCy and sklearn to use:")
log.error("pip install spacy sklearn")
log.error("python -m spacy download en_core_web_sm")
sys.exit(1)
if model:
return spacy.load(model)
model_sizes = ["lg", "md", "sm"]
loaded_model = None
for size in model_sizes:
try:
loaded_model = spacy.load(f"en_core_web_{size}")
log.info(f"Loaded 'en_core_web_{size}'")
except OSError:
pass
if loaded_model:
return loaded_model
log.error("Language model not found. Download a model first using the following commands:")
log.error("python -m spacy download en_core_web_sm")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment