-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdga_reader.py
More file actions
143 lines (105 loc) · 4.18 KB
/
dga_reader.py
File metadata and controls
143 lines (105 loc) · 4.18 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
from __future__ import print_function
from __future__ import division
import os
import codecs
import collections
from random import shuffle
import numpy as np
import pickle
class Vocab:
def __init__(self, token2index=None, index2token=None):
self._token2index = token2index or {}
self._index2token = index2token or []
def feed(self, token):
if token not in self._token2index:
# allocate new index for this token
index = len(self._token2index)
self._token2index[token] = index
self._index2token.append(token)
return self._token2index[token]
@property
def size(self):
return len(self._token2index)
def token(self, index):
return self._index2token[index]
def __getitem__(self, token):
index = self.get(token)
if index is None:
raise KeyError(token)
return index
def get(self, token, default=None):
return self._token2index.get(token, default)
def change(self, tokens):
res = ""
for token in tokens:
res += str(self.token(token))
return res
def save(self, filename):
with open(filename, 'wb') as f:
pickle.dump((self._token2index, self._index2token), f, pickle.HIGHEST_PROTOCOL)
@classmethod
def load(cls, filename):
with open(filename, 'rb') as f:
token2index, index2token = pickle.load(f)
return cls(token2index, index2token)
def load_data(data_dir, max_word_length):
char_vocab = Vocab()
char_vocab.feed(' ') # blank is at index 0 in char vocab
actual_max_word_length = 0
char_tokens = collections.defaultdict(list)
for fname in ['train']:
print('reading', fname)
with codecs.open(os.path.join(data_dir, fname + '.txt'), 'r', 'utf-8') as f:
for line in f:
line = line.strip()
if len(line) > max_word_length:
continue
# line += '*'
# line = line.split(".")[0]
char_array = [char_vocab.feed(c) for c in line]
char_tokens[fname].append(char_array)
actual_max_word_length = max(actual_max_word_length, len(char_array))
print('actual longest token length is:', actual_max_word_length)
print('size of char vocabulary:', char_vocab.size)
assert actual_max_word_length <= max_word_length
# now we know the sizes, create tensors
char_tensors = {}
char_lens = {}
for fname in ['train']:
char_tensors[fname] = np.zeros([len(char_tokens[fname]), actual_max_word_length], dtype=np.int32)
char_lens[fname] = np.zeros([len(char_tokens[fname])], dtype=np.int32)
for i, char_array in enumerate(char_tokens[fname]):
char_tensors[fname][i, :len(char_array)] = char_array
char_lens[fname][i] = len(char_array)
return char_vocab, char_tensors, char_lens, actual_max_word_length
class DataReader:
def __init__(self, char_tensor, char_lens, batch_size):
max_word_length = char_tensor.shape[1]
rollup_size = char_tensor.shape[0] // batch_size * batch_size
char_tensor = char_tensor[: rollup_size]
char_lens = char_lens[: rollup_size]
self.indexes = list(range(rollup_size // batch_size))
shuffle(self.indexes)
# round down length to whole number of slices
x_batches = char_tensor.reshape([batch_size, -1, max_word_length])
y_batches = char_lens.reshape([batch_size, -1])
x_batches = np.transpose(x_batches, axes=(1, 0, 2))
y_batches = np.transpose(y_batches, axes=(1, 0))
self._x_batches = list(x_batches)
self._y_batches = list(y_batches)
self.batch_size = batch_size
self.length = len(self._x_batches)
def shuf(self):
shuffle(self.indexes)
def iter(self):
for i in self.indexes:
yield self._x_batches[i], self._y_batches[i]
if __name__ == '__main__':
_, ct, cl, _ = load_data('dga_data', 65)
print(ct.keys())
count = 0
for x, y in DataReader(ct['train'], cl['train'], 35).iter():
count += 1
print(y)
if count > 0:
break