-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_loader.py
More file actions
71 lines (53 loc) · 2.4 KB
/
data_loader.py
File metadata and controls
71 lines (53 loc) · 2.4 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
import os
import spacy
import torchtext
from utils import (
create_debug_csv,
create_multi30k,
create_iwslt,
create_dummy_fixed_length_csv,
create_dummy_variable_length_csv,
get_or_create_dir,
load_from_csv
)
# load tokenizers for german and english
spacy_de = spacy.load('de')
spacy_en = spacy.load('en')
def tokenize_de(text):
return [token.text for token in spacy_de.tokenizer(text)]
def tokenize_en(text):
return [token.text for token in spacy_en.tokenizer(text)]
def tokenize_dummy(text):
return text.split(' ')
def load_debug(config, device):
csv_dir_path = get_or_create_dir('.data', 'debug')
if not os.path.exists(f'{csv_dir_path}/train.csv'):
create_debug_csv()
return load_from_csv(config, csv_dir_path, tokenize_de, tokenize_en, device)
def load_dummy_fixed_length(config, device):
csv_dir_path = get_or_create_dir('.data', 'dummy_fixed_length')
if not os.path.exists(f'{csv_dir_path}/train.csv'):
create_dummy_fixed_length_csv()
return load_from_csv(config, csv_dir_path, tokenize_dummy, tokenize_dummy, device)
def load_dummy_variable_length(config, device):
csv_dir_path = get_or_create_dir('.data', 'dummy_variable_length')
if not os.path.exists(f'{csv_dir_path}/train.csv'):
create_dummy_variable_length_csv()
return load_from_csv(config, csv_dir_path, tokenize_dummy, tokenize_dummy, device)
def load_iwslt(config, device):
csv_dir_path = get_or_create_dir('.data', 'iwslt')
if not os.path.exists(f'{csv_dir_path}/train.csv'):
if not os.path.exists(f'{csv_dir_path}/de-en'):
source_field = torchtext.data.Field(tokenize=tokenize_de)
target_field = torchtext.data.Field(tokenize=tokenize_en)
torchtext.datasets.IWSLT.splits(exts=('.de', '.en'), fields=(source_field, target_field))
create_iwslt()
return load_from_csv(config, csv_dir_path, tokenize_de, tokenize_en, device)
def load_multi30k(config, device):
csv_dir_path = get_or_create_dir('.data', 'multi30k')
if not os.path.exists(f'{csv_dir_path}/train.csv'):
source_field = torchtext.data.Field(tokenize=tokenize_de)
target_field = torchtext.data.Field(tokenize=tokenize_en)
torchtext.datasets.Multi30k.splits(exts=('.de', '.en'), fields=(source_field, target_field))
create_multi30k()
return load_from_csv(config, csv_dir_path, tokenize_de, tokenize_en, device)