CoolFace
Apppublic

MRiwu/Collection

sourceHugging Facemitupdated 4y agoView on Hugging Face
1likes
thai.py45 linesDownload Raw Back to text
1import re2from num_thai.thainumbers import NumThai3 4 5num = NumThai()6 7# List of (Latin alphabet, Thai) pairs:8_latin_to_thai = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [9    ('a', 'เอ'),10    ('b','บี'),11    ('c','ซี'),12    ('d','ดี'),13    ('e','อี'),14    ('f','เอฟ'),15    ('g','จี'),16    ('h','เอช'),17    ('i','ไอ'),18    ('j','เจ'),19    ('k','เค'),20    ('l','แอล'),21    ('m','เอ็ม'),22    ('n','เอ็น'),23    ('o','โอ'),24    ('p','พี'),25    ('q','คิว'),26    ('r','แอร์'),27    ('s','เอส'),28    ('t','ที'),29    ('u','ยู'),30    ('v','วี'),31    ('w','ดับเบิลยู'),32    ('x','เอ็กซ์'),33    ('y','วาย'),34    ('z','ซี')35]]36 37 38def num_to_thai(text):39    return re.sub(r'(?:\d+(?:,?\d+)?)+(?:\.\d+(?:,?\d+)?)?', lambda x: ''.join(num.NumberToTextThai(float(x.group(0).replace(',', '')))), text)40 41def latin_to_thai(text):42    for regex, replacement in _latin_to_thai:43        text = re.sub(regex, replacement, text)44    return text45