code: def _optimize(nlp, component: str, data: List, rehearse: bool):
      """Run either train or rehearse."""
      pipe = nlp.get_pipe(component)
      if component == "ner":
          _add_ner_label(pipe, data)
      elif component == "tagger":
          _add_tagger_label(pipe, data)
      elif component == "parser":
          _add_parser_label(pipe, data)
      elif component == "textcat_multilabel":
          _add_textcat_label(pipe, data)
      else:
          raise NotImplementedError
  
      if rehearse:
          optimizer = nlp.resume_training()
      else:
          optimizer = nlp.initialize()
  
      for _ in range(5):
          for text, annotation in data:
              doc = nlp.make_doc(text)
              example = Example.from_dict(doc, annotation)
              if rehearse:
                  nlp.rehearse([example], sgd=optimizer)
              else:
                  nlp.update([example], sgd=optimizer)
      return nlp
  
file_path: spaCy/spacy/tests/training/test_rehearse.py
imports: from typing import List
  import pytest
  import spacy
  from spacy.training import Example
label_desc: Use image node_id 5 for calling a global function with example usage: _optimize(nlp, component, data, rehearse) and returns: nlp