code: def substitute_space_misc(misc, space_misc):
      space_misc_pieces = space_misc.split("|") if space_misc else []
      space_misc_after = None
      space_misc_before = None
      for piece in space_misc_pieces:
          if piece.startswith("SpaceBefore"):
              space_misc_before = piece
          elif piece.startswith("SpaceAfter") or piece.startswith(
              "SpacesAfter"
          ):
              space_misc_after = piece
          else:
              raise AssertionError(
                  "An unknown piece wound up in the misc space fields: %s"
                  % piece
              )
  
      pieces = misc.split("|")
      new_pieces = []
      for piece in pieces:
          if piece.startswith("SpaceBefore"):
              if space_misc_before:
                  new_pieces.append(space_misc_before)
                  space_misc_before = None
          elif piece.startswith("SpaceAfter") or piece.startswith(
              "SpacesAfter"
          ):
              if space_misc_after:
                  new_pieces.append(space_misc_after)
                  space_misc_after = None
          else:
              new_pieces.append(piece)
      if space_misc_after:
          new_pieces.append(space_misc_after)
      if space_misc_before:
          new_pieces.append(space_misc_before)
      if len(new_pieces) == 0:
          return None
      return "|".join(new_pieces)
  
imports: from collections import deque
  import subprocess
  from stanza.models.common.utils import misc_to_space_after
  from stanza.models.constituency.parse_tree import Tree
  from stanza.protobuf import DependencyGraph, FlattenedParseTree
  from stanza.server.client import resolve_classpath
label_desc: Use image node_id 12 for calling a global function with example usage: substitute_space_misc(misc, space_misc) and returns: str, None