Skip to content

Commit e3a0177

Browse files
authored
Merge pull request #854 from BLKSerene/fix_test_khavee
Fix tests of khavee functions
2 parents 4f5b0cf + 30f6927 commit e3a0177

File tree

3 files changed

+160
-62
lines changed

3 files changed

+160
-62
lines changed

pythainlp/khavee/core.py

Lines changed: 139 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self):
2222
KhaveeVerifier: Thai Poetry verifier
2323
"""
2424

25-
def check_sara(self, word: str)-> str:
25+
def check_sara(self, word: str) -> str:
2626
"""
2727
Check the vowels in the Thai word.
2828
@@ -216,13 +216,17 @@ def check_marttra(self, word: str) -> str:
216216
print(kv.check_marttra('สาว'))
217217
# output: 'เกอว'
218218
"""
219-
if word[-1] == 'ร' and word[-2] in ['ต','ท'] :
219+
if word[-1] == 'ร' and word[-2] in ['ต', 'ท']:
220220
word = word[:-1]
221221
word = self.handle_karun_sound_silence(word)
222222
word = remove_tonemark(word)
223223
if 'ำ' in word or ('ํ' in word and 'า' in word) or 'ไ' in word or 'ใ' in word:
224224
return 'กา'
225-
elif word[-1] in ['า','ะ','ิ','ี','ุ','ู','อ'] or ('ี' in word and 'ย' in word[-1]) or ('ื' in word and 'อ' in word[-1]):
225+
elif (
226+
word[-1] in ['า', 'ะ', 'ิ', 'ี', 'ุ', 'ู', 'อ'] or
227+
('ี' in word and 'ย' in word[-1]) or
228+
('ื' in word and 'อ' in word[-1])
229+
):
226230
return 'กา'
227231
elif word[-1] in ['ง']:
228232
return 'กง'
@@ -235,11 +239,13 @@ def check_marttra(self, word: str) -> str:
235239
return 'เกย'
236240
elif word[-1] in ['ว']:
237241
return 'เกอว'
238-
elif word[-1] in ['ก','ข','ค','ฆ']:
242+
elif word[-1] in ['ก', 'ข', 'ค', 'ฆ']:
239243
return 'กก'
240-
elif word[-1] in ['จ','ช','ซ','ฎ','ฏ','ฐ','ฑ','ฒ','ด','ต','ถ','ท','ธ','ศ','ษ','ส'] :
244+
elif word[-1] in [
245+
'จ', 'ช', 'ซ', 'ฎ', 'ฏ', 'ฐ', 'ฑ', 'ฒ', 'ด', 'ต', 'ถ', 'ท', 'ธ', 'ศ', 'ษ', 'ส'
246+
]:
241247
return 'กด'
242-
elif word[-1] in ['ญ',', ณ' ,'น' ,'ร' ,'ล' ,'ฬ']:
248+
elif word[-1] in ['ญ', ', ณ', 'น', 'ร', 'ล', 'ฬ']:
243249
return 'กน'
244250
elif word[-1] in ['บ', 'ป', 'พ', 'ฟ', 'ภ']:
245251
return 'กบ'
@@ -249,8 +255,7 @@ def check_marttra(self, word: str) -> str:
249255
else:
250256
return 'Cant find Marttra in this word'
251257

252-
253-
def is_sumpus(self, word1: str,word2: str) -> bool:
258+
def is_sumpus(self, word1: str, word2: str) -> bool:
254259
"""
255260
Check the rhyme between two words.
256261
@@ -266,10 +271,10 @@ def is_sumpus(self, word1: str,word2: str) -> bool:
266271
267272
kv = KhaveeVerifier()
268273
269-
print(kv.is_sumpus('สรร','อัน'))
274+
print(kv.is_sumpus('สรร', 'อัน'))
270275
# output: True
271276
272-
print(kv.is_sumpus('สรร','แมว'))
277+
print(kv.is_sumpus('สรร', 'แมว'))
273278
# output: False
274279
"""
275280
marttra1 = self.check_marttra(word1)
@@ -290,13 +295,27 @@ def is_sumpus(self, word1: str,word2: str) -> bool:
290295
marttra2 = 'กา'
291296
return bool(marttra1 == marttra2 and sara1 == sara2)
292297

293-
def check_karu_lahu(self,text):
294-
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 ['บ่','ณ','ธ','ก็']:
298+
def check_karu_lahu(self, text):
299+
if (
300+
(
301+
self.check_marttra(text) != 'กา' or
302+
(
303+
self.check_marttra(text) == 'กา' and
304+
self.check_sara(text) in [
305+
'อา', 'อี', 'อือ', 'อู', 'เอ',
306+
'แอ', 'โอ', 'ออ', 'เออ', 'เอีย',
307+
'เอือ', 'อัว'
308+
]
309+
) or
310+
self.check_sara(text) in ['อำ', 'ไอ', 'เอา']
311+
) and
312+
text not in ['บ่', 'ณ', 'ธ', 'ก็']
313+
):
295314
return 'karu'
296315
else:
297316
return 'lahu'
298317

299-
def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
318+
def check_klon(self, text: str, k_type: int = 8) -> Union[List[str], str]:
300319
"""
301320
Check the suitability of the poem according to Thai principles.
302321
@@ -312,11 +331,22 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
312331
313332
kv = KhaveeVerifier()
314333
315-
print(kv.check_klon('''ฉันชื่อหมูกรอบ ฉันชอบกินไก่ แล้วก็วิ่งไล่ หมาชื่อนํ้าทอง ลคคนเก่ง เอ๋งเอ๋งคะนอง มีคนจับจอง เขาชื่อน้องเธียร''', k_type=4))
334+
print(kv.check_klon(
335+
'ฉันชื่อหมูกรอบ ฉันชอบกินไก่ แล้วก็วิ่งไล่ หมาชื่อนํ้าทอง ลคคนเก่ง เอ๋งเอ๋งคะนอง \
336+
มีคนจับจอง เขาชื่อน้องเธียร',
337+
k_type=4
338+
))
316339
# output: The poem is correct according to the principle.
317340
318-
print(kv.check_klon('''ฉันชื่อหมูกรอบ ฉันชอบกินไก่ แล้วก็วิ่งไล่ หมาชื่อนํ้าทอง ลคคนเก่ง เอ๋งเอ๋งเสียงหมา มีคนจับจอง เขาชื่อน้องเธียร''',k_type=4))
319-
# # -> ["Can't find rhyme between paragraphs ('หมา', 'จอง')in paragraph 2", "Can't find rhyme between paragraphs ('หมา', 'ทอง')in paragraph 2"]
341+
print(kv.check_klon(
342+
'ฉันชื่อหมูกรอบ ฉันชอบกินไก่ แล้วก็วิ่งไล่ หมาชื่อนํ้าทอง ลคคนเก่ง \
343+
เอ๋งเอ๋งเสียงหมา มีคนจับจอง เขาชื่อน้องเธียร',
344+
k_type=4
345+
))
346+
# output: [
347+
"Can't find rhyme between paragraphs ('หมา', 'จอง') in paragraph 2",
348+
"Can't find rhyme between paragraphs ('หมา', 'ทอง') in paragraph 2"
349+
]
320350
"""
321351
if k_type == 8:
322352
try:
@@ -327,34 +357,63 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
327357
list_sumpus_sent3 = []
328358
list_sumpus_sent4 = []
329359
for i, sent in enumerate(text.split()):
330-
sub_sent = subword_tokenize(sent,engine='dict')
360+
sub_sent = subword_tokenize(sent, engine='dict')
331361
if len(sub_sent) > 10:
332-
error.append('In sentence '+str(i+2)+', there are more than 10 words. '+str(sub_sent))
333-
if (i+1) % 4 == 1:
362+
error.append(
363+
'In sentence ' +
364+
str(i + 2) +
365+
', there are more than 10 words. ' +
366+
str(sub_sent)
367+
)
368+
if (i + 1) % 4 == 1:
334369
list_sumpus_sent1.append(sub_sent[-1])
335-
elif (i+1) % 4 == 2:
336-
list_sumpus_sent2h.append([sub_sent[1],sub_sent[2],sub_sent[3],sub_sent[4]])
370+
elif (i + 1) % 4 == 2:
371+
list_sumpus_sent2h.append(
372+
[sub_sent[1], sub_sent[2], sub_sent[3], sub_sent[4]]
373+
)
337374
list_sumpus_sent2l.append(sub_sent[-1])
338-
elif (i+1) % 4 == 3:
375+
elif (i + 1) % 4 == 3:
339376
list_sumpus_sent3.append(sub_sent[-1])
340-
elif (i+1) % 4 == 0:
377+
elif (i + 1) % 4 == 0:
341378
list_sumpus_sent4.append(sub_sent[-1])
342-
if len(list_sumpus_sent1) != len(list_sumpus_sent2h) or len(list_sumpus_sent2h) != len(list_sumpus_sent2l) or len(list_sumpus_sent2l) != len(list_sumpus_sent3) or len(list_sumpus_sent3) != len(list_sumpus_sent4) or len(list_sumpus_sent4) != len(list_sumpus_sent1):
379+
if (
380+
len(list_sumpus_sent1) != len(list_sumpus_sent2h) or
381+
len(list_sumpus_sent2h) != len(list_sumpus_sent2l) or
382+
len(list_sumpus_sent2l) != len(list_sumpus_sent3) or
383+
len(list_sumpus_sent3) != len(list_sumpus_sent4) or
384+
len(list_sumpus_sent4) != len(list_sumpus_sent1)
385+
):
343386
return 'The poem does not have 4 complete sentences.'
344387
else:
345388
for i in range(len(list_sumpus_sent1)):
346389
countwrong = 0
347390
for j in list_sumpus_sent2h[i]:
348391
if self.is_sumpus(list_sumpus_sent1[i], j) is False:
349-
countwrong +=1
350-
if countwrong > 3:
351-
error.append('Can\'t find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+' in paragraph '+str(i+1))
392+
countwrong += 1
393+
if countwrong > 3:
394+
error.append(
395+
'Can\'t find rhyme between paragraphs ' +
396+
str((list_sumpus_sent1[i], list_sumpus_sent2h[i])) +
397+
' in paragraph ' +
398+
str(i + 1)
399+
)
352400
if self.is_sumpus(list_sumpus_sent2l[i], list_sumpus_sent3[i]) is False:
353-
# print(sumpus_sent2l,sumpus_sent3)
354-
error.append('Can\'t find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+' in paragraph '+str(i+1))
401+
error.append(
402+
'Can\'t find rhyme between paragraphs ' +
403+
str((list_sumpus_sent2l[i], list_sumpus_sent3[i])) +
404+
' in paragraph ' +
405+
str(i + 1)
406+
)
355407
if i > 0:
356-
if self.is_sumpus(list_sumpus_sent2l[i], list_sumpus_sent4[i-1]) is False:
357-
error.append('Can\'t find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+' in paragraph '+str(i+1))
408+
if self.is_sumpus(
409+
list_sumpus_sent2l[i], list_sumpus_sent4[i - 1]
410+
) is False:
411+
error.append(
412+
'Can\'t find rhyme between paragraphs ' +
413+
str((list_sumpus_sent2l[i], list_sumpus_sent4[i - 1])) +
414+
' in paragraph ' +
415+
str(i + 1)
416+
)
358417
if not error:
359418
return 'The poem is correct according to the principle.'
360419
else:
@@ -370,36 +429,61 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
370429
list_sumpus_sent3 = []
371430
list_sumpus_sent4 = []
372431
for i, sent in enumerate(text.split()):
373-
sub_sent = subword_tokenize(sent,engine='dict')
432+
sub_sent = subword_tokenize(sent, engine='dict')
374433
if len(sub_sent) > 5:
375-
error.append('In sentence '+str(i+2)+', there are more than 4 words. '+str(sub_sent))
376-
if (i+1) % 4 == 1:
434+
error.append(
435+
'In sentence ' +
436+
str(i + 2) +
437+
', there are more than 4 words. ' +
438+
str(sub_sent)
439+
)
440+
if (i + 1) % 4 == 1:
377441
list_sumpus_sent1.append(sub_sent[-1])
378-
elif (i+1) % 4 == 2:
379-
# print([sub_sent[1],sub_sent[2]])
380-
list_sumpus_sent2h.append([sub_sent[1],sub_sent[2]])
442+
elif (i + 1) % 4 == 2:
443+
list_sumpus_sent2h.append([sub_sent[1], sub_sent[2]])
381444
list_sumpus_sent2l.append(sub_sent[-1])
382-
elif (i+1) % 4 == 3:
445+
elif (i + 1) % 4 == 3:
383446
list_sumpus_sent3.append(sub_sent[-1])
384-
elif (i+1) % 4 == 0:
447+
elif (i + 1) % 4 == 0:
385448
list_sumpus_sent4.append(sub_sent[-1])
386-
if len(list_sumpus_sent1) != len(list_sumpus_sent2h) or len(list_sumpus_sent2h) != len(list_sumpus_sent2l) or len(list_sumpus_sent2l) != len(list_sumpus_sent3) or len(list_sumpus_sent3) != len(list_sumpus_sent4) or len(list_sumpus_sent4) != len(list_sumpus_sent1):
449+
if (
450+
len(list_sumpus_sent1) != len(list_sumpus_sent2h) or
451+
len(list_sumpus_sent2h) != len(list_sumpus_sent2l) or
452+
len(list_sumpus_sent2l) != len(list_sumpus_sent3) or
453+
len(list_sumpus_sent3) != len(list_sumpus_sent4) or
454+
len(list_sumpus_sent4) != len(list_sumpus_sent1)
455+
):
387456
return 'The poem does not have 4 complete sentences.'
388457
else:
389458
for i in range(len(list_sumpus_sent1)):
390459
countwrong = 0
391460
for j in list_sumpus_sent2h[i]:
392-
# print(list_sumpus_sent1[i],j)
393461
if self.is_sumpus(list_sumpus_sent1[i], j) is False:
394-
countwrong +=1
395-
if countwrong > 1:
396-
error.append('Can\'t find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
462+
countwrong += 1
463+
if countwrong > 1:
464+
error.append(
465+
'Can\'t find rhyme between paragraphs ' +
466+
str((list_sumpus_sent1[i], list_sumpus_sent2h[i])) +
467+
' in paragraph ' +
468+
str(i + 1)
469+
)
397470
if self.is_sumpus(list_sumpus_sent2l[i], list_sumpus_sent3[i]) is False:
398-
# print(sumpus_sent2l,sumpus_sent3)
399-
error.append('Can\'t find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
471+
error.append(
472+
'Can\'t find rhyme between paragraphs ' +
473+
str((list_sumpus_sent2l[i], list_sumpus_sent3[i])) +
474+
' in paragraph ' +
475+
str(i + 1)
476+
)
400477
if i > 0:
401-
if self.is_sumpus(list_sumpus_sent2l[i], list_sumpus_sent4[i-1]) is False:
402-
error.append('Can\'t find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+' in paragraph '+str(i+1))
478+
if self.is_sumpus(
479+
list_sumpus_sent2l[i], list_sumpus_sent4[i - 1]
480+
) is False:
481+
error.append(
482+
'Can\'t find rhyme between paragraphs ' +
483+
str((list_sumpus_sent2l[i], list_sumpus_sent4[i - 1])) +
484+
' in paragraph ' +
485+
str(i + 1)
486+
)
403487
if not error:
404488
return 'The poem is correct according to the principle.'
405489
else:
@@ -410,7 +494,11 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
410494
else:
411495
return 'Something went wrong. Make sure you enter it in the correct form.'
412496

413-
def check_aek_too(self, text: Union[List[str], str], dead_syllable_as_aek:bool = False) -> Union[List[bool], List[str], bool, str]:
497+
def check_aek_too(
498+
self,
499+
text: Union[List[str], str],
500+
dead_syllable_as_aek: bool = False
501+
) -> Union[List[bool], List[str], bool, str]:
414502
"""
415503
Checker of Thai tonal words
416504
@@ -428,9 +516,9 @@ def check_aek_too(self, text: Union[List[str], str], dead_syllable_as_aek:bool =
428516
429517
# การเช็คคำเอกโท
430518
print(kv.check_aek_too('เอง'), kv.check_aek_too('เอ่ง'), kv.check_aek_too('เอ้ง'))
431-
## -> False, aek, too
519+
# -> False, aek, too
432520
print(kv.check_aek_too(['เอง', 'เอ่ง', 'เอ้ง'])) # ใช้ List ได้เหมือนกัน
433-
## -> [False, 'aek', 'too']
521+
# -> [False, 'aek', 'too']
434522
435523
436524
"""

pythainlp/khavee/example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
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.is_sumpus('สรร','อัน'))
15+
print('สรร อัน', kv.is_sumpus('สรร', 'อัน'))
1616
# True
1717

1818
# การตรวจสอบคำสำผัสที่ผิด
19-
print('เพื่อน ล้วน',kv.is_sumpus('เพื่อน','ล้วน'))
19+
print('เพื่อน ล้วน', kv.is_sumpus('เพื่อน', 'ล้วน'))
2020
# False
2121

2222
# การตรวจสอบคำ ครุ ลหุ
23-
print('สรร',kv.check_karu_lahu('สรร'))
23+
print('สรร', kv.check_karu_lahu('สรร'))
2424
#karu
2525

2626
# การตรวจสอบคำ ครุ ลหุ
27-
print('ชิชะ',kv.check_karu_lahu('ชิชะ'))
27+
print('ชิชะ', kv.check_karu_lahu('ชิชะ'))
2828
# lahu
2929

3030
# การตรวจสอบกลอน 8 ที่ถูกฉันทลักษณ์

0 commit comments

Comments
 (0)