diff --git a/pythainlp/classify/param_free.py b/pythainlp/classify/param_free.py index 96876ab6e..2bed7fbf3 100644 --- a/pythainlp/classify/param_free.py +++ b/pythainlp/classify/param_free.py @@ -41,9 +41,11 @@ def train(self): return temp_list def predict(self, x1: str, k: int = 1) -> str: - """:param str x1: the text that we want to predict label for. - :param str k: k - :return: label + """Predict the label for the given text. + + :param str x1: the text that we want to predict label for + :param int k: number of nearest neighbors to consider (default: 1) + :return: predicted label :rtype: str :Example: @@ -80,7 +82,7 @@ def predict(self, x1: str, k: int = 1) -> str: sorted_idx = np.argsort(np.array(disance_from_x1)) top_k_class = self.training_data[sorted_idx[:k], 1] _, counts = np.unique(top_k_class, return_counts=True) - predict_class = top_k_class[counts.argmax()] + predict_class = str(top_k_class[counts.argmax()]) return predict_class diff --git a/pythainlp/transliterate/pyicu.py b/pythainlp/transliterate/pyicu.py index 4dc431e85..2efc5576f 100644 --- a/pythainlp/transliterate/pyicu.py +++ b/pythainlp/transliterate/pyicu.py @@ -18,7 +18,9 @@ def transliterate(text: str) -> str: """Use ICU (International Components for Unicode) for transliteration - :param str text: Thai text to be transliterated. - :return: A string of Internaitonal Phonetic Alphabets indicating how the text should be pronounced. + + :param str text: Thai text to be transliterated + :return: A string of International Phonetic Alphabets indicating how the text should be pronounced + :rtype: str """ - return _ICU_THAI_TO_LATIN.transliterate(text) + return str(_ICU_THAI_TO_LATIN.transliterate(text)) diff --git a/pythainlp/util/spell_words.py b/pythainlp/util/spell_words.py index d96e0c3a0..e6d585984 100644 --- a/pythainlp/util/spell_words.py +++ b/pythainlp/util/spell_words.py @@ -5,6 +5,7 @@ import re from functools import lru_cache +from typing import Optional from pythainlp import ( thai_above_vowels, @@ -24,8 +25,12 @@ for i, j in zip(list(thai_tonemarks), ["เอก", "โท", "ตรี", "จัตวา"]) } -rule1: list[str] = [i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1] -rule2: list[str] = [i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2] +rule1: list[str] = [ + i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1 +] +rule2: list[str] = [ + i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2 +] rule3: list[str] = [ i.replace("–", f"([{thai_letters}])").replace(":", f"([{thai_tonemarks}])") for i in _r2 @@ -108,12 +113,12 @@ def spell_syllable(text: str) -> list[str]: return c_only + v_only + t_only + [text] -def spell_word(text: str) -> list[str]: +def spell_word(text: Optional[str]) -> list[str]: """Spell out words in Thai word distribution form. - :param str w: Thai words only - :return: List of spelled out words - :rtype: List[str] + :param Optional[str] text: Thai words only, or None + :return: List of spelled out words, empty list if text is None or empty + :rtype: list[str] :Example: :: @@ -122,7 +127,13 @@ def spell_word(text: str) -> list[str]: print(spell_word("คนดี")) # output: ['คอ', 'นอ', 'คน', 'ดอ', 'อี', 'ดี', 'คนดี'] + + print(spell_word(None)) + # output: [] """ + if not text: + return [] + spellouts = [] tokens = subword_tokenize(text, engine="han_solo") diff --git a/pythainlp/util/thai.py b/pythainlp/util/thai.py index 6b50e49ab..3707a6849 100644 --- a/pythainlp/util/thai.py +++ b/pythainlp/util/thai.py @@ -1,13 +1,13 @@ # SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project # SPDX-FileType: SOURCE # SPDX-License-Identifier: Apache-2.0 -"""Check if it is Thai text -""" +"""Check if it is Thai text""" from __future__ import annotations import string from collections import defaultdict +from typing import Optional from pythainlp import ( thai_above_vowels, @@ -215,16 +215,17 @@ def display_thai_char(ch: str) -> str: return ch -def thai_word_tone_detector(word: str) -> list[tuple[str, str]]: +def thai_word_tone_detector(word: Optional[str]) -> list[tuple[str, str]]: """Thai tone detector for word. It uses pythainlp.transliterate.pronunciate for converting word to\ pronunciation. - :param str word: Thai word. - :return: Thai pronunciation with tones in each syllable.\ - (l, m, h, r, f or empty if it cannot be detected) - :rtype: Tuple[str, str] + :param Optional[str] word: Thai word, or None + :return: List of tuples containing Thai pronunciation with tones in each syllable.\ + Tone values: l (low), m (mid), h (high), r (rising), f (falling), or empty if it cannot be detected.\ + Returns [] if word is None or empty. + :rtype: list[tuple[str, str]] :Example: :: @@ -236,7 +237,13 @@ def thai_word_tone_detector(word: str) -> list[tuple[str, str]]: print(thai_word_tone_detector("มือถือ")) # output: [('มือ', 'm'), ('ถือ', 'r')] + + print(thai_word_tone_detector(None)) + # output: [] """ + if not word: + return [] + from ..transliterate import pronunciate from ..util.syllable import tone_detector diff --git a/tests/compact/testc_util.py b/tests/compact/testc_util.py index 7ff9915e5..eb43fb27f 100644 --- a/tests/compact/testc_util.py +++ b/tests/compact/testc_util.py @@ -33,5 +33,5 @@ def test_thai_word_tone_detector(self): thai_word_tone_detector("ราคา"), [("รา", "m"), ("คา", "m")] ) # Edge cases: None and empty string - self.assertEqual(thai_word_tone_detector(None), [("", "")]) - self.assertEqual(thai_word_tone_detector(""), [("", "")]) + self.assertEqual(thai_word_tone_detector(None), []) + self.assertEqual(thai_word_tone_detector(""), [])