fix: improve OCR indexing and evidence review

This commit is contained in:
2026-09-25 19:03:19 +08:00
parent ac1f0ff46d
commit df5fcb4bda
12 changed files with 185 additions and 36 deletions
+32 -1
View File
@@ -1,6 +1,7 @@
import base64
import json
import time
import unicodedata
from pathlib import Path
from typing import Any
@@ -105,8 +106,38 @@ def validate_score(result: ScoreResult, criterion: dict, chunks: list[dict]):
if result.criterion_id != criterion["criterion_id"] or result.max_score != criterion["max_score"]:
raise ModelError("模型评分项或满分与规则版本不一致")
by_id = {chunk["chunk_id"]: chunk for chunk in chunks}
def locate_quote(text: str, quote: str):
def comparable(value: str):
return "".join(char for original in value for char in unicodedata.normalize("NFKC", original)
if not char.isspace() and not unicodedata.category(char).startswith("P"))
if quote in text:
return quote
needle = comparable(quote)
if len(needle) < 4:
return None
positions = []
normalized = []
for index, original in enumerate(text):
for char in unicodedata.normalize("NFKC", original):
if not char.isspace() and not unicodedata.category(char).startswith("P"):
normalized.append(char)
positions.append(index)
start = "".join(normalized).find(needle)
if start < 0:
return None
return text[positions[start]:positions[start + len(needle) - 1] + 1]
for evidence in result.evidence:
chunk = by_id.get(evidence.chunk_id)
if not chunk or evidence.page != chunk["page"] or evidence.quote not in chunk["text"]:
quote = locate_quote(chunk["text"], evidence.quote) if chunk else None
if quote is None and len(evidence.quote) >= 8:
matches = [(candidate, located) for candidate in chunks
if (located := locate_quote(candidate["text"], evidence.quote)) is not None]
if len(matches) == 1:
chunk, quote = matches[0]
if quote is None:
raise ModelError("模型证据未能匹配检索原文")
evidence.chunk_id, evidence.page, evidence.quote = chunk["chunk_id"], chunk["page"], quote
return result