-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data.py
More file actions
189 lines (149 loc) · 5.89 KB
/
prepare_data.py
File metadata and controls
189 lines (149 loc) · 5.89 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
Convert W&I corpus 2024 to JSONL format for training.
Usage:
uv run python prepare_data.py --input-dir ./corpus/whole-corpus --output-dir ./data
The 2024 corpus comes as a single TSV file with columns including:
- text: The essay text
- automarker_cefr_level: CEFR level from auto-marker (B1, B2, etc.)
- humannotator_cefr_level: CEFR level from human annotator (may be NA)
- split: train/dev/test (already pre-split!)
- is_final_version: TRUE/FALSE
We use the official splits and prefer human annotations when available.
"""
import csv
import json
from pathlib import Path
from collections import Counter
# CEFR level to numeric score (1-6 scale)
CEFR_TO_SCORE = {
"A1": 1.0,
"A2": 2.0,
"B1": 3.0,
"B2": 4.0,
"C1": 5.0,
"C2": 6.0,
}
def parse_wi_corpus_2024(input_dir: Path) -> dict[str, list[dict]]:
"""
Parse the Write & Improve 2024 corpus.
Returns:
Dict with keys 'train', 'dev', 'test' containing essay lists.
"""
# Find the main corpus file
corpus_file = input_dir / "en-writeandimprove2024-corpus.tsv"
if not corpus_file.exists():
# Try to find any TSV file
tsv_files = list(input_dir.glob("*.tsv"))
if tsv_files:
corpus_file = tsv_files[0]
else:
raise FileNotFoundError(f"No TSV file found in {input_dir}")
print(f"Reading corpus from: {corpus_file}")
essays = {"train": [], "dev": [], "test": []}
skipped = {"no_cefr": 0, "not_final": 0, "invalid_split": 0}
with open(corpus_file, encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t", quotechar='"')
for row in reader:
# Only use final versions of essays
is_final = row.get("is_final_version", "").upper() == "TRUE"
if not is_final:
skipped["not_final"] += 1
continue
# Get CEFR level (prefer human annotation, fallback to auto-marker)
cefr = row.get("humannotator_cefr_level", "").strip().upper()
if not cefr or cefr == "NA":
cefr = row.get("automarker_cefr_level", "").strip().upper()
if not cefr or cefr == "NA" or cefr not in CEFR_TO_SCORE:
skipped["no_cefr"] += 1
continue
# Get split
split = row.get("split", "").strip().lower()
if split not in essays:
skipped["invalid_split"] += 1
continue
# Get text
text = row.get("text", "").strip()
if not text:
continue
essays[split].append({
"id": row.get("public_essay_id", ""),
"text": text,
"cefr": cefr,
"score": CEFR_TO_SCORE[cefr],
})
print(f"\nSkipped essays:")
print(f" Not final version: {skipped['not_final']}")
print(f" No valid CEFR: {skipped['no_cefr']}")
print(f" Invalid split: {skipped['invalid_split']}")
return essays
def save_jsonl(data: list[dict], path: Path):
"""Save as JSONL (one JSON object per line)."""
with open(path, "w", encoding="utf-8") as f:
for item in data:
# Format for training: input/target pairs
f.write(json.dumps({
"input": item["text"],
"target": item["score"],
}, ensure_ascii=False) + "\n")
def print_stats(essays: list[dict], name: str):
"""Print dataset statistics."""
if not essays:
print(f"\n{name}: 0 essays")
return
cefr_counts = Counter(e["cefr"] for e in essays)
print(f"\n{name}:")
print(f" Total essays: {len(essays)}")
print(" CEFR distribution:")
for cefr in ["A1", "A2", "B1", "B2", "C1", "C2"]:
count = cefr_counts.get(cefr, 0)
pct = count / len(essays) * 100 if essays else 0
bar = "█" * int(pct / 5) # Simple bar chart
print(f" {cefr}: {count:4d} ({pct:5.1f}%) {bar}")
def main():
import argparse
parser = argparse.ArgumentParser(description="Prepare W&I corpus 2024 for training")
parser.add_argument(
"--input-dir",
required=True,
help="Directory containing en-writeandimprove2024-corpus.tsv"
)
parser.add_argument(
"--output-dir",
default="data",
help="Output directory for JSONL files"
)
args = parser.parse_args()
input_dir = Path(args.input_dir)
output_dir = Path(args.output_dir)
if not input_dir.exists():
print(f"Error: Input directory not found: {input_dir}")
print("\nExpected structure:")
print(" corpus/whole-corpus/en-writeandimprove2024-corpus.tsv")
return
# Parse corpus (already split into train/dev/test)
print("=" * 60)
print("Parsing W&I Corpus 2024")
print("=" * 60)
essays = parse_wi_corpus_2024(input_dir)
total = sum(len(split) for split in essays.values())
if total == 0:
print("\nError: No essays found. Check input directory and file format.")
return
print(f"\nLoaded {total} essays total (using official train/dev/test splits)")
print_stats(essays["train"], "Training Set")
print_stats(essays["dev"], "Dev Set")
print_stats(essays["test"], "Test Set")
# Save
output_dir.mkdir(parents=True, exist_ok=True)
save_jsonl(essays["train"], output_dir / "train.jsonl")
save_jsonl(essays["dev"], output_dir / "dev.jsonl")
save_jsonl(essays["test"], output_dir / "test.jsonl")
print(f"\n" + "=" * 60)
print("✅ Data preparation complete!")
print("=" * 60)
print(f"Saved to {output_dir}/")
print(f" train.jsonl: {len(essays['train']):,} samples")
print(f" dev.jsonl: {len(essays['dev']):,} samples")
print(f" test.jsonl: {len(essays['test']):,} samples")
if __name__ == "__main__":
main()