批量录入冠字号矫正功能

This commit is contained in:
甲辰生产 2026-04-12 00:31:24 +08:00
parent d18e269193
commit a5251a2794
1 changed files with 76 additions and 6 deletions

View File

@ -1277,7 +1277,42 @@ async def batch_parse_deals(text: str = Body(..., embed=True)):
# 尝试直接解析 # 尝试直接解析
data = json.loads(content.strip()) data = json.loads(content.strip())
return {"success": True, "data": data} # 对AI返回的数据进行冠字号矫正
def normalize_serial_ai(num_str):
"""矫正冠字号确保是J0+9位数字最后两位为01"""
if not num_str.startswith('J0'):
return None
num = num_str[2:] # 去掉J0
diff = 9 - len(num)
if diff == 0:
return num_str
elif diff == -1:
# 多1位去掉倒数第4位
if len(num) >= 4:
num = num[:-4] + num[-3:]
if len(num) == 9:
return 'J0' + num
elif diff == 1:
# 少1位加0
return 'J0' + '0' + num
elif diff == -2:
if len(num) >= 5:
num = num[:-5] + num[-4:]
if len(num) == 9:
return 'J0' + num
elif diff == 2:
return 'J0' + '00' + num
return None
# 矫正每条记录的冠字号
corrected_data = []
for item in data:
if 'serial' in item:
normalized = normalize_serial_ai(item['serial'])
if normalized:
item['serial'] = normalized
corrected_data.append(item)
return {"success": True, "data": corrected_data}
except json.JSONDecodeError: except json.JSONDecodeError:
# 尝试用正则提取 # 尝试用正则提取
match = re.search(r'\[.*\]', content, re.DOTALL) match = re.search(r'\[.*\]', content, re.DOTALL)
@ -1337,15 +1372,50 @@ def parse_deals_locally(text: str, default_packaging: str = '', default_date: st
if not line or 'J0' not in line: if not line or 'J0' not in line:
continue continue
# 提取冠字号 J0 + 8-9位数字 # 提取冠字号 J0 + 8-11位数字可能有多位或少位
serial_match = re.search(r'J0(\d{8,9})', line) serial_match = re.search(r'J0(\d{7,11})', line)
if not serial_match: if not serial_match:
continue continue
# 冠字号矫正函数
def normalize_serial(num_str):
"""矫正冠字号确保是J0+9位数字最后两位为01"""
num = num_str
diff = 9 - len(num)
if diff == 0:
# 正好9位检查最后两位是否为01
if num[-2:] != '01':
# 尝试矫正去掉倒数第4位如果倒数第4位不是0
if len(num) >= 4 and num[-4] != '0':
num = num[:-4] + num[-3:]
if num[-2:] == '01':
return 'J0' + num
return 'J0' + num
elif diff == -1:
# 多1位10位数字→ 去掉倒数第4位
if len(num) >= 4:
num = num[:-4] + num[-3:]
if len(num) == 9:
return 'J0' + num
elif diff == 1:
# 少1位8位数字→ 在J0后加0
return 'J0' + '0' + num
elif diff == -2:
# 多2位11位数字→ 去掉倒数第4、5位
if len(num) >= 5:
num = num[:-5] + num[-4:]
if len(num) == 9:
return 'J0' + num
elif diff == 2:
# 少2位7位数字→ 在J0后加00
return 'J0' + '00' + num
return None # 无法矫正
serial_num = serial_match.group(1) serial_num = serial_match.group(1)
if len(serial_num) == 9: normalized = normalize_serial(serial_num)
serial_num = serial_num[:8] if not normalized:
serial = 'J0' + serial_num continue # 跳过无法矫正的数据
serial = normalized
# 提取价格 ¥xxx,xxx 或 xxx,xxx必须在J0之后 # 提取价格 ¥xxx,xxx 或 xxx,xxx必须在J0之后
serial_pos = line.find(serial) serial_pos = line.find(serial)