code: def fix_surrogates(text):
      """
      Replace 16-bit surrogate codepoints with the characters they represent
      (when properly paired), or with \ufffd otherwise.
  
          >>> high_surrogate = chr(0xd83d)
          >>> low_surrogate = chr(0xdca9)
          >>> print(fix_surrogates(high_surrogate + low_surrogate))
          💩
          >>> print(fix_surrogates(low_surrogate + high_surrogate))
          ��
  
      The above doctest had to be very carefully written, because even putting
      the Unicode escapes of the surrogates in the docstring was causing
      various tools to fail, which I think just goes to show why this fixer is
      necessary.
      """
      if SURROGATE_RE.search(text):
          text = SURROGATE_PAIR_RE.sub(convert_surrogate_pair, text)
          text = SURROGATE_RE.sub("\ufffd", text)
      return text
  
imports: import codecs
  import html
  import re
  import warnings
  import ftfy
  from ftfy.chardata import ALTERED_UTF8_RE, C1_CONTROL_RE, CONTROL_CHARS, DOUBLE_QUOTE_RE, HTML_ENTITIES, HTML_ENTITY_RE, LIGATURES, LOSSY_UTF8_RE, SINGLE_QUOTE_RE, UTF8_DETECTOR_RE, WIDTH_MAP
  from ftfy.badness import is_bad
label_desc: Use image node_id 12 for calling a global function with example usage: fix_surrogates(text) and returns: text