def apply(self, context, clear, split, **kwargs):
        """Extract candidates from the given Context.

        :param context: A document to process.
        :param clear: Whether or not to clear the existing database entries.
        :param split: Which split to use.
        """
        logger.debug(f"Document: {context}")
        # Iterate over each candidate class
        for i, candidate_class in enumerate(self.candidate_classes):
            logger.debug(f"  Relation: {candidate_class.__name__}")
            # Generates and persists candidates
            candidate_args = {"split": split}
            candidate_args["document_id"] = context.id
            cands = product(
                *[
                    enumerate(
                        self.session.query(mention)
                        .filter(mention.document_id == context.id)
                        .order_by(mention.id)
                        .all()
                    )
                    for mention in candidate_class.mentions
                ]
            )
            for cand in cands:

                # Apply throttler if one was given.
                # Accepts a tuple of Mention objects
                # (throttler returns whether or not proposed candidate
                # passes throttling condition)
                if self.throttlers[i]:
                    if not self.throttlers[i](
                        tuple(cand[j][1] for j in range(self.arities[i]))
                    ):
                        continue

                # TODO: Make this work for higher-order relations
                if self.arities[i] == 2:
                    ai, a = (cand[0][0], cand[0][1].context)
                    bi, b = (cand[1][0], cand[1][1].context)

                    # Check for self-joins, "nested" joins (joins from context to
                    # its subcontext), and flipped duplicate "symmetric" relations
                    if not self.self_relations and a == b:
                        logger.debug(f"Skipping self-joined candidate {cand}")
                        continue
                    if not self.nested_relations and (a in b or b in a):
                        logger.debug(f"Skipping nested candidate {cand}")
                        continue
                    if not self.symmetric_relations and ai > bi:
                        logger.debug(f"Skipping symmetric candidate {cand}")
                        continue

                # Assemble candidate arguments
                for j, arg_name in enumerate(candidate_class.__argnames__):
                    candidate_args[arg_name + "_id"] = cand[j][1].id

                # Checking for existence
                if not clear:
                    q = select([candidate_class.id])
                    for key, value in list(candidate_args.items()):
                        q = q.where(getattr(candidate_class, key) == value)
                    candidate_id = self.session.execute(q).first()
                    if candidate_id is not None:
                        continue

                # Add Candidate to session
                yield candidate_class(**candidate_args)
def apply ( self , context , clear , split , ** kwargs ) : logger . debug ( f"Document: {context}" ) for i , candidate_class in enumerate ( self . candidate_classes ) : logger . debug ( f"  Relation: {candidate_class.__name__}" ) candidate_args = { "split" : split } candidate_args [ "document_id" ] = context . id cands = product ( * [ enumerate ( self . session . query ( mention ) . filter ( mention . document_id == context . id ) . order_by ( mention . id ) . all ( ) ) for mention in candidate_class . mentions ] ) for cand in cands : if self . throttlers [ i ] : if not self . throttlers [ i ] ( tuple ( cand [ j ] [ 1 ] for j in range ( self . arities [ i ] ) ) ) : continue if self . arities [ i ] == 2 : ai , a = ( cand [ 0 ] [ 0 ] , cand [ 0 ] [ 1 ] . context ) bi , b = ( cand [ 1 ] [ 0 ] , cand [ 1 ] [ 1 ] . context ) if not self . self_relations and a == b : logger . debug ( f"Skipping self-joined candidate {cand}" ) continue if not self . nested_relations and ( a in b or b in a ) : logger . debug ( f"Skipping nested candidate {cand}" ) continue if not self . symmetric_relations and ai > bi : logger . debug ( f"Skipping symmetric candidate {cand}" ) continue for j , arg_name in enumerate ( candidate_class . __argnames__ ) : candidate_args [ arg_name + "_id" ] = cand [ j ] [ 1 ] . id if not clear : q = select ( [ candidate_class . id ] ) for key , value in list ( candidate_args . items ( ) ) : q = q . where ( getattr ( candidate_class , key ) == value ) candidate_id = self . session . execute ( q ) . first ( ) if candidate_id is not None : continue yield candidate_class ( ** candidate_args )
Extract candidates from the given Context.

        :param context: A document to process.
        :param clear: Whether or not to clear the existing database entries.
        :param split: Which split to use.