Skip to content

Commit e9b8f3b

Browse files
authored
Merge pull request #1270 from PyThaiNLP/copilot/verify-type-hints-annotations
Align docstrings with type hints across codebase
2 parents 7f87547 + c656076 commit e9b8f3b

30 files changed

Lines changed: 800 additions & 773 deletions

File tree

pythainlp/augment/wordnet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def find_synonyms(
133133
:param Optional[str] pos: part-of-speech type. Default is None.
134134
:param str postag_corpus: name of POS tag corpus
135135
:return: list of synonyms
136-
:rtype: List[str]
136+
:rtype: list[str]
137137
"""
138138
self.synonyms = []
139139
if pos is None:
@@ -171,7 +171,7 @@ def augment(
171171
:param str postag_corpus: name of POS tag corpus
172172
173173
:return: list of synonyms
174-
:rtype: List[Tuple[str]]
174+
:rtype: list[list[str]]
175175
176176
:Example:
177177
::

pythainlp/benchmarks/word_tokenization.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import re
77
import sys
8+
from typing import Any
89

910
import numpy as np
1011
import pandas as pd
@@ -40,7 +41,7 @@ def _f1(precision: float, recall: float) -> float:
4041
return 2 * precision * recall / (precision + recall)
4142

4243

43-
def _flatten_result(my_dict: dict, sep: str = ":") -> dict:
44+
def _flatten_result(my_dict: dict, sep: str = ":") -> dict[str, Any]:
4445
"""Flatten two-dimension dictionary.
4546
4647
Use keys in the first dimension as a prefix for keys in the second dimension.
@@ -54,7 +55,7 @@ def _flatten_result(my_dict: dict, sep: str = ":") -> dict:
5455
:param str sep: separator between the two keys (default: ":")
5556
5657
:return: a one-dimension dictionary with keys combined
57-
:rtype: dict[str, Union[float, str]]
58+
:rtype: dict[str, Any]
5859
"""
5960
return {
6061
f"{k1}{sep}{k2}": v
@@ -129,7 +130,7 @@ def preprocessing(txt: str, remove_space: bool = True) -> str:
129130
return txt
130131

131132

132-
def compute_stats(ref_sample: str, raw_sample: str) -> dict:
133+
def compute_stats(ref_sample: str, raw_sample: str) -> dict[str, Any]:
133134
"""Compute statistics for tokenization quality
134135
135136
These statistics include:
@@ -146,7 +147,7 @@ def compute_stats(ref_sample: str, raw_sample: str) -> dict:
146147
:param str samples: samples that we want to evaluate
147148
148149
:return: metrics at character- and word-level and indicators of correctly tokenized words
149-
:rtype: dict[str, Union[float, str]]
150+
:rtype: dict[str, Any]
150151
"""
151152
ref_sample_arr = _binary_representation(ref_sample)
152153
sample_arr = _binary_representation(raw_sample)
@@ -222,7 +223,11 @@ def _binary_representation(txt: str, verbose: bool = False) -> np.ndarray:
222223
sample_wo_seps = list(txt.replace(SEPARATOR, ""))
223224

224225
# sanity check
225-
assert len(sample_wo_seps) == len(bin_rept)
226+
if len(sample_wo_seps) != len(bin_rept):
227+
raise ValueError(
228+
f"Length mismatch: sample_wo_seps={len(sample_wo_seps)}, "
229+
f"bin_rept={len(bin_rept)}"
230+
)
226231

227232
if verbose:
228233
for c, m in zip(sample_wo_seps, bin_rept):
@@ -231,13 +236,13 @@ def _binary_representation(txt: str, verbose: bool = False) -> np.ndarray:
231236
return bin_rept
232237

233238

234-
def _find_word_boundaries(bin_reps) -> list:
239+
def _find_word_boundaries(bin_reps) -> list[tuple[int, int]]:
235240
"""Find the starting and ending location of each word.
236241
237242
:param str bin_reps: binary representation of a text
238243
239244
:return: list of tuples (start, end)
240-
:rtype: list[tuple(int, int)]
245+
:rtype: list[tuple[int, int]]
241246
"""
242247
boundary = np.argwhere(bin_reps == 1).reshape(-1)
243248
start_idx = boundary
@@ -252,8 +257,8 @@ def _find_words_correctly_tokenised(
252257
) -> tuple[int, ...]:
253258
"""Find whether each word is correctly tokenized.
254259
255-
:param list[tuple(int, int)] ref_boundaries: word boundaries of reference tokenization
256-
:param list[tuple(int, int)] predicted_boundaries: word boundareies of predicted tokenization
260+
:param list[tuple[int, int]] ref_boundaries: word boundaries of reference tokenization
261+
:param list[tuple[int, int]] predicted_boundaries: word boundaries of predicted tokenization
257262
258263
:return: binary sequence where 1 indicates the corresponding word is tokenized correctly
259264
:rtype: tuple[int, ...]

pythainlp/cli/benchmark.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ def __init__(self, name: str, argv: Sequence[str]) -> None:
8181
actual = _read_file(args.input_file)
8282
expected = _read_file(args.test_file)
8383

84-
assert len(actual) == len(expected), (
85-
"Input and test files do not have the same number of samples"
86-
)
84+
if len(actual) != len(expected):
85+
raise ValueError(
86+
"Input and test files do not have the same number of samples"
87+
)
8788

8889
safe_print(
8990
"Benchmarking %s against %s with %d samples in total"

pythainlp/coref/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def coreference_resolution(
1515
) -> list[dict]:
1616
"""Coreference Resolution
1717
18-
:param List[str] texts: list of texts to apply coreference resolution to
18+
:param Union[str, list[str]] texts: list of texts to apply coreference resolution to
1919
:param str model_name: coreference resolution model
2020
:param str device: device for running coreference resolution model on\
2121
("cpu", "cuda", and others)
2222
:return: List of texts with coreference resolution
23-
:rtype: List[dict]
23+
:rtype: list[dict]
2424
2525
:Options for model_name:
2626
* *han-coref-v1.0* - (default) Han-Coref: Thai coreference resolution\

pythainlp/el/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def get_el(
4242
) -> Union[list[dict], str]:
4343
"""Get Entity Linking from Thai Text
4444
45-
:param str Union[List[str], str]: list of Thai text or text
45+
:param str Union[list[str], str]: list of Thai text or text
4646
:return: list of entity linking
47-
:rtype: Union[List[dict], str]
47+
:rtype: Union[list[dict], str]
4848
4949
:Example:
5050
::

pythainlp/generate/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def gen_sentence(
5858
:param bool duplicate: allow duplicate words in sentence
5959
6060
:return: list of words or a word string
61-
:rtype: List[str], str
61+
:rtype: list[str], str
6262
6363
:Example:
6464
::
@@ -153,7 +153,7 @@ def gen_sentence(
153153
:param bool duplicate: allow duplicate words in sentence
154154
155155
:return: list of words or a word string
156-
:rtype: List[str], str
156+
:rtype: list[str], str
157157
158158
:Example:
159159
::
@@ -244,7 +244,7 @@ def gen_sentence(
244244
:param bool duplicate: allow duplicate words in sentence
245245
246246
:return: list of words or a word string
247-
:rtype: List[str], str
247+
:rtype: list[str], str
248248
249249
:Example:
250250
::

pythainlp/generate/thai2fit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def gen_sentence(
120120
:param bool duplicate: allow duplicate words in sentence
121121
122122
:return: list words or str words
123-
:rtype: List[str], str
123+
:rtype: list[str], str
124124
125125
:Example:
126126
::

0 commit comments

Comments
 (0)