| |
| """ |
| Regenerate JSONL splits from dataset.csv |
| Useful if you lost the original JSONL files |
| """ |
|
|
| import pandas as pd |
| import json |
|
|
| def csv_to_jsonl(csv_path="dataset.csv", output_dir="."): |
| df = pd.read_csv(csv_path) |
|
|
| |
| records = [] |
| for _, row in df.iterrows(): |
| record = { |
| "id": row["id"], |
| "translation": { |
| "en": row["english"], |
| "kab": row["kabyle"] |
| }, |
| "category": row["category"], |
| "subcategory": row["subcategory"], |
| "domain": "road_traffic", |
| "complexity": row["complexity"], |
| "tokens_en": int(row["tokens_en"]), |
| "tokens_kab": int(row["tokens_kab"]) |
| } |
| records.append(record) |
|
|
| |
| train, val, test = [], [], [] |
|
|
| for r in records: |
| idx = int(r["id"].split("_")[-1]) |
| cat = r["category"] |
|
|
| |
| if cat == "dangers": |
| if idx % 10 < 7: |
| train.append(r) |
| elif idx % 10 < 8: |
| val.append(r) |
| else: |
| test.append(r) |
| |
| elif cat == "prohibitions": |
| if idx % 10 < 6: |
| train.append(r) |
| elif idx % 10 < 8: |
| val.append(r) |
| else: |
| test.append(r) |
| |
| elif cat == "obligations": |
| if idx % 10 < 5: |
| train.append(r) |
| elif idx % 10 < 7: |
| val.append(r) |
| else: |
| test.append(r) |
| |
| else: |
| if idx % 10 < 4: |
| train.append(r) |
| elif idx % 10 < 7: |
| val.append(r) |
| else: |
| test.append(r) |
|
|
| |
| for name, data in [("train.jsonl", train), ("validation.jsonl", val), ("test.jsonl", test)]: |
| filepath = f"{output_dir}/{name}" |
| with open(filepath, 'w', encoding='utf-8') as f: |
| for item in data: |
| f.write(json.dumps(item, ensure_ascii=False) + '\n') |
| print(f"✓ {name}: {len(data)} entries") |
|
|
| return train, val, test |
|
|
| if __name__ == "__main__": |
| print("Regeneration des fichiers JSONL depuis dataset.csv...") |
| csv_to_jsonl() |
| print("\nDone!") |
|
|