"""2.3: a names file, sent with every correction call, vs. no names file.
Input: plain Grok transcripts of the 38 synthetic memos (corpus/transcripts_grok.json).
Each memo is corrected in its own call (stateless), so the names file must be attached every time."""
import json, pathlib, sys
from concurrent.futures import ThreadPoolExecutor
ROOT = pathlib.Path(__file__).resolve().parents[1]; sys.path.insert(0, str(ROOT))
from common import chat, dump
sys.path.insert(0, str(ROOT / 'corpus')); from score_names import score, NAMES, TERMS

HERE = pathlib.Path(__file__).resolve().parent
d = json.load(open(ROOT / 'corpus/memos.json', encoding='utf-8'))
src = json.load(open(ROOT / 'corpus/transcripts_grok.json', encoding='utf-8'))
names_md = '\n'.join(f'- {k}：{v}' for k, v in d['people'].items()) + '\n\n常用术语：' + '、'.join(d['terms'])
(HERE / 'names.md').write_text('# 我常提到的人\n\n' + names_md + '\n', encoding='utf-8')
BASE = '下面是一段语音备忘录的自动转写，可能有识别错误。请改正识别错误，只输出改好的全文，不要解释。'
conds = {'no_list': lambda t: BASE + '\n\n转写：\n' + t,
         'with_list': lambda t: BASE + '\n\n这是我常提到的人和术语，转写里听起来像的地方请按这里的写法改：\n' + names_md + '\n\n转写：\n' + t}
res = json.load(open(HERE / 'results.json', encoding='utf-8')) if (HERE / 'results.json').exists() else {}
for model in sys.argv[1:] or ['deepseek-v4.1-flash', 'gemini-3.8-flash']:
    for c, mk in conds.items():
        def one(mid):
            out, u, _ = chat([{'role': 'user', 'content': mk(src[mid])}], model, temperature=0)
            return mid, out.strip()
        with ThreadPoolExecutor(8) as ex:
            outs = dict(ex.map(one, src))
        dump(outs, HERE / f'out_{model}_{c}.json')
        nt, nh, nm = score(outs, NAMES); tt, th, _ = score(outs, TERMS)
        res[f'{model}:{c}'] = {'names': f'{nh}/{nt}', 'terms': f'{th}/{tt}', 'name_misses': nm}
        print(model, c, f'names {nh}/{nt}', f'terms {th}/{tt}', nm)
bt, bh, _ = score(src, NAMES); res['asr_only'] = {'names': f'{bh}/{bt}'}
dump(res, HERE / 'results.json')
