import re


def normalize_text(docs, lang_model):
    """Normalizes a vector of documents using the Spacy package.
    In particular: replaces all non-alphanumeric characters with spaces before
    stripping whitespace from the edges of each document and setting everything
    to lowercase. It then uses a Spacy language object to lemmatize each word
    in each document. For more information on these language objects see:
    https://spacy.io/usage/models

    Arguments
    ---------
    docs: iterable
        Iterable of strings, where each string represents a single document.
    lang_model: spacy.Language
        Spacy language object which allows for the lemmatization.

    Returns
    -------
    normed_docs: list
        List of strings, where each string represents a normalized document.

    """

    normed_docs = []
    for doc in docs:
        clean_doc = re.sub('[^0-9a-zA-Z]+', ' ', doc)
        clean_doc = clean_doc.strip().lower()
        normed_docs.append(' '.join([w.lemma_ for w in lang_model(clean_doc)]))

    return normed_docs