diff --git a/pythainlp/benchmarks/word_tokenization.py b/pythainlp/benchmarks/word_tokenization.py index 376ec5190..2992f3d9c 100644 --- a/pythainlp/benchmarks/word_tokenization.py +++ b/pythainlp/benchmarks/word_tokenization.py @@ -196,7 +196,7 @@ def compute_stats(ref_sample: str, raw_sample: str) -> dict: } -def _binary_representation(txt: str, verbose: bool = False): +def _binary_representation(txt: str, verbose: bool = False) -> np.ndarray: """Transform text into {0, 1} sequence. where (1) indicates that the corresponding character is the beginning of diff --git a/pythainlp/coref/core.py b/pythainlp/coref/core.py index be6764966..d0a6301c6 100644 --- a/pythainlp/coref/core.py +++ b/pythainlp/coref/core.py @@ -8,7 +8,7 @@ def coreference_resolution( texts: list[str], model_name: str = "han-coref-v1.0", device: str = "cpu" -): +) -> list[dict]: """Coreference Resolution :param List[str] texts: list of texts to apply coreference resolution to diff --git a/pythainlp/spell/core.py b/pythainlp/spell/core.py index 2cca2dc81..d3a07bf26 100644 --- a/pythainlp/spell/core.py +++ b/pythainlp/spell/core.py @@ -8,12 +8,16 @@ import itertools from functools import lru_cache +from typing import TYPE_CHECKING from pythainlp.spell import DEFAULT_SPELL_CHECKER +if TYPE_CHECKING: + from pythainlp.spell.pn import NorvigSpellChecker + @lru_cache -def default_spell_checker(): +def default_spell_checker() -> "NorvigSpellChecker": """Lazy load default spell checker with cache""" return DEFAULT_SPELL_CHECKER() diff --git a/pythainlp/spell/symspellpy.py b/pythainlp/spell/symspellpy.py index 32919b75a..57d02d8ce 100644 --- a/pythainlp/spell/symspellpy.py +++ b/pythainlp/spell/symspellpy.py @@ -33,7 +33,7 @@ _load_lock = threading.Lock() # Thread safety for lazy loading -def _get_sym_spell(): +def _get_sym_spell() -> SymSpell: """Lazy load the symspell instance. This function uses a lock to ensure thread-safe initialization. diff --git a/pythainlp/tag/perceptron.py b/pythainlp/tag/perceptron.py index c856a864b..11dc63417 100644 --- a/pythainlp/tag/perceptron.py +++ b/pythainlp/tag/perceptron.py @@ -32,21 +32,21 @@ _TUD_TAGGER = None -def _orchid_tagger(): +def _orchid_tagger() -> PerceptronTagger: global _ORCHID_TAGGER if not _ORCHID_TAGGER: _ORCHID_TAGGER = PerceptronTagger(path=_ORCHID_PATH) return _ORCHID_TAGGER -def _pud_tagger(): +def _pud_tagger() -> PerceptronTagger: global _PUD_TAGGER if not _PUD_TAGGER: _PUD_TAGGER = PerceptronTagger(path=_PUD_PATH) return _PUD_TAGGER -def _blackboard_tagger(): +def _blackboard_tagger() -> PerceptronTagger: global _BLACKBOARD_TAGGER if not _BLACKBOARD_TAGGER: path = get_corpus_path(_BLACKBOARD_NAME) @@ -54,14 +54,14 @@ def _blackboard_tagger(): return _BLACKBOARD_TAGGER -def _tdtb(): +def _tdtb() -> PerceptronTagger: global _TDTB_TAGGER if not _TDTB_TAGGER: _TDTB_TAGGER = PerceptronTagger(path=_TDTB_PATH) return _TDTB_TAGGER -def _tud_tagger(): +def _tud_tagger() -> PerceptronTagger: global _TUD_TAGGER if not _TUD_TAGGER: _TUD_TAGGER = PerceptronTagger(path=_TUD_PATH) diff --git a/pythainlp/tag/thainer.py b/pythainlp/tag/thainer.py index 6e5a5c654..b139a0204 100644 --- a/pythainlp/tag/thainer.py +++ b/pythainlp/tag/thainer.py @@ -201,5 +201,5 @@ def get_ner( return sent_ner @staticmethod - def __extract_features(doc: list[str]) -> list[dict[str, str | bool]]: + def __extract_features(doc: list[str]) -> list[dict[str, Union[str, bool]]]: return [_doc2features(doc, i) for i in range(len(doc))] diff --git a/pythainlp/tag/unigram.py b/pythainlp/tag/unigram.py index ea3fa3c43..2c99a3a20 100644 --- a/pythainlp/tag/unigram.py +++ b/pythainlp/tag/unigram.py @@ -33,7 +33,7 @@ _TUD_TAGGER = None -def _orchid_tagger(): +def _orchid_tagger() -> dict: global _ORCHID_TAGGER if not _ORCHID_TAGGER: with open(_ORCHID_PATH, encoding="utf-8-sig") as fh: @@ -41,7 +41,7 @@ def _orchid_tagger(): return _ORCHID_TAGGER -def _pud_tagger(): +def _pud_tagger() -> dict: global _PUD_TAGGER if not _PUD_TAGGER: with open(_PUD_PATH, encoding="utf-8-sig") as fh: @@ -49,7 +49,7 @@ def _pud_tagger(): return _PUD_TAGGER -def _blackboard_tagger(): +def _blackboard_tagger() -> dict: global _BLACKBOARD_TAGGER if not _BLACKBOARD_TAGGER: path = get_corpus_path(_BLACKBOARD_NAME) @@ -58,7 +58,7 @@ def _blackboard_tagger(): return _BLACKBOARD_TAGGER -def _thai_tdtb(): +def _thai_tdtb() -> dict: global _TDTB_TAGGER if not _TDTB_TAGGER: with open(_TDTB_PATH, encoding="utf-8-sig") as fh: @@ -66,7 +66,7 @@ def _thai_tdtb(): return _TDTB_TAGGER -def _tud_tagger(): +def _tud_tagger() -> dict: global _TUD_TAGGER if not _TUD_TAGGER: with open(_TUD_PATH, encoding="utf-8-sig") as fh: diff --git a/pythainlp/tokenize/__init__.py b/pythainlp/tokenize/__init__.py index 4421d49dd..9799dd213 100644 --- a/pythainlp/tokenize/__init__.py +++ b/pythainlp/tokenize/__init__.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 """Tokenizers at different levels of linguistic analysis. """ +from __future__ import annotations __all__ = [ "thai2fit_tokenizer", diff --git a/pythainlp/tokenize/han_solo.py b/pythainlp/tokenize/han_solo.py index d2a0cc3d7..c94c78f79 100644 --- a/pythainlp/tokenize/han_solo.py +++ b/pythainlp/tokenize/han_solo.py @@ -10,6 +10,7 @@ import threading from importlib.resources import as_file, files +from typing import Optional try: import pycrfsuite @@ -47,7 +48,7 @@ def _get_tagger() -> pycrfsuite.Tagger: class Featurizer: # This class from ssg at https://github.com/ponrawee/ssg. - def __init__(self, N: int = 2, sequence_size: int = 1, delimiter: str | None = None) -> None: + def __init__(self, N: int = 2, sequence_size: int = 1, delimiter: Optional[str] = None) -> None: self.N = N self.delimiter = delimiter self.radius = N + sequence_size diff --git a/pythainlp/transliterate/core.py b/pythainlp/transliterate/core.py index 112d0bd4d..758ad6f95 100644 --- a/pythainlp/transliterate/core.py +++ b/pythainlp/transliterate/core.py @@ -3,6 +3,8 @@ # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations +from typing import Callable + DEFAULT_ROMANIZE_ENGINE = "royin" DEFAULT_TRANSLITERATE_ENGINE = "thaig2p" DEFAULT_PRONUNCIATE_ENGINE = "w2p" @@ -70,7 +72,7 @@ def romanize( """ - def select_romanize_engine(engine: str): + def select_romanize_engine(engine: str) -> Callable[[str], str]: if engine == "thai2rom": from pythainlp.transliterate.thai2rom import romanize elif engine == "thai2rom_onnx": diff --git a/pythainlp/word_vector/core.py b/pythainlp/word_vector/core.py index 9bfd19e0a..1c61ed0cc 100644 --- a/pythainlp/word_vector/core.py +++ b/pythainlp/word_vector/core.py @@ -43,7 +43,7 @@ def __init__(self, model_name: str = "thai2fit_wv") -> None: """ self.load_wordvector(model_name) - def load_wordvector(self, model_name: str): + def load_wordvector(self, model_name: str) -> None: """Load word vector model. :param str model_name: model name