def analyze(self, normalized): text = normalized.data() EntityTag.delete_set(normalized.package.collection, normalized.package.id) entities = set() for rex, matches in self.expressions(): for match in rex.finditer(text): _, match, _ = match.groups() entities.update(matches[match]) for entity in entities: tag = EntityTag() tag.collection = normalized.package.collection tag.package_id = normalized.package.id tag.entity_id = entity db.session.add(tag) db.session.commit() if len(entities): log.info("Tagged %r with %d entities", normalized.package.id, len(entities))