fix: resolve 3 MEDIUM code review findings
M1: Cache confusion-pair confidences in Path B (eliminate redundant
resolve_confusion_pair re-calls in _path_rule_engine)
M2: Resolve contradictions in Path C instead of hardcoding
resolved_count=0 in _path_llm_assisted
M4: Add DIVIDE_25 to contradiction pair coverage (50-25, 100-25)
and update test_contradiction_pairs_defined to verify all 3 variants
This commit is contained in:
@@ -157,11 +157,13 @@ def _path_rule_engine(
|
||||
|
||||
# 2. 运行所有混淆组解析器
|
||||
resolved_types: dict[str, str] = {}
|
||||
resolved_confidences: dict[str, float] = {}
|
||||
for pair_name in _PAIR_NAMES:
|
||||
try:
|
||||
result = resolve_confusion_pair(features, pair_name)
|
||||
if result["resolved_type"] != "unknown" and result["confidence"] > 0:
|
||||
resolved_types[pair_name] = result["resolved_type"]
|
||||
resolved_confidences[pair_name] = result["confidence"]
|
||||
except Exception as e:
|
||||
logger.debug("[pipeline] 混淆对 %s 解析异常: %s", pair_name, e)
|
||||
|
||||
@@ -192,16 +194,14 @@ def _path_rule_engine(
|
||||
final_base_confidence = keyword_info["confidence"]
|
||||
|
||||
# 如果规则引擎有更高置信度的结果, 则采纳
|
||||
# 使用第一轮缓存的结果(M1: 消除冗余重复调用)
|
||||
best_resolved_type = None
|
||||
best_resolved_conf = 0.0
|
||||
for pair_name, rtype in resolved_types.items():
|
||||
try:
|
||||
rr = resolve_confusion_pair(features, pair_name)
|
||||
if rr["confidence"] > best_resolved_conf:
|
||||
best_resolved_conf = rr["confidence"]
|
||||
best_resolved_type = rtype
|
||||
except Exception:
|
||||
continue
|
||||
cached_conf = resolved_confidences.get(pair_name, 0.0)
|
||||
if cached_conf > best_resolved_conf:
|
||||
best_resolved_conf = cached_conf
|
||||
best_resolved_type = rtype
|
||||
|
||||
if best_resolved_type and best_resolved_conf > final_base_confidence:
|
||||
final_category = best_resolved_type
|
||||
@@ -272,7 +272,7 @@ def _path_llm_assisted(
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# 3. 矛盾检测
|
||||
# 3. 矛盾检测与解决 (M2: 消除硬编码 resolved_count=0)
|
||||
resolved_types: dict[str, str] = {}
|
||||
for pair_name in _PAIR_NAMES:
|
||||
try:
|
||||
@@ -285,6 +285,19 @@ def _path_llm_assisted(
|
||||
features["resolved_types"] = resolved_types
|
||||
contradictions = detect_contradictions(features)
|
||||
|
||||
resolution_map: dict[str, Any] = {
|
||||
"resolved_count": 0,
|
||||
"total_count": len(contradictions),
|
||||
}
|
||||
for c in contradictions:
|
||||
try:
|
||||
winner = resolve_contradiction(features, c)
|
||||
if winner:
|
||||
resolution_map[c.get("name", "unknown")] = winner
|
||||
resolution_map["resolved_count"] += 1
|
||||
except Exception as e:
|
||||
logger.debug("[pipeline] Path C 矛盾解决异常: %s", e)
|
||||
|
||||
# 4. 确信度计算
|
||||
keyword_result_v2 = _build_keyword_result_for_v2(keyword_info)
|
||||
keyword_result_v2["base_confidence"] = validated_confidence
|
||||
@@ -295,7 +308,7 @@ def _path_llm_assisted(
|
||||
keyword_result=keyword_result_v2,
|
||||
structure_features=structure_features,
|
||||
contradictions=contradictions,
|
||||
resolution={"resolved_count": 0, "total_count": len(contradictions)},
|
||||
resolution=resolution_map,
|
||||
)
|
||||
|
||||
return {
|
||||
|
||||
@@ -45,6 +45,16 @@ CONTRADICTION_PAIRS: list[dict[str, str]] = [
|
||||
"type_a": "DIVIDE_50",
|
||||
"type_b": "DIVIDE_100",
|
||||
},
|
||||
{
|
||||
"name": "division_50_25_100",
|
||||
"type_a": "DIVIDE_50",
|
||||
"type_b": "DIVIDE_25",
|
||||
},
|
||||
{
|
||||
"name": "division_50_25_100",
|
||||
"type_a": "DIVIDE_100",
|
||||
"type_b": "DIVIDE_25",
|
||||
},
|
||||
{
|
||||
"name": "mn_output_mode",
|
||||
"type_a": "M:N",
|
||||
|
||||
Reference in New Issue
Block a user