@@ -171,14 +171,15 @@ def log_evaluation_table(report, # type: Text
171
171
logger .info ("Classification report: \n {}" .format (report ))
172
172
173
173
174
- def get_evaluation_metrics (targets , predictions ): # pragma: no cover
174
+ def get_evaluation_metrics (targets , predictions , output_dict = False ): # pragma: no cover
175
175
"""Compute the f1, precision, accuracy and summary report from sklearn."""
176
176
from sklearn import metrics
177
177
178
178
targets = clean_intent_labels (targets )
179
179
predictions = clean_intent_labels (predictions )
180
180
181
- report = metrics .classification_report (targets , predictions )
181
+ report = metrics .classification_report (targets , predictions ,
182
+ output_dict = output_dict )
182
183
precision = metrics .precision_score (targets , predictions ,
183
184
average = 'weighted' )
184
185
f1 = metrics .f1_score (targets , predictions , average = 'weighted' )
@@ -187,43 +188,6 @@ def get_evaluation_metrics(targets, predictions): # pragma: no cover
187
188
return report , precision , f1 , accuracy
188
189
189
190
190
- def report_to_dict (report , f1 , precision , accuracy ):
191
- """Convert sklearn metrics report into dict"""
192
-
193
- report_dict = {
194
- 'f1' : f1 ,
195
- 'precision' : precision ,
196
- 'accuracy' : accuracy ,
197
- 'intents' : []
198
- }
199
-
200
- lines = list (filter (None , report .split ('\n ' )))
201
- labels = lines [0 ].split ()
202
-
203
- report_dict ['intents' ] = report_row_to_dict (labels , lines [1 :- 1 ])
204
-
205
- return report_dict
206
-
207
-
208
- def report_row_to_dict (labels , lines ):
209
- """Convert sklearn metrics report row to dict"""
210
- import re
211
-
212
- array = []
213
- for line in lines :
214
- row_data = re .split ('\s{2,}' , line .strip ())
215
- name = row_data [0 ]
216
- values = row_data [1 :]
217
- r = {
218
- 'name' : name
219
- }
220
- for i in range (len (values )):
221
- r [labels [i ]] = values [i ]
222
- array .append (r )
223
-
224
- return array
225
-
226
-
227
191
def remove_empty_intent_examples (intent_results ):
228
192
"""Remove those examples without an intent."""
229
193
@@ -343,16 +307,21 @@ def evaluate_intents(intent_results,
343
307
"of {} examples" .format (len (intent_results ), num_examples ))
344
308
345
309
targets , predictions = _targets_predictions_from (intent_results )
346
- report , precision , f1 , accuracy = get_evaluation_metrics (targets ,
347
- predictions )
348
-
349
- log_evaluation_table (report , precision , f1 , accuracy )
350
310
351
311
if report_filename :
352
- save_json (report_to_dict (report , f1 , precision , accuracy ), report_filename )
312
+ report , precision , f1 , accuracy = get_evaluation_metrics (targets ,
313
+ predictions ,
314
+ output_dict = True )
315
+
316
+ save_json (report , report_filename )
353
317
logger .info ("Classification report saved to {}."
354
318
.format (report_filename ))
355
319
320
+ else :
321
+ report , precision , f1 , accuracy = get_evaluation_metrics (targets ,
322
+ predictions )
323
+ log_evaluation_table (report , precision , f1 , accuracy )
324
+
356
325
if successes_filename :
357
326
# save classified samples to file for debugging
358
327
collect_nlu_successes (intent_results , successes_filename )
0 commit comments