From 61d81ab523981af44dc009fbd7468a357b0ba520 Mon Sep 17 00:00:00 2001 From: Wannaphong Phatthiyaphaibun Date: Thu, 14 May 2026 01:37:52 +0700 Subject: [PATCH 1/3] Add pythainlp.transliterate.pronunciate_pali --- docs/api/transliterate.rst | 3 ++ pythainlp/transliterate/__init__.py | 1 + pythainlp/transliterate/pali.py | 59 ++++++++++++++++++++++ tests/core/test_transliterate.py | 77 ++++++++++++++++++++++++++++- 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 pythainlp/transliterate/pali.py 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..e1e0db52a 100644 --- a/pythainlp/transliterate/__init__.py +++ b/pythainlp/transliterate/__init__.py @@ -12,3 +12,4 @@ from pythainlp.transliterate.core import pronunciate, romanize, transliterate from pythainlp.transliterate.spoonerism import puan +from pythainlp.transliterate.pali import pronunciate_pali diff --git a/pythainlp/transliterate/pali.py b/pythainlp/transliterate/pali.py new file mode 100644 index 000000000..902cae7ee --- /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'(? Date: Thu, 14 May 2026 01:42:55 +0700 Subject: [PATCH 2/3] Update pronunciate_pali --- pythainlp/transliterate/__init__.py | 1 + pythainlp/transliterate/pali.py | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pythainlp/transliterate/__init__.py b/pythainlp/transliterate/__init__.py index e1e0db52a..96896cb45 100644 --- a/pythainlp/transliterate/__init__.py +++ b/pythainlp/transliterate/__init__.py @@ -8,6 +8,7 @@ "puan", "romanize", "transliterate", + "pronunciate_pali", ] from pythainlp.transliterate.core import pronunciate, romanize, transliterate diff --git a/pythainlp/transliterate/pali.py b/pythainlp/transliterate/pali.py index 902cae7ee..61910bc6e 100644 --- a/pythainlp/transliterate/pali.py +++ b/pythainlp/transliterate/pali.py @@ -31,11 +31,11 @@ def pronunciate_pali(word: str) -> str: # --- กฎข้อ 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) + 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_) @@ -43,11 +43,11 @@ def pronunciate_pali(word: str) -> str: # --- กฎข้อ 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) + 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_) @@ -56,4 +56,4 @@ def pronunciate_pali(word: str) -> str: # ทำความสะอาด Marker ที่เราใช้ทดไว้ตอนประมวลผล (_F_ = Final, _C_ = Cluster) word = word.replace('_F_', '').replace('_C_', '') - return word \ No newline at end of file + return word From 70ed111dc94a590fca2c19f71e7f0f0b8b18a6de Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 13 May 2026 18:54:52 +0000 Subject: [PATCH 3/3] Fix Ruff import ordering in transliterate module and tests Agent-Logs-Url: https://github.com/PyThaiNLP/pythainlp/sessions/49467f7c-5624-44e0-aa0e-f88aed3c6cab Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> --- pythainlp/transliterate/__init__.py | 2 +- tests/core/test_transliterate.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythainlp/transliterate/__init__.py b/pythainlp/transliterate/__init__.py index 96896cb45..f0b21d8c0 100644 --- a/pythainlp/transliterate/__init__.py +++ b/pythainlp/transliterate/__init__.py @@ -12,5 +12,5 @@ ] from pythainlp.transliterate.core import pronunciate, romanize, transliterate -from pythainlp.transliterate.spoonerism import puan from pythainlp.transliterate.pali import pronunciate_pali +from pythainlp.transliterate.spoonerism import puan diff --git a/tests/core/test_transliterate.py b/tests/core/test_transliterate.py index eaa61d472..d66bf8e15 100644 --- a/tests/core/test_transliterate.py +++ b/tests/core/test_transliterate.py @@ -4,7 +4,7 @@ import unittest -from pythainlp.transliterate import romanize, transliterate, pronunciate_pali +from pythainlp.transliterate import pronunciate_pali, romanize, transliterate BASIC_TESTS = { None: "",