File size: 12,429 Bytes
ba1d61a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | import json
import re
import ast
from sklearn.metrics import accuracy_score, f1_score, classification_report, confusion_matrix
from collections import Counter, defaultdict
from datetime import datetime
import numpy as np
def extract_emotion_intent_from_output(model_output):
if not model_output:
return None, None, False, "empty_output"
valid_emotions = ['happy', 'surprise', 'sad', 'disgust', 'anger', 'fear', 'neutral']
valid_intents = ['questioning', 'agreeing', 'acknowledging', 'encouraging',
'consoling', 'suggesting', 'wishing', 'neutral']
try:
if "{'emotion':" in model_output or '{"emotion":' in model_output:
cleaned_output = model_output.strip()
cleaned_output = re.sub(r"'intent'(\s*,\s*)'", r"'intent':\1'", cleaned_output)
cleaned_output = re.sub(r'"intent"(\s*,\s*)"', r'"intent":\1"', cleaned_output)
if not cleaned_output.startswith('{'):
json_match = re.search(r'\{[^}]*\}', cleaned_output)
if json_match:
cleaned_output = json_match.group()
try:
parsed = ast.literal_eval(cleaned_output)
except:
parsed = json.loads(cleaned_output)
if 'emotion' in parsed and 'intent' in parsed:
emotion = parsed['emotion'].lower().strip()
intent = parsed['intent'].lower().strip()
emotion_valid = emotion in valid_emotions
intent_valid = intent in valid_intents
if emotion_valid and intent_valid:
return emotion, intent, True, None
elif not emotion_valid and not intent_valid:
return emotion, intent, False, "invalid_emotion_and_intent"
elif not emotion_valid:
return emotion, intent, False, "invalid_emotion"
else:
return emotion, intent, False, "invalid_intent"
emotion_found = None
intent_found = None
for emotion in valid_emotions:
if emotion in model_output.lower():
emotion_found = emotion
break
for intent in valid_intents:
if intent in model_output.lower():
intent_found = intent
break
if emotion_found and intent_found:
return emotion_found, intent_found, False, "labels_found_but_not_properly_formatted"
elif emotion_found:
return emotion_found, None, False, "only_emotion_found"
elif intent_found:
return None, intent_found, False, "only_intent_found"
return None, None, False, "no_labels_pattern"
except Exception as e:
return None, None, False, f"parsing_error_{str(e)}"
def parse_ground_truth(ground_truth):
if isinstance(ground_truth, dict):
return ground_truth.get('emotion', '').lower(), ground_truth.get('intent', '').lower()
elif isinstance(ground_truth, str):
cleaned_gt = ground_truth.strip()
cleaned_gt = re.sub(r"'intent'(\s*,\s*)'", r"'intent':\1'", cleaned_gt)
try:
parsed = ast.literal_eval(cleaned_gt)
return parsed.get('emotion', '').lower(), parsed.get('intent', '').lower()
except:
return '', ''
else:
return '', ''
def evaluate_emotion_based_intent_analysis(result_file_path):
with open(result_file_path, 'r', encoding='utf-8') as f:
results = json.load(f)
emotion_predictions = []
emotion_ground_truths = []
intent_predictions = []
intent_ground_truths = []
detailed_results = []
extraction_errors = defaultdict(list)
prediction_errors = defaultdict(list)
emotion_labels = ['happy', 'surprise', 'sad', 'disgust', 'anger', 'fear', 'neutral']
intent_labels = ['questioning', 'agreeing', 'acknowledging', 'encouraging',
'consoling', 'suggesting', 'wishing', 'neutral']
for item in results:
item_id = item['id']
model_output = item['model_output']
gt_emotion, gt_intent = parse_ground_truth(item['ground_truth'])
pred_emotion, pred_intent, is_valid, error_type = extract_emotion_intent_from_output(model_output)
detailed_item = {
'id': item_id,
'model_output': model_output,
'extracted_prediction': {
'emotion': pred_emotion,
'intent': pred_intent
},
'ground_truth': {
'emotion': gt_emotion,
'intent': gt_intent
},
'emotion_correct': pred_emotion == gt_emotion if pred_emotion else False,
'intent_correct': pred_intent == gt_intent if pred_intent else False,
'both_correct': (pred_emotion == gt_emotion and pred_intent == gt_intent) if (pred_emotion and pred_intent) else False,
'valid': is_valid
}
detailed_results.append(detailed_item)
if not is_valid:
extraction_errors[error_type].append(item_id)
else:
if pred_emotion != gt_emotion:
error_pattern = f"emotion_{gt_emotion}_to_{pred_emotion}"
prediction_errors[error_pattern].append(item_id)
if pred_intent != gt_intent:
error_pattern = f"intent_{gt_intent}_to_{pred_intent}"
prediction_errors[error_pattern].append(item_id)
if is_valid:
emotion_predictions.append(pred_emotion)
emotion_ground_truths.append(gt_emotion)
intent_predictions.append(pred_intent)
intent_ground_truths.append(gt_intent)
if len(emotion_predictions) == 0:
return {
'error': 'No valid predictions found',
'total_samples': len(results),
'extraction_errors': dict(extraction_errors)
}
emotion_accuracy = accuracy_score(emotion_ground_truths, emotion_predictions)
emotion_weighted_f1 = f1_score(emotion_ground_truths, emotion_predictions, average='weighted')
emotion_macro_f1 = f1_score(emotion_ground_truths, emotion_predictions, average='macro')
intent_accuracy = accuracy_score(intent_ground_truths, intent_predictions)
intent_weighted_f1 = f1_score(intent_ground_truths, intent_predictions, average='weighted')
intent_macro_f1 = f1_score(intent_ground_truths, intent_predictions, average='macro')
both_correct = sum(1 for item in detailed_results if item['both_correct'])
joint_accuracy = both_correct / len([item for item in detailed_results if item['valid']])
emotion_cm = confusion_matrix(emotion_ground_truths, emotion_predictions, labels=emotion_labels)
intent_cm = confusion_matrix(intent_ground_truths, intent_predictions, labels=intent_labels)
emotion_class_report = classification_report(emotion_ground_truths, emotion_predictions,
target_names=emotion_labels,
output_dict=True, zero_division=0)
intent_class_report = classification_report(intent_ground_truths, intent_predictions,
target_names=intent_labels,
output_dict=True, zero_division=0)
evaluation_result = {
'task_info': {
'task_name': 'emotion.based.intent.analysis',
'dataset': 'MC-EIU',
'evaluation_time': datetime.now().isoformat(),
'total_samples': len(results),
'valid_predictions': len(emotion_predictions),
'extraction_success_rate': round(len(emotion_predictions) / len(results), 4)
},
'metrics': {
'emotion_metrics': {
'ACC': round(emotion_accuracy, 4),
'WAF': round(emotion_weighted_f1, 4),
'Macro_F1': round(emotion_macro_f1, 4)
},
'intent_metrics': {
'ACC': round(intent_accuracy, 4),
'WAF': round(intent_weighted_f1, 4),
'Macro_F1': round(intent_macro_f1, 4)
},
'joint_metrics': {
'Joint_ACC': round(joint_accuracy, 4)
}
},
'per_class_metrics': {
'emotion': {
label: {
'precision': round(emotion_class_report[label]['precision'], 4),
'recall': round(emotion_class_report[label]['recall'], 4),
'f1_score': round(emotion_class_report[label]['f1-score'], 4),
'support': int(emotion_class_report[label]['support'])
} for label in emotion_labels if label in emotion_class_report
},
'intent': {
label: {
'precision': round(intent_class_report[label]['precision'], 4),
'recall': round(intent_class_report[label]['recall'], 4),
'f1_score': round(intent_class_report[label]['f1-score'], 4),
'support': int(intent_class_report[label]['support'])
} for label in intent_labels if label in intent_class_report
}
},
'confusion_matrices': {
'emotion': {
'labels': emotion_labels,
'matrix': emotion_cm.tolist()
},
'intent': {
'labels': intent_labels,
'matrix': intent_cm.tolist()
}
},
'error_analysis': {
'extraction_errors': {
error_type: {
'count': len(sample_ids),
'sample_ids': sample_ids
} for error_type, sample_ids in extraction_errors.items()
},
'prediction_errors': {
error_pattern: {
'count': len(sample_ids),
'sample_ids': sample_ids
} for error_pattern, sample_ids in prediction_errors.items()
}
},
'distribution': {
'emotion': {
'ground_truth': dict(Counter(emotion_ground_truths)),
'predictions': dict(Counter(emotion_predictions))
},
'intent': {
'ground_truth': dict(Counter(intent_ground_truths)),
'predictions': dict(Counter(intent_predictions))
}
}
}
base_name = result_file_path.replace('.json', '')
eval_output_file = f"{base_name}_evaluation.json"
with open(eval_output_file, 'w', encoding='utf-8') as f:
json.dump(evaluation_result, f, ensure_ascii=False, indent=2)
detailed_output_file = f"{base_name}_detailed_results.json"
with open(detailed_output_file, 'w', encoding='utf-8') as f:
json.dump(detailed_results, f, ensure_ascii=False, indent=2)
problem_samples = [item for item in detailed_results if not item['both_correct']]
if problem_samples:
problem_report_file = f"{base_name}_problem_samples.json"
with open(problem_report_file, 'w', encoding='utf-8') as f:
json.dump(problem_samples, f, ensure_ascii=False, indent=2)
print(f"Evaluation complete: {len(results)} samples")
print(f"Emotion metrics: ACC={evaluation_result['metrics']['emotion_metrics']['ACC']}, WAF={evaluation_result['metrics']['emotion_metrics']['WAF']}")
print(f"Intent metrics: ACC={evaluation_result['metrics']['intent_metrics']['ACC']}, WAF={evaluation_result['metrics']['intent_metrics']['WAF']}")
print(f"Joint accuracy: {evaluation_result['metrics']['joint_metrics']['Joint_ACC']}")
print(f"Extraction success rate: {evaluation_result['task_info']['extraction_success_rate']}")
print(f"Results saved to: {eval_output_file}")
if problem_samples:
print(f"Problematic samples: {len(problem_samples)}; see {problem_report_file} for details")
return evaluation_result
if __name__ == "__main__":
result_file = "model_result.json"
try:
evaluation_result = evaluate_emotion_based_intent_analysis(result_file)
except FileNotFoundError:
print(f"Error: file not found {result_file}")
except json.JSONDecodeError:
print(f"Error: invalid format for {result_file}")
except Exception as e:
print(f"Evaluation failed: {str(e)}")
|