fix: keep word boxes when filtering by confidence

i2t 每次请求都会调用 filter_document,之前它会丢弃 words,导致 return_word_box 的结果无法返回。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
zimoyin
2026-10-13 14:40:21 +08:00
parent 52696aca90
commit 6821d0d137
2 changed files with 43 additions and 5 deletions
+33
View File
@@ -118,6 +118,39 @@ def test_build_document_preserves_word_boxes() -> None:
assert document.polygon["text1"].words == recognized[0]["words"]
def test_filter_document_keeps_word_boxes() -> None:
"""i2t 每次都走 filter_document,words 不能被过滤流程丢掉。"""
recognized = [
{
"text": "姓名:万镭",
"confidence": 0.9,
"polygon": [[0, 0], [100, 0], [100, 20], [0, 20]],
"words": [
[[0, 0], [50, 0], [50, 20], [0, 20]],
[[50, 0], [100, 0], [100, 20], [50, 20]],
],
}
]
document = OCRTaskQueue.build_document(recognized)
filtered = OCRTaskQueue.filter_document(document, 0.0)
assert filtered.polygon["text1"].words == recognized[0]["words"]
def test_filter_document_drops_low_confidence_with_words() -> None:
recognized = [
{"text": "保留", "confidence": 0.9, "polygon": [[0, 0], [10, 0], [10, 10], [0, 10]],
"words": [[[0, 0], [10, 0], [10, 10], [0, 10]]]},
{"text": "丢弃", "confidence": 0.2, "polygon": [[0, 20], [10, 20], [10, 30], [0, 30]],
"words": [[[0, 20], [10, 20], [10, 30], [0, 30]]]},
]
document = OCRTaskQueue.build_document(recognized)
filtered = OCRTaskQueue.filter_document(document, 0.5)
assert [item.text for item in filtered.polygon.values()] == ["保留"]
assert filtered.polygon["text1"].words is not None
def test_document_rows_groups_by_coordinates() -> None:
"""行文本按坐标(bbox 纵向重叠)归并,供字段抽取的 row 选项使用。"""
recognized = [