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
8 changes: 5 additions & 3 deletions pythainlp/corpus/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Union
from typing import Any, Union

__all__ = [
"countries",
Expand Down Expand Up @@ -384,10 +384,12 @@ def find_synonyms(word: str) -> list[str]:
# output: ['จรุก', 'วราหะ', 'วราห์', 'ศูกร', 'สุกร']
"""
synonyms = thai_synonyms() # get a dictionary of {word, synonym}
list_synonym = []
list_synonym: list[Any] = []

if word in synonyms["word"]: # find by word
list_synonym.extend(synonyms["synonym"][synonyms["word"].index(word)])
word_list = synonyms["word"]
if isinstance(word_list, list):
list_synonym.extend(synonyms["synonym"][word_list.index(word)]) # type: ignore[arg-type]

for idx, words in enumerate(synonyms["synonym"]): # find by synonym
if word in words:
Expand Down
32 changes: 16 additions & 16 deletions pythainlp/corpus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pythainlp.tools import get_full_data_path

if TYPE_CHECKING:
from typing import Optional
from typing import Any, Optional

_CHECK_MODE = os.getenv("PYTHAINLP_READ_MODE")
_USER_AGENT = (
Expand All @@ -38,10 +38,10 @@ def __init__(self, response: HTTPResponse) -> None:
self.headers = response.headers
self._content = response.read()

def json(self) -> dict[str, str]:
def json(self) -> dict[str, Any]:
"""Parse JSON content from response."""
try:
return json.loads(self._content.decode("utf-8"))
return json.loads(self._content.decode("utf-8")) # type: ignore[no-any-return]
except (json.JSONDecodeError, UnicodeDecodeError) as err:
raise ValueError(f"Failed to parse JSON response: {err}") from err

Expand Down Expand Up @@ -73,7 +73,7 @@ def get_corpus_db(url: str) -> Optional[_ResponseWrapper]:
return corpus_db


def get_corpus_db_detail(name: str, version: str = "") -> dict[str, str]:
def get_corpus_db_detail(name: str, version: str = "") -> dict[str, Any]:
"""Get details about a corpus, using information from local catalog.

:param str name: name of corpus
Expand All @@ -86,11 +86,11 @@ def get_corpus_db_detail(name: str, version: str = "") -> dict[str, str]:
if not version:
for corpus in local_db["_default"].values():
if corpus["name"] == name:
return corpus
return corpus # type: ignore[no-any-return]
else:
for corpus in local_db["_default"].values():
if corpus["name"] == name and corpus["version"] == version:
return corpus
return corpus # type: ignore[no-any-return]

return {}

Expand Down Expand Up @@ -550,14 +550,14 @@ def _check_version(cause: str) -> bool:
temp = cause.replace(">", "")
check = v > _version2int(temp)
elif cause.startswith(">=") and "<=" not in cause and "<" in cause:
temp = cause.replace(">=", "").split("<")
check = _version2int(temp[0]) <= v < _version2int(temp[1])
temp_parts = cause.replace(">=", "").split("<")
check = _version2int(temp_parts[0]) <= v < _version2int(temp_parts[1])
elif cause.startswith(">=") and "<=" in cause:
temp = cause.replace(">=", "").split("<=")
check = _version2int(temp[0]) <= v <= _version2int(temp[1])
temp_parts = cause.replace(">=", "").split("<=")
check = _version2int(temp_parts[0]) <= v <= _version2int(temp_parts[1])
elif cause.startswith(">") and "<" in cause:
temp = cause.replace(">", "").split("<")
check = _version2int(temp[0]) < v < _version2int(temp[1])
temp_parts = cause.replace(">", "").split("<")
check = _version2int(temp_parts[0]) < v < _version2int(temp_parts[1])
elif cause.startswith("<="):
temp = cause.replace("<=", "")
check = v <= _version2int(temp[0])
Expand Down Expand Up @@ -608,14 +608,14 @@ def download(name: str, force: bool = False, url: str = "", version: str = "") -
print(f"Cannot download corpus catalog from: {url}")
return False

corpus_db = corpus_db.json()
corpus_db_dict = corpus_db.json()

# check if corpus is available
if name in corpus_db:
if name in corpus_db_dict:
with open(corpus_db_path(), encoding="utf-8-sig") as f:
local_db = json.load(f)

corpus = corpus_db[name]
corpus = corpus_db_dict[name]
print("Corpus:", name)
if not version:
for v, file in corpus["versions"].items():
Expand Down Expand Up @@ -842,4 +842,4 @@ def get_hf_hub(repo_id: str, filename: str = "") -> str:
)
else:
output_path = snapshot_download(repo_id=repo_id, local_dir=root_project)
return output_path
return output_path # type: ignore[no-any-return]
6 changes: 3 additions & 3 deletions pythainlp/tag/named_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def load_engine(self, engine: str, corpus: str) -> None:
model="pythainlp/thainer-corpus-v2-base-model"
)
elif engine == "wangchanberta":
from pythainlp.wangchanberta import ThaiNameTagger as WangchanbertaThaiNameTagger # type: ignore[assignment] # noqa: I001,E501
from pythainlp.wangchanberta import ThaiNameTagger as WangchanbertaThaiNameTagger # noqa: I001,E501

self.engine = WangchanbertaThaiNameTagger(dataset_name=corpus) # type: ignore[call-arg]
self.engine = WangchanbertaThaiNameTagger(dataset_name=corpus)
elif corpus == "thainer-v2":
if engine == "phayathaibert":
from pythainlp.phayathaibert.core import NamedEntityTagger
Expand Down Expand Up @@ -101,7 +101,7 @@ def tag(
>>> ner.tag("ทดสอบ นายวรรณพงษ์ ภัททิยไพบูลย์", tag=True)
'ทดสอบ <PERSON>นายวรรณพงษ์ ภัททิยไพบูลย์</PERSON>'
"""
return self.engine.get_ner(text, tag=tag, pos=pos) # type: ignore[union-attr]
return self.engine.get_ner(text, tag=tag, pos=pos) # type: ignore[no-any-return]


class NNER:
Expand Down
10 changes: 5 additions & 5 deletions pythainlp/tag/unigram.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def _orchid_tagger() -> dict:
if not _ORCHID_TAGGER:
with open(_ORCHID_PATH, encoding="utf-8-sig") as fh:
_ORCHID_TAGGER = json.load(fh)
return _ORCHID_TAGGER
return _ORCHID_TAGGER # type: ignore[no-any-return]


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
return _PUD_TAGGER # type: ignore[no-any-return]


def _blackboard_tagger() -> dict:
Expand All @@ -57,23 +57,23 @@ def _blackboard_tagger() -> dict:
raise ValueError(f"Corpus path not found for {_BLACKBOARD_NAME}")
with open(path, encoding="utf-8-sig") as fh:
_BLACKBOARD_TAGGER = json.load(fh)
return _BLACKBOARD_TAGGER
return _BLACKBOARD_TAGGER # type: ignore[no-any-return]


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
return _TDTB_TAGGER # type: ignore[no-any-return]


def _tud_tagger() -> dict:
global _TUD_TAGGER
if not _TUD_TAGGER:
with open(_TUD_PATH, encoding="utf-8-sig") as fh:
_TUD_TAGGER = json.load(fh)
return _TUD_TAGGER
return _TUD_TAGGER # type: ignore[no-any-return]


def _find_tag(
Expand Down
14 changes: 7 additions & 7 deletions pythainlp/transliterate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,19 @@ def transliterate(
if engine in ("icu", "pyicu"):
from pythainlp.transliterate.pyicu import transliterate
elif engine == "ipa":
from pythainlp.transliterate.ipa import transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.transliterate.ipa import transliterate # noqa: I001
elif engine == "tltk_g2p":
from pythainlp.transliterate.tltk import tltk_g2p as transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.transliterate.tltk import tltk_g2p as transliterate # noqa: I001
elif engine == "tltk_ipa":
from pythainlp.transliterate.tltk import tltk_ipa as transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.transliterate.tltk import tltk_ipa as transliterate # noqa: I001
elif engine == "iso_11940":
from pythainlp.transliterate.iso_11940 import transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.transliterate.iso_11940 import transliterate # type: ignore[assignment] # noqa: I001
elif engine == "thaig2p_v2":
from pythainlp.transliterate.thaig2p_v2 import transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.transliterate.thaig2p_v2 import transliterate # noqa: I001
elif engine == "umt5_thaig2p":
from pythainlp.translate.umt5_thaig2p import transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.translate.umt5_thaig2p import transliterate # type: ignore[import-not-found, no-redef] # noqa: I001
else: # use default engine: "thaig2p"
from pythainlp.transliterate.thaig2p import transliterate # type: ignore[no-redef] # noqa: I001
from pythainlp.transliterate.thaig2p import transliterate # noqa: I001

return transliterate(text)

Expand Down
6 changes: 3 additions & 3 deletions pythainlp/transliterate/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def follow_rtgs(text: str) -> Optional[bool]:
except IndexError:
return None
else:
return follow
return follow # type: ignore[return-value]


def _romanize(text: str, fallback_func: Callable[[str], str]) -> str:
Expand All @@ -49,9 +49,9 @@ def _romanize(text: str, fallback_func: Callable[[str], str]) -> str:
except IndexError:
return fallback_func(text)
except TypeError as e:
raise TypeError(f"`fallback_engine` is not callable. {e}")
raise TypeError(f"`fallback_engine` is not callable. {e}") from e
else:
return lookup
return lookup # type: ignore[return-value]


def romanize(text: str, fallback_func: Callable[[str], str]) -> str:
Expand Down
6 changes: 3 additions & 3 deletions pythainlp/util/spell_words.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
for i, j in zip(list(thai_tonemarks), ["เอก", "โท", "ตรี", "จัตวา"])
}

rule1 = [i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1]
rule2 = [i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2]
rule3 = [
rule1: list[str] = [i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1]
rule2: list[str] = [i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2]
rule3: list[str] = [
i.replace("–", f"([{thai_letters}])").replace(":", f"([{thai_tonemarks}])")
for i in _r2
]
Expand Down
Loading