#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""04 s11 候选 + 送审版 ASR 回读（花酱 id=227/228）

卡：中文 lang / 文本完整 / 无英文漂移 / 逗号处是否断开（ASR 标点是辅证）。
⚠ 声调 ASR 验不了，「鲜」是否一声 xiān 必须刚耳检，本脚本只做客观回读。
"""
import glob, os, re, sys
try:
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
    pass
from funasr import AutoModel

PROBE = r"D:/WorkBuddyProd/chili04/probe_s11"
HERE = r"D:/Download/muse_exchange"

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

sense = AutoModel(model="iic/SenseVoiceSmall", trust_remote_code=True,
                  device="cpu", disable_update=True)
files = sorted(glob.glob(os.path.join(PROBE, "*.wav")))
print("files=%d" % len(files), flush=True)
for p in files:
    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)
    lat = sum(c.isascii() and c.isalpha() for c in t)
    chars = [c for c in "味觉只有酸甜苦咸鲜辣不在里头" if "\u4e00" <= c <= "\u9fff"]
    hit = sum(1 for c in chars if c in t) / max(1, len(chars))
    # 断句辅证：ASR 若在「鲜/先」与「辣」之间给出标点，说明听感上有停顿
    brk = bool(re.search(r"[鲜先仙咸]\s*[，。；、,.!?！？]\s*辣", t))
    lian = "鲜辣" in t or "先辣" in t or "仙辣" in t
    ok = "zh" in lang and lat <= 3 and hit >= 0.6
    print("%-34s lang=%-3s latin=%-2d hit=%.2f break=%-5s liandu=%-5s %-5s ASR=%r" %
          (os.path.basename(p), ",".join(lang[:1]), lat, hit, brk, lian,
           "OK" if ok else "CHECK", t), flush=True)
