-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (53 loc) · 2.03 KB
/
main.py
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
import os
from typing import List
from json_helper import *
from rouge_score import *
def create_data_directory():
if not os.path.exists("data"):
os.makedirs("data")
def check_dataset_file():
if not os.path.exists(os.path.join("data", "dataset.json")):
raise Exception("Dataset file (data/dataset.json) not found")
def main():
# Read dataset
print("Reading dataset...")
datasets = read_json(os.path.join("data", "dataset.json"))
scores = []
print("Calculating ROUGE scores...")
for dataset in datasets:
reference = dataset["reference"]
candidates_list = dataset["candidate"]
try:
for candidate in candidates_list:
rouge1_score = rouge1(candidate, reference)
rouge2_score = rouge2(candidate, reference)
rouge_l_score = rouge_l(candidate, reference)
scores.append({
"candidate": candidate,
"reference": reference,
"ROUGE-1": {
"precision": rouge1_score[0],
"recall": rouge1_score[1],
"f-measure": rouge1_score[2]
},
"ROUGE-2": {
"precision": rouge2_score[0],
"recall": rouge2_score[1],
"f-measure": rouge2_score[2]
},
"ROUGE-L": {
"precision": rouge_l_score[0],
"recall": rouge_l_score[1],
"f-measure": rouge_l_score[2]
}
})
except Exception as e:
print(f"Error processing candidate: {candidate}")
print(f"Error processing dataset: {e}")
print("Saving ROUGE scores...")
write_json(os.path.join("data", "scores.json"), scores, indent=True)
print("ROUGE scores saved to data/scores.json")
if __name__ == "__main__":
create_data_directory()
check_dataset_file()
main()