-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation-rewrite.py
More file actions
83 lines (63 loc) · 2.72 KB
/
Copy pathevaluation-rewrite.py
File metadata and controls
83 lines (63 loc) · 2.72 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
# Evaluate BLEU and ROUGE-2 F1 scores for reformulation inferences
import glob
from evaluate import load
import pandas as pd
import numpy as np
import re
import sqlite3
RES_FOLDER = "db-rewrite"
bleu = load("bleu")
rouge = load("rouge")
results = {}
for db in glob.glob(f"{RES_FOLDER}/*/*.db"):
m = re.match(rf"{RES_FOLDER}/([a-zA-Z0-9.-]+)/([a-zA-Z0-9.-]+)-([0-9]+)\.db", db)
if not m:
print(f"❌ Invalid DB name: {db}")
continue
print(f"=== Evaluating {db} ===")
algo = m.group(1)
model = m.group(2)
run_num = m.group(3)
results[f"{algo}-{model}"] = []
conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute("SELECT text_old, text_new, chatgpt_answer FROM records")
rows = cursor.fetchall()
for row in rows:
text_old, text_new, chatgpt_answer = row
if chatgpt_answer is None:
continue
results[f"{algo}-{model}"].append((text_old, text_new, chatgpt_answer))
for k in results.keys():
all_references = []
all_hypotheses = []
individual_bleu_scores = []
individual_rouge2_f1_scores = []
print(f"=== Evaluating {k} ===")
for r in results[k]:
text_old, text_new, chatgpt_answer = r
reference = [text_new] # Target as a single string
hypothesis = chatgpt_answer # Predicted as a single string
all_references.append(reference)
all_hypotheses.append(hypothesis)
# Calculate individual BLEU score
bleu_result = bleu.compute(predictions=[hypothesis], references=[reference])
individual_bleu_scores.append(bleu_result['bleu'])
# Calculate individual ROUGE-2 F1 score
rouge_result = rouge.compute(predictions=[hypothesis], references=[reference])
individual_rouge2_f1_scores.append(rouge_result['rouge2'])
# Calculate BLEU and ROUGE-2 F1 scores using Hugging Face evaluate
if all_references and all_hypotheses:
bleu_results = bleu.compute(predictions=all_hypotheses, references=all_references)
rouge_results = rouge.compute(predictions=all_hypotheses, references=all_references)
average_bleu = bleu_results['bleu']
bleu_stdev = np.std(individual_bleu_scores)
average_rouge2_f1 = rouge_results['rouge2']
rouge2_f1_stdev = np.std(individual_rouge2_f1_scores)
print(f"Average BLEU Score for Reformulation Inferences: {average_bleu:.4f}")
print(f"Standard Deviation of BLEU Scores: {bleu_stdev:.4f}")
print(f"Average ROUGE-2 F1 Score for Reformulation Inferences: {average_rouge2_f1:.4f}")
print(f"Standard Deviation of ROUGE-2 F1 Scores: {rouge2_f1_stdev:.4f}")
else:
print("No BLEU or ROUGE-2 F1 scores calculated. Check the input files.")
print("\n\n")