import collections, json, os, re, sys, time from concurrent.futures import ThreadPoolExecutor from . import pipeline as webqa from .pipeline import QWORDS _NAME = re.compile("\\b[A-Z][\\w'’.-]+(?:\\s+(?:(?:de|da|van|von|of|the|du|del|di|la|le)\\s+)?[A-Z][\\w'’.-]+){1,3}") STOPCAP = {'The', 'This', 'That', 'These', 'There', 'What', 'When', 'Where', 'Which', 'Who', 'How', 'Why', 'In', 'On', 'At', 'For', 'From', 'With', 'After', 'Before', 'During', 'Since', 'His', 'Her', 'Its', 'Their', 'A', 'An', 'Of', 'To', 'By', 'And', 'Or', 'But', 'It', 'He', 'She', 'They', 'We', 'You', 'I', 'Yes', 'No'} def content_words(q): return [t for t in re.findall('[a-z0-9]+', q.lower()) if t not in QWORDS and len(t) > 2] def entities_in(texts, exclude): c = collections.Counter() for t in texts: for m in _NAME.findall(t): m = m.strip(" .,'’") toks = m.split() if toks[0] in STOPCAP or len(m) > 40 or m.lower() in exclude: continue if any((w.lower() in exclude for w in toks)) and len(toks) <= 2: continue c[m] += 1 return c def covers(text, w): return re.search('(? 20 and e.split()[-1].lower() in s.lower()] best = None if sents: sc = self.verify.predict([(q, f"{r['title']}. {s}") for r, s in sents[:16]], batch_size=32, show_progress_bar=False) j = max(range(len(sc)), key=lambda i: float(sc[i])) best = (float(sc[j]), sents[j][1], sents[j][0]) scored.append({'entity': e, 'covered': len(cov), 'cov_words': cov, 'v': best[0] if best else -99.0, 'sent': best[1] if best else None, 'url': best[2]['href'] if best else None, 'title': best[2]['title'] if best else None}) scored.sort(key=lambda x: (-x['covered'], -x['v'])) top = scored[0] runner = scored[1] if len(scored) > 1 else None return {'sieve': top['entity'], 'covered': top['covered'], 'of': len(cw), 'cov_words': top['cov_words'], 'v': round(top['v'], 2), 'sent': top['sent'], 'url': top['url'], 'title': top['title'], 'runner_up': runner['entity'] if runner else None, 'runner_covered': runner['covered'] if runner else None, 'candidates': [s['entity'] for s in scored], 'ms': round(1000 * (time.time() - t0))}