Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions pythainlp/generate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pythainlp/generate/thai2fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pythainlp/khavee/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]")
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/soundex/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/util/abbreviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion pythainlp/util/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions pythainlp/util/numtoword.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from __future__ import annotations

from typing import Optional

__all__ = ["bahttext", "num_to_thaiword"]

_VALUES = [
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/util/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading