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

Offen Anfängerfreundlich
#3,796 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

Beginne mit funasr/utils/postprocess_hotwords.py, insbesondere mit _to_pinyin_key, _apply_fuzzy und PostprocessHotwordMatcher.__init__. Reproduziere das Problem mit dem bereitgestellten reinen Python-Beispiel und prüfe anschließend die bestehenden Tests des Matchers auf leere Pinyin-Schlüssel und Pinyin-Schlüssel mit gemischten Schriftsystemen. Die Aufgabe ist abgeschlossen, wenn nichtchinesische Hotwords keine unzusammenhängenden lateinischen oder Ziffernfolgen mehr ersetzen und gültiges chinesisches Homophon-Matching weiterhin funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

环境
  • 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;含英文/数字的术语交给其他纠正路径。

Vorherrschende Sprache
Python
Sterne
20.9k
Forks
2.1k
Ø Merge
1 T. 3 Std.
Gemergte PRs (30 T.)
46

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelscope/FunASR

Alle Issues in modelscope/FunASR

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.