File size: 3,359 Bytes
e4b9a7b | 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Transliterating Thai text using ISO 11940
:See Also:
* `Wikipedia \
<https://en.wikipedia.org/wiki/ISO_11940>`_
"""
_consonants = {
"ก": "k",
"ข": "k̄h",
"ฃ": "ḳ̄h",
"ค": "kh",
"ฅ": "k̛h",
"ฆ": "ḳh",
"ง": "ng",
"จ": "c",
"ฉ": "c̄h",
"ช": "ch",
"ซ": "s",
"ฌ": "c̣h",
"ญ": "ỵ",
"ฎ": "ḍ",
"ฏ": "ṭ",
"ฐ": "ṭ̄h",
"ฑ": "ṯh",
"ฒ": "t̛h",
"ณ": "ṇ",
"ด": "d",
"ต": "t",
"ถ": "t̄h",
"ท": "th",
"ธ": "ṭh",
"น": "n",
"บ": "b",
"ป": "p",
"ผ": "p̄h",
"ฝ": "f̄",
"พ": "ph",
"ฟ": "f",
"ภ": "p̣h",
"ม": "m",
"ย": "y",
"ร": "r",
"ฤ": "v",
"ล": "l",
"ฦ": "ł",
"ว": "w",
"ศ": "ṣ̄",
"ษ": "s̛̄",
"ส": "s̄",
"ห": "h̄",
"ฬ": "ḷ",
"อ": "x",
"ฮ": "ḥ",
}
_vowels = {
"ะ": "a",
"ั": "ạ",
"า": "ā",
"ำ": "å",
"ิ": "i",
"ี": "ī",
"ึ": "ụ",
"ื": "ụ̄",
"ุ": "u",
"ู": "ū",
"เ": "e",
"แ": "æ",
"โ": "o",
"ใ": "ı",
"ไ": "ị",
"ฤ": "v",
"ฤๅ": "vɨ",
"ฦ": "ł",
"ฦๅ": "łɨ",
"ย": "y",
"ว": "w",
"อ": "x",
}
_tone_marks = {
"่": "–̀".replace("–", ""),
"้": "–̂".replace("–", ""),
"๊": "–́".replace("–", ""),
"๋": "–̌".replace("–", ""),
"็": "–̆".replace("–", ""),
"์": "–̒".replace("–", ""),
"–๎".replace("–", ""): "~",
"–ํ".replace("–", ""): "–̊".replace("–", ""),
"–ฺ".replace("–", ""): "–̥".replace("–", ""),
}
_punctuation_and_digits = {
# ฯ can has two meanings in ISO 11940.
# If it is for abbrevation, it is paiyan noi.
# If it is for sentence termination, it is angkhan diao.
# Without semantic analysis, they cannot be distinguished from each other.
# In this simple implementation, we decided to always treat ฯ as paiyan noi.
# We commented out angkhan diao line to remove it from the dictionary
# and avoid having duplicate keys.
"ๆ": "«",
"ฯ": "ǂ", # paiyan noi: U+01C2 ǂ Alveolar Click; ICU uses ‡ (double dagger)
"๏": "§",
# "ฯ": "ǀ", # angkhan diao: U+01C0 ǀ Dental Click; ICU uses | (vertical bar)
"๚": "ǁ", # angkhan khu: U+01C1 ǁ Lateral Click; ICU uses || (two vertical bars)
"๛": "»",
"๐": "0",
"๑": "1",
"๒": "2",
"๓": "3",
"๔": "4",
"๕": "5",
"๖": "6",
"๗": "7",
"๘": "8",
"๙": "9",
}
_all_dict = {
**_consonants,
**_vowels,
**_tone_marks,
**_punctuation_and_digits,
}
_keys_set = _all_dict.keys()
def transliterate(word: str) -> str:
"""
Use ISO 11940 for transliteration
:param str text: Thai text to be transliterated.
:return: A string indicating how the text should be pronounced, according to ISO 11940.
"""
_str = ""
for i in word:
if i in _keys_set:
_str += _all_dict[i]
else:
_str += i
return _str
|