def card_titles(): alias = aliased(Alias) card = aliased(Card) q = db.session.query(alias.name) q = q.join(card, alias.card) q = q.add_column(card.id) to_d = lambda r: { 'id': r.id, 'text': r.name, 'normalized': normalize(r.name) } return [to_d(r) for r in q.all()]
def extract_known(parent): if parent.text is None or not len(parent.text): return cards = card_titles() names = [c['normalized'] for c in cards] names = re.compile('(%s)' % '|'.join(names)) matches = [] for match in names.finditer(normalize(parent.text)): for card in cards: if card['normalized'] == match.group(1): matches.append((card['id'], match.start(1))) ids = set([m[0] for m in matches]) q = db.session.query(Card) q = q.filter(Card.id.in_(ids)) cards = {c.id: c for c in q.all()} for id, offset in matches: card = cards.pop(id, None) if card is not None: yield offset, card