from data401_nlp.helpers.spacy import ensure_spacy_model
# This will automatically download if not present
nlp = ensure_spacy_model("en_core_web_sm")
# Now use nlp as normal
doc = nlp("This is a test.")
print([token.text for token in doc])spaCy Model Helper (spacy.py)
Helper functions for managing spaCy models across different platforms
This module provides utilities for downloading and loading spaCy models in a platform-agnostic way. It handles the common pattern of checking if a model exists before attempting to download it, which is especially useful in notebook environments.
ensure_spacy_model
def ensure_spacy_model(
model_name:str='en_core_web_sm', # Name of the spaCy model to ensure is installed (default: "en_core_web_sm")
verbose:bool=True, # Whether to print status messages (default: True)
): # The loaded spaCy model
Ensure a spaCy model is installed, downloading it if necessary.
This function checks if the specified spaCy model is available. If not, it downloads and installs the model using spaCy’s download command.
list_installed_models
def list_installed_models(
): # Names of installed spaCy models
List all installed spaCy models.
Usage Example
In your notebooks, use this instead of manually checking for models: