diff --git a/pythainlp/generate/core.py b/pythainlp/generate/core.py index 897f05a74..0ce3f0633 100644 --- a/pythainlp/generate/core.py +++ b/pythainlp/generate/core.py @@ -88,7 +88,7 @@ def _next_word( output_str: bool, prob: float, duplicate: bool = False, - ): + ) -> Union[list[str], str]: words = [] words.append(text) word_list = list(self._word_prob.keys()) @@ -230,7 +230,7 @@ def prob(self, t1: str, t2: str, t3: str) -> float: def gen_sentence( self, - start_seq: str = "", + start_seq: Union[str, tuple[str, str]] = "", N: int = 4, prob: float = 0.001, output_str: bool = True, @@ -254,14 +254,15 @@ def gen_sentence( gen.gen_sentence() # output: 'ยังทำตัวเป็นเซิร์ฟเวอร์คือ' """ + late_word: Union[str, tuple[str, str]] if not start_seq: # Non-cryptographic use, pseudo-random generator is acceptable here start_seq = random.choice(self.bi_keys) # noqa: S311 late_word = start_seq - list_word = [] + list_word: list[Union[str, tuple[str, str]]] = [] list_word.append(start_seq) - for i in range(N): + for _ in range(N): if duplicate: temp = [j for j in self.ti_keys if j[:2] == late_word] else: @@ -279,11 +280,14 @@ def gen_sentence( late_word = items[1:] list_word.append(late_word) - listdata = [] - for i in list_word: - for j in i: - if j not in listdata: - listdata.append(j) + listdata: list[str] = [] + for item in list_word: + if isinstance(item, tuple): + for j in item: + if j not in listdata: + listdata.append(j) + elif isinstance(item, str) and item not in listdata: + listdata.append(item) if output_str: return "".join(listdata) diff --git a/pythainlp/generate/thai2fit.py b/pythainlp/generate/thai2fit.py index 9fbf5a096..5e6455db9 100644 --- a/pythainlp/generate/thai2fit.py +++ b/pythainlp/generate/thai2fit.py @@ -136,9 +136,10 @@ def gen_sentence( if not start_seq: # Non-cryptographic use, pseudo-random generator is acceptable here start_seq = random.choice(list(thwiki_itos)) # noqa: S311 - list_word = learn.predict( + predicted_text: str = learn.predict( start_seq, N, temperature=0.8, min_p=prob, sep="-*-" - ).split("-*-") + ) + list_word = predicted_text.split("-*-") if output_str: return "".join(list_word) diff --git a/pythainlp/khavee/core.py b/pythainlp/khavee/core.py index de3132f9b..18135c245 100644 --- a/pythainlp/khavee/core.py +++ b/pythainlp/khavee/core.py @@ -4,7 +4,7 @@ # ruff: noqa: C901 from __future__ import annotations -from typing import List, Union +from typing import Union from pythainlp import thai_consonants from pythainlp.tokenize import subword_tokenize @@ -383,7 +383,7 @@ def check_karu_lahu(self, text): else: return "lahu" - def check_klon(self, text: str, k_type: int = 8) -> Union[List[str], str]: + def check_klon(self, text: str, k_type: int = 8) -> Union[list[str], str]: """ Check the suitability of the poem according to Thai principles. @@ -627,8 +627,8 @@ def check_klon(self, text: str, k_type: int = 8) -> Union[List[str], str]: return "Something went wrong. Make sure you enter it in the correct form." def check_aek_too( - self, text: Union[List[str], str], dead_syllable_as_aek: bool = False - ) -> Union[List[bool], List[str], bool, str]: + self, text: Union[list[str], str], dead_syllable_as_aek: bool = False + ) -> Union[list[Union[bool, str]], bool, str]: """ Checker of Thai tonal words @@ -657,7 +657,7 @@ def check_aek_too( # -> [False, 'aek', 'too'] """ if isinstance(text, list): - return [self.check_aek_too(t, dead_syllable_as_aek) for t in text] + return [self.check_aek_too(t, dead_syllable_as_aek) for t in text] # type: ignore[misc] if not isinstance(text, str): raise TypeError("text must be str or iterable list[str]") diff --git a/pythainlp/soundex/sound.py b/pythainlp/soundex/sound.py index 6cf5e9924..6977d3f85 100644 --- a/pythainlp/soundex/sound.py +++ b/pythainlp/soundex/sound.py @@ -71,7 +71,7 @@ def audio_vector(word: str) -> list[list[int]]: audio_vector("น้ำ") # output : [[-1, 1, 1, -1, -1, -1, ...]] """ - return _ft.word_to_vector_list(word2audio(word), numeric=True) + return _ft.word_to_vector_list(word2audio(word), numeric=True) # type: ignore[no-any-return] def word_approximation(word: str, list_word: list[str]) -> list[float]: diff --git a/pythainlp/util/abbreviation.py b/pythainlp/util/abbreviation.py index 542ca872f..437707f64 100644 --- a/pythainlp/util/abbreviation.py +++ b/pythainlp/util/abbreviation.py @@ -45,4 +45,4 @@ def abbreviation_to_full_text( pip install pythainlp[abbreviation]. """ ) - return _replace(text, top_k=top_k) + return _replace(text, top_k=top_k) # type: ignore[no-any-return] diff --git a/pythainlp/util/keyboard.py b/pythainlp/util/keyboard.py index ebe353d41..8ae9c2408 100644 --- a/pythainlp/util/keyboard.py +++ b/pythainlp/util/keyboard.py @@ -220,7 +220,7 @@ def get_char_coord( coord1 = get_char_coord(c1) coord2 = get_char_coord(c2) - distance = ( + distance: float = ( (coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2 ) ** (0.5) if distance == 0 and c1 != c2: diff --git a/pythainlp/util/numtoword.py b/pythainlp/util/numtoword.py index 81c437d13..0314bed28 100644 --- a/pythainlp/util/numtoword.py +++ b/pythainlp/util/numtoword.py @@ -10,6 +10,8 @@ from __future__ import annotations +from typing import Optional + __all__ = ["bahttext", "num_to_thaiword"] _VALUES = [ @@ -77,7 +79,7 @@ def bahttext(number: float) -> str: return ret -def num_to_thaiword(number: int) -> str: +def num_to_thaiword(number: Optional[int]) -> str: """Converts a number to Thai text. :param int number: an integer number to be converted to Thai text @@ -95,11 +97,12 @@ def num_to_thaiword(number: int) -> str: num_to_thaiword(11) # output: สิบเอ็ด """ - output = "" - number_temp = number if number is None: return "" - elif number == 0: + + output = "" + number_temp = number + if number == 0: output = "ศูนย์" number_str = str(abs(number)) diff --git a/pythainlp/util/time.py b/pythainlp/util/time.py index e168b49d4..b72aa1724 100644 --- a/pythainlp/util/time.py +++ b/pythainlp/util/time.py @@ -314,7 +314,7 @@ def thaiword_to_time(text: str, padding: bool = True) -> str: text += ":" # determine minute - if minute: + if minute and isinstance(minute, list): n = 0 for affix in minute: if affix in keys_dict: