Skip to content

postprocess_hotwords: non-Chinese hotword maps to an empty pinyin key and force-replaces every latin/digit span (ratio("","")=100) #3796

Open

Description

@shanyuhai007-netizen

环境

  • funasr 1.4.16(PyPI),paraformer-zh + fsmn-vad + ct-punc
  • 已安装 pypinyin / rapidfuzz(postprocess 模糊匹配依赖)

现象

postprocess_hotwords 传入含拉丁/数字的热词时,转写文本里的英文与数字片段会被整段替换成热词。一个 "AI" 热词会把识别结果刷成满屏 AIAI,数字也被吞掉。

最小复现(纯 Python,不需要模型与音频):

from funasr.utils.postprocess_hotwords import build_postprocess_hotword_matcher

m = build_postprocess_hotword_matcher(["AI"])
out, matches = m.apply_text("用AI做产品,2024年做AI的公司有很多。")
print(out)
# 实际输出: 用AIAI做产品AIAI年做AIAI的公司有很多。
# 期望输出: 原文不变(或仅做同音纠正)

生产环境实测(paraformer-zh 转写 1 小时中文播客,热词 "AI"):转写稿里出现大量 AIAIAIAIAI 连发、2024 等数字被替换成热词。

根因分析

funasr/utils/postprocess_hotwords.py 中:

  1. _to_pinyin_key 用 lazy_pinyin(..., errors="ignore") 构造拼音键,该配置会丢弃所有非中文字符。纯拉丁热词(如 "AI")的拼音键是空串 ""。
  2. _apply_fuzzy 对滑窗 segment 计算 fuzz.ratio(segment_py, target_py),而 rapidfuzz.fuzz.ratio("", "") == 100.0。于是任何拼音键同样为空的滑窗(纯英文/数字窗口)都得满分,超过默认阈值 0.85,被无条件替换成热词。
  3. 连带影响:"A"、"I" 等相邻单字符窗口也各自满分且互不重叠,双双命中后 "AI" 变成 "AIAI";数字窗口 "20"、"24" 同样被替换。
  4. 混合热词(如 "C端")拼音键只剩 "duan",每个同音中文段都会被粘上拉丁:这个端口 → 这个C端口。
  5. 反直觉的是,这类热词本来的目标收益永远无法命中:ratio("ai", "") == 0,期望中的「爱 → AI」同音纠正一次也不会发生。即该路径对非中文热词只有破坏、没有收益。

建议修复

PostprocessHotwordMatcher.__init__ 构造 _length_buckets 时跳过拼音键为空(或与原词长度严重不符)的目标,例如:

for target in self.fuzzy_targets:
    target_py = _to_pinyin_key(target)
    if not target_py:      # 纯拉丁/数字热词:模糊匹配无意义且误伤满分
        continue
    ...

(更严格的话可要求 target_py 覆盖原词的全部非中文字符长度,避免混合词残键误配。)

用户侧临时规避

只把全中文词条传给 postprocess_hotwords;含英文/数字的术语交给其他纠正路径。

Activity

  1. LauraGPT commented on Oct 27, 2026

    @LauraGPT
    Collaborator

    已复现并提交修复 PR #3798。1.4.16 中英文/数字拼音键丢失的问题,当前 main 已有修正;本次补上仍会把“这个端口”替换成“这个C端口”的跨中英文误匹配,同时保留“爱助手→AI助手”等完整同音转换。

    新增用例在未修改 main 上失败,修复后热词及相邻文本处理共 22 项测试通过,编译检查通过。提交 9a1d42e 的签名已验证,代码树与实测候选完全一致。这里只是纯文本 CPU 验证,不代表模型/GPU 或完整 suite。PR 尚未合并/发布,已请求独立审核;此 issue 保持开放。

  2. LauraGPT commented on Oct 28, 2026

    @LauraGPT
    Collaborator

    补充进展:PR #3798 已正常合并到 main(dd00b0e42261b628466037f3a522d713399360b6)。本次补齐“这个端口”被误改成“这个C端口”的跨中英文匹配限制,并保留完整同音转换与同脚本模糊纠正。

    当前主干合入结果的 22 项纯文本回归通过;PR exact-head 的 Python 3.10、3.12 CI 各 22 项通过、零跳过,测试产物与提交号已核对;合并后两个文本 CI 及文档 CI 均成功。这不代表模型/GPU 或完整 suite 验收。

    注意:1.4.17 的发布早于这次合并,因此 1.4.17 不包含 #3798 的剩余跨脚本修复;这部分目前仅在 main,待后续版本发布。此 issue 继续保持开放,等待发布版本及实际使用验证。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions