-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
119 lines (91 loc) · 3.68 KB
/
data.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
import os
from collections import namedtuple
import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
from tqdm import tqdm
####################################################################################
PUNCTUATION_VOCABULARY = ["_SPACE", ",COMMA", ".PERIOD", "?QUESTIONMARK", "!EXCLAMATIONMARK", ":COLON", ";SEMICOLON",
"-DASH"]
PUNCTUATION_MAPPING = {}
EOS_TOKENS = {".PERIOD", "?QUESTIONMARK", "!EXCLAMATIONMARK"}
END = "</S>"
UNK = "<UNK>"
SPACE = "_SPACE"
MAX_SUBSEQUENCE_LEN = 50
MAX_WORD_VOCABULARY_SIZE = 200000
####################################################################################
MData = namedtuple('MData', 'X, y, len')
def load(file):
fs = os.stat(file).st_size
resX = []
resY = []
with open(file, 'r') as f:
with tqdm(total=fs, unit='B', unit_scale=True, unit_divisor=1024) as pbar:
for l in f:
ld = eval(l)
resX.append(ld[0])
resY.append(keras.utils.to_categorical(ld[1], num_classes=len(PUNCTUATION_VOCABULARY)))
pbar.update(len(l))
return MData(X=np.array(resX).reshape(len(resX), len(resX[0])), y=np.array(resY), len=len(resX))
def get_size(file):
with open(file, 'r') as f:
for line in f:
return int(line)
return 0
class Generator(keras.utils.Sequence):
def __init__(self, X, y, batch_size):
self.X = X
self.y = y
self.batch_size = batch_size
def __len__(self):
return int(np.floor(len(self.X) / self.batch_size))
def __getitem__(self, batch_idx):
X = self.X[batch_idx * self.batch_size:(batch_idx + 1) * self.batch_size]
y = self.y[batch_idx * self.batch_size:(batch_idx + 1) * self.batch_size]
# print("shapes x, y", X.shape, y.shape)
return X, y
def on_epoch_end(self):
pass
def toDict(lines):
return dict((x.strip(), i) for (i, x) in enumerate(lines))
def readVocabulary(file):
with open(file, 'r', encoding='utf-8') as f:
return toDict(f.readlines())
class FeaturesGenerator(keras.utils.Sequence):
def __init__(self, m_data, features, batch_size):
self.batch_size = batch_size
self.m_data = m_data
self.features = features
def __len__(self):
return int(np.floor(self.m_data.len / self.batch_size))
def __getitem__(self, batch_idx):
# print("Get %d." % batch_idx, file=sys.stderr)
Xw = self.m_data.X[batch_idx * self.batch_size:(batch_idx + 1) * self.batch_size]
y = self.m_data.y[batch_idx * self.batch_size:(batch_idx + 1) * self.batch_size]
X = np.zeros(self.batch_size * self.features.len() * len(self.m_data.X[0]), dtype=np.float32).reshape(
(self.batch_size, len(self.m_data.X[0]), self.features.len()))
for i in range(len(Xw)):
for wi in range(len(Xw[i])):
self.features.setWordFeaturesTo(Xw[i, wi], X[i, wi])
# print("shapes x, y", X.shape, y.shape)
return X, y
def on_epoch_end(self):
pass
def count(self):
return self.m_data.len
def parse_line(t: tf.Tensor, feat, punct_vocab_size: int):
s = t.numpy()
ld = eval(s)
y = keras.utils.to_categorical(ld[1], num_classes=punct_vocab_size)
X = np.zeros(feat.len() * len(ld[0]), dtype=np.float32).reshape((len(ld[0]), feat.len()))
for wi in range(len(ld[0])):
feat.setWordFeaturesTo(ld[0][wi], X[wi])
return X, y
def parse_simple_line(t: tf.Tensor, punct_vocab_size: int):
s = t.numpy()
ld = eval(s)
y = keras.utils.to_categorical(ld[1], num_classes=punct_vocab_size)
X = np.array(ld[0])
# print("shapes x, y", X.shape, y.shape)
return X, y