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
535 changes: 519 additions & 16 deletions build_tools/analysis/output/type_hint_analysis.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pythainlp/benchmarks/word_tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def benchmark(ref_samples: list[str], samples: list[str]) -> "pd.DataFrame":
r, s = preprocessing(r), preprocessing(s)
if r and s:
stats = compute_stats(r, s)
stats = _flatten_result(stats)
stats["expected"] = r
stats["actual"] = s
results.append(stats)
flat_stats: dict[str, Union[int, str]] = _flatten_result(stats)
flat_stats["expected"] = r
flat_stats["actual"] = s
results.append(flat_stats)
except:
reason = """
[Error]
Expand Down
4 changes: 2 additions & 2 deletions pythainlp/chat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

from typing import TYPE_CHECKING, Optional, cast
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
import torch
Expand Down Expand Up @@ -97,6 +97,6 @@ def chat(self, text: str) -> str:
_temp += self.model.PROMPT_DICT["prompt_chatbot"].format_map(
{"human": text, "bot": ""}
)
_bot = cast(str, self.model.gen_instruct(_temp))
_bot = self.model.gen_instruct(_temp)
self.history.append((text, _bot))
return _bot
6 changes: 3 additions & 3 deletions pythainlp/summarize/keybert.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def embed(self, docs: Union[str, list[str]]) -> np.ndarray:
[np.array(emb[0]).mean(axis=0) for emb in embs]
)

return emb_mean # type: ignore[no-any-return]
return emb_mean


def _generate_ngrams(
Expand Down Expand Up @@ -224,10 +224,10 @@ def l2_norm(v: np.ndarray) -> np.ndarray:
)
if not np.isclose(np.linalg.norm(result, axis=1), 1).all():
raise ValueError("Cannot normalize a vector to unit vector.")
return result # type: ignore[no-any-return]
return result

def cosine_sim(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return (np.matmul(a, b.T).T).sum(axis=1) # type: ignore[no-any-return]
return (np.matmul(a, b.T).T).sum(axis=1)

doc_vector = l2_norm(doc_vector)
word_vectors = l2_norm(word_vectors)
Expand Down
6 changes: 4 additions & 2 deletions pythainlp/tag/named_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def tag(
>>> ner.tag("ทดสอบ นายวรรณพงษ์ ภัททิยไพบูลย์", tag=True)
'ทดสอบ <PERSON>นายวรรณพงษ์ ภัททิยไพบูลย์</PERSON>'
"""
return self.engine.get_ner(text, tag=tag, pos=pos) # type: ignore[no-any-return]
if self.engine is None:
raise RuntimeError("Engine not initialized")
return self.engine.get_ner(text, tag=tag, pos=pos)


class NNER:
Expand Down Expand Up @@ -223,4 +225,4 @@ def tag(
>>> nner.tag("แมวทำอะไรตอนห้าโมงเช้า", top_level_only=True)
([...], [{'text': ['', 'ห้า', '', 'โมง'], 'span': [7, 11], 'entity_type': 'time'}])
"""
return self.engine.tag(text, top_level_only=top_level_only) # type: ignore[no-any-return]
return self.engine.tag(text, top_level_only=top_level_only)
2 changes: 1 addition & 1 deletion pythainlp/tag/wangchanberta_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def postprocess(self, logits_data: "np.ndarray") -> "np.ndarray":
maxes = np.max(logits_t, axis=-1, keepdims=True)
shifted_exp = np.exp(logits_t - maxes)
scores = shifted_exp / shifted_exp.sum(axis=-1, keepdims=True)
return scores # type: ignore[no-any-return]
return scores

def clean_output(
self, list_text: list[tuple[str, str]]
Expand Down
9 changes: 7 additions & 2 deletions pythainlp/tokenize/nlpo3.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def load_dict(file_path: str, dict_name: str) -> bool:
"nlpo3 is not installed. Install it with: pip install nlpo3"
) from ex

msg, success = nlpo3_load_dict(file_path=file_path, dict_name=dict_name)
msg: str
success: bool
Comment thread
bact marked this conversation as resolved.
msg, success = nlpo3_load_dict(
file_path=file_path, dict_name=dict_name
)
Comment thread
bact marked this conversation as resolved.
if not success:
print(msg, file=stderr)
return success
Expand Down Expand Up @@ -127,9 +131,10 @@ def segment(
if custom_dict == _NLPO3_DEFAULT_DICT_NAME:
_ensure_default_dict_loaded()

return nlpo3_segment(
result: list[str] = nlpo3_segment(
text=text,
dict_name=custom_dict,
safe=safe_mode,
parallel=parallel_mode,
)
return result
2 changes: 1 addition & 1 deletion pythainlp/tools/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
if version_info >= (3, 11):
from importlib.resources import files # Available in Python 3.11+
else:
from importlib_resources import files # noqa: I001
from importlib_resources import files # type: ignore[no-redef] # noqa: I001

PYTHAINLP_DEFAULT_DATA_DIR: str = "pythainlp-data"

Expand Down
2 changes: 1 addition & 1 deletion pythainlp/transliterate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def transliterate(
elif engine == "thaig2p_v2":
from pythainlp.transliterate.thaig2p_v2 import transliterate # noqa: I001
elif engine == "umt5_thaig2p":
from pythainlp.translate.umt5_thaig2p import transliterate # type: ignore[import-untyped,no-redef] # noqa: I001
from pythainlp.translate.umt5_thaig2p import transliterate # type: ignore[no-redef] # noqa: I001
else: # use default engine: "thaig2p"
from pythainlp.transliterate.thaig2p import transliterate # noqa: I001

Expand Down
8 changes: 4 additions & 4 deletions pythainlp/transliterate/thai2rom_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _prepare_sequence_in(self, text: str) -> "np.ndarray":
else:
idxs.append(self._char_to_ix["<UNK>"])
idxs.append(self._char_to_ix["<end>"])
return np.array(idxs) # type: ignore[no-any-return]
return np.array(idxs)

def romanize(self, text: str) -> str:
""":param str text: Thai text to be romanized
Expand Down Expand Up @@ -131,7 +131,7 @@ def __init__(

def create_mask(self, source_seq: "np.ndarray") -> "np.ndarray":
mask = source_seq != self.pad_idx
return mask # type: ignore[no-any-return]
return mask

def run(
self, source_seq: "np.ndarray", source_seq_len: List[int]
Expand Down Expand Up @@ -196,9 +196,9 @@ def run(
decoder_input = np.array([topi])

if decoder_input == end_token:
return outputs[:di] # type: ignore[no-any-return]
return outputs[:di]

return outputs # type: ignore[no-any-return]
return outputs


_THAI_TO_ROM_ONNX: ThaiTransliterator_ONNX = ThaiTransliterator_ONNX()
Expand Down
6 changes: 3 additions & 3 deletions pythainlp/transliterate/w2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _load_variables(self) -> None:
def _sigmoid(self, x: "np.ndarray") -> "np.ndarray":
import numpy as np

return 1 / (1 + np.exp(-x)) # type: ignore[no-any-return]
return 1 / (1 + np.exp(-x))

def _grucell(
self,
Expand Down Expand Up @@ -205,7 +205,7 @@ def _gru(
h = self._grucell(x[:, t, :], h, w_ih, w_hh, b_ih, b_hh) # (b, h)
outputs[:, t, ::] = h

return outputs # type: ignore[no-any-return]
return outputs

def _encode(self, word: str) -> "np.ndarray":
import numpy as np
Expand All @@ -214,7 +214,7 @@ def _encode(self, word: str) -> "np.ndarray":
x = [self.g2idx.get(char, self.g2idx["<unk>"]) for char in chars]
x = np.take(self.enc_emb, np.expand_dims(x, 0), axis=0)

return x # type: ignore[no-any-return]
return x

def _short_word(self, word: str) -> Optional[str]:
self.word: str = word
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/ulmfit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def document_vector(
else:
raise ValueError("Aggregate by mean or sum")

return res # type: ignore[no-any-return]
return res


def merge_wgts(
Expand Down
4 changes: 2 additions & 2 deletions pythainlp/word_vector/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def sentence_vectorizer(self, text: str, use_mean: bool = True) -> ndarray:
len_words = len(words)

if not len_words:
return vec # type: ignore[no-any-return]
return vec

for word in words:
if word == " " and self.model_name == "thai2fit_wv":
Expand All @@ -321,4 +321,4 @@ def sentence_vectorizer(self, text: str, use_mean: bool = True) -> ndarray:
if use_mean:
vec /= len_words

return vec # type: ignore[no-any-return]
return vec
2 changes: 1 addition & 1 deletion tests/data/eval-details-input.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"metrics": {"char_level:tp": 4.0, "char_level:fp": 0.0, "char_level:tn": 9.0, "char_level:fn": 1.0, "word_level:correctly_tokenised_words": 3.0, "word_level:total_words_in_sample": 4.0, "word_level:total_words_in_ref_sample": 5.0, "char_level:precision": 1.0, "char_level:recall": 0.8, "word_level:precision": 0.75, "word_level:recall": 0.6}, "samples": [{"metrics": {"char_level:tp": 4, "char_level:fp": 0, "char_level:tn": 9, "char_level:fn": 1, "word_level:correctly_tokenised_words": 3, "word_level:total_words_in_sample": 4.0, "word_level:total_words_in_ref_sample": 5.0, "global:tokenisation_indicators": "1011"}, "expected": "ผม|ไม่|ชอบ|กิน|ผัก", "actual": "ผม|ไม่ชอบ|กิน|ผัก", "id": 0}]}
{"metrics": {"char_level:tp": 4.0, "char_level:fp": 0.0, "char_level:tn": 9.0, "char_level:fn": 1.0, "word_level:correctly_tokenised_words": 3.0, "word_level:total_words_in_sample": 4.0, "word_level:total_words_in_ref_sample": 5.0, "char_level:precision": 1.0, "char_level:recall": 0.8, "word_level:precision": 0.75, "word_level:recall": 0.6}, "samples": [{"metrics": {"char_level:tp": 4, "char_level:fp": 0, "char_level:tn": 9, "char_level:fn": 1, "word_level:correctly_tokenised_words": 3, "word_level:total_words_in_sample": 4, "word_level:total_words_in_ref_sample": 5, "global:tokenisation_indicators": "1011"}, "expected": "ผม|ไม่|ชอบ|กิน|ผัก", "actual": "ผม|ไม่ชอบ|กิน|ผัก", "id": 0}]}
Comment thread
bact marked this conversation as resolved.
Loading