-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccuracy_evaluator.py
More file actions
162 lines (125 loc) · 6 KB
/
Copy pathaccuracy_evaluator.py
File metadata and controls
162 lines (125 loc) · 6 KB
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
import logging
from typing import Optional, List, Mapping
from dataclasses import dataclass
from metrics import is_metric_enabled
logger = logging.getLogger(__name__)
@dataclass
class AccuracyMetrics:
bleu: Optional[float] = None
rouge: Optional[float] = None
meteor: Optional[float] = None
levenshtein: Optional[float] = None
bertscore: Optional[float] = None
geval_reasoning: Optional[float] = None
geval_faithfulness: Optional[float] = None
classification_accuracy: Optional[float] = None
precision_macro: Optional[float] = None
recall_macro: Optional[float] = None
f1_macro: Optional[float] = None
f1_weighted: Optional[float] = None
class AccuracyEvaluator:
def __init__(self):
pass
def calculate_accuracy_metrics(
self,
predictions: List[str],
references: Optional[List[str]],
selected: Optional[Mapping[str, bool]] = None,
) -> Optional[AccuracyMetrics]:
if references is None or len(references) == 0:
logger.info("No reference outputs available, skipping accuracy metrics")
return None
if all(ref is None or ref == "" for ref in references):
logger.info("All reference outputs are empty, skipping accuracy metrics")
return None
logger.info(
f"Calculating accuracy metrics for {len(predictions)} predictions "
f"(selected={dict(selected) if selected is not None else 'all'})"
)
try:
metrics = AccuracyMetrics()
if is_metric_enabled(selected, 'bleu'):
metrics.bleu = self._calculate_bleu(predictions, references)
if is_metric_enabled(selected, 'rouge'):
metrics.rouge = self._calculate_rouge(predictions, references)
if is_metric_enabled(selected, 'meteor'):
metrics.meteor = self._calculate_meteor(predictions, references)
if is_metric_enabled(selected, 'levenshtein'):
metrics.levenshtein = self._calculate_levenshtein(predictions, references)
if is_metric_enabled(selected, 'bertscore'):
metrics.bertscore = self._calculate_bertscore(predictions, references)
def fmt(v): return f"{v:.4f}" if v is not None else "N/A"
logger.info(
f"Accuracy metrics calculated - "
f"BLEU={fmt(metrics.bleu)}, ROUGE={fmt(metrics.rouge)}, "
f"METEOR={fmt(metrics.meteor)}, Levenshtein={fmt(metrics.levenshtein)}, "
f"BERTScore={fmt(metrics.bertscore)}"
)
return metrics
except Exception as e:
logger.error(f"Error calculating accuracy metrics: {e}", exc_info=True)
return None
def _calculate_bleu(self, predictions: List[str], references: List[str]) -> Optional[float]:
try:
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
smoothing = SmoothingFunction().method1
scores = []
for pred, ref in zip(predictions, references):
if ref and pred:
score = sentence_bleu(
[ref.split()], pred.split(),
smoothing_function=smoothing
)
scores.append(score)
return sum(scores) / len(scores) if scores else 0.0
except Exception as e:
logger.error(f"Error calculating BLEU: {e}")
return None
def _calculate_rouge(self, predictions: List[str], references: List[str]) -> Optional[float]:
try:
from rouge_score import rouge_scorer
scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)
scores = []
for pred, ref in zip(predictions, references):
if ref and pred:
score = scorer.score(ref, pred)
scores.append(score['rougeL'].fmeasure)
return sum(scores) / len(scores) if scores else 0.0
except Exception as e:
logger.error(f"Error calculating ROUGE: {e}")
return None
def _calculate_meteor(self, predictions: List[str], references: List[str]) -> Optional[float]:
try:
from nltk.translate.meteor_score import meteor_score
from nltk import word_tokenize
scores = []
for pred, ref in zip(predictions, references):
if ref and pred:
score = meteor_score([word_tokenize(ref)], word_tokenize(pred))
scores.append(score)
return sum(scores) / len(scores) if scores else 0.0
except Exception as e:
logger.error(f"Error calculating METEOR: {e}")
return None
def _calculate_levenshtein(self, predictions: List[str], references: List[str]) -> Optional[float]:
try:
from Levenshtein import distance
scores = []
for pred, ref in zip(predictions, references):
if ref and pred:
max_len = max(len(ref), len(pred))
if max_len > 0:
similarity = 1 - (distance(ref, pred) / max_len)
scores.append(similarity)
return sum(scores) / len(scores) if scores else 0.0
except Exception as e:
logger.error(f"Error calculating Levenshtein: {e}")
return None
def _calculate_bertscore(self, predictions: List[str], references: List[str]) -> Optional[float]:
try:
from bert_score import score
P, R, F1 = score(predictions, references, lang='en', verbose=False)
return F1.mean().item()
except Exception as e:
logger.error(f"Error calculating BERTScore: {e}")
return None