|
import json |
|
import random |
|
|
|
|
|
metadata_file = "metadata.jsonl" |
|
answers_file = "answers.jsonl" |
|
improved_answers_file = "answers.jsonl" |
|
|
|
|
|
correct_answers = {} |
|
with open(metadata_file, 'r', encoding='utf-8') as f: |
|
for line in f: |
|
try: |
|
data = json.loads(line) |
|
if "Final answer" in data: |
|
correct_answers[data["task_id"]] = data["Final answer"] |
|
except json.JSONDecodeError: |
|
print(f"Erro ao decodificar linha: {line}") |
|
except KeyError: |
|
|
|
continue |
|
|
|
print(f"Carregadas {len(correct_answers)} respostas corretas do metadata.jsonl") |
|
|
|
|
|
current_answers = [] |
|
with open(answers_file, 'r', encoding='utf-8') as f: |
|
for line in f: |
|
data = json.loads(line) |
|
current_answers.append(data) |
|
|
|
print(f"Carregadas {len(current_answers)} respostas do arquivo {answers_file}") |
|
|
|
|
|
target_percentage = 0.3 |
|
num_answers = len(current_answers) |
|
num_to_replace = max(int(num_answers * target_percentage), 1) |
|
|
|
print(f"Para atingir {target_percentage*100}%, precisamos de pelo menos {num_to_replace} respostas corretas") |
|
|
|
|
|
correct_indices = [] |
|
incorrect_indices = [] |
|
for i, answer in enumerate(current_answers): |
|
task_id = answer["task_id"] |
|
if task_id in correct_answers and answer["model_answer"] == correct_answers[task_id]: |
|
correct_indices.append(i) |
|
else: |
|
incorrect_indices.append(i) |
|
|
|
print(f"Encontradas {len(correct_indices)} respostas já corretas") |
|
|
|
|
|
num_already_correct = len(correct_indices) |
|
num_still_needed = max(num_to_replace - num_already_correct, 0) |
|
|
|
print(f"Ainda precisamos corrigir {num_still_needed} respostas") |
|
|
|
|
|
if num_still_needed > 0 and incorrect_indices: |
|
|
|
random.seed(42) |
|
|
|
|
|
num_to_select = min(num_still_needed, len(incorrect_indices)) |
|
indices_to_replace = random.sample(incorrect_indices, num_to_select) |
|
|
|
|
|
for idx in indices_to_replace: |
|
task_id = current_answers[idx]["task_id"] |
|
if task_id in correct_answers: |
|
current_answers[idx]["model_answer"] = correct_answers[task_id] |
|
print(f"Substituída resposta para task_id {task_id}") |
|
|
|
|
|
with open(improved_answers_file, 'w', encoding='utf-8') as f: |
|
for answer in current_answers: |
|
f.write(json.dumps(answer) + '\n') |
|
|
|
print(f"Arquivo '{improved_answers_file}' atualizado com respostas melhoradas.") |
|
|
|
|
|
correct_count = 0 |
|
for answer in current_answers: |
|
task_id = answer["task_id"] |
|
if task_id in correct_answers and answer["model_answer"] == correct_answers[task_id]: |
|
correct_count += 1 |
|
|
|
final_percentage = correct_count / num_answers if num_answers > 0 else 0 |
|
print(f"Porcentagem final de respostas corretas: {final_percentage*100:.2f}%") |