• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PyThaiNLP / pythainlp / 4445565965

pending completion
4445565965

push

github

GitHub
Merge pull request #777 from kangkengkhadev/dev

278 of 278 new or added lines in 3 files covered. (100.0%)

5534 of 6126 relevant lines covered (90.34%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.26
/pythainlp/khavee/core.py
1
from pythainlp.tokenize import subword_tokenize
1✔
2

3
class KhaveeVerifier:
1✔
4
    def check_sara(self,word):
1✔
5
        sara = []
×
6
        countoa = 0
×
7
        # In case การันย์
8
        if '์' in word[-1]:
×
9
            word = word[:-2]
×
10
        # In case สระเดี่ยว
11
        for i in word:
×
12
            if i == 'ะ' or i == 'ั':
×
13
                sara.append('อะ')
×
14
            elif i == 'ิ':
×
15
                sara.append('อิ')
×
16
            elif i == 'ุ':
×
17
                sara.append('อุ')
×
18
            elif i == 'ึ':
×
19
                sara.append('อึ')
×
20
            elif i == 'ี':
×
21
                sara.append('อี')
×
22
            elif i == 'ู':
×
23
                sara.append('อู')
×
24
            elif i == 'ื':
×
25
                sara.append('อือ')
×
26
            elif i == 'เ':
×
27
                sara.append('เอ')
×
28
            elif i == 'แ':
×
29
                sara.append('แอ')
×
30
            elif i == 'า':
×
31
                sara.append('อา') 
×
32
            elif i == 'โ':
×
33
                sara.append('โอ')
×
34
            elif i == 'ำ':
×
35
                sara.append('อำ')
×
36
            elif i == 'อ':
×
37
                countoa += 1
×
38
                sara.append('ออ')
×
39
            elif i == 'ั' and 'ว' in word:
×
40
                sara.append('อัว')
×
41
            elif i == 'ไ' or i == 'ใ':
×
42
                sara.append('ไอ') 
×
43
            elif 'รร' in word:
×
44
                if self.check_marttra(word) == 'กม':
×
45
                    sara.append('อำ')
×
46
                else:
47
                    sara.append('อะ')
×
48
        # Incase ออ
49
        if countoa == 1 and 'อ' in word[-1]:
×
50
            sara.remove('ออ')
×
51
        # In case เอ เอ 
52
        countA = 0
×
53
        for i in sara:
×
54
            if i == 'เอ':
×
55
                countA = countA + 1
×
56
            if countA > 1:
×
57
                sara.remove('เอ')
×
58
                sara.remove('เอ')
×
59
                sara.append('แ')
×
60
        # In case สระประสม
61
        if 'เอ' in sara and 'อะ' in sara:
×
62
            sara.remove('เอ')
×
63
            sara.remove('อะ')
×
64
            sara.append('เอะ')
×
65
        elif 'แอ' in sara and 'อะ' in sara:
×
66
            sara.remove('แอ')
×
67
            sara.remove('อะ')
×
68
            sara.append('แอะ')
×
69
        if 'เอะ' in sara and 'ออ' in sara:
×
70
            sara.remove('เอะ')
×
71
            sara.remove('ออ')
×
72
            sara.append('เออะ')
×
73
        elif 'เอ' in sara and 'อิ' in sara:
×
74
            sara.remove('เอ')
×
75
            sara.remove('อิ')
×
76
            sara.append('เออ')        
×
77
        elif 'เอะ' in sara and 'อา' in sara:
×
78
            sara.remove('เอะ')
×
79
            sara.remove('ออ')
×
80
            sara.append('เอาะ')
×
81
        elif 'เอ' in sara and 'ออ' in sara and 'อ' in word[-1]:
×
82
            sara.remove('เอ')
×
83
            sara.remove('ออ')
×
84
            sara.append('เออ')
×
85
        elif 'โอ' in sara and 'อะ' in sara: 
×
86
            sara.remove('โอ')
×
87
            sara.remove('อะ')
×
88
            sara.append('โอะ')
×
89
        elif 'เอ' in sara and 'อี' in sara: 
×
90
            sara.remove('เอ')
×
91
            sara.remove('อี')
×
92
            sara.append('เอีย')
×
93
        elif 'เอ' in sara and 'อือ' in sara: 
×
94
            sara.remove('เอ')
×
95
            sara.remove('อือ')
×
96
            sara.append('อัว')   
×
97
        elif 'เอ' in sara and 'อา' in sara: 
×
98
            sara.remove('เอ')
×
99
            sara.remove('อา')
×
100
            sara.append('เอา') 
×
101
        if 'อือ' in sara and 'เออ' in sara: 
×
102
            sara.remove('เออ')
×
103
            sara.remove('อือ')
×
104
            sara.append('เอือ')  
×
105
        elif 'ออ' in sara and len(sara) > 1:
×
106
            sara.remove('ออ')        
×
107
        elif 'ว' in word and len(sara) == 0:
×
108
            sara.append('อัว')
×
109
        if 'ั' in word and self.check_marttra(word) == 'กา':
×
110
            sara = []
×
111
            sara.append('ไอ')
×
112
        # In case อ
113
        if word == 'เออะ':
×
114
            sara = []
×
115
            sara.append('เออะ')
×
116
        elif word == 'เออ':
×
117
            sara = []
×
118
            sara.append('เออ')
×
119
        elif word == 'เอ':
×
120
            sara = []
×
121
            sara.append('เอ')
×
122
        elif word == 'เอะ':
×
123
            sara = []
×
124
            sara.append('เอะ')
×
125
        elif word == 'เอา':
×
126
            sara = []
×
127
            sara.append('เอา')
×
128
        if 'ฤา' in word or 'ฦา' in word:
×
129
            sara = []
×
130
            sara.append('อือ') 
×
131
        elif 'ฤ' in word or 'ฦ' in word:
×
132
            sara = []
×
133
            sara.append('อึ') 
×
134
        # In case กน
135
        if sara == [] and len(word) == 2:
×
136
            if word[-1] != 'ร':
×
137
                sara.append('โอะ')
×
138
            else:
139
                sara.append('ออ') 
×
140
        elif sara == [] and len(word) == 3:
×
141
            sara.append('ออ') 
×
142
        if sara == []:
×
143
            return 'Cant find Sara in this word'
×
144
        else:
145
            return sara[0]
×
146

147

148
    def check_marttra(self,word):
1✔
149
        if word[-1] == 'ร' and word[-2] in ['ต','ท'] :
×
150
            word = word[:-1]
×
151
            # print(word)
152
        if '์' in word[-1]:
×
153
            if 'ิ' in word[-2] or 'ุ' in word[-2]:
×
154
                word = word[:-3]
×
155
            else:
156
                word = word[:-2]
×
157
        if 'ำ' in word or ('ํ' in word and 'า' in word) or 'ไ' in word or 'ใ' in word:
×
158
            return 'กา'
×
159
        elif word[-1] in ['า','ะ','ิ','ี','ุ','ู','อ'] or ('ี' in word and 'ย' in word[-1]) or ('ื' in word and 'อ' in word[-1]):
×
160
            return 'กา'
×
161
        elif word[-1] in ['ง']:
×
162
            return 'กง'
×
163
        elif word[-1] in ['ม']:
×
164
            return 'กม'
×
165
        elif word[-1] in ['ย']:
×
166
            if 'ั' in word:
×
167
                return 'กา'
×
168
            else:
169
                return 'เกย'
×
170
        elif word[-1] in ['ว']:
×
171
            return 'เกอว'
×
172
        elif word[-1] in ['ก','ข','ค','ฆ']:
×
173
            return 'กก'
×
174
        elif word[-1] in ['จ','ช','ซ','ฎ','ฏ','ฐ','ฑ','ฒ','ด','ต','ถ','ท','ธ','ศ','ษ','ส'] :
×
175
            return 'กด'
×
176
        elif word[-1] in ['ญ',', ณ' ,'น' ,'ร' ,'ล' ,'ฬ']:
×
177
            return 'กน'
×
178
        elif word[-1] in ['บ', 'ป', 'พ', 'ฟ', 'ภ']:
×
179
            return 'กบ'
×
180
        else:
181
           return 'Cant find Marttra in this word'
×
182

183
    def check_sumpus(self,word1,word2):
1✔
184
        marttra1 = self.check_marttra(word1)
×
185
        marttra2 = self.check_marttra(word2)
×
186
        sara1 = self.check_sara(word1)
×
187
        sara2 = self.check_sara(word2)
×
188
        if sara1 == 'อะ' and marttra1 == 'เกย':
×
189
            sara1 = 'ไอ'
×
190
            marttra1 = 'กา'
×
191
        elif sara2 == 'อะ' and marttra2 == 'เกย':
×
192
            sara2 = 'ไอ'
×
193
            marttra2 = 'กา'
×
194
        if sara1 == 'อำ' and marttra1 == 'กม':
×
195
            sara1 = 'อำ'
×
196
            marttra1 = 'กา'
×
197
        elif sara2 == 'อำ' and marttra2 == 'กม':
×
198
            sara2 = 'อำ'
×
199
            marttra2 = 'กา'
×
200
        # print(marttra1,marttra2)
201
        # print(sara1,sara2)
202
        if marttra1 == marttra2 and sara1 == sara2:
×
203
            return True
×
204
        else:
205
            return False
×
206

207
    def check_klon(self,text,k_type=8):
1✔
208
        if k_type == 8:
×
209
            try:
×
210
                error = []
×
211
                list_sumpus_sent1 = []
×
212
                list_sumpus_sent2h = []
×
213
                list_sumpus_sent2l = []
×
214
                list_sumpus_sent3 = []
×
215
                list_sumpus_sent4 = []
×
216
                for i, sent in enumerate(text.split()):
×
217
                    sub_sent = subword_tokenize(sent, engine='dict')
×
218
                    # print(i)
219
                    if len(sub_sent) > 10:
×
220
                        error.append('In the sentence'+str(i+2)+'there are more than 10 words.'+str(sub_sent))
×
221
                    if (i+1) % 4 == 1:
×
222
                        list_sumpus_sent1.append(sub_sent[-1])
×
223
                    elif (i+1) % 4 == 2:
×
224
                        list_sumpus_sent2h.append([sub_sent[1],sub_sent[2],sub_sent[3],sub_sent[4]])
×
225
                        list_sumpus_sent2l.append(sub_sent[-1])
×
226
                    elif (i+1) % 4 == 3:
×
227
                        list_sumpus_sent3.append(sub_sent[-1])
×
228
                    elif (i+1) % 4 == 0:
×
229
                        list_sumpus_sent4.append(sub_sent[-1])
×
230
                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):
×
231
                    return 'The poem does not complete 4 sentences.'
×
232
                else:
233
                    for i in range(len(list_sumpus_sent1)):
×
234
                        countwrong = 0
×
235
                        for j in list_sumpus_sent2h[i]:
×
236
                            if self.check_sumpus(list_sumpus_sent1[i],j) == False:
×
237
                                    countwrong +=1
×
238
                        if  countwrong > 3:
×
239
                            error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
×
240
                        if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
×
241
                            # print(sumpus_sent2l,sumpus_sent3)
242
                            error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
×
243
                        if i > 0:
×
244
                            if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
×
245
                                error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+'in paragraph '+str(i+1))
×
246
                    if error == []:
×
247
                        return 'The poem is correct according to the principle.'
×
248
                    else:
249
                        return error
×
250
            except:
×
251
                return 'Something went wrong Make sure you enter it in correct form of klon4.'
×
252
        elif k_type == 4:
×
253
            try:
×
254
                error = []
×
255
                list_sumpus_sent1 = []
×
256
                list_sumpus_sent2h = []
×
257
                list_sumpus_sent2l = []
×
258
                list_sumpus_sent3 = []
×
259
                list_sumpus_sent4 = []
×
260
                for i, sent in enumerate(text.split()):
×
261
                    sub_sent = subword_tokenize(sent, engine='dict')
×
262
                    if len(sub_sent) > 5:
×
263
                        error.append('In the sentence'+str(i+2)+'there are more than 4 words.'+str(sub_sent))
×
264
                    if (i+1) % 4 == 1:
×
265
                        list_sumpus_sent1.append(sub_sent[-1])
×
266
                    elif (i+1) % 4 == 2:
×
267
                        # print([sub_sent[1],sub_sent[2]])
268
                        list_sumpus_sent2h.append([sub_sent[1],sub_sent[2]])
×
269
                        list_sumpus_sent2l.append(sub_sent[-1])
×
270
                    elif (i+1) % 4 == 3:
×
271
                        list_sumpus_sent3.append(sub_sent[-1])
×
272
                    elif (i+1) % 4 == 0:
×
273
                        list_sumpus_sent4.append(sub_sent[-1])
×
274
                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):
×
275
                    return 'The poem does not complete 4 sentences.'
×
276
                else:
277
                    for i in range(len(list_sumpus_sent1)):
×
278
                        countwrong = 0
×
279
                        for j in list_sumpus_sent2h[i]:
×
280
                            # print(list_sumpus_sent1[i],j)
281
                            if self.check_sumpus(list_sumpus_sent1[i],j) == False:
×
282
                                    countwrong +=1
×
283
                        if  countwrong > 1:
×
284
                            error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent1[i],list_sumpus_sent2h[i]))+'in paragraph '+str(i+1))
×
285
                        if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent3[i]) == False:
×
286
                            # print(sumpus_sent2l,sumpus_sent3)
287
                            error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent3[i]))+'in paragraph '+str(i+1))
×
288
                        if i > 0:
×
289
                            if self.check_sumpus(list_sumpus_sent2l[i],list_sumpus_sent4[i-1]) == False:
×
290
                                error.append('Cant find rhyme between paragraphs '+str((list_sumpus_sent2l[i],list_sumpus_sent4[i-1]))+'in paragraph '+str(i+1))
×
291
                    if error == []:
×
292
                        return 'The poem is correct according to the principle.'
×
293
                    else:
294
                        return error
×
295
            except:
×
296
                return 'Something went wrong Make sure you enter it in correct form.'
×
297
            
298
        else:
299
            return 'Something went wrong Make sure you enter it in correct form.'
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc