from typing import Iterable


def ngram_freq(
    text_docs_bow: Iterable[Iterable[str]], n: int = 3, only_n: bool = False
) -> "nltk.FreqDist":  # type: ignore
    """Generates frequency distribution of "n-grams" from all of the text documents.

    Args:
        text_docs_bow: A list of lists of words from a document
        n: Highest `n` for n-gram sequence to include. Default is 3
        only_n: If True, will only include n-grams for specified value of `n`.
            Default is False, which also includes n-grams for all numbers leading
            up to `n`

    Raises:
        ValueError: `n` must be >= 2.

    Returns:
        freq: Dictionary which contains all identified n-grams as keys and their
            respective counts as their values
    """
    if n < 2:
        raise ValueError("'n' must be a number 2 or greater")

    freq = _compat["nltk"].FreqDist()  # type: ignore
    for doc in text_docs_bow:
        if only_n:
            current_ngrams = _compat["nltk"].ngrams(doc, n)  # type: ignore
            freq.update(current_ngrams)
        else:
            for num in range(2, n + 1):
                current_ngrams = _compat["nltk"].ngrams(doc, num)  # type: ignore
                freq.update(current_ngrams)
    return freq