Skip to content

Commit 2e0acf8

Browse files
authored
Merge pull request #1277 from PyThaiNLP/copilot/incremental-type-annotations
Complete type annotations across codebase (97% coverage)
2 parents 006963a + f7672a4 commit 2e0acf8

95 files changed

Lines changed: 740 additions & 2919 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build_tools/analysis/output/type_hint_analysis.json

Lines changed: 175 additions & 2464 deletions
Large diffs are not rendered by default.

pythainlp/ancient/aksonhan.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
from pythainlp.util import Trie
1212

1313
_dict_aksonhan: dict[str, str] = {}
14+
i: str
1415
for i in list(thai_consonants):
1516
if i == "ร":
1617
continue
18+
j: str
1719
for j in list(thai_tonemarks):
1820
_dict_aksonhan[i + j + i] = "ั" + j + i
1921
_dict_aksonhan[i + i + j + i] = i + "ั" + j + i

pythainlp/augment/lm/fasttext.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ def __init__(self, model_path: str) -> None:
3030
from gensim.models.keyedvectors import KeyedVectors
3131

3232
if model_path.endswith(".bin"):
33-
self.model: Union[FastText, KeyedVectors] = (
34-
FastText_gensim.load_facebook_vectors(model_path)
35-
)
33+
self.model: Union["FastText", "KeyedVectors"] = FastText_gensim.load_facebook_vectors(model_path)
3634
elif model_path.endswith(".vec"):
37-
self.model = KeyedVectors.load_word2vec_format(model_path)
35+
self.model: Union["FastText", "KeyedVectors"] = KeyedVectors.load_word2vec_format(model_path)
3836
else:
39-
self.model = FastText_gensim.load(model_path)
37+
self.model: Union["FastText", "KeyedVectors"] = FastText_gensim.load(model_path)
4038
self.dict_wv: list[str] = list(self.model.key_to_index.keys())
4139

4240
def tokenize(self, text: str) -> list[str]:

pythainlp/augment/lm/phayathaibert.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ def __init__(self) -> None:
2828
pipeline,
2929
)
3030

31-
self.tokenizer = AutoTokenizer.from_pretrained(_MODEL_NAME) # type: ignore[assignment]
32-
self.model_for_masked_lm = AutoModelForMaskedLM.from_pretrained( # type: ignore[assignment]
31+
self.tokenizer: AutoTokenizer = AutoTokenizer.from_pretrained(_MODEL_NAME)
32+
self.model_for_masked_lm: AutoModelForMaskedLM = AutoModelForMaskedLM.from_pretrained(
3333
_MODEL_NAME
3434
)
35-
self.model = pipeline(
35+
self.model: Pipeline = pipeline(
3636
"fill-mask",
3737
tokenizer=self.tokenizer,
3838
model=self.model_for_masked_lm,
3939
)
40-
self.processor = ThaiTextProcessor()
40+
self.processor: ThaiTextProcessor = ThaiTextProcessor()
4141

4242
def generate(
4343
self,

pythainlp/augment/lm/wangchanberta.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@ def __init__(self) -> None:
2525
pipeline,
2626
)
2727

28-
self.model_name = "airesearch/wangchanberta-base-att-spm-uncased"
29-
self.target_tokenizer = CamembertTokenizer
30-
self.tokenizer = CamembertTokenizer.from_pretrained( # type: ignore[assignment]
28+
self.model_name: str = "airesearch/wangchanberta-base-att-spm-uncased"
29+
self.target_tokenizer: type[CamembertTokenizer] = CamembertTokenizer
30+
self.tokenizer: CamembertTokenizer = CamembertTokenizer.from_pretrained(
3131
self.model_name, revision="main"
3232
)
3333
self.tokenizer.additional_special_tokens = [
3434
"<s>NOTUSED",
3535
"</s>NOTUSED",
3636
"<_>",
3737
]
38-
self.fill_mask = pipeline(
38+
self.fill_mask: Pipeline = pipeline(
3939
task="fill-mask",
4040
tokenizer=self.tokenizer,
4141
model=f"{self.model_name}",
4242
revision="main",
4343
)
44-
self.MASK_TOKEN = self.tokenizer.mask_token
44+
self.MASK_TOKEN: str = self.tokenizer.mask_token
4545

4646
def generate(
4747
self, sentence: str, num_replace_tokens: int = 3
4848
) -> list[str]:
4949
sent2: list[str] = []
50-
self.input_text = sentence
50+
self.input_text: str = sentence
5151
sent = [
5252
i for i in self.tokenizer.tokenize(self.input_text) if i != "▁"
5353
]

pythainlp/augment/word2vec/bpemb_wv.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def __init__(
3232
) -> None:
3333
from bpemb import BPEmb
3434

35-
self.bpemb_temp = BPEmb(lang=lang, dim=dim, vs=vs)
36-
self.model = self.bpemb_temp.emb
35+
self.bpemb_temp: BPEmb = BPEmb(lang=lang, dim=dim, vs=vs)
36+
self.model: KeyedVectors = self.bpemb_temp.emb
3737
self.load_w2v()
3838

3939
def tokenizer(self, text: str) -> list[str]:
@@ -44,7 +44,7 @@ def tokenizer(self, text: str) -> list[str]:
4444

4545
def load_w2v(self) -> None:
4646
"""Load BPEmb model"""
47-
self.aug = Word2VecAug(
47+
self.aug: Word2VecAug = Word2VecAug(
4848
self.model, tokenize=self.tokenizer, type="model"
4949
)
5050

@@ -68,11 +68,11 @@ def augment(
6868
aug.augment("ผมเรียน", n_sent=2, p=0.5)
6969
# output: ['ผมสอน', 'ผมเข้าเรียน']
7070
"""
71-
self.sentence = sentence.replace(" ", "▁")
72-
self.temp = self.aug.augment(self.sentence, n_sent, p=p)
73-
self.temp_new = []
71+
self.sentence: str = sentence.replace(" ", "▁")
72+
self.temp: list[tuple[str, ...]] = self.aug.augment(self.sentence, n_sent, p=p)
73+
self.temp_new: list[str] = []
7474
for i in self.temp:
75-
self.t = ""
75+
self.t: str = ""
7676
for j in i:
7777
self.t += j.replace("▁", "")
7878
self.temp_new.append(self.t)

pythainlp/augment/word2vec/core.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ def __init__(
2929

3030
self.tokenizer: Callable[[str], list[str]] = tokenize
3131
if type == "file":
32-
self.model: "KeyedVectors" = (
33-
word2vec.KeyedVectors.load_word2vec_format(model)
34-
)
32+
self.model: "KeyedVectors" = word2vec.KeyedVectors.load_word2vec_format(model)
3533
elif type == "binary":
36-
self.model = word2vec.KeyedVectors.load_word2vec_format(
34+
self.model: "KeyedVectors" = word2vec.KeyedVectors.load_word2vec_format(
3735
model, binary=True, unicode_errors="ignore"
3836
)
3937
else:
40-
self.model = model
38+
self.model: "KeyedVectors" = model # type: ignore[assignment]
4139
self.dict_wv: list[str] = list(self.model.key_to_index.keys())
4240

4341
def modify_sent(self, sent: list[str], p: float = 0.7) -> list[list[str]]:

pythainlp/augment/word2vec/ltw2v.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
# SPDX-License-Identifier: Apache-2.0
44
from __future__ import annotations
55

6-
from typing import TYPE_CHECKING, Optional
6+
from typing import Optional
77

8-
if TYPE_CHECKING:
9-
from pythainlp.augment.word2vec.core import Word2VecAug
10-
11-
from pythainlp.augment.word2vec.core import Word2VecAug as _Word2VecAug
12-
13-
# Make it accessible for runtime
14-
Word2VecAug: type[_Word2VecAug] = _Word2VecAug
8+
from pythainlp.augment.word2vec.core import Word2VecAug
159
from pythainlp.corpus import get_corpus_path
1610
from pythainlp.tokenize import word_tokenize
1711

@@ -27,7 +21,7 @@ class LTW2VAug:
2721
aug: Word2VecAug
2822

2923
def __init__(self) -> None:
30-
self.ltw2v_wv = get_corpus_path("ltw2v")
24+
self.ltw2v_wv: Optional[str] = get_corpus_path("ltw2v")
3125
self.load_w2v()
3226

3327
def tokenizer(self, text: str) -> list[str]:
@@ -43,7 +37,7 @@ def load_w2v(self) -> None: # insert substitute
4337
"LTW2V word2vec model not found. "
4438
"Please download it first using pythainlp.corpus.download('ltw2v_wv')"
4539
)
46-
self.aug = Word2VecAug(self.ltw2v_wv, self.tokenizer, type="binary")
40+
self.aug: Word2VecAug = Word2VecAug(self.ltw2v_wv, self.tokenizer, type="binary")
4741

4842
def augment(
4943
self, sentence: str, n_sent: int = 1, p: float = 0.7

pythainlp/augment/word2vec/thai2fit.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
# SPDX-License-Identifier: Apache-2.0
44
from __future__ import annotations
55

6-
from typing import TYPE_CHECKING, Optional
6+
from typing import Optional
77

8-
if TYPE_CHECKING:
9-
from pythainlp.augment.word2vec.core import Word2VecAug
10-
11-
from pythainlp.augment.word2vec.core import Word2VecAug as _Word2VecAug
12-
13-
# Make it accessible for runtime
14-
Word2VecAug: type[_Word2VecAug] = _Word2VecAug
8+
from pythainlp.augment.word2vec.core import Word2VecAug
159
from pythainlp.corpus import get_corpus_path
1610
from pythainlp.tokenize import thai2fit_tokenizer
1711

@@ -27,15 +21,15 @@ class Thai2fitAug:
2721
aug: Word2VecAug
2822

2923
def __init__(self) -> None:
30-
self.thai2fit_wv = get_corpus_path("thai2fit_wv")
24+
self.thai2fit_wv: Optional[str] = get_corpus_path("thai2fit_wv")
3125
self.load_w2v()
3226

3327
def tokenizer(self, text: str) -> list[str]:
3428
""":param str text: Thai text
3529
:rtype: List[str]
3630
"""
3731
tok = thai2fit_tokenizer()
38-
return tok.word_tokenize(text) # type: ignore[no-any-return]
32+
return tok.word_tokenize(text)
3933

4034
def load_w2v(self) -> None:
4135
"""Load Thai2Fit's word2vec model"""
@@ -44,7 +38,7 @@ def load_w2v(self) -> None:
4438
"Thai2Fit word2vec model not found. "
4539
"Please download it first using pythainlp.corpus.download('thai2fit_wv')"
4640
)
47-
self.aug = Word2VecAug(self.thai2fit_wv, self.tokenizer, type="binary")
41+
self.aug: Word2VecAug = Word2VecAug(self.thai2fit_wv, self.tokenizer, type="binary")
4842

4943
def augment(
5044
self, sentence: str, n_sent: int = 1, p: float = 0.7

pythainlp/augment/wordnet.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import itertools
1414
from collections import OrderedDict
15-
from typing import Callable, Optional
15+
from typing import Any, Callable, Optional
1616

1717
from nltk.corpus import wordnet as wn
1818

@@ -112,12 +112,24 @@ def postype2wordnet(pos: str, corpus: str) -> Optional[str]:
112112
"""
113113
if corpus not in ["orchid"]:
114114
return None
115-
return orchid[pos] # type: ignore[no-any-return]
115+
return orchid[pos]
116116

117117

118118
class WordNetAug:
119119
"""Text Augment using wordnet"""
120120

121+
synonyms: list[str]
122+
list_synsets: list
123+
p2w_pos: Optional[str]
124+
synset: Any
125+
syn: str
126+
synonyms_without_duplicates: list[str]
127+
list_words: list[str]
128+
list_synonym: list
129+
p_all: int
130+
list_pos: list[tuple[str, str]]
131+
temp: list[str]
132+
121133
def __init__(self) -> None:
122134
pass
123135

@@ -137,13 +149,13 @@ def find_synonyms(
137149
"""
138150
self.synonyms: list[str] = []
139151
if pos is None:
140-
self.list_synsets = wordnet.synsets(word)
152+
self.list_synsets: list = wordnet.synsets(word)
141153
else:
142-
self.p2w_pos = postype2wordnet(pos, postag_corpus)
154+
self.p2w_pos: Optional[str] = postype2wordnet(pos, postag_corpus)
143155
if self.p2w_pos != "":
144-
self.list_synsets = wordnet.synsets(word, pos=self.p2w_pos)
156+
self.list_synsets: list = wordnet.synsets(word, pos=self.p2w_pos)
145157
else:
146-
self.list_synsets = wordnet.synsets(word)
158+
self.list_synsets: list = wordnet.synsets(word)
147159

148160
for self.synset in wordnet.synsets(word):
149161
for self.syn in self.synset.lemma_names(lang="tha"):
@@ -189,7 +201,7 @@ def augment(
189201
"""
190202
new_sentences = []
191203
self.list_words: list[str] = tokenize(sentence)
192-
self.list_synonym: list[list[str]] = []
204+
self.list_synonym: list = []
193205
self.p_all: int = 1
194206
if postag:
195207
self.list_pos: list[tuple[str, str]] = pos_tag(
@@ -206,7 +218,7 @@ def augment(
206218
self.p_all *= len(self.temp)
207219
else:
208220
for word in self.list_words:
209-
self.temp = self.find_synonyms(word)
221+
self.temp: list[str] = self.find_synonyms(word)
210222
if not self.temp:
211223
self.list_synonym.append([word])
212224
else:

0 commit comments

Comments
 (0)