3030TAILING_SEP_RX : re .Pattern [str ] = re .compile (f"{ re .escape (SEPARATOR )} $" )
3131
3232
33- class CharLevelStats (TypedDict ):
33+ class CharLevelStat (TypedDict ):
3434 """Character-level confusion matrix statistics for tokenization."""
3535
3636 tp : int
@@ -39,30 +39,26 @@ class CharLevelStats(TypedDict):
3939 fn : int
4040
4141
42- class WordLevelStats (TypedDict ):
42+ class WordLevelStat (TypedDict ):
4343 """Word-level tokenization statistics."""
4444
4545 correctly_tokenised_words : int
4646 total_words_in_sample : int
4747 total_words_in_ref_sample : int
4848
4949
50- class GlobalStats (TypedDict ):
51- """Global tokenization indicators as a binary indicator string."""
50+ class GlobalStat (TypedDict ):
51+ """Global tokenization indicator as a binary indicator string."""
5252
5353 tokenisation_indicators : str
5454
5555
56- # Functional form is required because 'global' is a Python reserved keyword.
57- TokenizationStats = TypedDict (
58- "TokenizationStats" ,
59- {
60- "char_level" : CharLevelStats ,
61- "word_level" : WordLevelStats ,
62- "global" : GlobalStats ,
63- },
64- )
65- """Tokenization quality statistics at character, word, and global level."""
56+ class TokenizationStat (TypedDict ):
57+ """Tokenization quality statistics at character, word, and global level."""
58+
59+ char_level : CharLevelStat
60+ word_level : WordLevelStat
61+ global_ : GlobalStat
6662
6763
6864def _f1 (precision : float , recall : float ) -> float :
@@ -81,7 +77,7 @@ def _f1(precision: float, recall: float) -> float:
8177
8278@overload
8379def _flatten_result (
84- my_dict : TokenizationStats , sep : str = ...
80+ my_dict : TokenizationStat , sep : str = ...
8581) -> dict [str , Union [int , str ]]: ...
8682
8783
@@ -105,7 +101,7 @@ def _flatten_result(
105101
106102
107103 :param my_dict: dictionary containing stats
108- :type my_dict: TokenizationStats or
104+ :type my_dict: TokenizationStat or
109105 collections.abc.Mapping[str, collections.abc.Mapping[str, Union[int, str]]]
110106 :param str sep: separator between the two keys (default: ":")
111107
@@ -189,7 +185,7 @@ def preprocessing(txt: str, remove_space: bool = True) -> str:
189185
190186def compute_stats (
191187 ref_sample : str , raw_sample : str
192- ) -> TokenizationStats :
188+ ) -> TokenizationStat :
193189 """Compute statistics for tokenization quality
194190
195191 These statistics include:
@@ -206,7 +202,7 @@ def compute_stats(
206202 :param str samples: samples that we want to evaluate
207203
208204 :return: metrics at character- and word-level and indicators of correctly tokenized words
209- :rtype: TokenizationStats
205+ :rtype: TokenizationStat
210206 """
211207 import numpy as np
212208
@@ -244,18 +240,18 @@ def compute_stats(
244240 tokenization_indicators_str = list (map (str , tokenization_indicators ))
245241
246242 return {
247- "char_level" : CharLevelStats (
243+ "char_level" : CharLevelStat (
248244 tp = c_tp ,
249245 fp = c_fp ,
250246 tn = c_tn ,
251247 fn = c_fn ,
252248 ),
253- "word_level" : WordLevelStats (
249+ "word_level" : WordLevelStat (
254250 correctly_tokenised_words = correctly_tokenised_words ,
255251 total_words_in_sample = int (np .sum (sample_arr )),
256252 total_words_in_ref_sample = int (np .sum (ref_sample_arr )),
257253 ),
258- "global " : GlobalStats (
254+ "global_ " : GlobalStat (
259255 tokenisation_indicators = "" .join (tokenization_indicators_str ),
260256 ),
261257 }
0 commit comments