refactor(question_gen): reduce check_forbidden_material complexity to A(3)
Extract _match_any helper and declarative _FORBIDDEN_MATERIAL_RULES table to replace repetitive per-category for-loops. Reduces cyclomatic complexity from C(11) to A(3). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -253,6 +253,29 @@ def has_time_anchor(question_text: str) -> bool:
|
||||
return any(pattern.search(question_text) for pattern in _TIME_ANCHOR_PHRASES)
|
||||
|
||||
|
||||
def _match_any(text: str, patterns: list[re.Pattern[str]]) -> bool:
|
||||
"""判断文本是否匹配任一正则模式。
|
||||
|
||||
参数:
|
||||
text: 待检测文本。
|
||||
patterns: 预编译正则列表。
|
||||
|
||||
返回:
|
||||
True 表示至少命中一条模式。
|
||||
"""
|
||||
return any(p.search(text) for p in patterns)
|
||||
|
||||
|
||||
# 素材禁区规则表:(模式列表, 违规描述)
|
||||
_FORBIDDEN_MATERIAL_RULES: list[tuple[list[re.Pattern[str]], str]] = [
|
||||
(_T1_INSTANT_ACTION_PATTERNS, "T1 违规: 素材包含瞬时动作描述,不适合出题"),
|
||||
(_T1_SCOREBOARD_PATTERNS, "T1 违规: 素材包含记分牌时序信息,不适合出题"),
|
||||
(_T1_NO_DIALOGUE_CAUSAL_PATTERNS, "T1 违规: 素材缺乏对白因果线索,不适合出题"),
|
||||
(_T7_OPTION_REPETITION_PATTERNS, "T7 违规: 素材暗示可能产生重复选项"),
|
||||
(_T7_COUNTING_AMBIGUITY_PATTERNS, "T7 违规: 素材包含计数边界含糊描述"),
|
||||
]
|
||||
|
||||
|
||||
def check_forbidden_material(source_nodes_text: str, task_type: str) -> list[str]:
|
||||
"""出题禁区:检测 T1 类素材和 T7 噪声模式。
|
||||
|
||||
@@ -272,39 +295,11 @@ def check_forbidden_material(source_nodes_text: str, task_type: str) -> list[str
|
||||
返回:
|
||||
违规描述列表,空列表表示通过。
|
||||
"""
|
||||
violations: list[str] = []
|
||||
|
||||
# Phase 1: T1 瞬时动作检测
|
||||
for pattern in _T1_INSTANT_ACTION_PATTERNS:
|
||||
if pattern.search(source_nodes_text):
|
||||
violations.append("T1 违规: 素材包含瞬时动作描述,不适合出题")
|
||||
break
|
||||
|
||||
# Phase 2: T1 记分牌时序检测
|
||||
for pattern in _T1_SCOREBOARD_PATTERNS:
|
||||
if pattern.search(source_nodes_text):
|
||||
violations.append("T1 违规: 素材包含记分牌时序信息,不适合出题")
|
||||
break
|
||||
|
||||
# Phase 3: T1 无对白因果检测
|
||||
for pattern in _T1_NO_DIALOGUE_CAUSAL_PATTERNS:
|
||||
if pattern.search(source_nodes_text):
|
||||
violations.append("T1 违规: 素材缺乏对白因果线索,不适合出题")
|
||||
break
|
||||
|
||||
# Phase 4: T7 选项重复检测
|
||||
for pattern in _T7_OPTION_REPETITION_PATTERNS:
|
||||
if pattern.search(source_nodes_text):
|
||||
violations.append("T7 违规: 素材暗示可能产生重复选项")
|
||||
break
|
||||
|
||||
# Phase 5: T7 计数边界口径含糊检测
|
||||
for pattern in _T7_COUNTING_AMBIGUITY_PATTERNS:
|
||||
if pattern.search(source_nodes_text):
|
||||
violations.append("T7 违规: 素材包含计数边界含糊描述")
|
||||
break
|
||||
|
||||
return violations
|
||||
return [
|
||||
message
|
||||
for patterns, message in _FORBIDDEN_MATERIAL_RULES
|
||||
if _match_any(source_nodes_text, patterns)
|
||||
]
|
||||
|
||||
|
||||
def run_postprocess(
|
||||
|
||||
Reference in New Issue
Block a user