#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
p61「梅殷」读音交叉验证：用 paraformer 独立转写，与 SenseVoice 对照。

工单读音锁：梅殷 méi yīn / 私谥 sī shì / 惠宗 huì zōng
SenseVoice 结果：梅英(疑似前后鼻音) / 私事(同音正字化) / 惠宗(命中)
本脚本用第二个引擎判「梅殷」到底 yīn 还是 yīng；ASR 只作疑似标记，最终耳检。
"""
import os, sys, re, json, time
try:
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
    pass

HERE = os.path.dirname(os.path.abspath(__file__))
VOX = os.path.join(HERE, "ep03_vox_fix6", "ep03_vox_fix6_p61.wav")
OUT = os.path.join(HERE, "ep03_fix6_asr_cross.json")


def main():
    from funasr import AutoModel
    t0 = time.time()
    para = AutoModel(
        model="iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch",
        device="cpu", disable_update=True)
    print("PARAFORMER_OK %.1fs" % (time.time() - t0), flush=True)
    r = para.generate(input=VOX, cache={})
    text = r[0]["text"] if isinstance(r, list) and r else str(r)
    print("PARA_ASR:", text, flush=True)

    # 目标词判定
    checks = {}
    for w, variants in [
        ("梅殷", ["梅英", "梅因", "美音", "煤殷", "梅荫"]),
        ("私谥", ["私事", "四是", "司事"]),
        ("惠宗", ["慧宗", "会宗"]),
    ]:
        hit = w in text
        wr = [v for v in variants if v in text]
        checks[w] = {"hit": hit, "variants": wr}
        print("  %s hit=%s variants=%s" % (w, hit, wr), flush=True)

    data = {"engine": "paraformer-large", "seg": "p61", "asr": text,
            "checks": checks,
            "conclusion": (
                "paraformer 命中「梅殷」→SenseVoice 的「梅英」多半是正字化，读音大概率正确"
                if checks["梅殷"]["hit"] else
                "paraformer 也未命中「梅殷」→前后鼻音疑似度升高，必须刚耳检")}
    with open(OUT, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    print("CONCLUSION:", data["conclusion"], flush=True)


if __name__ == "__main__":
    main()
