Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions pythainlp/khavee/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ def check_sara(self, word: str)-> str:
sara.append('อัว')
elif i == 'ไ' or i == 'ใ':
sara.append('ไอ')
elif i == '็':
sara.append('ออ')
elif 'รร' in word:
if self.check_marttra(word) == 'กม':
sara.append('อำ')
else:
sara.append('อะ')
# Incase ออ
if countoa == 1 and 'อ' in word[-1]:
if countoa == 1 and 'อ' in word[-1] and 'เ' not in word:
sara.remove('ออ')
# In case เอ เอ
countA = 0
Expand Down Expand Up @@ -181,7 +183,12 @@ def check_sara(self, word: str)-> str:
elif sara == [] and len(word) == 3:
sara.append('ออ')

if sara == []:
# incase บ่
if 'บ่' in word:
sara = []
sara.append('ออ')

if sara == []:
return 'Cant find Sara in this word'
else:
return sara[0]
Expand Down Expand Up @@ -236,9 +243,13 @@ def check_marttra(self, word: str) -> str:
elif word[-1] in ['บ', 'ป', 'พ', 'ฟ', 'ภ']:
return 'กบ'
else:
return 'Cant find Marttra in this word'
if '็' in word:
return 'กา'
else:
return 'Cant find Marttra in this word'


def check_sumpus(self, word1: str,word2: str) -> bool:
def is_sumpus(self, word1: str,word2: str) -> bool:
"""
Check the rhyme between two words.

Expand All @@ -254,10 +265,10 @@ def check_sumpus(self, word1: str,word2: str) -> bool:

kv = KhaveeVerifier()

print(kv.check_sumpus('สรร','อัน'))
print(kv.is_sumpus('สรร','อัน'))
# output: True

print(kv.check_sumpus('สรร','แมว'))
print(kv.is_sumpus('สรร','แมว'))
# output: False
"""
marttra1 = self.check_marttra(word1)
Expand All @@ -280,7 +291,13 @@ def check_sumpus(self, word1: str,word2: str) -> bool:
return True
else:
return False


def check_karu_lahu(self,text):
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 ['บ่','ณ','ธ','ก็']:
return 'karu'
else:
return 'lahu'

def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
"""
Check the suitability of the poem according to Thai principles.
Expand Down Expand Up @@ -331,15 +348,15 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
for i in range(len(list_sumpus_sent1)):
countwrong = 0
for j in list_sumpus_sent2h[i]:
if self.check_sumpus(list_sumpus_sent1[i],j) == False:
if self.is_sumpus(list_sumpus_sent1[i],j) == False:
countwrong +=1
if countwrong > 3:
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
# print(sumpus_sent2l,sumpus_sent3)
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
if i > 0:
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+'in paragraph '+str(i+1))
if error == []:
return 'The poem is correct according to the principle.'
Expand Down Expand Up @@ -376,15 +393,15 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:
countwrong = 0
for j in list_sumpus_sent2h[i]:
# print(list_sumpus_sent1[i],j)
if self.check_sumpus(list_sumpus_sent1[i],j) == False:
if self.is_sumpus(list_sumpus_sent1[i],j) == False:
countwrong +=1
if countwrong > 1:
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
# print(sumpus_sent2l,sumpus_sent3)
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
if i > 0:
if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
if self.is_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+'in paragraph '+str(i+1))
if error == []:
return 'The poem is correct according to the principle.'
Expand All @@ -395,12 +412,13 @@ def check_klon(self, text: str,k_type: int=8) -> Union[List[str], str]:

else:
return 'Something went wrong Make sure you enter it in correct form.'

def check_aek_too(self, text: Union[List[str], str]) -> Union[List[bool], List[str], bool, str]:
def check_aek_too(self, text: Union[List[str], str], dead_syllable_as_aek:bool = False) -> Union[List[bool], List[str], bool, str]:
"""
Thai tonal word checker

:param str or list[str] text: Thai word or list of Thai words
:param Union[List[str], str] text: Thai word or list of Thai words
:param bool dead_syllable_as_aek: if True, dead syllable will be considered as aek
:return: the check if the word is aek or too or False(not both) or list of the check if input is list
:rtype: Union[List[bool], List[str], bool, str]

Expand All @@ -416,9 +434,11 @@ def check_aek_too(self, text: Union[List[str], str]) -> Union[List[bool], List[s
## -> False, aek, too
print(kv.check_aek_too(['เอง', 'เอ่ง', 'เอ้ง'])) # ใช้ List ได้เหมือนกัน
## -> [False, 'aek', 'too']


"""
if isinstance(text, list):
return [self.check_aek_too(t) for t in text]
return [self.check_aek_too(t, dead_syllable_as_aek) for t in text]

if not isinstance(text, str):
raise TypeError('text must be str or iterable list[str]')
Expand All @@ -428,5 +448,7 @@ def check_aek_too(self, text: Union[List[str], str]) -> Union[List[bool], List[s
return 'aek'
elif '้' in word_characters and not '่' in word_characters:
return 'too'
if dead_syllable_as_aek and sound_syllable(text) == 'dead':
return 'aek'
else:
return False
15 changes: 11 additions & 4 deletions pythainlp/khavee/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@


# การเช็คสระ
print('เริง',kv.check_sara('เริง'))
print('เออ',kv.check_sara('เมอ'))
# 'เออ'

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

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

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

# การตรวจสอบคำ ครุ ลหุ
print('สรร',kv.check_karu_lahu('สรร'))
#karu
# การตรวจสอบคำ ครุ ลหุ
print('ชิชะ',kv.check_karu_lahu('ชิชะ'))
# lahu

# การตรวจสอบกลอน 8 ที่ถูกฉันทลักษณ์
print(kv.check_klon('''ณรงค์วุฒิผู้เปี่ยมวุฒิสมสง่า มากวิชาหาความรู้ไปสู่ผล
เรื่องฟิสิกส์คณิตศาสตร์เอิร์นอดทน เล่นเกมเก่งลำดับต้นของโรงเรียน
Expand Down