(
    doc_term_matrix: sp.csr_matrix,
    *,
    type_: Literal["linear", "sqrt", "log"] = "linear",
)
Compute the lengths (i.e. number of terms) for all documents in a
    document-term matrix.

    Args:
        doc_term_matrix: M x N sparse matrix, where M is the # of docs, N is the #
            of unique terms, and values are the absolute counts of term n per doc m.
        type_: Scaling applied to absolute doc lengths.
            If 'linear', lengths are left as-is, since the sums are already
            linear; if 'sqrt', dl => sqrt(dl); if 'log', dl => log(dl) + 1.

    Returns:
        Array of document lengths, with length equal to the # of documents
        (i.e. # of rows) in ``doc_term_matrix``.

    Raises:
        ValueError: if ``type_`` isn't one of {"linear", "sqrt", "log"}.
Compute the lengths (i.e. number of terms) for all documents in a
    document-term matrix.
Compute the lengths ( i . e . number of terms ) for all documents in a document - term matrix .
def get_doc_lengths(
    doc_term_matrix: sp.csr_matrix,
    *,
    type_: Literal["linear", "sqrt", "log"] = "linear",
) -> np.ndarray:
    """
    Compute the lengths (i.e. number of terms) for all documents in a
    document-term matrix.

    Args:
        doc_term_matrix: M x N sparse matrix, where M is the # of docs, N is the #
            of unique terms, and values are the absolute counts of term n per doc m.
        type_: Scaling applied to absolute doc lengths.
            If 'linear', lengths are left as-is, since the sums are already
            linear; if 'sqrt', dl => sqrt(dl); if 'log', dl => log(dl) + 1.

    Returns:
        Array of document lengths, with length equal to the # of documents
        (i.e. # of rows) in ``doc_term_matrix``.

    Raises:
        ValueError: if ``type_`` isn't one of {"linear", "sqrt", "log"}.
    """
    dls = np.asarray(doc_term_matrix.sum(axis=1)).ravel()
    if type_ == "linear":
        return dls  # dls is already linear
    elif type_ == "sqrt":
        return np.sqrt(dls)
    elif type_ == "log":
        return np.log(dls) + 1.0
    else:
        raise ValueError(
            errors.value_invalid_msg("type_", type_, {"linear", "sqrt", "log"})
        )
def get_doc_lengths ( doc_term_matrix : sp . csr_matrix , * , type_ : Literal [ "linear" , "sqrt" , "log" ] = "linear" , ) -> np . ndarray : dls = np . asarray ( doc_term_matrix . sum ( axis = 1 ) ) . ravel ( ) if type_ == "linear" : return dls # dls is already linear elif type_ == "sqrt" : return np . sqrt ( dls ) elif type_ == "log" : return np . log ( dls ) + 1.0 else : raise ValueError ( errors . value_invalid_msg ( "type_" , type_ , { "linear" , "sqrt" , "log" } ) )