diff --git a/pythainlp/augment/lm/phayathaibert.py b/pythainlp/augment/lm/phayathaibert.py index eada2f975..1cae15bc6 100644 --- a/pythainlp/augment/lm/phayathaibert.py +++ b/pythainlp/augment/lm/phayathaibert.py @@ -5,7 +5,7 @@ import random import re -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING if TYPE_CHECKING: from transformers import AutoModelForMaskedLM, AutoTokenizer, Pipeline diff --git a/pythainlp/augment/lm/wangchanberta.py b/pythainlp/augment/lm/wangchanberta.py index 49ee6ecd2..b2e71da50 100644 --- a/pythainlp/augment/lm/wangchanberta.py +++ b/pythainlp/augment/lm/wangchanberta.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING if TYPE_CHECKING: from transformers import CamembertTokenizer, Pipeline diff --git a/pythainlp/augment/word2vec/bpemb_wv.py b/pythainlp/augment/word2vec/bpemb_wv.py index 5e43bbdee..50a743372 100644 --- a/pythainlp/augment/word2vec/bpemb_wv.py +++ b/pythainlp/augment/word2vec/bpemb_wv.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING from pythainlp.augment.word2vec.core import Word2VecAug diff --git a/pythainlp/augment/word2vec/core.py b/pythainlp/augment/word2vec/core.py index 2a177625b..23185614e 100644 --- a/pythainlp/augment/word2vec/core.py +++ b/pythainlp/augment/word2vec/core.py @@ -4,7 +4,7 @@ from __future__ import annotations import itertools -from typing import TYPE_CHECKING, Any, Callable +from typing import TYPE_CHECKING, Callable if TYPE_CHECKING: from gensim.models.keyedvectors import KeyedVectors diff --git a/pythainlp/corpus/core.py b/pythainlp/corpus/core.py index ad6bf539b..3b3a28bd0 100644 --- a/pythainlp/corpus/core.py +++ b/pythainlp/corpus/core.py @@ -11,7 +11,7 @@ import sys import tarfile import zipfile -from http.client import HTTPResponse +from http.client import HTTPMessage, HTTPResponse from importlib.resources import files from typing import TYPE_CHECKING @@ -34,7 +34,7 @@ class _ResponseWrapper: """Wrapper to provide requests.Response-like interface for urllib response.""" status_code: int - headers: "http.client.HTTPMessage" + headers: HTTPMessage _content: bytes def __init__(self, response: HTTPResponse) -> None: diff --git a/pythainlp/el/_multiel.py b/pythainlp/el/_multiel.py index 135d4c763..f771b28b1 100644 --- a/pythainlp/el/_multiel.py +++ b/pythainlp/el/_multiel.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations -from typing import TYPE_CHECKING, Any, Union +from typing import TYPE_CHECKING, Union if TYPE_CHECKING: from multiel import BELA diff --git a/pythainlp/morpheme/thaiwordcheck.py b/pythainlp/morpheme/thaiwordcheck.py index 50f4eb44b..8b0ba48d7 100644 --- a/pythainlp/morpheme/thaiwordcheck.py +++ b/pythainlp/morpheme/thaiwordcheck.py @@ -17,7 +17,6 @@ from __future__ import annotations import re -from typing import Pattern _THANTHAKHAT_CHAR: str = "\u0e4c" # Thanthakhat (cancellation of sound) diff --git a/pythainlp/parse/transformers_ud.py b/pythainlp/parse/transformers_ud.py index fc5aac53c..8d19a00f5 100644 --- a/pythainlp/parse/transformers_ud.py +++ b/pythainlp/parse/transformers_ud.py @@ -16,8 +16,8 @@ if TYPE_CHECKING: from transformers import ( - AutoTokenizer, AutoModelForQuestionAnswering, + AutoTokenizer, TokenClassificationPipeline, ) diff --git a/pythainlp/spell/__init__.py b/pythainlp/spell/__init__.py index 6c78dfa9c..f13057bad 100644 --- a/pythainlp/spell/__init__.py +++ b/pythainlp/spell/__init__.py @@ -13,9 +13,10 @@ "get_words_spell_suggestion", ] -from pythainlp.spell.pn import NorvigSpellChecker from typing import Type +from pythainlp.spell.pn import NorvigSpellChecker + DEFAULT_SPELL_CHECKER: Type[NorvigSpellChecker] = NorvigSpellChecker # these imports are placed here to avoid circular imports diff --git a/pythainlp/spell/words_spelling_correction.py b/pythainlp/spell/words_spelling_correction.py index 6180b2718..81ab73f77 100644 --- a/pythainlp/spell/words_spelling_correction.py +++ b/pythainlp/spell/words_spelling_correction.py @@ -58,11 +58,13 @@ def __init__( """ try: - import numpy as np + import numpy as np # noqa: F401 except ModuleNotFoundError: - raise ModuleNotFoundError(""" + raise ModuleNotFoundError( + """ Please installing the package via 'pip install numpy onnxruntime'. - """) + """ + ) except Exception as e: raise RuntimeError(f"An unexpected error occurred: {e}") from e self.model_dir = model_dir @@ -81,7 +83,7 @@ def __init__( def _load_embeddings(self) -> tuple[list[str], NDArray[np.float32]]: """Loads embeddings matrix and vocabulary list.""" import numpy as np - + input_matrix = np.load( os.path.join(self.model_dir, "embeddings.npy") ) @@ -95,7 +97,7 @@ def _load_embeddings(self) -> tuple[list[str], NDArray[np.float32]]: def _load_suggestion_words(self, words_list: list[str]) -> NDArray[np.str_]: """Loads the list of words used for suggestions.""" import numpy as np - + words = np.array(words_list) return words @@ -152,7 +154,7 @@ def _get_subwords(self, word: str) -> tuple[list[str], NDArray[np.int_]]: def get_word_vector(self, word: str) -> NDArray[np.float32]: """Computes the normalized vector for a single word.""" import numpy as np - + # subword_ids[1] contains the array of indices for the word and its subwords subword_ids = self._get_subwords(word)[1] @@ -193,7 +195,7 @@ def _tokenize(self, sentence: str) -> list[str]: def get_sentence_vector(self, line: str) -> NDArray[np.float32]: """Computes the mean vector for a sentence.""" import numpy as np - + tokens = self._tokenize(line) vectors = [] for t in tokens: diff --git a/pythainlp/translate/small100.py b/pythainlp/translate/small100.py index df69611fe..852b7750a 100644 --- a/pythainlp/translate/small100.py +++ b/pythainlp/translate/small100.py @@ -6,8 +6,8 @@ from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: - from transformers import M2M100ForConditionalGeneration import torch + from transformers import M2M100ForConditionalGeneration from .tokenization_small100 import SMALL100Tokenizer diff --git a/pythainlp/translate/th_fr.py b/pythainlp/translate/th_fr.py index 3c5c50e4e..a78f8e1ca 100644 --- a/pythainlp/translate/th_fr.py +++ b/pythainlp/translate/th_fr.py @@ -14,11 +14,11 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING if TYPE_CHECKING: - from transformers import AutoModelForSeq2SeqLM, AutoTokenizer import torch + from transformers import AutoModelForSeq2SeqLM, AutoTokenizer class ThFrTranslator: diff --git a/pythainlp/translate/tokenization_small100.py b/pythainlp/translate/tokenization_small100.py index 86a2f9155..0d9cdc104 100644 --- a/pythainlp/translate/tokenization_small100.py +++ b/pythainlp/translate/tokenization_small100.py @@ -27,7 +27,7 @@ import os from pathlib import Path from shutil import copyfile -from typing import TYPE_CHECKING, Optional, Union, cast +from typing import TYPE_CHECKING, Any, Optional, Union, cast if TYPE_CHECKING: from sentencepiece import SentencePieceProcessor @@ -439,7 +439,7 @@ def load_spm( path: str, sp_model_kwargs: dict[str, str] ) -> SentencePieceProcessor: import sentencepiece - + spm = sentencepiece.SentencePieceProcessor(**sp_model_kwargs) spm.Load(str(path)) return spm diff --git a/pythainlp/translate/zh_th.py b/pythainlp/translate/zh_th.py index 52a331d98..f3bd33b3d 100644 --- a/pythainlp/translate/zh_th.py +++ b/pythainlp/translate/zh_th.py @@ -11,11 +11,11 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING if TYPE_CHECKING: - from transformers import AutoModelForSeq2SeqLM, AutoTokenizer import torch + from transformers import AutoModelForSeq2SeqLM, AutoTokenizer class ThZhTranslator: diff --git a/pythainlp/transliterate/thaig2p_v2.py b/pythainlp/transliterate/thaig2p_v2.py index 5e663fc50..c8fbadc95 100644 --- a/pythainlp/transliterate/thaig2p_v2.py +++ b/pythainlp/transliterate/thaig2p_v2.py @@ -9,7 +9,7 @@ # Use a pipeline as a high-level helper from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING if TYPE_CHECKING: from transformers import Pipeline diff --git a/pythainlp/transliterate/umt5_thaig2p.py b/pythainlp/transliterate/umt5_thaig2p.py index aec751187..593823683 100644 --- a/pythainlp/transliterate/umt5_thaig2p.py +++ b/pythainlp/transliterate/umt5_thaig2p.py @@ -9,7 +9,7 @@ # Use a pipeline as a high-level helper from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING if TYPE_CHECKING: from transformers import Pipeline diff --git a/pythainlp/util/emojiconv.py b/pythainlp/util/emojiconv.py index 8014c2c94..73067ff81 100644 --- a/pythainlp/util/emojiconv.py +++ b/pythainlp/util/emojiconv.py @@ -7,7 +7,6 @@ from __future__ import annotations import re -from typing import Pattern _emoji_th: dict[str, str] = { "😀": "หน้ายิ้มยิงฟัน", diff --git a/pythainlp/util/syllable.py b/pythainlp/util/syllable.py index 1e14b601d..14d5e234e 100644 --- a/pythainlp/util/syllable.py +++ b/pythainlp/util/syllable.py @@ -6,7 +6,6 @@ from __future__ import annotations import re - from typing import Pattern from pythainlp import thai_consonants, thai_tonemarks diff --git a/pythainlp/wangchanberta/core.py b/pythainlp/wangchanberta/core.py index 7f1ffb32a..9f1d36306 100644 --- a/pythainlp/wangchanberta/core.py +++ b/pythainlp/wangchanberta/core.py @@ -8,7 +8,11 @@ from typing import TYPE_CHECKING, Union if TYPE_CHECKING: - from transformers import CamembertTokenizer, PreTrainedModel, PreTrainedTokenizerBase + from transformers import ( + CamembertTokenizer, + PreTrainedModel, + PreTrainedTokenizerBase, + ) from transformers.pipelines import TokenClassificationPipeline from pythainlp.tokenize import word_tokenize