-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_score.py
55 lines (45 loc) · 1.88 KB
/
get_score.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
import argparse
import json
import numpy as np
import re
def calculate_average_score(judgment_file):
"""
Calculates the average score from a file containing judgments in JSON format (jsonl).
Handles Japanese text and extracts numerical scores.
Args:
judgment_file: Path to the file containing judgments, one per line.
Returns:
The average score as a float, or None if no valid judgments are found.
"""
judgments = []
with open(judgment_file, "r", encoding="utf-8") as f: # Specify UTF-8 encoding
for line in f:
try:
judgment_data = json.loads(line)
judgment_text = judgment_data["judgment"]
# Extract score from judgment text using regular expressions
match = re.search(r"(\d+(?:\.\d+)?)", judgment_text[-12:]) # Find numbers (including decimals)
if match:
score = float(match.group(1))
judgments.append(score)
else:
print(f"Warning: Could not parse score from judgment: '{judgment_text}'")
except json.JSONDecodeError:
print(f"Warning: Invalid JSON format in line: '{line.strip()}'")
if not judgments:
print("Error: No valid judgments found.")
return None
average_score = np.mean(judgments)
return average_score
def main(args):
"""
Parses command-line arguments and calls calculate_average_score.
"""
average_score = calculate_average_score(args.judgment_file)
if average_score is not None:
print(f"Average score: {average_score}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Calculate the average score from a judgment file.")
parser.add_argument("--judgment_file", type=str, required=True, help="Path to the judgment file.")
args = parser.parse_args()
main(args)