From e37610a35dee3f5df424c3a550230456dd89d85d Mon Sep 17 00:00:00 2001 From: xiaoxue_admin <9283713@qq.com> Date: Mon, 22 Jun 2026 18:22:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=91=E9=A2=9D=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=BD=8D=E7=BD=AE=E9=82=BB=E8=BF=91=E5=8E=BB?= =?UTF-8?q?=E9=87=8D=EF=BC=8C=E5=8E=BB=E6=8E=89=E5=90=88=E8=AE=A1=E8=A1=8C?= =?UTF-8?q?=E8=AF=AF=E6=9D=80=E9=80=BB=E8=BE=91=20-=20=E5=80=BC=E5=8E=BB?= =?UTF-8?q?=E9=87=8D=E6=94=B9=E4=B8=BA=E4=BD=8D=E7=BD=AE=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=EF=BC=88y=E5=B7=AE<15=E5=83=8F=E7=B4=A0=E4=B8=94=E5=80=BC?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E6=89=8D=E5=8E=BB=E9=87=8D=EF=BC=89=20-=20?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=90=88=E8=AE=A1=E8=A1=8C=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=88=E8=AF=AF=E6=9D=80=E5=90=8C=E5=B9=B4?= =?UTF-8?q?=E9=87=91=E9=A2=9D=EF=BC=89=20-=20=E5=8E=BB=E6=8E=89=E6=AD=BB?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/parser.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) 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