code: def detect_bad_ocr(
      text,
      spellchecker: Optional[SpellChecker],
      misspell_threshold=0.7,
      space_threshold=0.6,
      newline_threshold=0.5,
      alphanum_threshold=0.4,
  ):
      if len(text) == 0:
          # Assume OCR failed if we have no text
          return True
  
      words = wordpunct_tokenize(text)
      words = [w for w in words if w.strip()]
      alpha_words = [word for word in words if word.isalnum()]
  
      if spellchecker:
          misspelled = spellchecker.unknown(alpha_words)
          if len(misspelled) > len(alpha_words) * misspell_threshold:
              return True
  
      spaces = len(re.findall(r"\s+", text))
      alpha_chars = len(re.sub(r"\s+", "", text))
      if spaces / (alpha_chars + spaces) > space_threshold:
          return True
  
      newlines = len(re.findall(r"\n+", text))
      non_newlines = len(re.sub(r"\n+", "", text))
      if newlines / (newlines + non_newlines) > newline_threshold:
          return True
  
      if alphanum_ratio(text) < alphanum_threshold:  # Garbled text
          return True
  
      invalid_chars = len(
          [c for c in text if c in settings.INVALID_CHARS]
      )
      if invalid_chars > max(3.0, len(text) * 0.02):
          return True
  
      return False
  
imports: from typing import Optional
  from nltk import wordpunct_tokenize
  from spellchecker import SpellChecker
  from marker.settings import settings
  import re
label_desc: Use image node_id 1 for calling a global function with example usage: detect_bad_ocr(text, spellchecker, misspell_threshold, space_threshold, newline_threshold, alphanum_threshold) and returns: False, True, True, True, True, True, True