fix(shipping-notice): 收紧邮件补充匹配并归一预约号
This commit is contained in:
1 parent
b4c793145e
commit
f77f267091
11 files changed
+282
-31
No files matched your search
@@ -3,7 +3,8 @@ services/shipping_notice/reservation_id.py
|
||||
|
||||
用途:提取、标准化并校验发货通知与库存中的 Reservation ID。
|
||||
依赖:正则表达式与标准 `typing` 工具。
|
||||
逻辑:由 notice_lines、inventory_snapshot、match、extract 等多个解析/匹配模块复用本文件完成 Reservation ID 归一化。
|
||||
逻辑:由 notice_lines、inventory_snapshot、match、extract、EML 补充解析等多个模块复用;
|
||||
归一结果恒为 5/10 位纯数字(可剥括号备注、@ 前缀、短数字行序后缀如 35354-1)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -23,6 +24,8 @@ INTERNAL_UUID_RE = re.compile(
|
||||
RESERVATION_VALUE_RE = re.compile(r"^(\d{10}|\d{5})(?!\d)\s*(?:\([^()\r\n]*\)|([^()\r\n]*))?\s*$")
|
||||
# ``@`` 是预约号与前缀说明的显式分隔,后缀复用 Reservation 值的括号备注契约。
|
||||
RESERVATION_AT_SUFFIX_RE = re.compile(r"@\s*(\d{10}|\d{5})(?!\d)(?=\s*(?:\([^()\r\n]*\)|([^()\r\n]*))?\s*$)")
|
||||
# QUANTA IR# 等:合法 5/10 位本体 + 一段短数字行序后缀(- / - / _);多段后缀拒绝。
|
||||
RESERVATION_SHORT_NUMERIC_SUFFIX_RE = re.compile(r"^(\d{10}|\d{5})[--_](\d+)$")
|
||||
PR_NUMBER_LINE_RE = re.compile(
|
||||
r"PR\s*Number\s*[::]?\s*([^\n\r]+)",
|
||||
re.IGNORECASE,
|
||||
@@ -59,25 +62,38 @@ def normalize_reservation_id(value: Any) -> str:
|
||||
|
||||
|
||||
def extract_reservation_id(value: Any) -> str:
|
||||
"""标准化 Reservation ID,并识别 ``@`` 后带可选括号备注的预约号。
|
||||
"""标准化 Reservation ID,并识别括号备注、``@`` 分隔与短数字行序后缀。
|
||||
|
||||
核心逻辑:先匹配纯 5/10 位(可带括号备注);再搜 ``@`` 后本体;最后剥离
|
||||
一段 ``-``/``-``/``_`` + 数字后缀(如 ``35354-1`` → ``35354``)。多段后缀不采纳。
|
||||
|
||||
Args:
|
||||
value: Reservation ID 字段原始值;仅允许整体纯数字,或明确 ``@`` 分隔后可带括号备注的纯数字。
|
||||
value: Reservation ID 字段原始值。
|
||||
|
||||
Returns:
|
||||
str: 已确认的五位或十位纯数字 Reservation ID;普通尾随文本或金额文本返回空字符串。
|
||||
str: 已确认的五位或十位纯数字 Reservation ID;无法安全抽取时返回空字符串。
|
||||
"""
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
# [STEP 1] 整体纯数字(可选括号备注)
|
||||
direct = normalize_reservation_id(text)
|
||||
direct_match = RESERVATION_VALUE_RE.fullmatch(direct)
|
||||
if direct_match:
|
||||
return direct_match.group(1)
|
||||
|
||||
# [STEP 2] 显式 @ 分隔(可带括号备注)
|
||||
match = RESERVATION_AT_SUFFIX_RE.search(text)
|
||||
return match.group(1) if match else ""
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
||||
# [STEP 3] 短数字行序后缀(IR# 35354-1);仅一段后缀
|
||||
suffix_match = RESERVATION_SHORT_NUMERIC_SUFFIX_RE.fullmatch(direct)
|
||||
if suffix_match:
|
||||
return suffix_match.group(1)
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def find_reservation_id_in_document(text: str) -> str:
|
||||
|
||||
Reference in new issue
Block a user