File size: 2,609 Bytes
e98488f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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.")