From d8d54581c3a77f64bb6f52cabca52b73f82a8380 Mon Sep 17 00:00:00 2001 From: xiaoxue_admin <9283713@qq.com> Date: Tue, 23 Jun 2026 00:48:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=9A=E7=BA=A7=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=90=88=E5=B9=B6=EF=BC=88=E4=B8=A5=E6=A0=BC=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=EF=BC=89+=20=E4=BF=AE=E5=A4=8Dcode=5Fmap=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 多级名称合并:仅当内联名称为已知一级科目名时(预收账款/其他应付款等) 从下方50px内含横杠的行补二级名→合并后取最后一级 避免手续费→淇县支行之类误合并 2. code_map更新逻辑:检查新名称是否含横杠(更完整) 解决2203.01出现两次时第二次(完整名)未能覆盖第一次(不完整)的问题 --- core/parser.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/core/parser.py b/core/parser.py index 84405c4..1208001 100644 --- a/core/parser.py +++ b/core/parser.py @@ -219,6 +219,61 @@ class K3VoucherParser: # 按 y 排序 codes.sort(key=lambda c: c['y_pos']) + # ---- 多级名称合并:内联名称看起来是一级名+多级代码→从下方极近距离补二级 ---- + # 仅当名称是已知一级科目名(如"预收账款"而非"手续费"),且下方50px内有含横杠文本 + FIRST_LEVEL_NAMES = frozenset([ + '银行存款', '库存现金', '其他货币资金', + '应收账款', '预付账款', '其他应收款', + '应付账款', '预收账款', '其他应付款', + '主营业务成本', '主营业务收入', '其他业务收入', + '管理费用', '财务费用', '销售费用', + '营业外收入', '营业外支出', '代扣代缴', '实收资本', + ]) + for code in codes: + if not code['name'] or code['name'] not in FIRST_LEVEL_NAMES: + continue + code_levels = code['code'].count('.') + 1 + if code_levels < 2: + continue + + best_sub = None + best_sub_dist = 99999 + for text, conf, cx, cy, box in self.lines: + if conf < 0.5: + continue + if cy <= code['y_pos'] or cy > code['y_pos'] + 50: + continue + if abs(cx - code['x_pos']) > 150: + continue + t = text.strip() + # 排除其他代码行 + if re.match(r'\d{3,4}\.\d', t): + continue + if not re.search(r'[\u4e00-\u9fff]', t): + continue + if '-' not in t and '-' not in t and '—' not in t: + continue + dist = cy - code['y_pos'] + if dist < best_sub_dist: + n2 = t.lstrip('--—= ') + n2 = re.sub(r'[\d.\-—=]+$', '', n2).strip() + n2 = n2.lstrip('-—-=') + if '\\' in n2: + n2 = n2.split('\\', 1)[0].strip() + else: + idx = max(n2.rfind('-'), n2.rfind('-'), n2.rfind('—')) + if idx >= 0: + n2 = n2[idx+1:].strip() + if n2 and len(n2) >= 2: + best_sub = n2 + best_sub_dist = dist + + if best_sub: + merged = code['name'] + '-' + best_sub + idx = max(merged.rfind('-'), merged.rfind('-'), merged.rfind('—')) + if idx >= 0: + code['name'] = merged[idx+1:].strip() + # ---- 名称补位:代码行没有名称时,从下方最近行补取 ---- # 收集科目区所有含中文的文本(用于名称补位) name_candidates = []