103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
"""
|
||
菜品名清洗工具
|
||
用于将VLM输出的菜品名进行标准化处理,提升向量匹配准确率
|
||
"""
|
||
import re
|
||
from typing import Dict
|
||
|
||
|
||
# 同义词映射表(可根据实际情况扩展)
|
||
SYNONYM_MAP: Dict[str, str] = {
|
||
"西红柿": "番茄",
|
||
"土豆": "马铃薯",
|
||
"洋芋": "马铃薯",
|
||
"青椒": "柿子椒",
|
||
# 可继续添加...
|
||
}
|
||
|
||
|
||
def clean_dish_name(name: str) -> str:
|
||
"""
|
||
清洗菜品名:去除括号注释、前缀、英文等干扰信息
|
||
|
||
示例:
|
||
"宫保鸡丁(川菜)" -> "宫保鸡丁"
|
||
"川菜-麻婆豆腐" -> "麻婆豆腐"
|
||
"红烧肉 Braised Pork" -> "红烧肉"
|
||
"""
|
||
if not isinstance(name, str):
|
||
return ""
|
||
|
||
# 1. 去除括号及内容(中英文括号)
|
||
name = re.sub(r"[((].*?[))]", "", name)
|
||
name = re.sub(r"\[.*?\]", "", name)
|
||
|
||
# 2. 去除常见前缀(菜系、地域等)
|
||
prefixes = ["川菜", "粤菜", "鲁菜", "苏菜", "浙菜", "闽菜", "湘菜", "徽菜",
|
||
"东北", "西北", "西南", "华南", "华北"]
|
||
for prefix in prefixes:
|
||
if name.startswith(prefix):
|
||
name = name[len(prefix):]
|
||
break
|
||
|
||
# 3. 去除分隔符后的前缀(如 "川菜-宫保鸡丁")
|
||
name = re.sub(r"^[^-—]*[-—]", "", name)
|
||
|
||
# 4. 去除英文部分(保留中文)
|
||
name = re.sub(r"[a-zA-Z\s]+", "", name)
|
||
|
||
# 5. 去除多余空格和标点
|
||
name = re.sub(r"[,,、·\s]+", "", name)
|
||
|
||
# 6. 去除可能的烹饪方式后缀(如果VLM违规输出)
|
||
cooking_suffixes = ["炒制", "烹饪", "料理", "做法"]
|
||
for suffix in cooking_suffixes:
|
||
if name.endswith(suffix):
|
||
name = name[:-len(suffix)]
|
||
|
||
return name.strip()
|
||
|
||
|
||
def normalize_dish_name(name: str) -> str:
|
||
"""
|
||
标准化菜品名:应用同义词映射
|
||
|
||
示例:
|
||
"西红柿炒鸡蛋" -> "番茄炒鸡蛋"
|
||
"""
|
||
cleaned = clean_dish_name(name)
|
||
|
||
# 应用同义词替换
|
||
for synonym, standard in SYNONYM_MAP.items():
|
||
cleaned = cleaned.replace(synonym, standard)
|
||
|
||
return cleaned
|
||
|
||
|
||
def extract_main_dish_name(text: str) -> str:
|
||
"""
|
||
从VLM返回文本中提取主要菜品名(兼容多种输出格式)
|
||
|
||
示例:
|
||
"这是宫保鸡丁" -> "宫保鸡丁"
|
||
"菜品:红烧肉" -> "红烧肉"
|
||
"""
|
||
if not text:
|
||
return ""
|
||
|
||
# 尝试匹配常见模式
|
||
patterns = [
|
||
r"菜品[::]\s*([^,,。\n]+)",
|
||
r"识别为[::]\s*([^,,。\n]+)",
|
||
r"这是\s*([^,,。\n]+)",
|
||
r"应该是\s*([^,,。\n]+)",
|
||
]
|
||
|
||
for pattern in patterns:
|
||
match = re.search(pattern, text)
|
||
if match:
|
||
return match.group(1).strip()
|
||
|
||
# 如果没有匹配到,返回清洗后的整个文本
|
||
return clean_dish_name(text)
|