-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.py
More file actions
203 lines (154 loc) · 6.42 KB
/
generator.py
File metadata and controls
203 lines (154 loc) · 6.42 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sqlite3
import numpy as np
class DataGenerator:
"""
A DataGenerator class that reads data from a SQLite database and returns in a batch
"""
batch_size = 32
maxlen = 40
database_path=''
counterTrain = 0
counterVal = 0
counterTest = 0
one_hot_mode=False
def __init__(self, database_path, batch_size=32, maxlen=40, one_hot_mode=False):
"""
Constructor method for the DataGenerator class
# Arguments
database_path: String
Name of the folder that contains all the databases
batch_size: Integer
Size of each batch, Default size is 32
maxlen: Integer
Max length of each sequence, Default is 40
"""
# Setting the One Hot Mode
self.one_hot_mode = one_hot_mode
# If there is a batch size provided, then setting it, else using the default
if batch_size > 0:
self.batch_size = batch_size
# If there is max length, then setting it, else using the default
if maxlen > 0:
self.maxlen = maxlen
# If there is database, then setting it, else thworing an ValueError
if database_path and database_path != '':
self.database_path = database_path
else:
raise ValueError("Please provide an valid database link")
def ont_hot(self, seq, nxt):
"""
DataGenerator class method one_hot for converting sequence into ont hot ventors
# Arguments
seq: List
List of sequences
nxt: List
List of next characters
"""
# Initializng numpy arrays
if self.one_hot_mode:
x = np.zeros((self.batch_size, self.maxlen, 128), dtype=np.bool)
y = np.zeros((self.batch_size, 128), dtype=np.bool)
# Iterating through the seq and nxt
for i, sentence in enumerate(seq):
for t, char in enumerate(sentence):
if self.one_hot_mode:
if char < 0 or char > 128:
char = 97
x[i, t, char] = 1
if nxt[i] < 0 or nxt[i] > 128:
y[i, 97] = 1
else:
y[i, nxt[i]] = 1
if self.one_hot_mode:
return x, y
else:
return np.array(seq), y
def trainGenerator(self):
"""
DataGenerator class trainGenerator method generates batches of data
# Arguments
No arguments
"""
while True:
# Initializng a SQLite database connection
# TODO: Need to find a better way to connect with db
connection = sqlite3.connect(self.database_path + '/sequence_train.db')
cursor = connection.cursor()
# SQL query
sql = "SELECT * FROM reviews LIMIT {} OFFSET {};".format(self.batch_size, self.counterTrain * self.batch_size)
# Updateing the counter
self.counterTrain += 1
# Executing the sql
cursor.execute(sql)
# Fetching the data
rows = cursor.fetchall()
seq_arr = []
nxt_arr = []
# Converting the charcacters into ASCII numbers
for seq, nxt in rows:
temp = []
for char in seq:
temp.append(ord(char))
seq_arr.append(temp)
nxt_arr.append(ord(nxt))
yield self.ont_hot(seq_arr, nxt_arr)
def validationGenerator(self):
"""
DataGenerator class generator method validationGenerates batches of data
# Arguments
No arguments
"""
while True:
# Initializng a SQLite database connection
# TODO: Need to find a better way to connect with db
connection = sqlite3.connect(self.database_path + '/sequence_val.db')
cursor = connection.cursor()
# SQL query
sql = "SELECT * FROM reviews LIMIT {} OFFSET {};".format(self.batch_size, self.counterVal * self.batch_size)
# Updateing the counter
self.counterVal += 1
# Executing the sql
cursor.execute(sql)
# Fetching the data
rows = cursor.fetchall()
seq_arr = []
nxt_arr = []
# Converting the charcacters into ASCII numbers
for seq, nxt in rows:
temp = []
for char in seq:
temp.append(ord(char))
seq_arr.append(temp)
nxt_arr.append(ord(nxt))
yield self.ont_hot(seq_arr, nxt_arr)
def testGenerator(self):
"""
DataGenerator class testGenerator method generates batches of data
# Arguments
No arguments
"""
# Counter for tracking the index
# counter = 0
while True:
# Initializng a SQLite database connection
# TODO: Need to find a better way to connect with db
connection = sqlite3.connect(self.database_path + '/sequence_test.db')
cursor = connection.cursor()
# SQL query
sql = "SELECT * FROM reviews LIMIT {} OFFSET {};".format(self.batch_size, self.counterTest * self.batch_size)
# Updateing the counter
self.counterTest += 1
# Executing the sql
cursor.execute(sql)
# Fetching the data
rows = cursor.fetchall()
seq_arr = []
nxt_arr = []
# Converting the charcacters into ASCII numbers
for seq, nxt in rows:
temp = []
for char in seq:
temp.append(ord(char))
seq_arr.append(temp)
nxt_arr.append(ord(nxt))
yield self.ont_hot(seq_arr, nxt_arr)