(
    doc_or_tokens: types.DocOrTokens,
    segment_size: int = 50,
    variant: Literal["mean", "moving-avg"] = "mean",
)
return statistics.mean(ttr(seg_words) for seg_words in segments)
Compute the Mean Segmental TTR (MS-TTR) or Moving Average TTR (MA-TTR)
    of ``doc_or_tokens``, in which the TTR of tumbling or rolling segments of words,
    respectively, each with length ``segment_size``, are computed and then averaged.

    Higher values indicate higher lexical diversity.

    Args:
        doc_or_tokens: If a spaCy ``Doc``, non-punctuation tokens (words) are extracted;
            if an iterable of spaCy ``Token`` s, all non-punct elements are used.
        segment_size: Number of consecutive words to include in each segment.
        variant: Variant of segmented TTR to compute.
            - "mean" => MS-TTR
            - "moving-avg" => MA-TTR

    References:
        - Johnson, W. (1944). Studies in language behavior: I. A program of research.
          Psychological Monographs, 56, 1-15.
        - Covington, M. A., & McFall, J. D. (2010). Cutting the Gordian knot:
          The moving-average type–token ratio (MATTR). Journal of quantitative
          linguistics, 17(2), 94-100.
Compute the Mean Segmental TTR (MS-TTR) or Moving Average TTR (MA-TTR)
    of ``doc_or_tokens``, in which the TTR of tumbling or rolling segments of words,
    respectively, each with length ``segment_size``, are computed and then averaged.
Compute the Mean Segmental TTR ( MS - TTR ) or Moving Average TTR ( MA - TTR ) of doc_or_tokens in which the TTR of tumbling or rolling segments of words respectively each with length segment_size are computed and then averaged .
def segmented_ttr(
    doc_or_tokens: types.DocOrTokens,
    segment_size: int = 50,
    variant: Literal["mean", "moving-avg"] = "mean",
) -> float:
    """
    Compute the Mean Segmental TTR (MS-TTR) or Moving Average TTR (MA-TTR)
    of ``doc_or_tokens``, in which the TTR of tumbling or rolling segments of words,
    respectively, each with length ``segment_size``, are computed and then averaged.

    Higher values indicate higher lexical diversity.

    Args:
        doc_or_tokens: If a spaCy ``Doc``, non-punctuation tokens (words) are extracted;
            if an iterable of spaCy ``Token`` s, all non-punct elements are used.
        segment_size: Number of consecutive words to include in each segment.
        variant: Variant of segmented TTR to compute.
            - "mean" => MS-TTR
            - "moving-avg" => MA-TTR

    References:
        - Johnson, W. (1944). Studies in language behavior: I. A program of research.
          Psychological Monographs, 56, 1-15.
        - Covington, M. A., & McFall, J. D. (2010). Cutting the Gordian knot:
          The moving-average type–token ratio (MATTR). Journal of quantitative
          linguistics, 17(2), 94-100.
    """
    words = tuple(utils.get_words(doc_or_tokens))
    if len(words) < segment_size:
        LOGGER.warning(
            "number of words in document (%s) must be greater than segment size (%s) "
            "to compute segmented-TTR; setting segment size equal to number of words, "
            "which effectively reduces this method to standard TTR",
            len(words),
            segment_size,
        )
        segment_size = len(words)
    if variant == "mean":
        # TODO: keep or drop shorter last segments?
        segments = itertoolz.partition(segment_size, words)
        # segments = itertoolz.partition_all(segment_size, words)
    elif variant == "moving-avg":
        segments = itertoolz.sliding_window(segment_size, words)
    else:
        raise ValueError(
            errors.value_invalid_msg("variant", variant, {"mean", "moving-avg"})
        )
    return statistics.mean(ttr(seg_words) for seg_words in segments)
def segmented_ttr ( doc_or_tokens : types . DocOrTokens , segment_size : int = 50 , variant : Literal [ "mean" , "moving-avg" ] = "mean" , ) -> float : words = tuple ( utils . get_words ( doc_or_tokens ) ) if len ( words ) < segment_size : LOGGER . warning ( "number of words in document (%s) must be greater than segment size (%s) " "to compute segmented-TTR; setting segment size equal to number of words, " "which effectively reduces this method to standard TTR" , len ( words ) , segment_size , ) segment_size = len ( words ) if variant == "mean" : # TODO: keep or drop shorter last segments? segments = itertoolz . partition ( segment_size , words ) # segments = itertoolz.partition_all(segment_size, words) elif variant == "moving-avg" : segments = itertoolz . sliding_window ( segment_size , words ) else : raise ValueError ( errors . value_invalid_msg ( "variant" , variant , { "mean" , "moving-avg" } ) ) return statistics . mean ( ttr ( seg_words ) for seg_words in segments )