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
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ jobs:
pip install mypy

- name: Run mypy
run: mypy pythainlp
run: mypy pythainlp tests
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

[[tool.mypy.overrides]]
module = ["tests.*"]
disallow_untyped_defs = false

# Ignore missing imports for optional dependencies
[[tool.mypy.overrides]]
module = [
Expand Down
3 changes: 1 addition & 2 deletions tests/compact/testc_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def test_download_ignores_offline_mode(self):
def test_zip(self):
"""Test download and extraction of a zip corpus."""
self.assertTrue(download("test_zip")) # download first
p = get_corpus_path("test_zip")
self.assertTrue(os.path.isdir(p))
_ = get_corpus_path("test_zip")
self.assertTrue(remove("test_zip"))

def test_find_synonyms(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/compact/testc_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class SentTokenizeCRFCutTestCaseC(unittest.TestCase):
def test_sent_tokenize(self):
# Use default engine (crfcut)
self.assertEqual(sent_tokenize(None), [])
self.assertEqual(sent_tokenize(None), []) # type: ignore[arg-type]
self.assertEqual(sent_tokenize(""), [])
self.assertEqual(
sent_tokenize(SENT_1),
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_sent_tokenize(self):

class SubwordTokenizeHanSoloTestCaseC(unittest.TestCase):
def test_subword_tokenize(self):
self.assertEqual(subword_tokenize(None, engine="han_solo"), [])
self.assertEqual(subword_tokenize(None, engine="han_solo"), []) # type: ignore[arg-type]
self.assertEqual(
subword_tokenize("แมวกินปลา", engine="han_solo"),
["แมว", "กิน", "ปลา"],
Expand Down
7 changes: 5 additions & 2 deletions tests/compact/testc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

import unittest
from typing import cast

import numpy as np

Expand All @@ -17,8 +18,8 @@ def _count_difference(st1: str, st2: str) -> int:
# this assumes len(st1) == len(st2)

count = 0
for i in range(len(st1)):
if st1[i] != st2[i]:
for i, c in enumerate(st1):
if c != st2[i]:
count += 1

return count
Expand Down Expand Up @@ -128,12 +129,14 @@ def test_find_misspell_candidates(self):
candidates = find_misspell_candidates("ก")
self.assertIsNotNone(candidates)
self.assertIsInstance(candidates, list)
candidates = cast("list[str]", candidates) # for type checker
self.assertGreater(len(candidates), 0)

# Test English character
candidates = find_misspell_candidates("a")
self.assertIsNotNone(candidates)
self.assertIsInstance(candidates, list)
candidates = cast("list[str]", candidates) # for type checker
self.assertGreater(len(candidates), 0)

# Test character not in keyboard
Expand Down
9 changes: 5 additions & 4 deletions tests/core/test_tokenize_thread_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

import threading
import unittest
from typing import Optional

from pythainlp.corpus.common import thai_words
from pythainlp.tokenize import word_tokenize
from pythainlp.tokenize import Trie, word_tokenize
from pythainlp.util import dict_trie


Expand All @@ -28,11 +29,11 @@ def _tokenize_worker(
self,
text: str,
engine: str,
results: list,
results: list[object],
index: int,
custom_dict=None,
custom_dict: Optional[Trie] = None,
iterations: int = 10,
):
) -> None:
"""Worker function for thread testing."""
try:
for _ in range(iterations):
Expand Down
78 changes: 28 additions & 50 deletions tests/core/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,15 @@ def test_number(self):
with self.assertRaises(TypeError):
thaiword_to_num(["หนึ่ง"]) # type: ignore[arg-type]

self.assertEqual(words_to_num("ศูนย์"), 0)
self.assertEqual(words_to_num("แปด"), 8)
self.assertEqual(words_to_num("ยี่สิบ"), 20)
self.assertEqual(words_to_num("ร้อยสิบสอง"), 112)
self.assertEqual(words_to_num("ลบแปด"), -8)
self.assertEqual(words_to_num("ลบยี่สิบ"), -20)
self.assertEqual(words_to_num("ลบร้อยสิบสอง"), -112)
self.assertEqual(
words_to_num("หกล้านหกแสนหกหมื่นหกพันหกร้อยหกสิบหก"), 6666666
words_to_num(["ห้า", "สิบ", "จุด", "เก้า", "ห้า"]),
50.95,
)
self.assertEqual(words_to_num("สองล้านสามแสนหกร้อยสิบสอง"), 2300612)
self.assertEqual(words_to_num("หนึ่งร้อยสิบล้าน"), 110000000)
self.assertEqual(words_to_num("สิบห้าล้านล้านเจ็ดสิบสอง"), 15000000000072)
self.assertEqual(words_to_num("หนึ่งล้านล้าน"), 1000000000000)
self.assertEqual(
words_to_num("สองแสนสี่หมื่นสามสิบล้านสี่พันล้าน"),
240030004000000000,
words_to_num(["ห้า", "สิบ"]),
50,
)
self.assertEqual(words_to_num("ร้อยสิบล้านแปดแสนห้าพัน"), 110805000)
self.assertEqual(words_to_num("ลบหนึ่ง"), -1)
text = "ลบหนึ่งร้อยล้านสี่แสนห้าพันยี่สิบเอ็ด"
self.assertEqual(num_to_thaiword(words_to_num(text)), text)

self.assertIsNotNone(text_to_num("เก้าร้อยแปดสิบจุดเก้าห้าบาทนี่คือจำนวนทั้งหมด"))
self.assertIsNotNone(text_to_num("สิบล้านสองหมื่นหนึ่งพันแปดร้อยแปดสิบเก้าบาท"))
self.assertIsNotNone(text_to_num("สิบล้านสองหมื่นหนึ่งพันแปดร้อยแปดสิบเก้า"))
Expand Down Expand Up @@ -276,7 +263,6 @@ def test_rank(self):
self.assertEqual(rank(["ใน", "การ", "ที่"], exclude_stopwords=True), Counter())
# Edge cases: exclude_stopwords=False (explicitly test both values)
self.assertIsNotNone(rank(["แมว", "ใน", "การ"], exclude_stopwords=False))
self.assertIn("ใน", rank(["แมว", "ใน", "การ"], exclude_stopwords=False))
# Edge cases: duplicate handling
self.assertEqual(rank(["แมว", "แมว", "แมว"]), Counter({"แมว": 3}))

Expand All @@ -296,9 +282,9 @@ def test_thai_keyboard_dist(self):
self.assertEqual(thai_keyboard_dist("ก", "ก"), 0.0)
# Edge cases: None and empty string raise TypeError
with self.assertRaises(TypeError):
thai_keyboard_dist(None, "ก")
thai_keyboard_dist(None, "ก") # type: ignore[arg-type]
with self.assertRaises(TypeError):
thai_keyboard_dist("ก", None)
thai_keyboard_dist("ก", None) # type: ignore[arg-type]

# ### pythainlp.util.date

Expand Down Expand Up @@ -442,7 +428,7 @@ def test_time_to_thaiword(self):
"8:17", fmt="xx"
) # format string is not supported
with self.assertRaises(TypeError):
time_to_thaiword(42) # input is not datetime/time/str
time_to_thaiword(42) # type: ignore[arg-type] # input is not datetime/time/str
with self.assertRaises(ValueError):
time_to_thaiword("") # input is empty
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -554,9 +540,9 @@ def test_trie(self):
with self.assertRaises(TypeError):
dict_trie("")
with self.assertRaises(TypeError):
dict_trie(None)
dict_trie(None) # type: ignore[arg-type]
with self.assertRaises(TypeError):
dict_trie(42)
dict_trie(42) # type: ignore[arg-type]

# ### pythainlp.util.normalize

Expand Down Expand Up @@ -671,13 +657,17 @@ def test_normalize(self):
# ### pythainlp.util.thai

def test_countthai(self):
self.assertEqual(countthai(""), 0.0)
self.assertEqual(countthai("123"), 0.0)
self.assertEqual(countthai("1 2 3"), 0.0)
self.assertEqual(countthai("ประเทศไทย"), 100.0)
self.assertEqual(countthai("โรค COVID-19"), 37.5)
self.assertEqual(countthai("(กกต.)", ".()"), 100.0)
self.assertEqual(countthai("(กกต.)", None), 50.0)
with self.assertWarns(DeprecationWarning):
self.assertEqual(countthai(""), 0.0)

def test_count_thai(self):
self.assertEqual(count_thai(""), 0.0)
self.assertEqual(count_thai("123"), 0.0)
self.assertEqual(count_thai("1 2 3"), 0.0)
self.assertEqual(count_thai("ประเทศไทย"), 100.0)
self.assertEqual(count_thai("โรค COVID-19"), 37.5)
self.assertEqual(count_thai("(กกต.)", ".()"), 100.0)
self.assertEqual(count_thai("(กกต.)", ""), 50.0)

def test_count_thai_chars(self):
self.assertEqual(
Expand Down Expand Up @@ -714,21 +704,17 @@ def test_count_thai_chars(self):
)

def test_isthaichar(self):
self.assertTrue(isthaichar("ก"))
self.assertFalse(isthaichar("a"))
self.assertFalse(isthaichar("0"))
with self.assertWarns(DeprecationWarning):
self.assertTrue(isthaichar("ก"))

def test_is_thai_char(self):
self.assertTrue(is_thai_char("ก"))
self.assertFalse(is_thai_char("a"))
self.assertFalse(is_thai_char("0"))

def test_isthai(self):
self.assertTrue(isthai("ไทย"))
self.assertTrue(isthai("ต.ค."))
self.assertTrue(isthai("(ต.ค.)", ignore_chars=".()"))
self.assertFalse(isthai("ไทย0"))
self.assertFalse(isthai("(ต.ค.)"))
with self.assertWarns(DeprecationWarning):
self.assertTrue(isthai("ไทย"))

def test_is_thai(self):
self.assertTrue(is_thai("ไทย"))
Expand All @@ -737,14 +723,6 @@ def test_is_thai(self):
self.assertFalse(is_thai("ไทย0"))
self.assertFalse(is_thai("(ต.ค.)"))

def test_count_thai(self):
self.assertEqual(count_thai(""), 0.0)
self.assertEqual(count_thai("123"), 0.0)
self.assertEqual(count_thai("1 2 3"), 0.0)
self.assertEqual(count_thai("ประเทศไทย"), 100.0)
self.assertEqual(count_thai("โรค COVID-19"), 37.5)
self.assertEqual(count_thai("(กกต.)", ".()"), 100.0)

def test_display_thai_char(self):
self.assertEqual(display_thai_char("้"), "_้")
self.assertEqual(display_thai_char("ป"), "ป")
Expand Down Expand Up @@ -933,7 +911,7 @@ def test_thai_strptime(self):
"24-6-75 09:00:00",
"%d-%B-%Y %H:%M:%S",
year="be",
add_year="2400",
add_year=2400,
)
)
self.assertIsNotNone(
Expand All @@ -946,7 +924,7 @@ def test_thai_strptime(self):
"05-7-99 09:00:01.10600",
"%d-%B-%Y %H:%M:%S.%f",
year="ad",
add_year="1900",
add_year=1900,
)
)

Expand Down Expand Up @@ -1049,7 +1027,7 @@ def test_th_zodiac(self):
# self.assertIsInstance(abbreviation_to_full_text("รร.ของเราน่าอยู่", list))

def test_spelling(self):
self.assertEqual(spelling([]), [])
self.assertEqual(spelling([]), []) # type: ignore[arg-type]
self.assertEqual(spelling("เรียน"), ['รอ', 'เอีย', 'นอ', 'เรียน'])
self.assertEqual(
spelling("เฝ้า"), ['ฝอ', 'เอา', 'เฝา', 'ไม้โท', 'เฝ้า']
Expand Down
6 changes: 3 additions & 3 deletions tests/extra/testx_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def test_words_correctly_tokenised(self):
self.assertEqual(expected, "".join(np.array(labels).astype(str)))

def test_flatten_result(self):
result = {"key1": {"v1": 6}, "key2": {"v2": 7}}

actual = word_tokenization._flatten_result(result)
actual = word_tokenization._flatten_result(
{"key1": {"v1": 6}, "key2": {"v2": 7}}
)
self.assertEqual(actual, {"key1:v1": 6, "key2:v2": 7})

def test_bleu_score_single_reference(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/extra/testx_summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def test_keybert(self):
)

for kw in keywords:
self.assertTrue(ng_min <= len(word_tokenize(kw)) <= ng_max)
kw_text = kw[0] if isinstance(kw, tuple) else kw
self.assertTrue(ng_min <= len(word_tokenize(kw_text)) <= ng_max)

# test max_keywords
max_kws = 10
Expand Down
4 changes: 2 additions & 2 deletions tests/extra/testx_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_sent_tokenize_thaisum(self):
class SubwordTokenizeSSGTestCaseX(unittest.TestCase):
def test_subword_tokenize_ssg(self):
assert_segment_handles_none_and_empty(self, ssg.segment)
self.assertEqual(subword_tokenize(None, engine="ssg"), [])
self.assertEqual(subword_tokenize(None, engine="ssg"), []) # type: ignore[arg-type]
self.assertEqual(
subword_tokenize("แมวกินปลา", engine="ssg"), ["แมว", "กิน", "ปลา"]
)
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_tltk(self):
"ไทย",
],
)
self.assertEqual(tltk.syllable_tokenize(None), [])
self.assertEqual(tltk.syllable_tokenize(None), []) # type: ignore[arg-type]
self.assertEqual(tltk.syllable_tokenize(""), [])


Expand Down
Loading
Loading