#!/usr/bin/env python3 import json input_path = "magpie_conversations_cemig_v0.jsonl" output_path = "magpie_conversations_cemig_v0.jsonl" ORDER = ["pedagogica", "aplicada", "objetiva", "instrucional"] ALIASES = {"objetica": "objetiva"} def norm_style(s): if not isinstance(s, str): return None s = s.strip().lower() return ALIASES.get(s, s) def order_idx(s): s = norm_style(s) return ORDER.index(s) if s in ORDER else len(ORDER) records = [] with open(input_path, encoding="utf-8") as f: for i, line in enumerate(f): if not line.strip(): continue obj = json.loads(line) style = obj.get("question_style") cid = obj.get("context_id") ns = norm_style(style) if ns and ns != style: obj["question_style"] = ns key = (order_idx(ns), cid if isinstance(cid, int) else float("inf"), i) records.append((key, obj)) records.sort(key=lambda x: x[0]) with open(output_path, "w", encoding="utf-8") as out: for _, obj in records: out.write(json.dumps(obj, ensure_ascii=False) + "\n")