Skip to content

Commit 7db158d

Browse files
authored
Merge pull request #1264 from PyThaiNLP/copilot/add-type-hints-to-submodules-yet-again
Add type hints to core modules (stdlib-only)
2 parents c3e39ed + a765bd3 commit 7db158d

8 files changed

Lines changed: 32 additions & 24 deletions

File tree

pythainlp/generate/core.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _next_word(
8888
output_str: bool,
8989
prob: float,
9090
duplicate: bool = False,
91-
):
91+
) -> Union[list[str], str]:
9292
words = []
9393
words.append(text)
9494
word_list = list(self._word_prob.keys())
@@ -230,7 +230,7 @@ def prob(self, t1: str, t2: str, t3: str) -> float:
230230

231231
def gen_sentence(
232232
self,
233-
start_seq: str = "",
233+
start_seq: Union[str, tuple[str, str]] = "",
234234
N: int = 4,
235235
prob: float = 0.001,
236236
output_str: bool = True,
@@ -254,14 +254,15 @@ def gen_sentence(
254254
gen.gen_sentence()
255255
# output: 'ยังทำตัวเป็นเซิร์ฟเวอร์คือ'
256256
"""
257+
late_word: Union[str, tuple[str, str]]
257258
if not start_seq:
258259
# Non-cryptographic use, pseudo-random generator is acceptable here
259260
start_seq = random.choice(self.bi_keys) # noqa: S311
260261
late_word = start_seq
261-
list_word = []
262+
list_word: list[Union[str, tuple[str, str]]] = []
262263
list_word.append(start_seq)
263264

264-
for i in range(N):
265+
for _ in range(N):
265266
if duplicate:
266267
temp = [j for j in self.ti_keys if j[:2] == late_word]
267268
else:
@@ -279,11 +280,14 @@ def gen_sentence(
279280
late_word = items[1:]
280281
list_word.append(late_word)
281282

282-
listdata = []
283-
for i in list_word:
284-
for j in i:
285-
if j not in listdata:
286-
listdata.append(j)
283+
listdata: list[str] = []
284+
for item in list_word:
285+
if isinstance(item, tuple):
286+
for j in item:
287+
if j not in listdata:
288+
listdata.append(j)
289+
elif isinstance(item, str) and item not in listdata:
290+
listdata.append(item)
287291

288292
if output_str:
289293
return "".join(listdata)

pythainlp/generate/thai2fit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ def gen_sentence(
136136
if not start_seq:
137137
# Non-cryptographic use, pseudo-random generator is acceptable here
138138
start_seq = random.choice(list(thwiki_itos)) # noqa: S311
139-
list_word = learn.predict(
139+
predicted_text: str = learn.predict(
140140
start_seq, N, temperature=0.8, min_p=prob, sep="-*-"
141-
).split("-*-")
141+
)
142+
list_word = predicted_text.split("-*-")
142143

143144
if output_str:
144145
return "".join(list_word)

pythainlp/khavee/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ruff: noqa: C901
55
from __future__ import annotations
66

7-
from typing import List, Union
7+
from typing import Union
88

99
from pythainlp import thai_consonants
1010
from pythainlp.tokenize import subword_tokenize
@@ -383,7 +383,7 @@ def check_karu_lahu(self, text):
383383
else:
384384
return "lahu"
385385

386-
def check_klon(self, text: str, k_type: int = 8) -> Union[List[str], str]:
386+
def check_klon(self, text: str, k_type: int = 8) -> Union[list[str], str]:
387387
"""
388388
Check the suitability of the poem according to Thai principles.
389389
@@ -627,8 +627,8 @@ def check_klon(self, text: str, k_type: int = 8) -> Union[List[str], str]:
627627
return "Something went wrong. Make sure you enter it in the correct form."
628628

629629
def check_aek_too(
630-
self, text: Union[List[str], str], dead_syllable_as_aek: bool = False
631-
) -> Union[List[bool], List[str], bool, str]:
630+
self, text: Union[list[str], str], dead_syllable_as_aek: bool = False
631+
) -> Union[list[Union[bool, str]], bool, str]:
632632
"""
633633
Checker of Thai tonal words
634634
@@ -657,7 +657,7 @@ def check_aek_too(
657657
# -> [False, 'aek', 'too']
658658
"""
659659
if isinstance(text, list):
660-
return [self.check_aek_too(t, dead_syllable_as_aek) for t in text]
660+
return [self.check_aek_too(t, dead_syllable_as_aek) for t in text] # type: ignore[misc]
661661

662662
if not isinstance(text, str):
663663
raise TypeError("text must be str or iterable list[str]")

pythainlp/soundex/sound.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def audio_vector(word: str) -> list[list[int]]:
7171
audio_vector("น้ำ")
7272
# output : [[-1, 1, 1, -1, -1, -1, ...]]
7373
"""
74-
return _ft.word_to_vector_list(word2audio(word), numeric=True)
74+
return _ft.word_to_vector_list(word2audio(word), numeric=True) # type: ignore[no-any-return]
7575

7676

7777
def word_approximation(word: str, list_word: list[str]) -> list[float]:

pythainlp/util/abbreviation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ def abbreviation_to_full_text(
4545
pip install pythainlp[abbreviation].
4646
"""
4747
)
48-
return _replace(text, top_k=top_k)
48+
return _replace(text, top_k=top_k) # type: ignore[no-any-return]

pythainlp/util/keyboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def get_char_coord(
220220

221221
coord1 = get_char_coord(c1)
222222
coord2 = get_char_coord(c2)
223-
distance = (
223+
distance: float = (
224224
(coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2
225225
) ** (0.5)
226226
if distance == 0 and c1 != c2:

pythainlp/util/numtoword.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
from __future__ import annotations
1212

13+
from typing import Optional
14+
1315
__all__ = ["bahttext", "num_to_thaiword"]
1416

1517
_VALUES = [
@@ -77,7 +79,7 @@ def bahttext(number: float) -> str:
7779
return ret
7880

7981

80-
def num_to_thaiword(number: int) -> str:
82+
def num_to_thaiword(number: Optional[int]) -> str:
8183
"""Converts a number to Thai text.
8284
8385
:param int number: an integer number to be converted to Thai text
@@ -95,11 +97,12 @@ def num_to_thaiword(number: int) -> str:
9597
num_to_thaiword(11)
9698
# output: สิบเอ็ด
9799
"""
98-
output = ""
99-
number_temp = number
100100
if number is None:
101101
return ""
102-
elif number == 0:
102+
103+
output = ""
104+
number_temp = number
105+
if number == 0:
103106
output = "ศูนย์"
104107

105108
number_str = str(abs(number))

pythainlp/util/time.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def thaiword_to_time(text: str, padding: bool = True) -> str:
314314
text += ":"
315315

316316
# determine minute
317-
if minute:
317+
if minute and isinstance(minute, list):
318318
n = 0
319319
for affix in minute:
320320
if affix in keys_dict:

0 commit comments

Comments
 (0)