#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""对 s05/s08 改词候选做 ASR 回读：先卡「中文+文本完整」，声调 ASR 验不了须人耳。"""
import glob, os, re, sys
try:
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
    pass

from funasr import AutoModel

D = r"D:/WorkBuddyProd/sci06/probe_s05s08_v2"
REF = {
    "s05_zheng": "散射的程度，跟波长有关：",
    "s05_san":   "伞射的程度，跟波长有关：",
    "s08_zheng": "蓝光散射的程度，将近是红光的六倍。",
    "s08_san":   "蓝光伞射的程度，将近是红光的六倍。",
}


def clean(raw):
    return re.sub(r"<\|[^|>]*\|>", "", raw).strip()


def main():
    sense = AutoModel(model="iic/SenseVoiceSmall", trust_remote_code=True,
                      device="cpu", disable_update=True)
    for p in sorted(glob.glob(os.path.join(D, "s*.wav"))):
        r = sense.generate(input=p, cache={}, language="zh", use_itn=True)
        raw = r[0]["text"] if isinstance(r, list) and r else str(r)
        t = clean(raw)
        lang = re.findall(r"<\|(\w+)\|>", raw)
        name = os.path.splitext(os.path.basename(p))[0]
        ref = REF.get(name, "")
        chars = [c for c in ref if "\u4e00" <= c <= "\u9fff"]
        hit = sum(1 for c in chars if c in t) / max(1, len(chars))
        print("%-12s lang=%-3s hit=%.2f ASR=%r" %
              (name, ",".join(lang[:1]), hit, t), flush=True)


if __name__ == "__main__":
    main()
