Skip to content

Commit f3e772e

Browse files
authored
Reduce number of elif
1 parent 463dd23 commit f3e772e

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

pythainlp/util/syllable.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
Syllable tools
77
"""
8+
89
import re
910

1011
from pythainlp import thai_consonants, thai_tonemarks
@@ -23,9 +24,7 @@
2324
thai_consonants_all = list(thai_consonants)
2425
thai_consonants_all.remove("อ")
2526

26-
_temp = list(
27-
"".join(["".join(v) for v in spelling_class.values()])
28-
)
27+
_temp = list("".join(["".join(v) for v in spelling_class.values()]))
2928
not_spelling_class = [j for j in thai_consonants_all if j not in _temp]
3029

3130
# vowel's short sound
@@ -37,6 +36,7 @@
3736
# These spelling consonant ares live syllables.
3837
for i in ["กง", "กน", "กม", "เกย", "เกอว"]:
3938
_check_1.extend(spelling_class[i])
39+
4040
# These spelling consonants are dead syllables.
4141
_check_2 = spelling_class["กก"] + spelling_class["กบ"] + spelling_class["กด"]
4242

@@ -54,6 +54,7 @@
5454
"high": thai_high_aspirates + thai_high_irregular,
5555
}
5656
thai_initial_consonant_to_type = {}
57+
5758
for k, v in thai_initial_consonant_type.items():
5859
for i in v:
5960
thai_initial_consonant_to_type[i] = k
@@ -67,7 +68,7 @@ def sound_syllable(syllable: str) -> str:
6768
The syllable is a live syllable or dead syllable.
6869
6970
:param str syllable: Thai syllable
70-
:return: syllable's type (live or dead)
71+
:return: syllable's type ("live" or "dead")
7172
:rtype: str
7273
7374
:Example:
@@ -84,14 +85,17 @@ def sound_syllable(syllable: str) -> str:
8485
# if len of syllable < 2
8586
if len(syllable) < 2:
8687
return "dead"
88+
8789
# get consonants
8890
consonants = [i for i in syllable if i in list(thai_consonants_all)]
89-
if (len(consonants) == 0 and
90-
"อ" in syllable
91+
if (
92+
(len(consonants) == 0)
93+
and ("อ" in syllable)
9194
and any((c in set("เ")) for c in syllable)
92-
and len(syllable)== 2
95+
and (len(syllable) == 2)
9396
):
9497
return "live"
98+
9599
# get spelling consonants
96100
spelling_consonant = consonants[-1]
97101
if (spelling_consonant in _check_2) and (
@@ -100,45 +104,56 @@ def sound_syllable(syllable: str) -> str:
100104
and bool(pattern.search(syllable)) is not True
101105
):
102106
return "dead"
103-
elif any((c in set("าีืแูาโ")) for c in syllable): # in syllable:
107+
108+
if any((c in set("าีืแูาโ")) for c in syllable): # in syllable:
104109
if (
105110
spelling_consonant in _check_1
106111
and bool(re_short.search(syllable)) is not True
107112
):
108113
return "live"
109-
elif (
114+
115+
if (
110116
spelling_consonant != syllable[-1]
111117
and bool(re_short.search(syllable)) is not True
112118
):
113119
return "live"
114-
elif spelling_consonant in _check_2:
120+
121+
if spelling_consonant in _check_2:
115122
return "dead"
116-
elif bool(re_short.search(syllable)) or any(
123+
124+
if bool(re_short.search(syllable)) or any(
117125
(c in set(short)) for c in syllable
118126
):
119127
return "dead"
128+
120129
return "live"
121-
elif any((c in set("ำใไ")) for c in syllable):
130+
131+
if any((c in set("ำใไ")) for c in syllable):
122132
return "live" # if these vowel's long sounds are live syllables
123-
elif bool(pattern.search(syllable)): # if it is เ-า
133+
134+
if bool(pattern.search(syllable)): # if it is เ-า
124135
return "live"
125-
elif spelling_consonant in _check_1:
136+
137+
if spelling_consonant in _check_1:
126138
if (
127139
bool(re_short.search(syllable))
128140
or any((c in set(short)) for c in syllable)
129141
) and len(consonants) < 2:
130142
return "dead"
131-
elif syllable[-1] in set(short):
143+
144+
if syllable[-1] in set(short):
132145
return "dead"
146+
133147
return "live"
134-
elif bool(
148+
149+
if bool(
135150
re_short.search(syllable)
136151
) or any( # if vowel's short sound is found
137152
(c in set(short)) for c in syllable
138153
): # consonant in short
139154
return "dead"
140-
else:
141-
return "dead"
155+
156+
return "dead"
142157

143158

144159
def syllable_open_close_detector(syllable: str) -> str:
@@ -163,10 +178,13 @@ def syllable_open_close_detector(syllable: str) -> str:
163178
# output: open
164179
"""
165180
consonants = [i for i in syllable if i in list(thai_consonants)]
181+
166182
if len(consonants) < 2:
167183
return "open"
168-
elif len(consonants) == 2 and consonants[-1] == "อ":
184+
185+
if len(consonants) == 2 and consonants[-1] == "อ":
169186
return "open"
187+
170188
return "close"
171189

172190

@@ -194,27 +212,31 @@ def syllable_length(syllable: str) -> str:
194212
consonants = [i for i in syllable if i in list(thai_consonants)]
195213
if len(consonants) <= 3 and any((c in set(short)) for c in syllable):
196214
return "short"
197-
elif bool(re_short.search(syllable)):
215+
216+
if bool(re_short.search(syllable)):
198217
return "short"
199-
else:
200-
return "long"
218+
219+
return "long"
201220

202221

203222
def _tone_mark_detector(syllable: str) -> str:
204223
tone_mark = [i for i in syllable if i in list(thai_tonemarks)]
205224
if tone_mark == []:
206225
return ""
207-
else:
208-
return tone_mark[0]
226+
227+
return tone_mark[0]
209228

210229

211230
def _check_sonorant_syllable(syllable: str) -> bool:
212231
_sonorant = [i for i in syllable if i in thai_low_sonorants]
213232
consonants = [i for i in syllable if i in list(thai_consonants)]
233+
214234
if _sonorant[-1] == consonants[-2]:
215235
return True
216-
elif _sonorant[-1] == consonants[-1]:
236+
237+
if _sonorant[-1] == consonants[-1]:
217238
return True
239+
218240
return False
219241

220242

@@ -256,9 +278,7 @@ def tone_detector(syllable: str) -> str:
256278
initial_consonant_type = thai_initial_consonant_to_type[initial_consonant]
257279
# r for store value
258280
r = ""
259-
if len(consonants) > 1 and (
260-
initial_consonant in ("อ", "ห")
261-
):
281+
if len(consonants) > 1 and (initial_consonant in ("อ", "ห")):
262282
consonant_ending = _check_sonorant_syllable(syllable)
263283
if (
264284
initial_consonant == "อ"
@@ -333,4 +353,5 @@ def tone_detector(syllable: str) -> str:
333353
r = "m"
334354
elif initial_consonant_type == "high" and s == "live":
335355
r = "r"
356+
336357
return r

0 commit comments

Comments
 (0)