-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
executable file
·348 lines (268 loc) · 10.7 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
from utils import Dict
# Modulis atbild par treniņdatu datu ģenerēšanu no treniņfailiem (.json'iem)
#
# API:
# Data objekts
# Tam pakārtoti Category objekti
#
# Data(features, ...) : daudzpunktes vietā varētu būt storage ceļš/direktorija vai kas tāds
# Data.categories[name] = Category()
# Data.add(feature values)
# Category.add(value)
# In memory Datu klašu šabloni
# defaultā klase: in memory datubāze, saglabāšana varētu tikt realizēta teksta failos
class Data:
# varbūt te labāk dirname ?
def __init__(self, features, filename):
self.features = features
self.categories = {} # by name: vai nu frame type vai element type
self.filename = filename
self.names = tuple(feature.name for feature in features)
self.data = []
# load/save
def load(self):
# TODO: save in text file
# self.data = ...
# TODO: te no kaut kurienes ir jāielādē saraksta ar kategorijām, kuras ielādēt
# for name, category in self.categories.items():
# category.load()
pass
def save(self):
# TODO: save in text file
# rekursīvi izsauc kategoriju save
for name, category in self.categories.items():
category.save()
# TODO: kaut kur ir jāsaglabā saraksts ar kategorijām
pass
def reset(self):
for category in self.categories.values():
category.reset()
# darbības ar kategorijām
#
# datu ievade, izvade
#
# named -> indexed tuple
def indexed(self, *featureValues, **namedFeatureValues):
n = len(self.names)
data = [None]*n
if featureValues:
data[:min(n, len(featureValues))] = featureValues[:min(n, len(featureValues))]
for name, value in namedFeatureValues.items():
try:
data[self.names.index(name)] = value
except ValueError:
pass
return data
def add(self, *args, **kargs):
# konvertē no ievaddatiem uz pazīmju datiem (ievaddati ir token=..., tokens=... utt.), pazīmju dati: NLEMMA=..., LEMMA=..., ...
self.addData(**self.features(*args, **kargs))
def addData(self, *featureValues, **namedFeatureValues):
data = self.indexed(*featureValues, **namedFeatureValues)
index = len(self.data)
self.data.append(data)
return data
# search
def indexes(self, *featureValues, **namedFeatureValues):
# meklējamo datu šablons, None laukus uzskata par relaksētiem
# template = self.indexed(*featureValues, **namedFeatureValues)
# for index in range(len(self.data)):
# data = self.data[i]
# match = True
# # salīdzina pa komponentēm
# for d,t in zip(data, template):
# if t is None: # relaksēts parametrs
# continue
# if d != t:
# match = False
# break
# if match:
# yield index
# alternatīvs variants būtu meklēt pēc nosaukumiem
template = self.named(self.indexed(*featureValues, **namedFeatureValues))
components = [(self.names.index(name),value) for name,value in template.items() if name in self.names]
for index in range(len(self.data)):
data = self.data[i]
match = True
for component in components:
if data[component[0]] != component[1]:
match = False
break
if match:
yield index
# end
def cover(self, rules):
if type(rules) != list and type(rules) != tuple:
rules = [rules]
else:
rules = list(rules) # kopē sarakstu, lai nebojātu oriģinālu
for i in range(len(rules)):
conditions = [(self.names.index(condition.name), condition.op, condition.value)
for condition in rules[i].conditions if condition.name in self.names]
print(conditions)
rules[i] = Dict(value=rules[i].value, conditions=conditions)
for index in range(len(self.data)):
data = self.data[index]
match = True
for rule in rules:
for condition in rule.conditions:
if condition[1] == '==':
if data[condition[0]] != condition[2]:
match = False
elif condition[1] == '<=':
if data[condition[0]] > condition[2]:
match = False
elif condition[1] == '>':
if data[condition[0]] <= condition[2]:
match = False
elif condition[1] == 'in':
if data[condition[0]] not in condition[2]:
match = False
if not match:
break
if match and rule.value == False and rule != rules[-1]: # nav pēdējais, jo pēdējam mēs gribam saskaitīt
match = False
if not match:
break
if match:
yield index
def __iter__(self):
return iter(self.data)
def __len__(self):
return len(self.data)
# ko atgriež ? tuple vai dict ar key=value vērtībām ?
# var darīt tā: atgriezt tuple (jo tas ir efektīvāk) un piedāvāt konvertoru uz kv dict
def __getitem__(self, index):
return self.data[index]
def named(self, data):
named = Dict()
for name,value in zip(self.names, data):
named[name] = value
return named
class Category:
def __init__(self, data, name='TestCategory', filename='TestCategory.dat'):
self.data = data
self.name = name
self.filename = filename
self.values = []
def __getitem__(self, index):
return self.values[index]
def __len__(self):
return len(self.values)
def __iter__(self):
return iter(self.values)
def reset(self):
pass
# pievieno vērtību, atgriež indeksu (salīdzināšanai ?)
def add(self, value):
self.values.append(value)
# dotajai klasei/kategorijai atgriež cover/ok vērtības
# def cover(self, *featureValues, **namedFeatureValues):
# cover = 0
# ok = 0
# for index in self.data.indexes(*featureValues, **namedFeatureValues):
# cover +=1
# value = self[index]
# if value:
# ok += 1
# return Dict(cover=cover, ok=ok)
# vēl varētu viena likuma vietā filtrēt cauri vairākiem un tikai, ja rezultātā paliek pāri true (lai tiktu galā ar false)
def cover(self, rules):
if type(rules) != list and type(rules) != tuple:
rules = [rules]
cover = 0
ok = 0
for index in self.data.cover(rules):
value = self[index]
# if rule.value != value: pass
expectedValue = rules[-1].value
if value is not None:
cover +=1
if value == expectedValue:
ok += 1
return Dict(cover=cover, ok=ok)
def load(self):
pass
def save(self):
pass
# SQLite versijas, kas datus saglabā SQLite datubāzē
import sqlite3
class SQLiteData(Data):
def __init__(self, features, filename):
super().__init__(features, filename)
# kura klase kā tieši izmanto filename ir implementācijas specifiska informācija
if not self.filename.endswith('db3'):
self.filename += '.db3'
self.conn = sqlite3.connect(self.filename)
self.cur = self.conn.cursor()
def reset(self):
def featureType(feature):
if feature.type == int:
return 'INTEGER'
if feature.type == str:
return 'TEXT'
for category in self.categories.values():
category.reset()
# izveido datu tabulu
self.cur.execute("DROP INDEX IF EXISTS data_index;")
self.cur.execute("DROP TABLE IF EXISTS data;")
self.conn.commit()
self.cur.execute("CREATE TABLE IF NOT EXISTS data (%s);" % (', '.join(feature.name+' '+featureType(feature) for feature in self.features),))
self.cur.execute("CREATE INDEX IF NOT EXISTS data_index ON data (%s);" % (', '.join(feature.name for feature in self.features),))
self.conn.commit()
def addData(self, *featureValues, **namedFeatureValues):
data = super().addData(*featureValues, **namedFeatureValues)
self.cur.execute('INSERT INTO data VALUES('+','.join(('?',)*len(data))+');', data)
# self.conn.commit()
def save(self):
self.conn.commit()
pass
def load(self, noData=False):
# ielādē tabulu atmiņā no sqlite datubāzes
if not noData:
rows = self.cur.execute('SELECT * FROM data;')
for row in rows:
self.data.append(row)
for name in self.cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"):
name = name[0]
if name == 'data':
continue
category = SQLiteCategory(self, name)
self.categories[name] = category
if not noData:
category.load()
# privāti
def open(self):
pass
def close(self):
self.conn.close()
pass
class SQLiteCategory(Category):
def __init__(self, data, name):
super().__init__(data, name)
self.cur = self.data.conn.cursor()
def reset(self):
self.cur.execute("DROP INDEX IF EXISTS %s_index;" % (self.name,))
self.cur.execute("DROP TABLE IF EXISTS '%s';" % (self.name,))
self.data.conn.commit()
self.cur.execute("CREATE TABLE IF NOT EXISTS '%s' (value INTEGER);" % (self.name,))
self.cur.execute("CREATE INDEX IF NOT EXISTS %s_index ON '%s' (value);" % (self.name, self.name))
self.data.conn.commit()
def add(self, value):
super().add(value)
if value is not None:
value = int(value)
self.cur.execute("INSERT INTO '%s' VALUES(?);" % (self.name,), (value,))
# self.data.conn.commit()
def save(self):
self.data.conn.commit()
pass
def load(self):
# ielādē tabulu atmiņā no sqlite datubāzes
rows = self.cur.execute("SELECT * FROM '%s';" % (self.name,))
for row in rows:
row = row[0]
if row is not None:
self.values.append(bool(row))
else:
self.values.append(row)