1010if TYPE_CHECKING :
1111 import numpy as np
1212 import pandas as pd
13+ from numpy .typing import NDArray
1314
1415SEPARATOR : str = "|"
1516
@@ -43,7 +44,7 @@ def _f1(precision: float, recall: float) -> float:
4344
4445
4546def _flatten_result (
46- my_dict : dict , sep : str = ":"
47+ my_dict : dict [ str , dict [ str , Union [ int , str ]]] , sep : str = ":"
4748) -> dict [str , Union [int , str ]]:
4849 """Flatten two-dimension dictionary.
4950
@@ -54,7 +55,8 @@ def _flatten_result(
5455 { "a:b": 7 }
5556
5657
57- :param dict my_dict: dictionary containing stats
58+ :param dict[str, dict[str, Union[int, str]]] my_dict: dictionary
59+ containing stats
5860 :param str sep: separator between the two keys (default: ":")
5961
6062 :return: a one-dimension dictionary with keys combined
@@ -91,7 +93,7 @@ def benchmark(ref_samples: list[str], samples: list[str]) -> "pd.DataFrame":
9193 flat_stats ["expected" ] = r
9294 flat_stats ["actual" ] = s
9395 results .append (flat_stats )
94- except Exception :
96+ except Exception as exc :
9597 reason = """
9698[Error]
9799Reason: %s
@@ -107,7 +109,7 @@ def benchmark(ref_samples: list[str], samples: list[str]) -> "pd.DataFrame":
107109 r ,
108110 s ,
109111 )
110- raise SystemExit (reason )
112+ raise SystemExit (reason ) from exc
111113
112114 return pd .DataFrame (results )
113115
@@ -209,7 +211,9 @@ def compute_stats(
209211 }
210212
211213
212- def _binary_representation (txt : str , verbose : bool = False ) -> "np.ndarray" :
214+ def _binary_representation (
215+ txt : str , verbose : bool = False
216+ ) -> "NDArray[np.int8]" :
213217 """Transform text into {0, 1} sequence.
214218
215219 where (1) indicates that the corresponding character is the beginning of
@@ -219,7 +223,7 @@ def _binary_representation(txt: str, verbose: bool = False) -> "np.ndarray":
219223 :param bool verbose: for debugging purposes
220224
221225 :return: {0, 1} sequence
222- :rtype: np.ndarray
226+ :rtype: numpy.typing.NDArray[numpy.int8]
223227 """
224228 import numpy as np
225229
@@ -228,7 +232,7 @@ def _binary_representation(txt: str, verbose: bool = False) -> "np.ndarray":
228232 boundary = np .argwhere (chars == SEPARATOR ).reshape (- 1 )
229233 boundary = boundary - np .array (range (boundary .shape [0 ]))
230234
231- bin_rept : np . ndarray = np .zeros (len (txt ) - boundary .shape [0 ])
235+ bin_rept = np .zeros (len (txt ) - boundary .shape [0 ], dtype = np . int8 )
232236 bin_rept [list (boundary ) + [0 ]] = 1
233237
234238 sample_wo_seps = list (txt .replace (SEPARATOR , "" ))
@@ -247,10 +251,13 @@ def _binary_representation(txt: str, verbose: bool = False) -> "np.ndarray":
247251 return bin_rept
248252
249253
250- def _find_word_boundaries (bin_reps : "np.ndarray" ) -> list [tuple [int , int ]]:
254+ def _find_word_boundaries (
255+ bin_reps : "NDArray[np.int8]" ,
256+ ) -> list [tuple [int , int ]]:
251257 """Find the starting and ending location of each word.
252258
253- :param numpy.ndarray bin_reps: binary representation of a text
259+ :param numpy.typing.NDArray[numpy.int8] bin_reps: binary representation
260+ of a text
254261
255262 :return: list of tuples (start, end)
256263 :rtype: list[tuple[int, int]]
0 commit comments