-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepair.py
179 lines (145 loc) · 4.72 KB
/
repair.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
import sys
import re
from argparse import ArgumentParser
import pandas as pd
from jaccard_index.jaccard import jaccard_index
from tools import read_conf
C_ALPHABET_PATTERN = re.compile('[a-z]')
MIN_J_INDEX = 0.3
def j_index(a: str, b: str) -> float:
if len(a) < 2 or len(b) < 2:
return 0
if C_ALPHABET_PATTERN.search(b):
return jaccard_index(a, b)
return 0
def nice_str(s: str, ref: list):
lower_ref = [x.lower() for x in ref]
lower_s = s.lower()
if lower_s in lower_ref:
index = lower_ref.index(lower_s)
return ref[index]
return s
def clean_category(data: dict, cf: dict):
if data['category'] != cf['category']:
return
for column in cf.get('not_null_columns', []):
value = data[column]
if pd.isnull(value) or isinstance(value, str) and \
value.lower().find('tidak') > -1:
data['category'] = 'lainnya'
# Nama awal sebagai bekal Jaccard Index
names = dict()
def clean_name(data: dict, column: str, nice_names: list, back_ref=dict()):
if pd.isnull(data[column]):
return
data[column] = nice_str(data[column], nice_names)
for key, value in back_ref.items():
if data[column].lower().find(key.lower()) > -1:
data[column] = value
return
if column not in names:
names[column] = [data[column]]
return
best_index = 0
for name in names[column]:
index = j_index(name.lower(), data[column].lower())
if index < MIN_J_INDEX:
continue
if best_index > index:
continue
best_index = index
best_name = name
if best_index:
data[column] = best_name
else:
names[column].append(data[column])
OTHERS = ('tidak', 'unknown', 'none', 'lainnya', '-')
def clean_names(data: dict, cf: dict):
for column, items in cf.get('names', {}).items():
keys, alias = items
value = data[column]
if value and not pd.isnull(value):
is_unknown = False
for other in OTHERS:
if value.lower().find(other) > -1:
data[column] = None
is_unknown = True
break
if not is_unknown:
data[column] = value.strip().replace('.', '')
clean_name(data, column, keys, alias)
def clean_str(data: dict):
for column, value in data.items():
if isinstance(value, str):
value = value.strip()
if not value:
data[column] = None
def clean_numeric(data: dict, cf: dict):
def clean_orig_column():
orig_column = '_'.join(column.split('_')[:-1])
if orig_column in data:
data[orig_column] = None
for column in cf.get('numeric_columns', []):
if pd.isnull(data[column]) or not data[column]:
clean_orig_column()
else:
try:
data[column] = float(data[column])
except ValueError:
data[column] = None
clean_orig_column()
def clean_range_value(data: dict, cf: dict):
for column in cf.get('range_values', []):
if data[column] is None:
continue
min_, max_ = cf['range_values'][column]
if min_ <= data[column] <= max_:
continue
data[column] = None
NEGATIVE_BOOLEAN = ['tidak', 'no']
def clean_boolean(data: dict):
def update_if_false():
for word in value.lower().split():
for ref in NEGATIVE_BOOLEAN:
if word.find(ref) > -1:
data[column] = None
return
for column, value in data.items():
if column.find('is_') != 0:
continue
if pd.isnull(value):
continue
if not isinstance(value, str):
continue
value = value.strip()
if not value:
data[column] = None
continue
update_if_false()
def repair(cf, csv_file):
df = pd.read_csv(csv_file)
rows = dict()
for column in df.columns:
rows[column] = []
for index, row in df.iterrows():
data = dict(row)
clean_str(data)
clean_category(data, cf)
clean_names(data, cf)
clean_numeric(data, cf)
clean_range_value(data, cf)
clean_boolean(data)
for column in df.columns:
value = data[column]
rows[column].append(value)
df = pd.DataFrame(rows)
df.to_csv(csv_file, index=False)
def main(argv=sys.argv[1:]):
pars = ArgumentParser()
pars.add_argument('conf')
pars.add_argument('--csv-file', required=True)
option = pars.parse_args(argv)
cf = read_conf(option.conf)
repair(cf, option.csv_file)
if __name__ == '__main__':
main()