1010
1111import threading
1212from importlib .resources import as_file , files
13- from typing import Optional , Union , cast
13+ from typing import Optional
1414
1515try :
1616 import pycrfsuite
@@ -61,20 +61,16 @@ def featurize(
6161 ) -> dict [str , list ]:
6262 if padding :
6363 sentence = self .pad (sentence )
64- all_features = []
65- all_labels = []
64+ all_features_list : list [ list [ str ]] = []
65+ all_labels_int : list [ int ] = []
6666 skip_next = False
6767 for current_position in range (
6868 self .radius , len (sentence ) - self .radius + 1
6969 ):
7070 if skip_next :
7171 skip_next = False
7272 continue
73- features : Union [dict [str , int ], list [str ]]
74- if return_type == "list" :
75- features = []
76- else :
77- features = {}
73+ features : list [str ] = []
7874 cut = 0
7975 char = sentence [current_position ]
8076 if char == self .delimiter :
@@ -83,7 +79,6 @@ def featurize(
8379 counter = 0
8480 chars_left = ""
8581 chars_right = ""
86- chars = ""
8782 abs_index_left = current_position # left start at -1
8883 abs_index_right = current_position - 1 # right start at 0
8984 while counter < self .radius :
@@ -100,10 +95,7 @@ def featurize(
10095 # ใส่ลง feature
10196 if indiv_char :
10297 left_key = "|" .join ([str (relative_index_left ), char_left ])
103- if return_type == "dict" :
104- cast (dict [str , int ], features )[left_key ] = 1
105- else :
106- cast (list [str ], features ).append (left_key )
98+ features .append (left_key )
10799
108100 abs_index_right += (
109101 1 # สมมุติคือตำแหน่งที่ 0 จะได้ 0, 1, 2, 3, 4 (radius = 5)
@@ -118,28 +110,32 @@ def featurize(
118110 right_key = "|" .join (
119111 [str (relative_index_right ), char_right ]
120112 )
121- if return_type == "dict" :
122- cast (dict [str , int ], features )[right_key ] = 1
123- else :
124- cast (list [str ], features ).append (right_key )
113+ features .append (right_key )
125114
126115 counter += 1
127116
128117 chars = chars_left + chars_right
129118 for i in range (0 , len (chars ) - self .N + 1 ):
130119 ngram = chars [i : i + self .N ]
131120 ngram_key = "|" .join ([str (i - self .radius ), ngram ])
132- if return_type == "dict" :
133- cast (dict [str , int ], features )[ngram_key ] = 1
134- else :
135- cast (list [str ], features ).append (ngram_key )
136- all_features .append (features )
137- if return_type == "list" :
138- all_labels .append (str (cut ))
139- else :
140- all_labels .append (cut )
141-
142- return {"X" : all_features , "Y" : all_labels }
121+ features .append (ngram_key )
122+ all_features_list .append (features )
123+ all_labels_int .append (cut )
124+
125+ # Convert to the requested return type
126+ if return_type == "list" :
127+ return {
128+ "X" : all_features_list ,
129+ "Y" : [str (label ) for label in all_labels_int ]
130+ }
131+ else :
132+ return {
133+ "X" : [
134+ {key : 1 for key in feature_list }
135+ for feature_list in all_features_list
136+ ],
137+ "Y" : all_labels_int
138+ }
143139
144140
145141_to_feature = Featurizer ()
0 commit comments