code: 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)
  
file_path: textacy/src/textacy/text_stats/diversity.py
imports: from __future__ import annotations
  import logging
  import math
  import statistics
  from typing import Iterable, Literal
  from scipy.stats import hypergeom
  from spacy.tokens import Token
  from toolz import itertoolz
  from ..None import errors, types
  from .None import utils
label_desc: Use image node_id 3 for calling a global function with example usage: segmented_ttr(doc_or_tokens, segment_size, variant) and returns: statistics