Skip to content

Commit a22ff19

Browse files
authored
Merge pull request #1266 from PyThaiNLP/copilot/add-type-hints-to-submodules-one-more-time
Add type hints and update docstrings for compact test suite modules
2 parents 0c21974 + 77e4b8c commit a22ff19

5 files changed

Lines changed: 44 additions & 22 deletions

File tree

pythainlp/classify/param_free.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ def train(self):
4141
return temp_list
4242

4343
def predict(self, x1: str, k: int = 1) -> str:
44-
""":param str x1: the text that we want to predict label for.
45-
:param str k: k
46-
:return: label
44+
"""Predict the label for the given text.
45+
46+
:param str x1: the text that we want to predict label for
47+
:param int k: number of nearest neighbors to consider (default: 1)
48+
:return: predicted label
4749
:rtype: str
4850
4951
:Example:
@@ -80,7 +82,7 @@ def predict(self, x1: str, k: int = 1) -> str:
8082
sorted_idx = np.argsort(np.array(disance_from_x1))
8183
top_k_class = self.training_data[sorted_idx[:k], 1]
8284
_, counts = np.unique(top_k_class, return_counts=True)
83-
predict_class = top_k_class[counts.argmax()]
85+
predict_class = str(top_k_class[counts.argmax()])
8486

8587
return predict_class
8688

pythainlp/transliterate/pyicu.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
def transliterate(text: str) -> str:
2020
"""Use ICU (International Components for Unicode) for transliteration
21-
:param str text: Thai text to be transliterated.
22-
:return: A string of Internaitonal Phonetic Alphabets indicating how the text should be pronounced.
21+
22+
:param str text: Thai text to be transliterated
23+
:return: A string of International Phonetic Alphabets indicating how the text should be pronounced
24+
:rtype: str
2325
"""
24-
return _ICU_THAI_TO_LATIN.transliterate(text)
26+
return str(_ICU_THAI_TO_LATIN.transliterate(text))

pythainlp/util/spell_words.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import re
77
from functools import lru_cache
8+
from typing import Optional
89

910
from pythainlp import (
1011
thai_above_vowels,
@@ -24,8 +25,12 @@
2425
for i, j in zip(list(thai_tonemarks), ["เอก", "โท", "ตรี", "จัตวา"])
2526
}
2627

27-
rule1: list[str] = [i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1]
28-
rule2: list[str] = [i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2]
28+
rule1: list[str] = [
29+
i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1
30+
]
31+
rule2: list[str] = [
32+
i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2
33+
]
2934
rule3: list[str] = [
3035
i.replace("–", f"([{thai_letters}])").replace(":", f"([{thai_tonemarks}])")
3136
for i in _r2
@@ -108,12 +113,12 @@ def spell_syllable(text: str) -> list[str]:
108113
return c_only + v_only + t_only + [text]
109114

110115

111-
def spell_word(text: str) -> list[str]:
116+
def spell_word(text: Optional[str]) -> list[str]:
112117
"""Spell out words in Thai word distribution form.
113118
114-
:param str w: Thai words only
115-
:return: List of spelled out words
116-
:rtype: List[str]
119+
:param Optional[str] text: Thai words only, or None
120+
:return: List of spelled out words, empty list if text is None or empty
121+
:rtype: list[str]
117122
118123
:Example:
119124
::
@@ -122,7 +127,13 @@ def spell_word(text: str) -> list[str]:
122127
123128
print(spell_word("คนดี"))
124129
# output: ['คอ', 'นอ', 'คน', 'ดอ', 'อี', 'ดี', 'คนดี']
130+
131+
print(spell_word(None))
132+
# output: []
125133
"""
134+
if not text:
135+
return []
136+
126137
spellouts = []
127138
tokens = subword_tokenize(text, engine="han_solo")
128139

pythainlp/util/thai.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
22
# SPDX-FileType: SOURCE
33
# SPDX-License-Identifier: Apache-2.0
4-
"""Check if it is Thai text
5-
"""
4+
"""Check if it is Thai text"""
65

76
from __future__ import annotations
87

98
import string
109
from collections import defaultdict
10+
from typing import Optional
1111

1212
from pythainlp import (
1313
thai_above_vowels,
@@ -215,16 +215,17 @@ def display_thai_char(ch: str) -> str:
215215
return ch
216216

217217

218-
def thai_word_tone_detector(word: str) -> list[tuple[str, str]]:
218+
def thai_word_tone_detector(word: Optional[str]) -> list[tuple[str, str]]:
219219
"""Thai tone detector for word.
220220
221221
It uses pythainlp.transliterate.pronunciate for converting word to\
222222
pronunciation.
223223
224-
:param str word: Thai word.
225-
:return: Thai pronunciation with tones in each syllable.\
226-
(l, m, h, r, f or empty if it cannot be detected)
227-
:rtype: Tuple[str, str]
224+
:param Optional[str] word: Thai word, or None
225+
:return: List of tuples containing Thai pronunciation with tones in each syllable.\
226+
Tone values: l (low), m (mid), h (high), r (rising), f (falling), or empty if it cannot be detected.\
227+
Returns [] if word is None or empty.
228+
:rtype: list[tuple[str, str]]
228229
229230
:Example:
230231
::
@@ -236,7 +237,13 @@ def thai_word_tone_detector(word: str) -> list[tuple[str, str]]:
236237
237238
print(thai_word_tone_detector("มือถือ"))
238239
# output: [('มือ', 'm'), ('ถือ', 'r')]
240+
241+
print(thai_word_tone_detector(None))
242+
# output: []
239243
"""
244+
if not word:
245+
return []
246+
240247
from ..transliterate import pronunciate
241248
from ..util.syllable import tone_detector
242249

tests/compact/testc_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ def test_thai_word_tone_detector(self):
3333
thai_word_tone_detector("ราคา"), [("รา", "m"), ("คา", "m")]
3434
)
3535
# Edge cases: None and empty string
36-
self.assertEqual(thai_word_tone_detector(None), [("", "")])
37-
self.assertEqual(thai_word_tone_detector(""), [("", "")])
36+
self.assertEqual(thai_word_tone_detector(None), [])
37+
self.assertEqual(thai_word_tone_detector(""), [])

0 commit comments

Comments
 (0)