ihhereanth commited on
Commit
f15eeff
·
verified ·
1 Parent(s): cc31d30

Create normalize.py

Browse files
Files changed (1) hide show
  1. app/normalize.py +19 -0
app/normalize.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unicodedata, re
2
+ from pythainlp.util import normalize as th_normalize
3
+
4
+ ZERO_WIDTH = dict.fromkeys(map(ord, '\u200b\u200c\u200d\ufeff\u00ad'), None)
5
+
6
+ def normalize_th(s: str) -> str:
7
+ """Canonical form — ต้องใช้ตัวเดียวกันนี้ทั้งใน pipeline และ runtime"""
8
+ if not s:
9
+ return ''
10
+ s = unicodedata.normalize('NFC', s) # 1. Unicode NFC
11
+ s = s.translate(ZERO_WIDTH) # 2. ลบ zero-width
12
+ s = th_normalize(s) # 3. จัดลำดับวรรณยุกต์/สระซ้ำ (PyThaiNLP)
13
+ s = re.sub(r'\s+', '', s) # 4. ลบช่องว่างภายใน
14
+ s = s.strip()
15
+ return s
16
+
17
+ # ทดสอบ: สระ/วรรณยุกต์สลับลำดับต้องยุบเป็นรูปเดียวกัน
18
+ assert normalize_th('เเมว') == normalize_th('แมว') # เ+เ vs แ
19
+ print("normalizer OK")