code: def to_bag_of_words(
      doclike: types.DocLike,
      *,
      by: TokenGroupByType = "lemma_",
      weighting: WeightingType = "count",
      **kwargs,
  ) -> dict[int, int | float] | dict[str, int | float]:
      """
      Transform a ``Doc`` or ``Span`` into a bag-of-words: the set of unique words therein
      mapped to their absolute, relative, or binary frequencies of occurrence.
  
      Args:
          doclike
          by: Attribute by which spaCy ``Token`` s are grouped before counting,
              as given by ``getattr(token, by)``.
              If "lemma", tokens are grouped by their base form w/o inflectional suffixes;
              if "lower", by the lowercase form of the token text;
              if "norm", by the normalized form of the token text;
              if "orth", by the token text exactly as it appears in ``doc``.
              To output keys as strings, simply append an underscore to any of these;
              for example, "lemma_" creates a bag whose keys are token lemmas as strings.
          weighting: Type of weighting to assign to unique words given by ``by``.
              If "count", weights are the absolute number of occurrences (i.e. counts);
              if "freq", weights are counts normalized by the total token count,
              giving their relative frequency of occurrence;
              if "binary", weights are set equal to 1.
          **kwargs: Passed directly on to :func:`textacy.extract.words()`
              - filter_stops: If True, stop words are removed before counting.
              - filter_punct: If True, punctuation tokens are removed before counting.
              - filter_nums: If True, number-like tokens are removed before counting.
  
      Returns:
          Mapping of a unique word id or string (depending on the value of ``by``)
          to its absolute, relative, or binary frequency of occurrence
          (depending on the value of ``weighting``).
  
      Note:
          For "freq" weighting, the resulting set of frequencies won't (necessarily) sum
          to 1.0, since all tokens are used when normalizing counts but some (punctuation,
          stop words, etc.) may be filtered out of the bag afterwards.
  
      See Also:
          :func:`textacy.extract.words()`
      """
      words = basics.words(doclike, **kwargs)
      bow = cytoolz.recipes.countby(operator.attrgetter(by), words)
      bow = _reweight_bag(weighting, bow, doclike)
      return bow
  
file_path: textacy/src/textacy/extract/bags.py
imports: from __future__ import annotations
  import operator
  from typing import Any, Collection, Literal, Optional, Union
  import cytoolz
  from ..None import errors, types
  from .None import basics
label_desc: Use image node_id 1 for calling a global function with example usage: to_bag_of_words(doclike) and returns: bow