Skip to content

Commit c2edfb3

Browse files
Copilotbact
andcommitted
Add proper numpy type annotations and fix benchmarks module
- Added explicit np.intp type annotations for numpy sum operations - Fixed _binary_representation to properly annotate bin_rept as np.ndarray - Added type ignore for bpemb and wangchanberta external library returns - All tests in benchmarks and augment modules pass Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent 988c818 commit c2edfb3

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

pythainlp/augment/lm/wangchanberta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ def augment(self, sentence: str, num_replace_tokens: int = 3) -> list[str]:
7474
'ช้างมีทั้งหมด 50 ตัว บนหัว']
7575
"""
7676
sent2 = self.generate(sentence, num_replace_tokens)
77-
return sent2
77+
return sent2 # type: ignore[no-any-return]

pythainlp/augment/word2vec/bpemb_wv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def tokenizer(self, text: str) -> list[str]:
2424
""":param str text: Thai text
2525
:rtype: List[str]
2626
"""
27-
return self.bpemb_temp.encode(text)
27+
return self.bpemb_temp.encode(text) # type: ignore[no-any-return]
2828

2929
def load_w2v(self):
3030
"""Load BPEmb model

pythainlp/benchmarks/word_tokenization.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ def compute_stats(ref_sample: str, raw_sample: str) -> dict:
157157
c_pos_pred = c_pos_pred[c_pos_pred < ref_sample_arr.shape[0]]
158158
c_neg_pred = c_neg_pred[c_neg_pred < ref_sample_arr.shape[0]]
159159

160-
c_tp = np.sum(ref_sample_arr[c_pos_pred] == 1)
161-
c_fp = np.sum(ref_sample_arr[c_pos_pred] == 0)
160+
c_tp: np.intp = np.sum(ref_sample_arr[c_pos_pred] == 1)
161+
c_fp: np.intp = np.sum(ref_sample_arr[c_pos_pred] == 0)
162162

163-
c_tn = np.sum(ref_sample_arr[c_neg_pred] == 0)
164-
c_fn = np.sum(ref_sample_arr[c_neg_pred] == 1)
163+
c_tn: np.intp = np.sum(ref_sample_arr[c_neg_pred] == 0)
164+
c_fn: np.intp = np.sum(ref_sample_arr[c_neg_pred] == 1)
165165

166166
# Compute word-level statistics
167167

@@ -174,7 +174,7 @@ def compute_stats(ref_sample: str, raw_sample: str) -> dict:
174174
word_boundaries, ss_boundaries
175175
)
176176

177-
correctly_tokenised_words = np.sum(tokenization_indicators)
177+
correctly_tokenised_words: np.intp = np.sum(tokenization_indicators)
178178

179179
tokenization_indicators_str = list(map(str, tokenization_indicators))
180180

@@ -206,14 +206,14 @@ def _binary_representation(txt: str, verbose: bool = False) -> np.ndarray:
206206
:param bool verbose: for debugging purposes
207207
208208
:return: {0, 1} sequence
209-
:rtype: str
209+
:rtype: np.ndarray
210210
"""
211211
chars = np.array(list(txt))
212212

213213
boundary = np.argwhere(chars == SEPARATOR).reshape(-1)
214214
boundary = boundary - np.array(range(boundary.shape[0]))
215215

216-
bin_rept = np.zeros(len(txt) - boundary.shape[0])
216+
bin_rept: np.ndarray = np.zeros(len(txt) - boundary.shape[0])
217217
bin_rept[list(boundary) + [0]] = 1
218218

219219
sample_wo_seps = list(txt.replace(SEPARATOR, ""))

0 commit comments

Comments
 (0)