兼容更丰富的番号描述

This commit is contained in:
liuwei
2025-12-11 16:01:14 +08:00
parent e204610bc5
commit cddb3e82ff

View File

@@ -147,7 +147,20 @@ class FanhaoSearchPlugin(MessagePluginInterface):
raise
def _normalize_code(self, text: str) -> str:
return (text or "").strip().upper()
# 1. 基础清理:判空、去首尾空格、转大写
text = (text or "").strip().upper()
# 用户输入 处理后 说明
# IPzz108 IPZZ-108 目标场景:自动补全了横杠
# ipzz108 IPZZ-108 全小写自动转大写并补全
# IPZZ-108 IPZZ-108 已经有横杠,正则不匹配(字母后是横杠不是数字),保持原样
# ipzz-108 IPZZ-108 小写带横杠,仅转大写,保持原样
# ipzz108 IPZZ-108 去除前后空格
# A1 A-1 极短代码也能兼容
# 2. 核心逻辑:使用正则查找“字母后面紧跟数字”的情况,并在中间插入横杠
# r'([A-Z])(\d)' 含义捕获组1是任意大写字母捕获组2是任意数字
# r'\1-\2' 含义将匹配到的内容替换为“组1 + 横杠 + 组2”
return re.sub(r'([A-Z])(\d)', r'\1-\2', text)
def _build_queries(self, code_upper: str) -> List[Dict[str, Any]]:
# 精确匹配查询