fix: 多级名称合并(严格条件)+ 修复code_map更新逻辑

1. 多级名称合并:仅当内联名称为已知一级科目名时(预收账款/其他应付款等)
   从下方50px内含横杠的行补二级名→合并后取最后一级
   避免手续费→淇县支行之类误合并

2. code_map更新逻辑:检查新名称是否含横杠(更完整)
   解决2203.01出现两次时第二次(完整名)未能覆盖第一次(不完整)的问题
This commit is contained in:
2026-06-23 00:48:39 +08:00
parent 3e23e399e8
commit d8d54581c3
+55
View File
@@ -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 = []