fix: 科目名称提取3个Bug修复

1. 代码去重(Bug 1): code_map 代替 seen,重复代码优先保留带完整名称的行
   - 解决 2241.01.01 等代码行先出现空名、后出现完整名却被跳过的问题

2. 评分制补位(Bug 2): 不再找最近中文文本,而是综合评分
   - 含横杠(全角/ASCII)+10 分 → 科目名称信号
   - 同列(x差<50px)+25 分 → 同一列更可能是科目名
   - 银行账号(10位以上数字)-30 分 → 强烈惩罚
   - 4字以上文本 +3 分 → 长文本更可信

3. 尾部全角横杠清洗(Bug 3): 清洗时也去掉全角-,防止'—银行存款-'变成空名

注:科目名称规则不变——取最后一个横杠之后的文字(用户确认的规则)
This commit is contained in:
2026-06-22 23:50:36 +08:00
parent de4bb6934b
commit f1041972a7
+54 -30
View File
@@ -143,7 +143,7 @@ class K3VoucherParser:
def _extract_codes(self, bounds):
"""提取科目代码和名称"""
codes = []
seen = set()
code_map = {} # code_part → 在 codes 列表中的索引
# 安全获取边界值
y_subj = bounds.get('y_subject_header')
@@ -160,7 +160,6 @@ class K3VoucherParser:
continue
# 科目代码区域:摘要标题上方 ~ 借方标题之间
# 实际中代码可能在"科目"标题上方(y<y_subj),所以要放宽下界
if cy < y_lower - 100 or cy > y_debit_val:
continue
@@ -173,7 +172,7 @@ class K3VoucherParser:
if t in ('摘要', '科目', '借方', '贷方', '合计') or len(t) < 4:
continue
# 匹配科目代码模式:4位数字开头,后面跟 .数字
# 匹配科目代码模式:3-4位数字开头,后面跟 .数字
m = re.match(r'(\d{3,4}\.\d[\d.]*)', t)
if not m:
continue
@@ -183,13 +182,8 @@ class K3VoucherParser:
first_num = int(code_part.split('.')[0])
if first_num < 1000 or first_num > 9999:
continue
if code_part in seen:
continue
seen.add(code_part)
# 提取名称 — 按用户规则:
# 有 \ → 取反斜杠前面(横杠和反斜杠之间)
# 无 \ → 取最后一个横杠之后
# 提取名称 — 按用户规则:取最后一个横杠之后的文字
name = t[m.end():].lstrip('--—= ')
name = re.sub(r'[\d.\-—=]+$', '', name).strip()
name = name.lstrip('-—-=')
@@ -201,6 +195,16 @@ class K3VoucherParser:
if idx >= 0:
name = name[idx+1:].strip()
# 去重:优先保留带完整名称的行
if code_part in code_map:
existing = codes[code_map[code_part]]
# 已有名称为空,而新行有名称 → 更新
if (not existing['name'] or len(existing['name']) < 2) and len(name) >= 2:
existing['name'] = name
existing['y_pos'] = cy
existing['x_pos'] = cx
continue
codes.append({
'code': code_part,
'name': name,
@@ -210,6 +214,7 @@ class K3VoucherParser:
'y_pos': cy,
'x_pos': cx,
})
code_map[code_part] = len(codes) - 1
# 按 y 排序
codes.sort(key=lambda c: c['y_pos'])
@@ -237,33 +242,52 @@ class K3VoucherParser:
for code in codes:
if code['name'] and len(code['name']) >= 2:
continue # 已有名称,跳过
# 找代码行下方最近的含中文文本
best = None
best_dist = 99999
# 评分制选取最佳名称补位:优先含全角横杠的科目名称文本
scored = []
for t, cy, cx in name_candidates:
if cy <= code['y_pos']:
continue # 只找下方的
# x 坐标接近(左右偏差不超过600px)
if abs(cx - code['x_pos']) > 600:
continue
dist = cy - code['y_pos']
if dist < best_dist and dist < 600:
best = (t, cy, cx)
best_dist = dist
if best:
t, cy, cx = best
# 按用户规则提取名称
n = t.lstrip('--—= ')
n = re.sub(r'[\d.\-—=]+$', '', n).strip()
n = n.lstrip('-—-=')
if '\\' in n:
n = n.split('\\', 1)[0].strip()
else:
idx = max(n.rfind('-'), n.rfind('-'), n.rfind('—'))
if idx >= 0:
n = n[idx+1:].strip()
if n and len(n) >= 2:
code['name'] = n
if dist >= 600:
continue
# 评分:高的优先
score = 0
if '-' in t or '-' in t:
score += 10 # 含横杠(全角/ASCII)→ 科目名称信号
if re.search(r'\d{10,}', t):
score -= 30 # 银行账号(10位以上数字)→ 强烈惩罚
if len(t) >= 4:
score += 3 # 长文本更可能是有效科目名
# 同列优先(科目名称通常在代码同一列下方)
x_dist = abs(cx - code['x_pos'])
if x_dist < 50:
score += 25
elif x_dist < 150:
score += 15
elif x_dist < 300:
score += 5
score -= dist / 30 # 近的优先(tiebreaker)
scored.append((score, t, cy, cx))
if not scored:
continue
scored.sort(key=lambda x: -x[0])
t, cy, cx = scored[0][1], scored[0][2], scored[0][3]
# 按用户规则提取名称(先清洗尾部全角横杠,防止取到最后无内容)
n = t.lstrip('--—= ')
n = re.sub(r'[\d.\-—=-]+$', '', n).strip()
n = n.lstrip('-—-=')
if '\\' in n:
n = n.split('\\', 1)[0].strip()
else:
idx = max(n.rfind('-'), n.rfind('-'), n.rfind('—'))
if idx >= 0:
n = n[idx+1:].strip()
if n and len(n) >= 2:
code['name'] = n
return codes