1111
1212from pythainlp import cli
1313from pythainlp .benchmarks import word_tokenization
14+ from pythainlp .tools import safe_print
1415
1516
1617def _read_file (path ):
@@ -81,7 +82,7 @@ def __init__(self, name, argv):
8182 expected
8283 ), "Input and test files do not have the same number of samples"
8384
84- print (
85+ safe_print (
8586 "Benchmarking %s against %s with %d samples in total"
8687 % (args .input_file , args .test_file , len (actual ))
8788 )
@@ -121,12 +122,12 @@ def __init__(self, name, argv):
121122 / statistics ["word_level:total_words_in_ref_sample" ]
122123 )
123124
124- print ("============== Benchmark Result ==============" )
125+ safe_print ("============== Benchmark Result ==============" )
125126
126127 for c in ["tp" , "fn" , "tn" , "fp" , "precision" , "recall" ]:
127128 c = f"char_level:{ c } "
128129 v = statistics [c ]
129- print (f"{ c :>40s} { v :.4f} " )
130+ safe_print (f"{ c :>40s} { v :.4f} " )
130131
131132 for c in [
132133 "total_words_in_sample" ,
@@ -137,20 +138,20 @@ def __init__(self, name, argv):
137138 ]:
138139 c = f"word_level:{ c } "
139140 v = statistics [c ]
140- print (f"{ c :>40s} { v :.4f} " )
141+ safe_print (f"{ c :>40s} { v :.4f} " )
141142
142143 if args .save_details :
143144 dir_name = os .path .dirname (args .input_file )
144145 file_name = args .input_file .split ("/" )[- 1 ].split ("." )[0 ]
145146
146147 res_path = "%s/eval-%s.yml" % (dir_name , file_name )
147- print ("Evaluation result is saved to %s" % res_path )
148+ safe_print ("Evaluation result is saved to %s" % res_path )
148149
149150 with open (res_path , "w" , encoding = "utf-8" ) as outfile :
150151 yaml .dump (statistics , outfile , default_flow_style = False )
151152
152153 res_path = "%s/eval-details-%s.json" % (dir_name , file_name )
153- print ("Details of comparisons is saved to %s" % res_path )
154+ safe_print ("Details of comparisons is saved to %s" % res_path )
154155
155156 with open (res_path , "w" , encoding = "utf-8" ) as f :
156157 samples = []
@@ -160,7 +161,12 @@ def __init__(self, name, argv):
160161 del r ["actual" ]
161162
162163 samples .append (
163- {"metrics" : r , "expected" : expected , "actual" : actual , "id" : i }
164+ {
165+ "metrics" : r ,
166+ "expected" : expected ,
167+ "actual" : actual ,
168+ "id" : i ,
169+ }
164170 )
165171
166172 details = {"metrics" : statistics , "samples" : samples }
0 commit comments