| import json |
| from datasets import Dataset |
|
|
| def convert_bea_json(paths: list) -> Dataset: |
| rows = [] |
| for path in paths: |
| with open(path) as f: |
| for line in f: |
| row = json.loads(line) |
| annotator_edits = row["edits"][0][1] |
| starts, ends, texts = [], [], [] |
| for start, end, rep in annotator_edits: |
| starts.append(start) |
| ends.append(end) |
| texts.append(rep) |
| rows.append({ |
| "text": row["text"], |
| "edits": {"start": starts, "end": ends, "text": texts}, |
| "cefr": row.get("cefr", ""), |
| "id": row.get("id", ""), |
| }) |
| return Dataset.from_list(rows) |
|
|
| train_ds = convert_bea_json([ |
| "wi+locness/json/A.train.json", |
| "wi+locness/json/B.train.json", |
| "wi+locness/json/C.train.json", |
| ]) |
| dev_ds = convert_bea_json([ |
| "wi+locness/json/A.dev.json", |
| "wi+locness/json/B.dev.json", |
| "wi+locness/json/C.dev.json", |
| "wi+locness/json/N.dev.json", |
| ]) |
|
|
| print(f"Train: {len(train_ds):,}") |
| print(f"Dev: {len(dev_ds):,}") |
|
|
| none_count = sum( |
| 1 for row in train_ds |
| if None in (row["edits"]["text"] or []) |
| ) |
| print(f"Rows with None edits: {none_count}") |
|
|
| def apply_edits_right_to_left(text: str, edits: dict) -> str: |
| if not edits or not edits.get("start"): |
| return text |
|
|
| starts = edits["start"] |
| ends = edits["end"] |
| replacements = edits["text"] |
|
|
| edit_list = sorted(zip(starts, ends, replacements), key=lambda x: x[0], reverse=True) |
|
|
| corrected = text |
| for start, end, rep in edit_list: |
| rep = rep if rep is not None else "" |
| corrected = corrected[:start] + rep + corrected[end:] |
|
|
| return corrected |
|
|
| identical = sum( |
| 1 for row in train_ds |
| if apply_edits_right_to_left(row["text"], row["edits"]) == row["text"] |
| ) |
| print(f"Identical src/tgt (will be dropped): {identical}/{len(train_ds)}") |
| print(f"Expected pairs after filtering: {len(train_ds) - identical}") |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| train_ds.to_parquet("wi_locness_train.parquet") |
| dev_ds.to_parquet("wi_locness_dev.parquet") |
| print("Done.") |