Skip to content

Commit 4019183

Browse files
Copilotwannaphong
andcommitted
Optimize helper function by moving to class method
- Moved has_true_final_yl from inline function to class method _has_true_final_yl - Avoids recreating function object on every check_marttra call - Improves performance and code organization Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
1 parent ff3a4c3 commit 4019183

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

pythainlp/khavee/core.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ def __init__(self):
1717
KhaveeVerifier: Thai Poetry verifier
1818
"""
1919

20+
def _has_true_final_yl(self, word: str) -> bool:
21+
"""
22+
Check if ย or ล is a true final consonant
23+
(not just part of the vowel sound with ไ/ใ)
24+
25+
:param str word: Thai word
26+
:return: True if ย or ล is a true final consonant
27+
:rtype: bool
28+
"""
29+
if len(word) < 2:
30+
return False
31+
# Count consonants in the word
32+
consonant_count = sum(1 for c in word if c in thai_consonants)
33+
# If there are 2+ consonants and word ends with ย or ล, it's a true final
34+
return consonant_count >= 2 and word[-1] in ["ย", "ล"]
35+
2036
def check_sara(self, word: str) -> str:
2137
"""
2238
Check the vowels in the Thai word.
@@ -241,23 +257,12 @@ def check_marttra(self, word: str) -> str:
241257
if word[-1] == "ำ":
242258
return "กม"
243259

244-
# Helper function to check if ย or ล is a true final consonant
245-
# (not just part of the vowel sound with ไ/ใ)
246-
def has_true_final_yl(w):
247-
"""Check if ย or ล is a true final consonant"""
248-
if len(w) < 2:
249-
return False
250-
# Count consonants in the word
251-
consonant_count = sum(1 for c in w if c in thai_consonants)
252-
# If there are 2+ consonants and word ends with ย or ล, it's a true final
253-
return consonant_count >= 2 and w[-1] in ["ย", "ล"]
254-
255260
# Check for vowels and special patterns that indicate open syllables (กา)
256261
# For words with ไ/ใ, check if ย/ล is a true final or just part of vowel
257262
if "ไ" in word or "ใ" in word:
258263
if word[-1] not in ["ย", "ล"]:
259264
return "กา"
260-
elif not has_true_final_yl(word):
265+
elif not self._has_true_final_yl(word):
261266
# ย/ล is part of the vowel sound, not a true final
262267
return "กา"
263268
# else: ย/ล is a true final, continue to consonant classification below

0 commit comments

Comments
 (0)