diff --git a/docs/api/transliterate.rst b/docs/api/transliterate.rst index fc5c259c8..e47a54568 100644 --- a/docs/api/transliterate.rst +++ b/docs/api/transliterate.rst @@ -22,6 +22,9 @@ Modules This function provides assistance in generating phonetic representations of Thai words, which is particularly useful for language learning and pronunciation practice. +.. autofunction:: pronunciate_pali + :noindex: + .. autofunction:: puan :noindex: diff --git a/pythainlp/transliterate/__init__.py b/pythainlp/transliterate/__init__.py index 60a6bcf21..f0b21d8c0 100644 --- a/pythainlp/transliterate/__init__.py +++ b/pythainlp/transliterate/__init__.py @@ -8,7 +8,9 @@ "puan", "romanize", "transliterate", + "pronunciate_pali", ] from pythainlp.transliterate.core import pronunciate, romanize, transliterate +from pythainlp.transliterate.pali import pronunciate_pali from pythainlp.transliterate.spoonerism import puan diff --git a/pythainlp/transliterate/pali.py b/pythainlp/transliterate/pali.py new file mode 100644 index 000000000..61910bc6e --- /dev/null +++ b/pythainlp/transliterate/pali.py @@ -0,0 +1,59 @@ +# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project +# SPDX-FileType: SOURCE +# SPDX-License-Identifier: Apache-2.0 +import re + + +def pronunciate_pali(word: str) -> str: + """ + Convert pali word to Thai pronunciation + + :param str word: Pali word (written in Thai script) to be pronunciated. + + :return: A string of Thai letters indicating + how the input text should be pronounced. + + :Example: + + >>> from pythainlp.transliterate import pronunciate_pali + >>> pronunciate_pali('ภควา') + 'ภะคะวา' + >>> pronunciate_pali('สมฺมา') + 'สัมมา' + >>> pronunciate_pali('มยํ') + 'มะยัง' + >>> pronunciate_pali('สฺวากฺขา') + 'สวากขาโต' + + """ + # 1. จัดการช่องว่างที่อาจพิมพ์เกินมาก่อนนฤคหิต + word = word.replace(" ํ", "ํ").replace("ฺ ", "ฺ") + + # --- กฎข้อ 3: นฤคหิต (ํ) --- + # แบบมีสระหน้า (เ-ไ) เช่น โกํ -> โกง + word = re.sub(r'([เ-ไ][ก-ฮ])ํ', r'\1ง_F_', word) + # แบบมีสระบน/ล่าง (ะ-ู) เช่น สุ ํ -> สุง + word = re.sub(r'([ก-ฮ][ะ-ู])ํ', r'\1ง_F_', word) + # แบบพยัญชนะเดี่ยว เช่น หํ -> หัง + word = re.sub(r'([ก-ฮ])ํ', r'\1ัง_F_', word) + + # --- กฎข้อ 4 & 5: พินทุ (ฺ) เป็นตัวควบกล้ำ (ต้นคำ) --- + # ถ้าพินทุอยู่พยัญชนะตัวแรกของคำ (ไม่มีตัวอื่นนำหน้า) ให้มาร์คเป็นตัวควบกล้ำ (_C_) + word = re.sub(r'(^|\s)([ก-ฮ])ฺ', r'\1\2_C_', word) + + # --- กฎข้อ 2: พินทุ (ฺ) เป็นตัวสะกด --- + # แบบมีสระนำหน้า เช่น เนยฺ -> เนย + word = re.sub(r'([เ-ไ][ก-ฮ])([ก-ฮ])ฺ', r'\1\2_F_', word) + # แบบมีสระบน/ล่าง เช่น ทิฏฺ -> ทิฏ + word = re.sub(r'([ก-ฮ][ะ-ู])([ก-ฮ])ฺ', r'\1\2_F_', word) + # แบบพยัญชนะเดี่ยว (สะกด) เช่น สมฺ -> สัม (เพิ่มไม้หันอากาศ) + word = re.sub(r'([ก-ฮ])([ก-ฮ])ฺ', r'\1ั\2_F_', word) + + # --- กฎข้อ 1: พยัญชนะที่ไม่มีเครื่องหมาย ให้อ่านเสียง "อะ" --- + # หาพยัญชนะที่: ไม่ได้ตามหลัง เ-ไ และ ไม่ได้นำหน้าสระ, ไม่ใช่ตัวสะกด(_F_), ไม่ใช่ตัวควบ(_C_) + word = re.sub(r'(?