55
66import re
77import sys
8+ from typing import Any
89
910import numpy as np
1011import 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, ...]
0 commit comments