code: def make_spacy_doc(
      data: types.DocData,
      lang: types.LangLikeInContext,
      *,
      chunk_size: Optional[int] = None,
  ) -> Doc:
      """
      Make a :class:`spacy.tokens.Doc` from valid inputs, and automatically
      load/validate :class:`spacy.language.Language` pipelines to process ``data``.
  
      Make a ``Doc`` from text:
  
      .. code-block:: pycon
  
          >>> text = "To be, or not to be, that is the question."
          >>> doc = make_spacy_doc(text, "en_core_web_sm")
          >>> doc._.preview
          'Doc(13 tokens: "To be, or not to be, that is the question.")'
  
      Make a ``Doc`` from a (text, metadata) pair, aka a "record":
  
      .. code-block:: pycon
  
          >>> record = (text, {"author": "Shakespeare, William"})
          >>> doc = make_spacy_doc(record, "en_core_web_sm")
          >>> doc._.preview
          'Doc(13 tokens: "To be, or not to be, that is the question.")'
          >>> doc._.meta
          {'author': 'Shakespeare, William'}
  
      Specify the language pipeline used to process the text in a few different ways:
  
      .. code-block:: pycon
  
          >>> make_spacy_doc(text, lang="en_core_web_sm")
          >>> make_spacy_doc(text, lang=textacy.load_spacy_lang("en_core_web_sm"))
          >>> make_spacy_doc(text, lang=lambda txt: "en_core_web_sm")
  
      Ensure that an already-processed ``Doc`` is compatible with ``lang``:
  
      .. code-block:: pycon
  
          >>> spacy_lang = textacy.load_spacy_lang("en_core_web_sm")
          >>> doc = spacy_lang(text)
          >>> make_spacy_doc(doc, lang="en_core_web_sm")
          >>> make_spacy_doc(doc, lang="es_core_news_sm")
          ...
          ValueError: `spacy.Vocab` used to process document must be the same as that used by the `lang` pipeline ('es_core_news_sm')
  
      Args:
          data: Make a :class:`spacy.tokens.Doc` from a text or (text, metadata) pair.
              If already a ``Doc``, ensure that it's compatible with ``lang``
              to avoid surprises downstream, and return it as-is.
          lang: Language with which spaCy processes (or processed) ``data``,
              represented as the full name of a spaCy language pipeline, the path on disk
              to it, an already instantiated pipeline, or a callable function that takes
              the text component of ``data`` and outputs one of the above representations.
          chunk_size: Size of chunks in number of characters into which ``text`` will be
              split before processing each via spaCy and concatenating the results
              into a single ``Doc``.
  
              .. note:: This is intended as a workaround for processing very long texts,
                 for which spaCy is unable to allocate enough RAM. For best performance,
                 chunk size should be somewhere between 1e3 and 1e7 characters,
                 depending on how much RAM you have available.
  
                 Since chunking is done by *character*, chunks' boundaries likely
                 won't respect natural language segmentation, and as a result
                 spaCy's models may make mistakes on sentences/words that cross them.
  
      Returns:
          Processed spaCy Doc.
  
      Raises:
          TypeError
          ValueError
      """
      if isinstance(data, str):
          return _make_spacy_doc_from_text(data, lang, chunk_size)
      elif isinstance(data, Doc):
          return _make_spacy_doc_from_doc(data, lang)
      elif utils.is_record(data):
          return _make_spacy_doc_from_record(data, lang, chunk_size)
      else:
          raise TypeError(
              errors.type_invalid_msg("data", type(data), types.DocData)
          )
  
imports: from __future__ import annotations
  import functools
  import logging
  import pathlib
  from typing import Optional
  import spacy
  from cachetools import cached
  from cachetools.keys import hashkey
  from spacy.language import Language
  from spacy.tokens import Doc
  from ..None import cache, errors, types, utils
  from .None import extensions
  from .None import utils
label_desc: Use image node_id 2 for calling a global function with example usage: make_spacy_doc(data, lang) and returns: _make_spacy_doc_from_text, _make_spacy_doc_from_doc, _make_spacy_doc_from_record