fix: resolve #7693 — Cross-script title guard rejects valid CJK+English titles
When the user writes in CJK, the title language mismatch guard now allows Latin characters in the title as long as the title also contains CJK characters (cjk >= 2). This permits CJK titles with borrowed English product/technical terms (e.g. 'WeChat Pay 回调失败排查', 'Python 代码修复') while still rejecting pure-Latin titles for CJK conversations and unrelated scripts (Cyrillic, Arabic, etc.). Added regression tests: test_cjk_start_mixed_cjk_latin_title_is_allowed and test_cjk_start_pure_latin_title_still_rejected.
This commit is contained in:
1 parent
e168b67e42
commit
c42c5b4d30
2 files changed
+43
-1
No files matched your search
+14
-1
@@ -4040,13 +4040,26 @@ def _title_language_mismatch(user_text: str, title: str) -> bool:
|
||||
return False
|
||||
|
||||
# (1) Cross-script mismatch — language-agnostic.
|
||||
# CJK text routinely borrows Latin product/technical terms (e.g. "WeChat
|
||||
# Pay", "Python", "ProRes RAW"), so when the user writes in CJK, Latin
|
||||
# characters in the title are acceptable as long as the title also
|
||||
# contains CJK — i.e. the title is genuinely mixed, not pure drift.
|
||||
# A pure-Latin title for a CJK conversation is still rejected.
|
||||
# Unrelated scripts (Cyrillic, Arabic, Greek …) are always flagged.
|
||||
user_script = _dominant_script(user_text)
|
||||
if user_script:
|
||||
title_counts = _script_counts(candidate)
|
||||
title_total = sum(title_counts.values())
|
||||
if title_total >= 2:
|
||||
for script, n in title_counts.items():
|
||||
if script != user_script and n >= 2 and (n / title_total) >= 0.35:
|
||||
if script == user_script:
|
||||
continue
|
||||
# When user writes in CJK, Latin in the title is a borrowed
|
||||
# term as long as the title also contains CJK characters.
|
||||
if user_script == 'cjk' and script == 'latin':
|
||||
if title_counts.get('cjk', 0) >= 2:
|
||||
continue
|
||||
if n >= 2 and (n / title_total) >= 0.35:
|
||||
return True
|
||||
|
||||
# (2) Legacy same-script German→English heuristic.
|
||||
|
||||
@@ -71,6 +71,35 @@ def test_cjk_start_english_title_is_rejected():
|
||||
assert _title_language_mismatch("如何修复这个错误问题", "Fixing the Bug") is True
|
||||
|
||||
|
||||
def test_cjk_start_mixed_cjk_latin_title_is_allowed():
|
||||
"""CJK conversations frequently embed English product/technical terms in
|
||||
titles (e.g. 'WeChat Pay 回调失败排查'). As long as the title also
|
||||
contains CJK characters, Latin borrowed terms should not trigger rejection.
|
||||
Regression test for #7693."""
|
||||
from api.streaming import _title_language_mismatch
|
||||
|
||||
# Pure CJK user, CJK title with English product name
|
||||
assert _title_language_mismatch(
|
||||
"微信支付回调一直失败怎么办", "WeChat Pay 回调失败排查"
|
||||
) is False
|
||||
# CJK user, CJK title with English tech term
|
||||
assert _title_language_mismatch(
|
||||
"如何修复这个错误问题", "Python 代码修复"
|
||||
) is False
|
||||
# Mixed CJK+Latin user, mixed title
|
||||
assert _title_language_mismatch(
|
||||
"prores raw是否能选择压缩?", "ProRes RAW 压缩选项与 BRAW 对比"
|
||||
) is False
|
||||
|
||||
|
||||
def test_cjk_start_pure_latin_title_still_rejected():
|
||||
"""A purely Latin title for a CJK conversation is still a genuine drift."""
|
||||
from api.streaming import _title_language_mismatch
|
||||
|
||||
assert _title_language_mismatch("如何修复这个错误问题", "Fixing the Bug") is True
|
||||
assert _title_language_mismatch("如何修复这个错误问题", "Python Error Guide") is True
|
||||
|
||||
|
||||
# ── regression guards: legitimate same-script titles must NOT be rejected ───
|
||||
|
||||
def test_english_start_english_title_allowed():
|
||||
|
||||
Reference in new issue
Block a user