from typing import Iterable
from typing import Optional
from typing import List


def remove_stopwords(
    text_docs_bow: Iterable[Iterable[str]], custom_stopwords: Optional[List[str]] = None
) -> Iterable[Iterable[str]]:
    """Removes all "stop words" from documents.

    "Stop words" can be defined as commonly used words which are typically useless
    for NLP.

    Args:
        text_docs_bow: A list of lists of words from a document
        custom_stopwords: An optional list of words to remove along with the stop words.
            Defaults to nltk english stopwords.

    Returns:
        A generator expression for all of the processed documents
    """
    stop_words_original = set(
        _compat["nltk"].corpus.stopwords.words("english")  # type: ignore
    )

    if custom_stopwords:
        stop_words = stop_words_original.union(custom_stopwords)
    else:
        stop_words = stop_words_original

    return ((word for word in doc if word not in stop_words) for doc in text_docs_bow)