fix(contact): 优化合同审核误报
This commit is contained in:
1 parent
74745a9d10
commit
6b96d5a14f
16 files changed
+2218
-18
No files matched your search
@@ -55,6 +55,9 @@ _CRITICAL_VALUE_PATTERNS = (
|
||||
r"\d+\s*(?:套|台|件|个|只|箱|批|吨|千克|KG|kg|台)",
|
||||
r"(?=[A-Za-z0-9-]{5,})(?=[A-Za-z0-9-]*[A-Za-z])(?=[A-Za-z0-9-]*\d)[A-Za-z0-9-]+",
|
||||
)
|
||||
_MATERIAL_DIRECTION_TERMS = (
|
||||
"不得", "无须", "无需", "不承担", "无权", "有权", "免除", "免责", "赔偿", "解除", "终止",
|
||||
)
|
||||
|
||||
|
||||
def _compact_text(value: Any) -> str:
|
||||
@@ -87,23 +90,69 @@ def _eligible_location_field(item: dict[str, Any]) -> str | None:
|
||||
|
||||
def _eligible_high_similarity_field(item: dict[str, Any]) -> bool:
|
||||
"""
|
||||
判断无稳定字段标签的高相似 modify 是否可进入视觉 OCR 复核。
|
||||
判断无稳定字段标签的条款 modify 是否可进入双边同源 OCR 复核。
|
||||
|
||||
核心逻辑:仅允许长文本中的少量字符识别差异;金额、日期、比例、数量、型号和
|
||||
明确的关键字段始终保留确定性 Diff,不能借 OCR 一致自动忽略。
|
||||
核心逻辑:混合 PDF 的初始文本可能来自“原生文字层 vs 扫描 OCR”,因此付款、交期、
|
||||
质保等关键条款也允许进入候选;但只有双方已抽取的数字、型号及权利义务方向完全一致时
|
||||
才执行视觉复核。最终仍须两份 PDF 命中页使用同一 OCR 后文本完全一致,才会忽略差异。
|
||||
|
||||
Args:
|
||||
item: analyze_compare 生成的单条 modify 差异。
|
||||
|
||||
Returns:
|
||||
True 表示该条款可进行命中页双边 OCR;关键业务事实已变化时返回 False。
|
||||
"""
|
||||
if str(item.get("change_type") or "") != "modify":
|
||||
return False
|
||||
old_key = _compact_text(item.get("old_text"))
|
||||
new_key = _compact_text(item.get("new_text"))
|
||||
old_text = str(item.get("old_text") or "")
|
||||
new_text = str(item.get("new_text") or "")
|
||||
old_key = _compact_text(old_text)
|
||||
new_key = _compact_text(new_text)
|
||||
if min(len(old_key), len(new_key)) < 8 or old_key == new_key:
|
||||
return False
|
||||
blob = "\n".join(str(item.get(key) or "") for key in ("title", "old_text", "new_text"))
|
||||
if any(hint in blob for hint in _CRITICAL_VALUE_HINTS):
|
||||
|
||||
# [STEP 1] 数字、比例、日期、型号任一变化都保留原 Diff,不以 OCR 复核自动撤销。
|
||||
if _critical_fact_tokens(old_text) != _critical_fact_tokens(new_text):
|
||||
return False
|
||||
if any(re.search(pattern, blob, re.IGNORECASE) for pattern in _CRITICAL_VALUE_PATTERNS):
|
||||
|
||||
# [STEP 2] 否定、免责、赔偿和解除等方向变化属于实质权利义务变化。
|
||||
old_directions = {term for term in _MATERIAL_DIRECTION_TERMS if term in old_text}
|
||||
new_directions = {term for term in _MATERIAL_DIRECTION_TERMS if term in new_text}
|
||||
if old_directions != new_directions:
|
||||
return False
|
||||
return SequenceMatcher(None, old_key, new_key).ratio() >= 0.85
|
||||
|
||||
# [STEP 3] 候选门槛只用于控制 OCR 成本;是否忽略仍由双边视觉文本完全一致决定。
|
||||
similarity = SequenceMatcher(None, old_key, new_key).ratio()
|
||||
return similarity >= (0.55 if min(len(old_key), len(new_key)) >= 24 else 0.78)
|
||||
|
||||
|
||||
def _critical_fact_tokens(value: str) -> dict[str, tuple[str, ...]]:
|
||||
"""
|
||||
提取不能被视觉 OCR 自动忽略的合同业务事实。
|
||||
|
||||
核心逻辑:统一全半角并移除开头条款序号,分别收集数字、百分比/千分比和字母数字型号;
|
||||
比较候选资格时要求两侧 token 完全一致,避免把 24 小时改成 48 小时等真实变化吞掉。
|
||||
|
||||
Args:
|
||||
value: 单侧差异原文。
|
||||
|
||||
Returns:
|
||||
按 numbers、rates、models 分类并排序的不可变 token 字典。
|
||||
"""
|
||||
normalized = unicodedata.normalize("NFKC", str(value or ""))
|
||||
normalized = re.sub(r"^\s*(?:第\s*)?[一二三四五六七八九十百千万\d]+(?:条|款|项)?[.、::))\s]+", "", normalized)
|
||||
number_matches = tuple(re.findall(r"\d+(?:[,.]\d+)*", normalized))
|
||||
rates = tuple(re.findall(r"\d+(?:\.\d+)?\s*(?:%|‰)|(?:百分之|千分之)[零〇一二两三四五六七八九十百千万点\d.]+", normalized))
|
||||
models = tuple(
|
||||
token.lower()
|
||||
for token in re.findall(r"(?=[A-Za-z0-9-]{4,})(?=[A-Za-z0-9-]*[A-Za-z])(?=[A-Za-z0-9-]*\d)[A-Za-z0-9-]+", normalized)
|
||||
)
|
||||
critical_context = any(
|
||||
hint in normalized
|
||||
for hint in _CRITICAL_VALUE_HINTS + ("天", "日", "个月", "月", "年", "小时", "工作日", "期限")
|
||||
)
|
||||
numbers = number_matches if critical_context or rates else ()
|
||||
return {"numbers": numbers, "rates": rates, "models": models}
|
||||
|
||||
|
||||
def _pdf_text_profile(pdf_bytes: bytes) -> dict[str, int] | None:
|
||||
@@ -220,18 +269,31 @@ def _matching_field_line(lines: list[dict[str, Any]], field_label: str) -> str:
|
||||
|
||||
|
||||
def _matching_similar_line(lines: list[dict[str, Any]], expected_text: str) -> str:
|
||||
"""从 OCR 行及相邻行窗口中选出最接近原始差异文本的视觉行,供无标签短差异复核。"""
|
||||
"""
|
||||
从 OCR 行及相邻行窗口中选出最接近原始差异文本的视觉条款。
|
||||
|
||||
核心逻辑:扫描合同中的一个完整条款通常跨越多行,因此窗口会随预期文本长度扩大到八行;
|
||||
只有与初始差异文本达到最低相似度的窗口才返回,避免误取同页页眉或相邻条款。
|
||||
|
||||
Args:
|
||||
lines: 同一 PDF 页面按阅读顺序排列的 OCR 行。
|
||||
expected_text: 初始 Diff 中该侧的条款文本。
|
||||
|
||||
Returns:
|
||||
最接近的 OCR 行窗口;无可靠候选时返回空字符串。
|
||||
"""
|
||||
expected_key = _compact_text(expected_text)
|
||||
texts = [str(line.get("text") or "").strip() for line in lines if isinstance(line, dict)]
|
||||
best_text = ""
|
||||
best_score = 0.0
|
||||
for window_size in (1, 2, 3):
|
||||
max_window = min(8, max(3, len(expected_key) // 22 + 2))
|
||||
for window_size in range(1, max_window + 1):
|
||||
for start in range(0, max(0, len(texts) - window_size + 1)):
|
||||
candidate = "".join(texts[start : start + window_size])
|
||||
score = SequenceMatcher(None, expected_key, _compact_text(candidate)).ratio()
|
||||
if score > best_score:
|
||||
best_text, best_score = candidate, score
|
||||
return best_text[:500] if best_score >= 0.65 else ""
|
||||
return best_text[:500] if best_score >= 0.55 else ""
|
||||
|
||||
|
||||
def _line_bounds(line: dict[str, Any]) -> tuple[float, float, float, float] | None:
|
||||
@@ -624,7 +686,7 @@ def suppress_mixed_extraction_ocr_noise(
|
||||
"clause_no": item.get("clause_no"),
|
||||
"title": str(item.get("title") or "")[:200],
|
||||
"validation_type": validation_type,
|
||||
"field_label": field_label or "高相似非关键文本",
|
||||
"field_label": field_label or "同源 OCR 条款复核",
|
||||
"original_extraction_mode": str((original_extraction_meta or {}).get("mode") or ""),
|
||||
"returned_extraction_mode": str((returned_extraction_meta or {}).get("mode") or ""),
|
||||
"original_page_no": original_page,
|
||||
@@ -775,7 +837,7 @@ def suppress_mixed_extraction_ocr_noise(
|
||||
result,
|
||||
status="complete",
|
||||
mixed_extraction=True,
|
||||
checked=len(pending_locations) + (1 if equipment_items else 0),
|
||||
checked=len(pending_text_candidates) + (1 if equipment_items else 0),
|
||||
ignored=ignored,
|
||||
kept=kept,
|
||||
items=diagnostic_items,
|
||||
|
||||
Reference in new issue
Block a user