def apply(self, context, clear, split, check_for_existing=True, **kwargs):
    'Extract Candidates from a Context'
    if (not isinstance(context, Sentence)):
        raise NotImplementedError(('%s is currently only implemented for Sentence contexts.' % self.__name__))
    entity_idxs = dict(((et, defaultdict(list)) for et in set(self.entity_types)))
    L = len(context.words)
    for i in range(L):
        if (context.entity_types[i] is not None):
            ets = context.entity_types[i].split(self.entity_sep)
            cids = context.entity_cids[i].split(self.entity_sep)
            for (et, cid) in zip(ets, cids):
                if (et in entity_idxs):
                    entity_idxs[et][cid].append(i)
    entity_spans = defaultdict(list)
    entity_cids = {}
    for (et, cid_idxs) in iteritems(entity_idxs):
        for (cid, idxs) in iteritems(entity_idxs[et]):
            while (len(idxs) > 0):
                i = idxs.pop(0)
                char_start = context.char_offsets[i]
                char_end = ((char_start + len(context.words[i])) - 1)
                while ((len(idxs) > 0) and (idxs[0] == (i + 1))):
                    i = idxs.pop(0)
                    char_end = ((context.char_offsets[i] + len(context.words[i])) - 1)
                tc = TemporarySpan(char_start=char_start, char_end=char_end, sentence=context)
                tc.load_id_or_insert(self.session)
                entity_cids[tc.id] = cid
                entity_spans[et].append(tc)
    candidate_args = {'split': split}
    for args in product(*[enumerate(entity_spans[et]) for et in self.entity_types]):
        if (self.arity == 2):
            (ai, a) = args[0]
            (bi, b) = args[1]
            if ((not self.self_relations) and (a == b)):
                continue
            elif ((not self.nested_relations) and ((a in b) or (b in a))):
                continue
            elif ((not self.symmetric_relations) and (ai > bi)):
                continue
        for (i, arg_name) in enumerate(self.candidate_class.__argnames__):
            candidate_args[(arg_name + '_id')] = args[i][1].id
            candidate_args[(arg_name + '_cid')] = entity_cids[args[i][1].id]
        if check_for_existing:
            q = select([self.candidate_class.id])
            for (key, value) in iteritems(candidate_args):
                q = q.where((getattr(self.candidate_class, key) == value))
            candidate_id = self.session.execute(q).first()
            if (candidate_id is not None):
                continue
        (yield self.candidate_class(**candidate_args))
Extract Candidates from a Context
def apply(self, context, clear, split, check_for_existing=True, **kwargs):
    
    if (not isinstance(context, Sentence)):
        raise NotImplementedError(('%s is currently only implemented for Sentence contexts.' % self.__name__))
    entity_idxs = dict(((et, defaultdict(list)) for et in set(self.entity_types)))
    L = len(context.words)
    for i in range(L):
        if (context.entity_types[i] is not None):
            ets = context.entity_types[i].split(self.entity_sep)
            cids = context.entity_cids[i].split(self.entity_sep)
            for (et, cid) in zip(ets, cids):
                if (et in entity_idxs):
                    entity_idxs[et][cid].append(i)
    entity_spans = defaultdict(list)
    entity_cids = {}
    for (et, cid_idxs) in iteritems(entity_idxs):
        for (cid, idxs) in iteritems(entity_idxs[et]):
            while (len(idxs) > 0):
                i = idxs.pop(0)
                char_start = context.char_offsets[i]
                char_end = ((char_start + len(context.words[i])) - 1)
                while ((len(idxs) > 0) and (idxs[0] == (i + 1))):
                    i = idxs.pop(0)
                    char_end = ((context.char_offsets[i] + len(context.words[i])) - 1)
                tc = TemporarySpan(char_start=char_start, char_end=char_end, sentence=context)
                tc.load_id_or_insert(self.session)
                entity_cids[tc.id] = cid
                entity_spans[et].append(tc)
    candidate_args = {'split': split}
    for args in product(*[enumerate(entity_spans[et]) for et in self.entity_types]):
        if (self.arity == 2):
            (ai, a) = args[0]
            (bi, b) = args[1]
            if ((not self.self_relations) and (a == b)):
                continue
            elif ((not self.nested_relations) and ((a in b) or (b in a))):
                continue
            elif ((not self.symmetric_relations) and (ai > bi)):
                continue
        for (i, arg_name) in enumerate(self.candidate_class.__argnames__):
            candidate_args[(arg_name + '_id')] = args[i][1].id
            candidate_args[(arg_name + '_cid')] = entity_cids[args[i][1].id]
        if check_for_existing:
            q = select([self.candidate_class.id])
            for (key, value) in iteritems(candidate_args):
                q = q.where((getattr(self.candidate_class, key) == value))
            candidate_id = self.session.execute(q).first()
            if (candidate_id is not None):
                continue
        (yield self.candidate_class(**candidate_args))