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
16 changes: 11 additions & 5 deletions pythainlp/util/digitconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ def thai_digit_to_arabic_digit(text: str) -> str:
thai_digit_to_arabic_digit(text)
# output: เป็นจำนวน 123,400.25 บาท
"""
if not text or not isinstance(text, str):
if not isinstance(text, str):
raise TypeError("The text must be str type.")
if not text:
return ""

return text.translate(_thai_arabic_translate_table)

Expand All @@ -114,8 +116,10 @@ def arabic_digit_to_thai_digit(text: str) -> str:
arabic_digit_to_thai_digit(text)
# output: เป็นจำนวน ๑๒๓,๔๐๐.๒๕ บาท
"""
if not text or not isinstance(text, str):
if not isinstance(text, str):
raise TypeError("The text must be str type.")
if not text:
return ""

# Convert Arabic to Thai numerals
return text.translate(_arabic_thai_translate_table)
Expand All @@ -139,8 +143,10 @@ def digit_to_text(text: str) -> str:
digit_to_text("๕๖๗")
# output: 'ห้าหกเจ็ด'
"""
if not text or not isinstance(text, str):
if not isinstance(text, str):
raise TypeError("The text must be str type.")
if not text:
return ""

# Convert Thai numerals to Arabic ones
text = text.translate(_thai_arabic_translate_table)
Expand Down Expand Up @@ -180,7 +186,7 @@ def text_to_arabic_digit(text: str) -> str:
"""
if not isinstance(text, str):
raise TypeError("The text must be str type.")
elif not text or text not in _spell_digit:
if not text or text not in _spell_digit:
return ""

return _spell_digit[text]
Expand Down Expand Up @@ -217,7 +223,7 @@ def text_to_thai_digit(text: str) -> str:
"""
if not isinstance(text, str):
raise TypeError("The text must be str type.")
elif not text:
if not text:
return ""

return arabic_digit_to_thai_digit(text_to_arabic_digit(text))
8 changes: 5 additions & 3 deletions tests/compact/testc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_misspell_edge_cases(self):
self.assertEqual(len(result), 1)
# Edge case: None raises TypeError
with self.assertRaises(TypeError):
misspell(None)
misspell(None) # type: ignore[arg-type]

def test_misspell_naive(self):
for text in self.texts:
Expand Down Expand Up @@ -95,12 +95,14 @@ def test_search_location_of_character(self):
# Test Thai characters
loc = search_location_of_character("ก")
self.assertIsNotNone(loc)
self.assertEqual(len(loc), 4) # (language_ix, is_shift, row, pos)
# loc shape is (language_ix, is_shift, row, pos)
self.assertEqual(len(loc), 4) # type: ignore[arg-type]

# Test English characters
loc = search_location_of_character("a")
self.assertIsNotNone(loc)
self.assertEqual(len(loc), 4)
# loc shape is (language_ix, is_shift, row, pos)
self.assertEqual(len(loc), 4) # type: ignore[arg-type]

# Test shifted characters
loc = search_location_of_character("A")
Expand Down
5 changes: 3 additions & 2 deletions tests/core/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import io
import unittest
from argparse import ArgumentError
from argparse import ArgumentError, ArgumentParser
from contextlib import redirect_stderr, redirect_stdout
from unittest.mock import MagicMock, patch

Expand All @@ -18,8 +18,9 @@

class CliTestCase(unittest.TestCase):
def test_cli(self):
parser = ArgumentParser()
with self.assertRaises((ArgumentError, SystemExit)):
cli.exit_if_empty("", None)
cli.exit_if_empty("", parser)

def test_cli_main(self):
# Suppress output to keep test log clean
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_khavee.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def setUp(self):

def test_non_string_raises_type_error(self):
with self.assertRaises(TypeError):
self.kv.check_aek_too(123)
self.kv.check_aek_too(123) # type: ignore[arg-type]

def test_dead_syllable_as_aek_flag(self):
self.assertEqual(self.kv.check_aek_too("บท", dead_syllable_as_aek=True), "aek")
Expand All @@ -187,7 +187,7 @@ def test_dead_syllable_without_flag_returns_false(self):

def test_list_with_non_string_element_raises(self):
with self.assertRaises(TypeError):
self.kv.check_aek_too(["ไก่", 42])
self.kv.check_aek_too(["ไก่", 42]) # type: ignore[list-item]

def test_both_tone_marks_returns_false(self):
# word with both ่ and ้ should return False
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_morpheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_nighit(self):
nighit("สํ", "มาร") # consonant ม is not in any supported group

def test_is_native_thai(self):
self.assertFalse(is_native_thai(None), False)
self.assertFalse(is_native_thai(None), False) # type: ignore[arg-type]
self.assertFalse(is_native_thai(""), False)
self.assertFalse(is_native_thai("116"), False)
self.assertFalse(is_native_thai("abc"), False)
Expand Down
10 changes: 5 additions & 5 deletions tests/core/test_soundex.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_soundex(self):
self.assertIsNotNone(soundex("a", engine="complete_soundex"))
self.assertIsNotNone(soundex("a", engine="XXX"))

self.assertEqual(lk82(None), "")
self.assertEqual(lk82(None), "") # type: ignore
self.assertEqual(lk82(""), "")
self.assertEqual(lk82("เหตุ"), lk82("เหด"))
self.assertEqual(lk82("รถ"), "ร3000")
Expand All @@ -43,13 +43,13 @@ def test_soundex(self):
self.assertIsNotNone(lk82("หืออือ"))
self.assertEqual(lk82("น์"), "")

self.assertEqual(udom83(None), "")
self.assertEqual(udom83(None), "") # type: ignore
self.assertEqual(udom83(""), "")
self.assertEqual(udom83("เหตุ"), udom83("เหด"))
self.assertEqual(udom83("รถ"), "ร800000")
self.assertEqual(udom83("น์"), "")

self.assertEqual(metasound(None), "")
self.assertEqual(metasound(None), "") # type: ignore
self.assertEqual(metasound(""), "")
self.assertEqual(metasound("เหตุ"), metasound("เหด"))
self.assertEqual(metasound("รักษ์"), metasound("รัก"))
Expand All @@ -70,7 +70,7 @@ def test_soundex(self):
self.assertIsNotNone(metasound("สุวรรณา"))
self.assertIsNotNone(metasound("ดอยบอย"))

self.assertEqual(prayut_and_somchaip(None), "")
self.assertEqual(prayut_and_somchaip(None), "") # type: ignore
self.assertEqual(prayut_and_somchaip(""), "")
self.assertEqual(prayut_and_somchaip("vp"), "11")
self.assertIsNotNone(prayut_and_somchaip("บา"))
Expand All @@ -86,7 +86,7 @@ def test_soundex(self):
self.assertIsNotNone(prayut_and_somchaip("ว้าว"))

# Test complete_soundex
self.assertEqual(complete_soundex(None), "")
self.assertEqual(complete_soundex(None), "") # type: ignore
self.assertEqual(complete_soundex(""), "")

# Single syllable test cases from the paper
Expand Down
18 changes: 9 additions & 9 deletions tests/core/test_spell.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class SpellTestCase(unittest.TestCase):
def test_spell(self):
self.assertEqual(spell(None), [""])
self.assertEqual(spell(None), [""]) # type: ignore
self.assertEqual(spell(""), [""])

result = spell("เน้ร")
Expand All @@ -29,7 +29,7 @@ def test_spell(self):
self.assertGreater(len(result), 0)

def test_word_correct(self):
self.assertEqual(correct(None), "")
self.assertEqual(correct(None), "") # type: ignore
self.assertEqual(correct(""), "")
self.assertEqual(correct("1"), "1")
self.assertEqual(correct("05"), "05")
Expand All @@ -53,7 +53,7 @@ def test_norvig_spell_checker(self):
self.assertGreater(dict_size, 30000) # Should have substantial words
self.assertLess(dict_size, len(orst) + 1000) # Should not exceed ORST by much

user_dict = [
user_list_tuple = [
("การงาน", 31), # longer than max_len
("กาม", 1), # fewer than min_freq
("กาล0", 64), # has digit
Expand All @@ -63,20 +63,20 @@ def test_norvig_spell_checker(self):
("การ", 42), # OK
]
checker = NorvigSpellChecker(
custom_dict=user_dict, min_freq=2, max_len=5
custom_dict=user_list_tuple, min_freq=2, max_len=5
)
self.assertEqual(len(checker.dictionary()), 1)

user_dict = [
user_list_str = [
"เอกราช",
"ปลอดภัย",
"เศรษฐกิจ",
"เสมอภาค",
"เสรีภาพ",
"การศึกษา",
]
checker = NorvigSpellChecker(custom_dict=user_dict)
self.assertEqual(len(checker.dictionary()), len(user_dict))
checker = NorvigSpellChecker(custom_dict=user_list_str)
self.assertEqual(len(checker.dictionary()), len(user_list_str))

user_dict = {
"พหลโยธิน": 1,
Expand All @@ -92,9 +92,9 @@ def test_norvig_spell_checker(self):
# as it has frequency less than default min_freq (2)
self.assertEqual(len(checker.dictionary()), len(user_dict) - 1)

user_dict = [24, 6, 2475]
user_list_int = [24, 6, 2475]
with self.assertRaises(TypeError):
_ = NorvigSpellChecker(custom_dict=user_dict)
_ = NorvigSpellChecker(custom_dict=user_list_int) # type: ignore[arg-type]

def test_issue_680_orst_filtering(self):
"""Test for issue #680: Spell checker uses only ORST words.
Expand Down
22 changes: 11 additions & 11 deletions tests/core/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUpClass(cls) -> None:
download("blackboard_unigram_tagger")

def test_pos_tag(self):
self.assertEqual(pos_tag(None), [])
self.assertEqual(pos_tag(None), []) # type: ignore[arg-type]
self.assertEqual(pos_tag([]), [])
self.assertEqual(
pos_tag(["นักเรียน", "ถาม", "ครู"]),
Expand All @@ -38,13 +38,13 @@ def test_pos_tag(self):
len(pos_tag(["การ", "เดินทาง", "มี", "ความ", "ท้าทาย"])), 5
)

self.assertEqual(unigram.tag(None, corpus="pud"), [])
self.assertEqual(unigram.tag(None, corpus="pud"), []) # type: ignore[arg-type]
self.assertEqual(unigram.tag([], corpus="pud"), [])
self.assertEqual(unigram.tag(None, corpus="orchid"), [])
self.assertEqual(unigram.tag(None, corpus="orchid"), []) # type: ignore[arg-type]
self.assertEqual(unigram.tag([], corpus="orchid"), [])
self.assertEqual(unigram.tag(None, corpus="blackboard"), [])
self.assertEqual(unigram.tag(None, corpus="blackboard"), []) # type: ignore[arg-type]
self.assertEqual(unigram.tag([], corpus="blackboard"), [])
self.assertEqual(unigram.tag(None, corpus="tud"), [])
self.assertEqual(unigram.tag(None, corpus="tud"), []) # type: ignore[arg-type]
self.assertEqual(unigram.tag([], corpus="tud"), [])
self.assertIsNotNone(
pos_tag(TEST_TOKENS, engine="unigram", corpus="orchid")
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_pos_tag(self):
pos_tag(["ความ", "พอเพียง"], corpus="orchid_ud")[0][1], "NOUN"
)

self.assertEqual(pos_tag_sents(None), [])
self.assertEqual(pos_tag_sents(None), []) # type: ignore[arg-type]
self.assertEqual(pos_tag_sents([]), [])
self.assertEqual(
pos_tag_sents([["ผม", "กิน", "ข้าว"], ["แมว", "วิ่ง"]]),
Expand Down Expand Up @@ -123,15 +123,15 @@ def setUpClass(cls) -> None:
download("blackboard_pt_tagger")

def test_perceptron_tagger(self):
self.assertEqual(perceptron.tag(None, corpus="orchid"), [])
self.assertEqual(perceptron.tag(None, corpus="orchid"), []) # type: ignore[arg-type]
self.assertEqual(perceptron.tag([], corpus="orchid"), [])
self.assertEqual(perceptron.tag(None, corpus="orchid_ud"), [])
self.assertEqual(perceptron.tag(None, corpus="orchid_ud"), []) # type: ignore[arg-type]
self.assertEqual(perceptron.tag([], corpus="orchid_ud"), [])
self.assertEqual(perceptron.tag(None, corpus="pud"), [])
self.assertEqual(perceptron.tag(None, corpus="pud"), []) # type: ignore[arg-type]
self.assertEqual(perceptron.tag([], corpus="pud"), [])
self.assertEqual(perceptron.tag(None, corpus="blackboard"), [])
self.assertEqual(perceptron.tag(None, corpus="blackboard"), []) # type: ignore[arg-type]
self.assertEqual(perceptron.tag([], corpus="blackboard"), [])
self.assertEqual(perceptron.tag(None, corpus="tud"), [])
self.assertEqual(perceptron.tag(None, corpus="tud"), []) # type: ignore[arg-type]
self.assertEqual(perceptron.tag([], corpus="tud"), [])

self.assertIsNotNone(
Expand Down
12 changes: 6 additions & 6 deletions tests/core/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def test_Tokenizer(self):
_tokenizer = Tokenizer(word_dict_trie())
self.assertEqual(_tokenizer.word_tokenize(""), [])
_tokenizer.set_tokenize_engine("longest")
self.assertEqual(_tokenizer.word_tokenize(None), [])
self.assertEqual(_tokenizer.word_tokenize(None), []) # type: ignore[arg-type]

_tokenizer = Tokenizer()
self.assertEqual(_tokenizer.word_tokenize("ก"), ["ก"])
Expand Down Expand Up @@ -335,7 +335,7 @@ def test_sent_tokenize(self):
sent_tokenize("ฉันไป กิน", engine="XX") # engine does not exist

def test_subword_tokenize(self):
self.assertEqual(subword_tokenize(None), [])
self.assertEqual(subword_tokenize(None), []) # type: ignore[arg-type]
self.assertEqual(subword_tokenize(""), [])
self.assertIsInstance(
subword_tokenize("สวัสดีดาวอังคาร", engine="tcc"), list
Expand All @@ -345,7 +345,7 @@ def test_subword_tokenize(self):
subword_tokenize("สวัสดีดาวอังคาร", engine="tcc_p"), list
)
self.assertNotIn("า", subword_tokenize("สวัสดีดาวอังคาร", engine="tcc_p"))
self.assertEqual(subword_tokenize(None, engine="etcc"), [])
self.assertEqual(subword_tokenize(None, engine="etcc"), []) # type: ignore[arg-type]
self.assertEqual(subword_tokenize("", engine="etcc"), [])
self.assertIsInstance(
subword_tokenize("สวัสดิีดาวอังคาร", engine="etcc"), list
Expand Down Expand Up @@ -495,7 +495,7 @@ def test_mm(self):
self.assertIsNotNone(
multi_cut.find_all_segment("รถไฟฟ้ากรุงเทพมหานครBTS")
)
self.assertEqual(multi_cut.find_all_segment(None), [])
self.assertEqual(multi_cut.find_all_segment(None), []) # type: ignore[arg-type]

def test_newmm(self):
assert_segment_handles_none_and_empty(self, newmm.segment)
Expand Down Expand Up @@ -686,8 +686,8 @@ def test_tcc_p(self):
self.assertEqual(tcc_p.tcc_pos(""), set())
# tcc_pos_array: edge cases
self.assertIsInstance(tcc_p.tcc_pos_array(""), bytearray)
self.assertIsInstance(tcc_p.tcc_pos_array(None), bytearray)
self.assertIsInstance(tcc_p.tcc_pos_array(42), bytearray)
self.assertIsInstance(tcc_p.tcc_pos_array(None), bytearray) # type: ignore[arg-type]
self.assertIsInstance(tcc_p.tcc_pos_array(42), bytearray) # type: ignore[arg-type]
# valid text: array length must equal len(text)+1 and mark boundaries
arr = tcc_p.tcc_pos_array("ประเทศ")
self.assertEqual(len(arr), len("ประเทศ") + 1)
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_transliterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@

class TransliterateTestCase(unittest.TestCase):
def test_romanize(self):
self.assertEqual(romanize(None), "")
self.assertEqual(romanize(None), "") # type: ignore[arg-type]
self.assertEqual(romanize(""), "")
self.assertEqual(romanize("แมว"), "maeo")

def test_romanize_royin_basic(self):
for word, expect in BASIC_TESTS.items():
self.assertEqual(romanize(word, engine="royin"), expect)
self.assertEqual(romanize(word, engine="royin"), expect) # type: ignore[arg-type]

def test_romanize_royin_consistency(self):
for word, part1, part2 in CONSISTENCY_TESTS:
Expand Down
Loading
Loading