code: def apply(self, doc, clear, **kwargs):
          
  
          # Reattach doc with the current session or DetachedInstanceError happens
          doc = self.session.merge(doc)
          # Iterate over each mention class
          for i, mention_class in enumerate(self.mention_classes):
              tc_to_insert = defaultdict(list)
              # Generate TemporaryContexts that are children of the context using
              # the mention_space and filtered by the Matcher
              self.child_context_set.clear()
              for tc in self.matchers[i].apply(self.mention_spaces[i].apply(doc)):
                  rec = tc._load_id_or_insert(self.session)
                  if rec:
                      tc_to_insert[tc._get_table()].append(rec)
                  self.child_context_set.add(tc)
  
              # Bulk insert temporary contexts
              for table, records in tc_to_insert.items():
                  stmt = insert(table.__table__).values(records)
                  self.session.execute(stmt)
  
              # Generates and persists mentions
              mention_args = {"document_id": doc.id}
              for child_context in self.child_context_set:
                  # Assemble mention arguments
                  for arg_name in mention_class.__argnames__:
                      mention_args[arg_name + "_id"] = child_context.id
  
                  # Checking for existence
                  if not clear:
                      q = select([mention_class.id])
                      for key, value in list(mention_args.items()):
                          q = q.where(getattr(mention_class, key) == value)
                      mention_id = self.session.execute(q).first()
                      if mention_id is not None:
                          continue
  
                  # Add Mention to session
                  yield mention_class(**mention_args)
docs: Extract mentions from the given Document.
  
          :param doc: A document to process.
          :param clear: Whether or not to clear the existing database entries.