-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
157 lines (138 loc) · 4.52 KB
/
Parser.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
import AutoSchemer
import csv, itertools
import re
from random import randint
#class Types:
# num_types = 4
# INT, FLOAT, STRING, DATE = range(num_types)
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
enums['num_types'] = len(reverse)
return type('Enum', (), enums)
# TypeGuesser uses histogram heuristics to guess the type of each column
class TypeGuesser(object):
Types = enum('INT', 'FLOAT', 'VARCHAR', 'DATE')
def __init__(self):
self.count = [0 for _ in range(self.Types.num_types)]
# can improve this metric
def add(self, val):
try:
float(val)
self.count[self.Types.FLOAT]+=1
except ValueError:
try:
int(val)
self.count[self.Types.INT]+=1
except ValueError:
self.count[self.Types.VARCHAR] += 1
def get_type(self):
t = max(xrange(len(self.count)), key=self.count.__getitem__)
return self.Types.reverse_mapping[t]
def parse_simple(file):
data = []
with open(file, 'rb') as csvfile:
reader, reader2 = itertools.tee(csv.reader(csvfile))
data = [set() for _ in next(reader)]
columns = range(len(data))
tgs = [TypeGuesser() for _ in columns]
count = 0
for row in reader2:
count += 1
for j, v in enumerate(row):
#de = re.escape(row[j])
de = row[j]
data[j].add(de)
tgs[j].add(de)
distinctRows = [(i,len(x)) for i,x in enumerate(data)]
col_order = [i for i,v in sorted(distinctRows, key=lambda v: v[1], reverse=True)]
types = [tg.get_type() for tg in tgs]
return (distinctRows, col_order, types)
def parse_prune_simple(file, threshold):
data = []
count = 0
with open(file, 'rb') as csvfile:
reader, reader2 = itertools.tee(csv.reader(csvfile))
data = [set() for _ in next(reader)]
columns = range(0,len(data))
tgs = [TypeGuesser() for _ in columns]
#print len(tgs)
for row in reader2:
count += 1
for j, v in enumerate(row):
#de = re.escape(row[j])
#print j,v
de = row[j]
data[j].add(de)
tgs[j].add(de)
distinctRows = []
separate_columns = []
for i,x in enumerate(data):
print i, len(x), count
if len(x) < threshold * count:
distinctRows.append((i, len(x)))
else:
separate_columns.append(i)
#distinctRows = [(i,len(x)) for i,x in enumerate(data)]
col_order = [i for i,v in sorted(distinctRows, key=lambda v: v[1], reverse=True)]
types = [tg.get_type() for tg in tgs]
return (distinctRows, col_order, types, separate_columns)
def parse_cords(file):
k = 5000
data = []
sampled_data = []
with open(file, 'rb') as csvfile:
reader, reader2 = itertools.tee(csv.reader(csvfile))
data = [set() for _ in next(reader)]
sampled_data = [[] for j in range(min(len(data), k))]
k = len(sampled_data)
columns = range(len(data))
tgs = [TypeGuesser() for _ in columns]
count = 0
for row in reader2:
update = False
index = 0
count += 1;
if (count < k - 1):
index = count;
update = True
else:
if (randint(0,count-1) < k):
# replace with k/count probability
update = True
index = randint(0, k-1)
if (update):
sampled_data[index] = []
for j, v in enumerate(row):
#de = re.escape(row[j])
de = row[j]
tgs[j].add(de)
sampled_data[index].append(de)
# calculate data after figuring out waht sampled data is
data = [set() for i in sampled_data]
for j in sampled_data:
for col, celldata in enumerate(j):
data[col].add(celldata)
distinctRows = [len(x) for i, x in enumerate(data)]
types = [tg.get_type() for tg in tgs]
return (sampled_data, distinctRows, columns, types)
def parse_prune_cords(file):
data = []
with open(file, 'rb') as csvfile:
reader, reader2 = itertools.tee(csv.reader(csvfile, delimiter=',', quotechar='|'))
data = [set() for _ in next(reader)]
columns = range(len(data))
tgs = [TypeGuesser() for _ in columns]
count = 0
for row in reader2:
count += 1
for j, v in enumerate(row):
#de = re.escape(row[j])
de = row[j]
data[j].add(de)
tgs[j].add(de)
distinctRows = [(i,len(x)) for i,x in enumerate(data)]
col_order = [i for i,v in sorted(distinctRows, key=lambda v: v[1], reverse=True)]
types = [tg.get_type() for tg in tgs]
return (distinctRows, types)