#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""校验主题 JSON 中的引文是否逐字出现在对应原文里
用法：python verify_ima_quotes.py
"""
import json
import os
import sys

W = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
D = os.path.join(W, '数据')


def load(name):
    return json.load(open(os.path.join(D, name), encoding='utf-8'))


corp = {}
for f in ('ima_corpus_wechat.json', 'ima_corpus_local.json'):
    for x in load(f)['items']:
        corp[x['title'].strip()] = x.get('text') or ''

total = bad = 0
fails = []
for f in ('ima_themes_wechat.json', 'ima_themes_local.json'):
    d = load(f)
    for t in d.get('themes', []):
        for p in t.get('points', []):
            ev = p.get('evidence') or {}
            q = (ev.get('quote') or '').strip()
            title = (ev.get('title') or '').strip()
            total += 1
            text = corp.get(title)
            if text is None:
                bad += 1
                fails.append(('标题未命中', title[:40], q[:30]))
            elif q and q not in text:
                bad += 1
                fails.append(('引文不在原文', title[:40], q[:30]))
            elif not q:
                bad += 1
                fails.append(('引文为空', title[:40], ''))
print('引文总数 %d ｜通过 %d ｜不通过 %d ｜通过率 %.1f%%' % (
    total, total - bad, bad, 100.0 * (total - bad) / max(1, total)))
for x in fails[:15]:
    print('  ✗', x)
if fails:
    print('（报告生成前应删除或修正以上条目）')
