-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
51 lines (38 loc) · 1.43 KB
/
dataset.py
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
import torchtext
from torchtext import data
from torchtext.data import Field, BucketIterator
import spacy
import pandas as pd
nlp = spacy.load("en_core_web_sm")
def tokenize_en(text):
return [tok.text for tok in nlp.tokenizer(text)]
def load_csv(path):
train = pd.read_csv(path + '/train.csv', encoding='utf-8')
valid = pd.read_csv(path + '/valid.csv', lineterminator='\n')
test = pd.read_csv(path + '/test.csv', lineterminator='\n')
train = train.sample(100)
test = test.sample(100)
valid = valid.sample(100)
train_descs = list(train['description'].values)
train_slogans = list(train['slogan'].values)
valid_descs = list(valid['description'].values)
valid_slogans = list(valid['slogan'].values)
test_descs = list(test['decription'].values)
test_slogans = list(test['slogan'].values)
return train_descs, train_slogans, valid_descs, valid_slogans, test_descs, test_slogans
SRC = Field(tokenize = tokenize_en,
init_token = '<sos>',
eos_token = '<eos>',
include_lengths = True,
lower = False)
TRG = Field(tokenize = tokenize_en,
init_token = '<sos>',
eos_token = '<eos>',
lower = False)
class Seq2SeqDataset(data.Dataset):
def __init__(self, description, slogan, fields, **kwargs):
examples = []
fields = [('src', fields[0]), ('trg', fields[1])]
for d, s in zip(description, slogan):
examples.append(data.Example.fromlist([d, s], fields))
super(Seq2SeqDataset, self).__init__(examples, fields, **kwargs)