Files
inquiry_robot/inquiry-agent/agent/schema/sea_options.py
T

123 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
海运运输分类:只认 Port to Port、其他。
本文件职责:从原话收成标准词、生成补问清单。
禁止:发明第三项;把分类带去 TMS;改六态。
线程:纯函数,可在 Worker 槽调用。
"""
from __future__ import annotations
import re
SEA_CLASSES = ("Port to Port", "其他")
# 补问清单下一行「(如:Port to Port、其他)」只是举例,不能当成销售已选。
_CLARIFY_EXAMPLE_LINE = re.compile(r"^\s*[((]如\s*[::]")
# 长词优先,避免 P2P 误伤其它词;门到门不在名单里。
_ALIASES = (
("port to port", "Port to Port"),
("港到港", "Port to Port"),
("p2p", "Port to Port"),
("其它", "其他"),
("其他", "其他"),
)
def canonicalize_sea_class(raw: str) -> str:
"""
把销售原话收成 Port to Port 或 其他;对不上返回空串。
「门到门」不算。先匹配更长别名。副作用:无。
"""
text = (raw or "").strip()
if not text:
return ""
# 企微常把空格发成不换行空格,Port to Port 对不上普通空格就会整段丢掉。
folded = re.sub(r"\s+", " ", text).strip()
lowered = folded.lower()
squashed = re.sub(r"\s+", "", lowered)
if "port to port" in lowered or "porttoport" in squashed:
return "Port to Port"
for needle, canon in _ALIASES:
if needle == "port to port":
continue
if needle in lowered or needle in text or needle in squashed:
return canon
stripped = text.replace("运输分类", "").replace(":", "").replace(":", "").strip()
if stripped in SEA_CLASSES:
return stripped
return ""
def _without_clarify_examples(text: str) -> str:
"""丢掉补问举例行。销售只在冒号后作答,「(如:…)」整行不参与判断。"""
return "\n".join(
line for line in (text or "").splitlines() if not _CLARIFY_EXAMPLE_LINE.match(line)
)
def harvest_sea_class(text: str, current: dict[str, str] | None = None) -> dict[str, str]:
"""
当前句能认则覆盖;认不出则保留已有标准值。
参数:本轮原文、已有 facts。返回合并后的 facts 副本。
举例行「(如:Port to Port、其他)」先丢掉,避免示例里的 Port to Port 盖掉冒号后的「其它」。
「其它」与「其他」都收成「其他」。非法旧值会丢掉,避免「门到门」一直占着必填位。
"""
merged = dict(current or {})
found = canonicalize_sea_class(_without_clarify_examples(text or ""))
if found:
merged["运输分类"] = found
return merged
kept = canonicalize_sea_class(str(merged.get("运输分类") or ""))
if kept:
merged["运输分类"] = kept
return merged
from agent.schema.land_options import canonicalize_land_class
# 多段陆运补「急件」时,不能按海运名单把已收的陆运分类清掉。
land_kept = canonicalize_land_class(str(merged.get("运输分类") or ""))
if land_kept:
merged["运输分类"] = land_kept
return merged
merged.pop("运输分类", None)
return merged
# 运输分类用语,不能冒充贸易条款。销售原话写了「贸易条款」才保留。
_DOOR_TRADE_TERMS = frozenset(
{
"门到门",
"door to door",
"door-to-door",
"doortodoor",
"d2d",
}
)
def drop_invented_door_trade_terms(text: str, facts: dict[str, str] | None = None) -> dict[str, str]:
"""
丢掉冒充贸易条款的「门到门」。
测服 WO202609200013:原话没写贸易条款,确认卡却出现「贸易条款:门到门」。
门到门只是运输分类说法,不是 FOB/CIF 这类条款;原话没有「贸易条款」四字时不准收下。
销售明确写了「贸易条款:门到门」则保留原文,不改六态。
"""
merged = dict(facts or {})
trade = str(merged.get("贸易条款") or "").strip()
if not trade:
return merged
if "贸易条款" in (text or ""):
return merged
if trade.lower().replace(" ", "") in {x.replace(" ", "") for x in _DOOR_TRADE_TERMS} or trade in _DOOR_TRADE_TERMS:
merged.pop("贸易条款", None)
return merged
def sea_class_prompt() -> str:
"""缺运输分类时只列两项,不再追问「其他是什么」。"""
return "运输分类(必填):请回复 Port to Port 或 其他。"