# -*- coding: utf-8 -*- """The deterministic draft check, and its measurement. The check compares an outgoing draft against the counterparty's record and the thread it replies to. It reads no language, scores no text and contains no model. It asks four questions: is every bank detail in this draft already on record for this counterparty is every recipient on record, or already in the thread is every link domain on record is every attachment one the thread actually carried Anything that is not gets flagged for a person. That is the whole mechanism. The honest framing, which the paper keeps: **this is not a detector.** It cannot tell a fraudulent new IBAN from a counterparty that genuinely changed bank, because both are an IBAN that is not on record. It is a gate that routes to a human, so the thing to measure is not precision in the usual sense but the flag rate and what it costs to review, set against the recall on the attack that every machine-learned detector in paper 13 missed. python check.py scores drafts.jsonl, writes results.json """ import collections import io import json import os HERE = os.path.dirname(os.path.abspath(__file__)) def check(draft, record): """Return the list of reasons this draft needs a human. Empty means send.""" reasons = [] known_ibans = {i.replace(' ', '') for i in record['ibans']} for i in draft.get('ibans_in_draft', []): if i.replace(' ', '') not in known_ibans: reasons.append('bank detail not on record') break known_to = set(record['contacts']) for r in draft.get('to', []): if r not in known_to: reasons.append('recipient not on record') break known_links = set(record['link_domains']) for l in draft.get('links', []): if l not in known_links: reasons.append('link domain not on record') break carried = set(draft.get('thread_attachments', [])) for a in draft.get('attachments', []): if a not in carried: reasons.append('attachment not in the thread') break return reasons def main(): records = json.load(io.open(os.path.join(HERE, 'records.json'), encoding='utf-8')) drafts = [json.loads(l) for l in io.open(os.path.join(HERE, 'drafts.jsonl'), encoding='utf-8') if l.strip()] per_class = collections.defaultdict(lambda: {'n': 0, 'flagged': 0}) tp = fp = tn = fn = 0 rows = [] for d in drafts: reasons = check(d, records[d['counterparty']]) flagged = bool(reasons) c = per_class[d['cls']] c['n'] += 1 c['flagged'] += int(flagged) if d['fraudulent'] and flagged: tp += 1 elif d['fraudulent'] and not flagged: fn += 1 elif not d['fraudulent'] and flagged: fp += 1 else: tn += 1 rows.append(dict(id=d['id'], cls=d['cls'], fraudulent=d['fraudulent'], flagged=flagged, reasons=reasons)) n = len(drafts) res = { 'n': n, 'per_class': {k: dict(v) for k, v in sorted(per_class.items())}, 'confusion': {'tp': tp, 'fp': fp, 'tn': tn, 'fn': fn}, 'recall_on_fraud': round(tp / (tp + fn), 4) if tp + fn else None, 'flag_rate_overall': round((tp + fp) / n, 4), 'flag_rate_on_legitimate': round(fp / (fp + tn), 4) if fp + tn else None, 'rows': rows, } io.open(os.path.join(HERE, 'results.json'), 'w', encoding='utf-8', newline='').write(json.dumps(res, indent=1)) print('drafts: %d' % n) print() print('%-20s %5s %8s %8s' % ('class', 'n', 'flagged', 'rate')) for k, v in sorted(per_class.items()): print('%-20s %5d %8d %7.1f%%' % (k, v['n'], v['flagged'], 100.0 * v['flagged'] / v['n'])) print() print('recall on fraudulent drafts %.1f%% (%d of %d)' % (100.0 * tp / (tp + fn), tp, tp + fn)) print('flag rate on legitimate drafts %.1f%% (%d of %d)' % (100.0 * fp / (fp + tn), fp, fp + tn)) print('overall flag rate %.1f%% (%d of %d)' % (100.0 * (tp + fp) / n, tp + fp, n)) if __name__ == '__main__': main()