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 pythainlp/benchmarks/word_tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/coref/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pythainlp/spell/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pythainlp/spell/symspellpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions pythainlp/tag/perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,36 @@
_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)
_BLACKBOARD_TAGGER = PerceptronTagger(path=path)
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)
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/tag/thainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
10 changes: 5 additions & 5 deletions pythainlp/tag/unigram.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@
_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:
_ORCHID_TAGGER = json.load(fh)
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:
_PUD_TAGGER = json.load(fh)
return _PUD_TAGGER


def _blackboard_tagger():
def _blackboard_tagger() -> dict:
global _BLACKBOARD_TAGGER
if not _BLACKBOARD_TAGGER:
path = get_corpus_path(_BLACKBOARD_NAME)
Expand All @@ -58,15 +58,15 @@ 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:
_TDTB_TAGGER = json.load(fh)
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:
Expand Down
1 change: 1 addition & 0 deletions pythainlp/tokenize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
"""Tokenizers at different levels of linguistic analysis.
"""
from __future__ import annotations

__all__ = [
"thai2fit_tokenizer",
Expand Down
3 changes: 2 additions & 1 deletion pythainlp/tokenize/han_solo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import threading
from importlib.resources import as_file, files
from typing import Optional

try:
import pycrfsuite
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pythainlp/transliterate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/word_vector/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading