批量录入冠字号矫正功能
This commit is contained in:
parent
d18e269193
commit
a5251a2794
|
|
@ -1277,7 +1277,42 @@ async def batch_parse_deals(text: str = Body(..., embed=True)):
|
|||
|
||||
# 尝试直接解析
|
||||
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:
|
||||
# 尝试用正则提取
|
||||
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:
|
||||
continue
|
||||
|
||||
# 提取冠字号 J0 + 8-9位数字
|
||||
serial_match = re.search(r'J0(\d{8,9})', line)
|
||||
# 提取冠字号 J0 + 8-11位数字(可能有多位或少位)
|
||||
serial_match = re.search(r'J0(\d{7,11})', line)
|
||||
if not serial_match:
|
||||
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)
|
||||
if len(serial_num) == 9:
|
||||
serial_num = serial_num[:8]
|
||||
serial = 'J0' + serial_num
|
||||
normalized = normalize_serial(serial_num)
|
||||
if not normalized:
|
||||
continue # 跳过无法矫正的数据
|
||||
serial = normalized
|
||||
|
||||
# 提取价格 ¥xxx,xxx 或 xxx,xxx(必须在J0之后)
|
||||
serial_pos = line.find(serial)
|
||||
|
|
|
|||
Loading…
Reference in New Issue