Skip to content

Commit 1dd81c8

Browse files
authored
Merge pull request #787 from kangkengkhadev/dev
fix some bugs and add check_karu_lahu function
2 parents b881672 + 686bcd0 commit 1dd81c8

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

pythainlp/khavee/core.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ def check_sara(self, word: str)-> str:
7979
sara.append('อัว')
8080
elif i == 'ไ' or i == 'ใ':
8181
sara.append('ไอ')
82+
elif i == '็':
83+
sara.append('ออ')
8284
elif 'รร' in word:
8385
if self.check_marttra(word) == 'กม':
8486
sara.append('อำ')
8587
else:
8688
sara.append('อะ')
8789
# Incase ออ
88-
if countoa == 1 and 'อ' in word[-1]:
90+
if countoa == 1 and 'อ' in word[-1] and 'เ' not in word:
8991
sara.remove('ออ')
9092
# In case เอ เอ
9193
countA = 0
@@ -181,7 +183,12 @@ def check_sara(self, word: str)-> str:
181183
elif sara == [] and len(word) == 3:
182184
sara.append('ออ')
183185

184-
if sara == []:
186+
# incase บ่
187+
if 'บ่' in word:
188+
sara = []
189+
sara.append('ออ')
190+
191+
if sara == []:
185192
return 'Cant find Sara in this word'
186193
else:
187194
return sara[0]
@@ -236,9 +243,13 @@ def check_marttra(self, word: str) -> str:
236243
elif word[-1] in ['บ', 'ป', 'พ', 'ฟ', 'ภ']:
237244
return 'กบ'
238245
else:
239-
return 'Cant find Marttra in this word'
246+
if '็' in word:
247+
return 'กา'
248+
else:
249+
return 'Cant find Marttra in this word'
250+
240251

241-
def check_sumpus(self, word1: str,word2: str) -> bool:
252+
def is_sumpus(self, word1: str,word2: str) -> bool:
242253
"""
243254
Check the rhyme between two words.
244255
@@ -254,10 +265,10 @@ def check_sumpus(self, word1: str,word2: str) -> bool:
254265
255266
kv = KhaveeVerifier()
256267
257-
print(kv.check_sumpus('สรร','อัน'))
268+
print(kv.is_sumpus('สรร','อัน'))
258269
# output: True
259270
260-
print(kv.check_sumpus('สรร','แมว'))
271+
print(kv.is_sumpus('สรร','แมว'))
261272
# output: False
262273
"""
263274
marttra1 = self.check_marttra(word1)
@@ -280,7 +291,13 @@ def check_sumpus(self, word1: str,word2: str) -> bool:
280291
return True
281292
else:
282293
return False
283-
294+
295+
def check_karu_lahu(self,text):
296+
if (self.check_marttra(text) != 'กา' or (self.check_marttra(text) == 'กา' and self.check_sara(text) in ['อา','อี', 'อือ', 'อู', 'เอ', 'แอ', 'โอ', 'ออ', 'เออ', 'เอีย', 'เอือ' ,'อัว']) or self.check_sara(text) in ['อำ','ไอ','เอา']) and text not in ['บ่','ณ','ธ','ก็']:
297+
return 'karu'
298+
else:
299+
return 'lahu'
300+
284301
def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
285302
"""
286303
Check the suitability of the poem according to Thai principles.
@@ -331,15 +348,15 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
331348
for i in range(len(list_sumpus_sent1)):
332349
countwrong = 0
333350
for j in list_sumpus_sent2h[i]:
334-
if self.check_sumpus(list_sumpus_sent1[i],j) == False:
351+
if self.is_sumpus(list_sumpus_sent1[i],j) == False:
335352
countwrong +=1
336353
if countwrong > 3:
337354
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
338-
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
355+
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
339356
# print(sumpus_sent2l,sumpus_sent3)
340357
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
341358
if i > 0:
342-
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
359+
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
343360
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+'in paragraph '+str(i+1))
344361
if error == []:
345362
return 'The poem is correct according to the principle.'
@@ -376,15 +393,15 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
376393
countwrong = 0
377394
for j in list_sumpus_sent2h[i]:
378395
# print(list_sumpus_sent1[i],j)
379-
if self.check_sumpus(list_sumpus_sent1[i],j) == False:
396+
if self.is_sumpus(list_sumpus_sent1[i],j) == False:
380397
countwrong +=1
381398
if countwrong > 1:
382399
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
383-
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
400+
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
384401
# print(sumpus_sent2l,sumpus_sent3)
385402
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
386403
if i > 0:
387-
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
404+
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
388405
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+'in paragraph '+str(i+1))
389406
if error == []:
390407
return 'The poem is correct according to the principle.'
@@ -395,12 +412,13 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
395412

396413
else:
397414
return 'Something went wrong Make sure you enter it in correct form.'
398-
399-
def check_aek_too(self, text: Union[List[str], str]) -> Union[List[bool], List[str], bool, str]:
415+
416+
def check_aek_too(self, text: Union[List[str], str], dead_syllable_as_aek:bool = False) -> Union[List[bool], List[str], bool, str]:
400417
"""
401418
Thai tonal word checker
402419
403-
:param str or list[str] text: Thai word or list of Thai words
420+
:param Union[List[str], str] text: Thai word or list of Thai words
421+
:param bool dead_syllable_as_aek: if True, dead syllable will be considered as aek
404422
:return: the check if the word is aek or too or False(not both) or list of the check if input is list
405423
:rtype: Union[List[bool], List[str], bool, str]
406424
@@ -416,9 +434,11 @@ def check_aek_too(self, text: Union[List[str], str]) -> Union[List[bool], List[s
416434
## -> False, aek, too
417435
print(kv.check_aek_too(['เอง', 'เอ่ง', 'เอ้ง'])) # ใช้ List ได้เหมือนกัน
418436
## -> [False, 'aek', 'too']
437+
438+
419439
"""
420440
if isinstance(text, list):
421-
return [self.check_aek_too(t) for t in text]
441+
return [self.check_aek_too(t, dead_syllable_as_aek) for t in text]
422442

423443
if not isinstance(text, str):
424444
raise TypeError('text must be str or iterable list[str]')
@@ -428,5 +448,7 @@ def check_aek_too(self, text: Union[List[str], str]) -> Union[List[bool], List[s
428448
return 'aek'
429449
elif '้' in word_characters and not '่' in word_characters:
430450
return 'too'
451+
if dead_syllable_as_aek and sound_syllable(text) == 'dead':
452+
return 'aek'
431453
else:
432454
return False

pythainlp/khavee/example.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44

55

66
# การเช็คสระ
7-
print('เริง',kv.check_sara('เริง'))
7+
print('เออ',kv.check_sara('เมอ'))
88
# 'เออ'
99

1010
# การเช็คมาตราตัวสะกด
11-
print('สาว',kv.check_marttra('สาว'))
11+
print('เทอว',kv.check_marttra('เทอว'))
1212
# 'เกอว'
1313

1414
# การตรวจสอบคำสำผัสที่ถูกต้อง
15-
print('สรร อัน',kv.check_sumpus('สรร','อัน'))
15+
print('สรร อัน',kv.is_sumpus('สรร','อัน'))
1616
# True
1717

1818
# การตรวจสอบคำสำผัสที่ผิด
19-
print('สรร อัน',kv.check_sumpus('หมัน','อัน'))
19+
print('สรร ขวาน',kv.is_sumpus('สรร','ขวาน'))
2020
# False
2121

22+
# การตรวจสอบคำ ครุ ลหุ
23+
print('สรร',kv.check_karu_lahu('สรร'))
24+
#karu
25+
# การตรวจสอบคำ ครุ ลหุ
26+
print('ชิชะ',kv.check_karu_lahu('ชิชะ'))
27+
# lahu
28+
2229
# การตรวจสอบกลอน 8 ที่ถูกฉันทลักษณ์
2330
print(kv.check_klon('''ณรงค์วุฒิผู้เปี่ยมวุฒิสมสง่า มากวิชาหาความรู้ไปสู่ผล
2431
เรื่องฟิสิกส์คณิตศาสตร์เอิร์นอดทน เล่นเกมเก่งลำดับต้นของโรงเรียน

0 commit comments

Comments
 (0)