|
|
import json |
|
|
import re |
|
|
import argparse |
|
|
|
|
|
|
|
|
|
|
|
letter_to_number = { |
|
|
"A": "0", |
|
|
"B": "1", |
|
|
"C": "2", |
|
|
"D": "3" |
|
|
} |
|
|
|
|
|
|
|
|
def extract_predicted_answer(text): |
|
|
|
|
|
match = re.search(r'\b[A-D]\b', text) |
|
|
if match: |
|
|
return match.group(0) |
|
|
return None |
|
|
|
|
|
|
|
|
def calculate_accuracy(file_path): |
|
|
total = 0 |
|
|
correct = 0 |
|
|
|
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
|
for line in file: |
|
|
item = json.loads(line) |
|
|
total += 1 |
|
|
pred_letter = extract_predicted_answer(item['text']) |
|
|
true_answer = item['answer'] |
|
|
if pred_letter: |
|
|
pred = letter_to_number[pred_letter] |
|
|
true_answer = str(item['answer']) |
|
|
if pred == true_answer: |
|
|
correct += 1 |
|
|
|
|
|
|
|
|
accuracy = (correct / total) * 100 |
|
|
|
|
|
|
|
|
print(f"总样本数: {total}") |
|
|
print(f"正确样本数: {correct}") |
|
|
print(f"错误样本数: {total - correct}") |
|
|
print(f"正确率: {accuracy:.2f}%") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
parser = argparse.ArgumentParser(description="Evaluate model accuracy from JSONL file.") |
|
|
parser.add_argument("file_path", type=str, help="Path to the JSONL file containing inference results.") |
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
|
calculate_accuracy(args.file_path) |
|
|
|