"""Helper functions for managing spaCy models across different platforms"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/helpers/03_spacy.ipynb.

# %% auto 0
__all__ = ['ensure_spacy_model', 'list_installed_models']

# %% ../../nbs/helpers/03_spacy.ipynb 3
import subprocess
import sys

# %% ../../nbs/helpers/03_spacy.ipynb 4
def ensure_spacy_model(model_name: str = "en_core_web_sm", verbose: bool = True):
    """
    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.
    
    Parameters
    ----------
    model_name : str, optional
        Name of the spaCy model to ensure is installed (default: "en_core_web_sm")
    verbose : bool, optional
        Whether to print status messages (default: True)
    
    Returns
    -------
    spacy.Language
        The loaded spaCy model
    
    Examples
    --------
    >>> nlp = ensure_spacy_model("en_core_web_sm")
    >>> doc = nlp("This is a test sentence.")
    >>> print([token.text for token in doc])
    ['This', 'is', 'a', 'test', 'sentence', '.']
    """
    import spacy
    
    try:
        # Try to load the model
        nlp = spacy.load(model_name)
        if verbose:
            print(f"✅ spaCy model '{model_name}' loaded successfully")
        return nlp
    except OSError:
        # Model not found, download it
        if verbose:
            print(f"📥 Downloading spaCy model '{model_name}'...")
        
        subprocess.check_call(
            [sys.executable, "-m", "spacy", "download", model_name],
            stdout=subprocess.DEVNULL if not verbose else None
        )
        
        # Load the newly downloaded model
        nlp = spacy.load(model_name)
        if verbose:
            print(f"✅ spaCy model '{model_name}' downloaded and loaded successfully")
        return nlp

# %% ../../nbs/helpers/03_spacy.ipynb 5
def list_installed_models():
    """
    List all installed spaCy models.
    
    Returns
    -------
    list of str
        Names of installed spaCy models
    """
    import spacy
    return list(spacy.util.get_installed_models())
