forked from openspending/dpkg-uk25k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_columns.py
176 lines (147 loc) · 5.74 KB
/
map_columns.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
import sys
import sqlaload as sl
import Levenshtein
import re
import json
import traceback
from tempfile import NamedTemporaryFile
from common import *
from sqlalchemy import select
PREDEFINED = [u'SupplierName', u'DepartmentFamily', u'Entity', u'Amount',
u'ExpenseType', u'ExpenseArea', u'VATNumber', u'Date', u'TransactionNumber']
aliases = {'VATNumber': ['vatregistrationnumber', 'suppliervatregistrationnumber'],
'TransactionNumber': ['transno'],
'Date': ['dateofpayment', 'transactiondate', 'paymentdate'],
'Amount': ['amountinsterling'],
'SupplierName': ['merchantname'],
'DepartmentFamily': ['department']
}
# Add all the identities to aliases, so we can use it for lookup
for k in PREDEFINED:
aliases.setdefault(k, []).append(normalise_header(k))
# Find the distance to the closest alias
def alias_distance(from_col, to_col):
distances = map(lambda x: Levenshtein.distance(from_col, unicode(x)), aliases[to_col])
return min(distances)
def assign_defaults(cols):
mapping = {}
left_over = set(PREDEFINED)
unmapped = set(cols)
distances = {}
for to_col in list(left_over):
to_norm = normalise_header(to_col)
for from_col in list(unmapped):
# First, assign all literal matches
if to_norm == from_col:
mapping[from_col] = to_col
left_over.remove(to_col)
unmapped.remove(from_col)
else:
distances.setdefault(from_col, {})[to_col] = alias_distance(from_col, to_col)
# Then, assign each predefined name to the closest match
while len(left_over) and len(unmapped):
distance_list = []
for to_col in left_over:
unmapped_tmp = unmapped.copy()
while len(unmapped_tmp) > 0:
from_col = unmapped_tmp.pop()
distance_list.append( (distances[from_col][to_col], from_col, to_col) )
d, f, t = min(distance_list, key=lambda x: x[0])
if d > 5:
break
mapping[f] = t
left_over.remove(t)
unmapped.remove(f)
return mapping, list(left_over)
def prompt_for(cols, mapping, left_over, count):
print
print "Matching: %s" % ', '.join(cols)
if count is not None:
print "Used in %d tables" % count
print "Mapping:"
for col in cols:
print " %s: %s" % (col, mapping.get(col, ''))
print "No mapping for: %s" % ', '.join(sorted(left_over))
sys.stdout.write("\n[A]ccept, [E]dit, mark as [B]roken, [S]kip, or [Q]uit? ")
line = ''
while line not in ['a', 'e', 'b', 's', 'q']:
line = sys.stdin.readline().strip().lower()
return line
def edit_mapping(cols, mapping, left_over):
editor = os.getenv('EDITOR', None)
if editor is None:
print "Please set $EDITOR first"
return mapping, left_over
with NamedTemporaryFile() as temp:
print >>temp, "# -*- coding: utf-8 -*-"
print >>temp, "# Lines beginning with # are ignored, as are blank lines"
print >>temp, "# Other lines should be of the form 'normalisedinputname: StandardName'"
print >>temp, ''
for col in cols:
print >>temp, (u'%s: %s' % (col, mapping.get(col, ''))).encode('utf-8')
print >>temp, ''
print >>temp, '# Unassigned standard names:'
for col in sorted(left_over):
print >>temp, '# %s' % col.encode('utf-8')
temp.flush()
x = os.spawnlp(os.P_WAIT,editor,editor,temp.name)
if x != 0:
print "Error %d from editor!" % x
temp.seek(0)
mapping = {}
left_over = set(PREDEFINED)
for line in temp:
line = unicode(line, 'utf-8').strip()
if line.startswith('#') or len(line) == 0:
continue
from_name, to_name = line.split(':')
from_name = from_name.strip()
to_name = to_name.strip()
if len(to_name):
mapping[from_name] = to_name
left_over.discard(to_name)
return mapping, list(left_over)
def map_column(engine, columns_table, normalised, count):
cols = filter(lambda c: len(c) > 0, normalised.split(','))
mapping,left_over = assign_defaults(cols)
while True:
line = prompt_for(cols, mapping, left_over, count)
if line == 'a':
return mapping
elif line == 'e':
mapping,left_over = edit_mapping(cols, mapping, left_over)
elif line == 's':
raise ValueError('Skipping')
elif line == 'b':
return None
elif line == 'q':
sys.exit(0)
def connect():
engine = db_connect()
columns_table = sl.get_table(engine, 'column_sets')
return engine,columns_table
def map_columns():
engine, columns_table = connect()
q = select([columns_table.c.normalised, columns_table.c.count, columns_table.c.valid], order_by=[columns_table.c.count.desc().nullslast()])
for normalised, count, valid in engine.execute(q):
if valid is not None:
continue
try:
columns = map_column(engine, columns_table, normalised, count)
if columns is not None:
sl.upsert(engine, columns_table,
{'normalised': normalised,
'valid': True,
'column_map': json.dumps(columns)},
['normalised'])
else:
sl.upsert(engine, columns_table,
{'normalised': normalised,
'valid': False},
['normalised'])
except SystemExit:
raise
except:
traceback.print_exc()
if __name__ == '__main__':
map_columns()