修正冠字号矫正逻辑
This commit is contained in:
parent
dd1790b0be
commit
7db66d5a7b
|
|
@ -455,18 +455,18 @@ def create_information(
|
||||||
# 验证并矫正冠字号:J0 + 8位数字 = 共10位
|
# 验证并矫正冠字号:J0 + 8位数字 = 共10位
|
||||||
if data.title:
|
if data.title:
|
||||||
import re
|
import re
|
||||||
# 提取冠字号(J开头后面跟数字)
|
# 提取冠字号(J0开头后面跟数字)
|
||||||
match = re.search(r'J(\d+)', data.title)
|
match = re.search(r'J0(\d+)', data.title)
|
||||||
if match:
|
if match:
|
||||||
num = match.group(1)
|
num = match.group(1)
|
||||||
# 必须是10位:J + 9位数字
|
# 必须是10位:J0 + 8位数字
|
||||||
if len(num) > 9:
|
if len(num) > 8:
|
||||||
num = num[:9]
|
num = num[:8]
|
||||||
elif len(num) < 9:
|
elif len(num) < 8:
|
||||||
num = num.zfill(9)
|
num = num.zfill(8)
|
||||||
# 重新构建title
|
# 重新构建title
|
||||||
original = match.group(0)
|
original = match.group(0)
|
||||||
data.title = data.title.replace(original, 'J' + num, 1)
|
data.title = data.title.replace(original, 'J0' + num, 1)
|
||||||
# 生成行情编号:日期 + 5位自然数(从00001开始)
|
# 生成行情编号:日期 + 5位自然数(从00001开始)
|
||||||
deal_no = None
|
deal_no = None
|
||||||
if data.info_type == 'deal':
|
if data.info_type == 'deal':
|
||||||
|
|
@ -1294,31 +1294,32 @@ async def batch_parse_deals(text: str = Body(..., embed=True)):
|
||||||
data = json.loads(content.strip())
|
data = json.loads(content.strip())
|
||||||
# 对AI返回的数据进行冠字号矫正
|
# 对AI返回的数据进行冠字号矫正
|
||||||
def normalize_serial_ai(num_str):
|
def normalize_serial_ai(num_str):
|
||||||
"""矫正冠字号:J+9位数字(共10位)"""
|
"""矫正冠字号:J0开头,9位数字"""
|
||||||
if not num_str.startswith('J'):
|
if not num_str.startswith('J0'):
|
||||||
return None
|
return None
|
||||||
num = num_str[1:] # 去掉J
|
num = num_str[2:] # 去掉J0
|
||||||
diff = 9 - len(num)
|
diff = 9 - len(num)
|
||||||
# 位数正好9位,不需要矫正
|
# 位数正好9位,不需要矫正
|
||||||
if diff == 0:
|
if diff == 0:
|
||||||
return num_str
|
return num_str
|
||||||
elif diff == -1:
|
elif diff == -1:
|
||||||
|
# 多1位:取前5位+最后4位
|
||||||
if len(num) >= 4:
|
if len(num) >= 4:
|
||||||
result = num[:5] + num[-4:]
|
result = num[:5] + num[-4:]
|
||||||
if len(result) == 9:
|
if len(result) == 9:
|
||||||
return 'J' + result
|
return 'J0' + result
|
||||||
result2 = num[:4] + num[-5:]
|
|
||||||
if len(result2) == 9:
|
|
||||||
return 'J' + result2
|
|
||||||
elif diff == 1:
|
elif diff == 1:
|
||||||
return 'J' + '0' + num
|
# 少1位:J0 + 0 + 数字
|
||||||
|
return 'J0' + '0' + num
|
||||||
elif diff == -2:
|
elif diff == -2:
|
||||||
|
# 多2位:取前5位+最后4位
|
||||||
if len(num) >= 5:
|
if len(num) >= 5:
|
||||||
result = num[:5] + num[-4:]
|
result = num[:5] + num[-4:]
|
||||||
if len(result) == 9:
|
if len(result) == 9:
|
||||||
return 'J' + result
|
return 'J0' + result
|
||||||
elif diff == 2:
|
elif diff == 2:
|
||||||
return 'J' + '00' + num
|
# 少2位:J0 + 00 + 数字
|
||||||
|
return 'J0' + '00' + num
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 矫正每条记录的冠字号
|
# 矫正每条记录的冠字号
|
||||||
|
|
@ -1394,37 +1395,34 @@ def parse_deals_locally(text: str, default_packaging: str = '', default_date: st
|
||||||
if not serial_match:
|
if not serial_match:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 冠字号矫正函数 - 统一为J+9位数字(10位)
|
# 冠字号矫正函数 - J0开头的情况
|
||||||
def normalize_serial(num_str):
|
def normalize_serial(num_str):
|
||||||
"""矫正冠字号:J+9位数字(共10位)"""
|
"""矫正冠字号:J0开头,9位数字"""
|
||||||
num = num_str
|
num = num_str
|
||||||
diff = 9 - len(num)
|
diff = 9 - len(num)
|
||||||
|
|
||||||
# 位数正好9位,不需要矫正
|
# 位数正好9位,不需要矫正
|
||||||
if diff == 0:
|
if diff == 0:
|
||||||
return 'J' + num
|
return 'J0' + num
|
||||||
# 位数不对才需要矫正
|
# 位数不对才需要矫正
|
||||||
elif diff == -1:
|
elif diff == -1:
|
||||||
# 多1位(10位数字)→ 去掉倒数第4位
|
# 多1位(10位数字)→ 取前5位+最后4位
|
||||||
if len(num) >= 4:
|
if len(num) >= 4:
|
||||||
result = num[:5] + num[-4:]
|
result = num[:5] + num[-4:]
|
||||||
if len(result) == 9:
|
if len(result) == 9:
|
||||||
return 'J' + result
|
return 'J0' + result
|
||||||
result2 = num[:4] + num[-5:]
|
|
||||||
if len(result2) == 9:
|
|
||||||
return 'J' + result2
|
|
||||||
elif diff == 1:
|
elif diff == 1:
|
||||||
# 少1位(8位数字)→ 在J后加0
|
# 少1位(8位数字)→ J0 + 0 + 数字
|
||||||
return 'J' + '0' + num
|
return 'J0' + '0' + num
|
||||||
elif diff == -2:
|
elif diff == -2:
|
||||||
# 多2位(11位数字)→ 去掉倒数第4、5位
|
# 多2位(11位数字)→ 取前5位+最后4位
|
||||||
if len(num) >= 5:
|
if len(num) >= 5:
|
||||||
result = num[:5] + num[-4:]
|
result = num[:5] + num[-4:]
|
||||||
if len(result) == 9:
|
if len(result) == 9:
|
||||||
return 'J' + result
|
return 'J0' + result
|
||||||
elif diff == 2:
|
elif diff == 2:
|
||||||
# 少2位(7位数字)→ 在J后加00
|
# 少2位(7位数字)→ J0 + 00 + 数字
|
||||||
return 'J' + '00' + num
|
return 'J0' + '00' + num
|
||||||
return None # 无法矫正
|
return None # 无法矫正
|
||||||
|
|
||||||
serial_num = serial_match.group(1)
|
serial_num = serial_match.group(1)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue