File size: 2,273 Bytes
2f603e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Chuẩn hóa transcript — PHẢI giống hệt hàm dùng để sinh cột `transcript_normalized`
trong metadata.csv (xem 01_build_and_audit_metadata.py::normalize_text), để reference
và hypothesis ASR được so sánh trên cùng một quy tắc chuẩn hóa (mục 6.3, checklist #2).

`normalize_text` (bí danh `normalize_text_v103`) là hàm gốc dùng cho toàn bộ kết quả
v1.0.3 đã công bố — KHÔNG sửa hàm này, mọi thay đổi phải đi qua `normalize_text_v11`
(Implementation Plan v1.1, Phase 2) để kết quả cũ vẫn tái lập được nguyên vẹn.
"""
import re
import unicodedata


def normalize_text(text: str) -> str:
    if text is None:
        return ""
    text = str(text)
    if text.strip().lower() in ("nan", "none"):
        return ""
    text = unicodedata.normalize("NFC", text).strip().lower()
    text = re.sub(r"[.,!?;:\"'“”‘’()\[\]…]", "", text)
    text = re.sub(r"\s+", " ", text).strip()
    return text


# Bí danh tường minh — dùng khi cần phân biệt rõ với normalize_text_v11 trong code v1.1.
normalize_text_v103 = normalize_text

_V11_QUOTE_MAP = {
    "‘": "'", "’": "'", "“": '"', "”": '"',
    "–": "-", "—": "-",
}
# Các ký tự không mang nội dung lexical: THAY bằng khoảng trắng (không xóa hẳn), để
# không nối liền hai từ hai bên — vd "google.com" -> "google com", không phải
# "googlecom" (bug đã biết của normalize_text_v103, xem DATA_CARD.md mục 13.7).
_V11_STRIP_TO_SPACE_RE = re.compile(r'[.,!?;:"\'“”‘’()\[\]…\-–—/]')
_V11_WHITESPACE_RE = re.compile(r"\s+")


def normalize_text_v11(text: str) -> str:
    """Chuẩn hóa v1.1 (Implementation Plan Phase 2). Reference và hypothesis cho mọi
    benchmark v1.1 PHẢI dùng cùng hàm này, cùng version, để so sánh công bằng."""
    if text is None:
        return ""
    text = str(text)
    if text.strip().lower() in ("nan", "none"):
        return ""
    text = unicodedata.normalize("NFC", text)
    for src, dst in _V11_QUOTE_MAP.items():
        text = text.replace(src, dst)
    text = text.lower().strip()
    text = _V11_STRIP_TO_SPACE_RE.sub(" ", text)
    text = _V11_WHITESPACE_RE.sub(" ", text).strip()
    return text