-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
138 lines (105 loc) · 4.28 KB
/
datasets.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
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
import collections
import csv
import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
PADDING_TOKEN = 0
class IMDBReviewDataset(Dataset):
def __init__(self,
csv_path,
vocabulary=None,
vocab_min_count=10,
vocab_max_size=None,
review_max_length=200):
self.csv_path = csv_path
self.vocab_min_count = vocab_min_count
self.vocab_max_size = vocab_max_size
self.review_max_length = review_max_length - 2
self.data = []
with open(csv_path, 'r') as fp:
reader = csv.DictReader(fp, delimiter=',')
for row in tqdm(reader):
self.data.append((row['review'].split(' ')[:review_max_length],
int(row['sentiment'] == 'positive')))
if vocabulary is not None:
print('Using external vocabulary - vocab-related configs ignored.')
self.vocabulary = vocabulary
else:
self.vocabulary = self._build_vocabulary()
self.word2index = {w: i for (i, w) in enumerate(self.vocabulary)}
self.index2word = {i: w for (i, w) in enumerate(self.vocabulary)}
self.oov_token_id = self.word2index['OOV_TOKEN']
self.pad_token_id = self.word2index['PAD_TOKEN']
def __len__(self):
return len(self.data)
def __getitem__(self, index):
review, label = self.data[index]
review = ['BEGIN_TOKEN'] + review + ['END_TOKEN']
token_ids = [self.word2index.get(w, self.oov_token_id) for w in review]
return token_ids, label
def _build_vocabulary(self):
special_tokens = ['PAD_TOKEN', 'BEGIN_TOKEN', 'OOV_TOKEN', 'END_TOKEN']
counter = collections.Counter()
for review, _ in self.data:
counter.update(review)
vocab = counter.most_common(self.vocab_max_size - 4)
if self.vocab_min_count is not None:
vocab_tokens = [w for (w, c) in vocab if c >= self.vocab_min_count]
else:
vocab_tokens, _ = zip(vocab)
return special_tokens + vocab_tokens
def get_vocabulary(self):
return self.vocabulary
def print_statistics(self):
reviews, labels = zip(*self.data)
lengths = [len(x) for x in reviews]
positive = np.sum(labels)
negative = len(labels) - positive
print('Total instances: %d, positive: %d, negative: %d' %
(len(self.data), positive, negative))
print('Review lengths: max: %d, min: %d, mean: %d, median: %d' %
(max(lengths), min(lengths), np.mean(lengths), np.median(lengths)))
print('Vocabulary size: %d' % len(self.vocabulary))
return
def convert_to_chars(self, sequence):
if isinstance(sequence, torch.Tensor):
sequence = sequence.squeeze(0).detach().numpy().tolist()
return [self.index2word[x] for x in sequence]
def imdb_collate_fn(batch_data, padding_token_id=PADDING_TOKEN):
"""Padding variable-length sequences."""
batch_tokens, batch_labels = zip(*batch_data)
lengths = [len(x) for x in batch_tokens]
max_length = max(lengths)
padded_tokens = []
for tokens, length in zip(batch_tokens, lengths):
padded_tokens.append(tokens + [padding_token_id] * (max_length - length))
padded_tokens = torch.tensor(padded_tokens, dtype=torch.int64)
lengths = torch.tensor(lengths, dtype=torch.int64)
labels = torch.tensor(batch_labels, dtype=torch.int64)
return padded_tokens, lengths, labels
class TweetDataset(Dataset):
def __init__(self, txt_path, history_length):
self.txt_path = txt_path
self.history_length = history_length
with open(txt_path, 'rb') as fp:
raw_text = fp.read().strip().decode(encoding='utf-8')
self.vocab = self._build_vocabulary(raw_text)
self.char2index = {x: i for (i, x) in enumerate(self.vocab)}
self.index2char = {i: x for (i, x) in enumerate(self.vocab)}
self.data = [(raw_text[i:i + history_length], raw_text[i + history_length])
for i in range(len(raw_text) - history_length)]
return
def __len__(self):
return len(self.data)
def __getitem__(self, index):
history, label = self.data[index]
token_ids = np.array([self.char2index[x] for x in history])
label = self.char2index[label]
return token_ids, label
def _build_vocabulary(self, text):
special_tokens = [chr(2), chr(3)]
vocab = sorted(set(text))
return special_tokens + vocab
def get_vocabulary(self):
return self.vocab