-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_dataset.py
More file actions
52 lines (43 loc) · 1.55 KB
/
split_dataset.py
File metadata and controls
52 lines (43 loc) · 1.55 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
import csv
import os
from csv import DictWriter
def load_data(task):
nrows = 300 if task == 'recognition' else 301
with open("data/annotations.csv") as f:
fieldnames = ['id', 'lemma_id', 'de_term', 'de_freq', 'pos', 'pos_perc', 'bar', 'bar_freq', 'ld', 'label',
'contexts']
reader = csv.DictReader(f, fieldnames=fieldnames)
next(reader) # skip header
offset = nrows
dev, test, full = [], [], []
for i, row in enumerate(reader):
if task == "translation" and row["label"] != "yes":
continue
if offset > 0:
dev.append(row)
offset -= 1
else:
test.append(row)
full.append(row)
pass
return dev, test, full
def write_data(data, task, split, fieldnames):
filename = f"data/{task}_{split}.csv"
if not os.path.exists(filename):
with open(filename, "w") as f:
writer = DictWriter(f,fieldnames=fieldnames)
writer.writeheader()
for r in data:
if r["pos"] == "ADV" and r["label"] == "inflected":
r["label"] = "no"
writer.writerow(r)
else:
print(f"Skipping, file exists {filename}")
def main():
for task in ["recognition", "translation"]:
dev, test, full = load_data(task=task)
fieldnames = list(dev[0].keys())
write_data(dev, task, "dev", fieldnames)
write_data(test, task, "test", fieldnames)
if __name__ == '__main__':
main()