diff --git a/core/parser.py b/core/parser.py index 96df0dd..56f1fc4 100644 --- a/core/parser.py +++ b/core/parser.py @@ -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