dict-server/app/utils/textnorm.py

24 lines
612 B
Python
Raw Permalink 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.

import re
import unicodedata
def normalize_text(s: str) -> str:
"""
规范化字符串,用于搜索/存储 search_text
- Unicode 标准化
- 去除重音符号(é -> e
- 转小写
- 去掉前后空格,多空格合并
"""
if not s:
return ""
# 1. Unicode 标准化NFKD 拆分)
s = unicodedata.normalize("NFKD", s)
# 2. 去掉音标/重音符
s = "".join(ch for ch in s if not unicodedata.combining(ch))
# 3. 转小写
s = s.lower()
# 4. 去掉首尾空格 & 合并多个空格
s = re.sub(r"\s+", " ", s.strip())
return s