fix: 金额去重改为位置邻近去重,去掉合计行误杀逻辑

- 值去重改为位置去重(y差<15像素且值相同才去重)
- 去掉合计行去除逻辑(误杀同年金额)
- 去掉死代码
This commit is contained in:
2026-06-22 18:22:49 +08:00
parent 1bcda1ab8c
commit e37610a35d
+15 -19
View File
@@ -255,31 +255,27 @@ class K3VoucherParser:
return debit_amts, credit_amts
def _deduplicate_amounts(self, amts):
"""对金额去重:相同值的只保留一个(OCR常把同一金额读两次)"""
"""对金额去重:同一位置 OCR 重复读取的只保留一个(按 y 坐标邻近去重)
不按值去重——同一区域完全可能有多个相同金额(如借方3笔3000.00)。
只在同一位值(y差<15像素)且值相同时才认为是 OCR 重复。
"""
if not amts:
return []
# 按 y 排序后,相同值的只保留第一个
# 按 y 排序
amts.sort(key=lambda a: a['y'])
seen_vals = set()
deduped = []
for a in amts:
rounded = round(a['val'], 2)
if rounded in seen_vals:
continue # 相同值的金额只保留一个
seen_vals.add(rounded)
deduped.append(a)
# 进一步:如果某个金额等于其他金额之和,去除(可能是合计行)
if len(deduped) >= 3:
total = sum(a['val'] for a in deduped)
for a in deduped[:]:
other_sum = total - a['val']
if other_sum > 0.01 and abs(a['val'] - other_sum) < 0.01:
deduped = [x for x in deduped if x is not a]
break
return deduped
# 检查是否与上一条金额在同一位置附近(y差<15像素且值相同)
is_dup = False
if deduped:
last = deduped[-1]
if abs(a['y'] - last['y']) < 15 and abs(a['val'] - last['val']) < 0.01:
is_dup = True
if not is_dup:
deduped.append(a)
return deduped