Download convert.py.example from martinsr/wi_locness: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/datasets/martinsr/wi_locness/resolve/main/convert.py.example
- Command line
-
hf download hf://datasets/martinsr/wi_locness/convert.py.example
-
curl -L -o convert.py.example https://huggingface.co/datasets/martinsr/wi_locness/resolve/main/convert.py.example
2.61 kB
| 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) # if you need to get rid of None values, this is the place to do it! | |
| 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 "" # None means deletion | |
| 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}") | |
| # Push to your existing dataset, overwriting with proper splits | |
| #from datasets import DatasetDict | |
| #DatasetDict({"train": train_ds, "validation": dev_ds}).push_to_hub( | |
| # "martinsr/wi_locness", | |
| # config_name="wi", # or restructure configs as needed | |
| # commit_message="Add full BEA-2019 train split, convert to loader-compatible format" | |
| #) | |
| train_ds.to_parquet("wi_locness_train.parquet") | |
| dev_ds.to_parquet("wi_locness_dev.parquet") | |
| print("Done.") |