-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias_merge.py
418 lines (341 loc) · 15.5 KB
/
alias_merge.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
import re
fakeusr_rex = re.compile(r'\A[A-Z]{8}$')
from alias import Alias
from collections import Counter
from itertools import combinations, product
USR_FAKE = 'FAKE'
USR_REAL = 'REAL'
EMAIL = 'EMAIL'
COMP_EMAIL_PREFIX = 'COMP_EMAIL_PREFIX'
SIMPLE_EMAIL_PREFIX = 'SIMPLE_EMAIL_PREFIX'
PREFIX_LOGIN = 'PREFIX_LOGIN'
PREFIX_NAME = 'PREFIX_NAME'
LOGIN_NAME = 'LOGIN_NAME'
FULL_NAME = 'FULL_NAME'
SIMPLE_NAME = 'SIMPLE_NAME'
NAME_PARTS = 'NAME_PARTS'
NAME_APPENDED = 'NAME_APPENDED'
DOMAIN = 'EMAIL_DOMAIN'
TWO = 'TWO_OR_MORE_RULES'
THR_MIN = 1
THR_MAX = 20
def resolve_aliases(inputs):
print_flag = 0
unmask = {}
aliases = {}
# Helper structures
d_email_uid = {}
d_uid_email = {}
d_prefix_uid = {}
d_uid_prefix = {}
d_comp_prefix_uid = {}
d_uid_comp_prefix = {}
d_uid_domain = {}
d_domain_uid = {}
d_name_uid = {}
d_uid_name = {}
d_name_parts_uid = {}
d_uid_name_parts = {}
d_name_app_uid = {}
d_uid_app_parts = {}
d_login_uid = {}
d_uid_login = {}
d_uid_type = {}
#d_type_usr = {}
uid = 0
for ind, row in inputs.iterrows():
uid = row["uid"]
name = row["name"]
email = row["email"]
login = row["login"]
unmask[uid] = uid
if not row["fake"]:
record_type = USR_REAL
else:
record_type = USR_FAKE
a = Alias(record_type, uid, login, name, email)
aliases[uid] = a
# - email
d_uid_email[a.uid] = a.email
if a.email is not None:
d_email_uid.setdefault(a.email, set([a.uid]))
d_email_uid[a.email].add(a.uid)
# - prefix
d_uid_prefix[a.uid] = a.email_prefix
d_uid_comp_prefix[a.uid] = a.email_prefix
if a.email_prefix is not None:
if len(a.email_prefix.split('.')) > 1 or len(a.email_prefix.split('_')) > 1:
d_comp_prefix_uid.setdefault(a.email_prefix, set([a.uid]))
d_comp_prefix_uid[a.email_prefix].add(a.uid)
else:
d_prefix_uid.setdefault(a.email_prefix, set([a.uid]))
d_prefix_uid[a.email_prefix].add(a.uid)
# - domain
d_uid_domain[a.uid] = a.email_domain
if a.email_domain is not None:
d_domain_uid.setdefault(a.email_domain, set([a.uid]))
d_domain_uid[a.email_domain].add(a.uid)
# - login
d_uid_login[a.uid] = a.login
if a.login is not None:
d_login_uid.setdefault(a.login, set([a.uid]))
d_login_uid[a.login].add(a.uid)
if a.record_type == USR_REAL:
d_login_uid.setdefault(a.login.lower(), set([a.uid]))
d_login_uid[a.login.lower()].add(a.uid)
# - name
d_uid_name[a.uid] = a.name
if a.name is not None and len(a.name):
d_name_uid.setdefault(a.name, set([a.uid]))
d_name_uid[a.name].add(a.uid)
if len(a.name.split(' ')) == 1:
d_name_uid.setdefault(a.name.lower(), set([a.uid]))
d_name_uid[a.name.lower()].add(a.uid)
# janejohnson -> janejohnson
# we need this for matching
d_name_app_uid.setdefault(a.name.lower(), set([a.uid]))
d_name_app_uid[a.name.lower()].add(a.uid)
# jane johnson -> janejohnson
d_name_app_uid.setdefault("".join(a.name.split(" ")).lower(), set([a.uid]))
d_name_app_uid["".join(a.name.split(" ")).lower()].add(a.uid)
if "@" in a.name: # otherwise it will make "gmail", "com" as names
name_subpart = a.name.split("@")[0]
d_name_parts_uid.setdefault(name_subpart.lower(), set([a.uid]))
d_name_parts_uid[name_subpart.lower()].add(a.uid)
else:
# xiyi ji -> ji xiyi
name_parts_split = a.name.lower().replace(",", " ").replace(".", " ").split(' ')
if len(name_parts_split) != 2:
continue
new_name_parts = name_parts_split[-1] + " " + name_parts_split[0]
d_name_parts_uid.setdefault(new_name_parts, set([a.uid]))
d_name_parts_uid[new_name_parts].add(a.uid)
# print 'Done: helpers'
clues = {}
for email, set_uid in d_email_uid.items():
if len(set_uid) > THR_MIN:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(EMAIL)
# print a,b,EMAIL
# print 'Done: email'
for prefix, set_uid in d_comp_prefix_uid.items():
if len(set_uid) > THR_MIN and len(set_uid) < THR_MAX:
if len(prefix) >= 3:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(COMP_EMAIL_PREFIX)
# print a,b,COMP_EMAIL_PREFIX
# print 'Done: comp email prefix'
for prefix, set_uid in d_prefix_uid.items():
if len(set_uid) > THR_MIN and len(set_uid) < THR_MAX:
if len(prefix) >= 3:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(SIMPLE_EMAIL_PREFIX)
# print a,b,SIMPLE_EMAIL_PREFIX
# print 'Done: email prefix'
for prefix in set(d_prefix_uid.keys()).intersection(set(d_login_uid.keys())):
if len(d_prefix_uid[prefix]) < THR_MAX:
for a,b in product(sorted(d_login_uid[prefix], key=lambda uid:int(uid)), sorted(d_prefix_uid[prefix], key=lambda uid:int(uid))):
if a < b:
clues.setdefault((a, b), [])
if not SIMPLE_EMAIL_PREFIX in clues[(a, b)]:
clues[(a, b)].append(PREFIX_LOGIN)
# print a,b,PREFIX_LOGIN
# print 'Done: prefix=login'
for prefix in set(d_prefix_uid.keys()).intersection(set(d_name_uid.keys())):
if len(d_prefix_uid[prefix]) < THR_MAX and len(d_name_uid[prefix]) < THR_MAX:
for a,b in product(sorted(d_name_uid[prefix], key=lambda uid:int(uid)), sorted(d_prefix_uid[prefix], key=lambda uid:int(uid))):
if a < b:
clues.setdefault((a, b), [])
if not SIMPLE_EMAIL_PREFIX in clues[(a, b)]:
clues[(a, b)].append(PREFIX_NAME)
# print 'Done: prefix=name'
for prefix in set(d_login_uid.keys()).intersection(set(d_name_uid.keys())):
if len(d_name_uid[prefix]) < THR_MAX:
for a,b in product(sorted(d_name_uid[prefix], key=lambda uid:int(uid)), sorted(d_login_uid[prefix], key=lambda uid:int(uid))):
if a < b:
clues.setdefault((a, b), [])
if not SIMPLE_EMAIL_PREFIX in clues[(a, b)]:
clues[(a, b)].append(LOGIN_NAME)
# print 'Done: login=name'
for name, set_uid in d_name_uid.items():
if len(set_uid) > THR_MIN and len(set_uid) < THR_MAX:
if len(name.split(' ')) > 1:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(FULL_NAME)
else:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(SIMPLE_NAME)
# print 'Done: full/simple name'
for name, set_uid in d_name_parts_uid.items():
#out.write(name + "," + str(set_uid) + "\n")
if len(set_uid) > THR_MIN and len(set_uid) < THR_MAX:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(NAME_PARTS)
#out.write("\n")
# print 'Done: name parts'
for name, set_uid in d_name_app_uid.items():
#out.write(name + "," + str(set_uid))
if len(set_uid) > THR_MIN and len(set_uid) < THR_MAX:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(NAME_APPENDED)
# print 'Done: name parts appended'
for domain, set_uid in d_domain_uid.items():
if len(set_uid) > THR_MIN and len(set_uid) < THR_MAX:
for a,b in combinations(sorted(set_uid, key=lambda uid:int(uid)), 2):
clues.setdefault((a, b), [])
clues[(a, b)].append(DOMAIN)
# print 'Done: email domain'
d_alias_map = {}
clusters = {}
labels = {}
def merge(a,b,rule):
# Contract: a < b
assert a<b, "A must be less than B"
if a in d_alias_map:
if b in d_alias_map:
if d_alias_map[a] == d_alias_map[b]:
labels[d_alias_map[a]].append(rule)
else:
lowest = min(d_alias_map[a], d_alias_map[b])
highest = max(d_alias_map[a], d_alias_map[b])
labels[lowest].extend(labels[highest])
labels[lowest].append(rule)
clusters[lowest].update(clusters[highest])
for x in clusters[highest]: d_alias_map[x] = lowest
del labels[highest]
del clusters[highest]
d_alias_map[a] = lowest
d_alias_map[b] = lowest
else:
# a is an alias; first time I see b
d_alias_map[b] = d_alias_map[a]
clusters[d_alias_map[a]].add(b)
labels[d_alias_map[a]].append(rule)
else:
if b in d_alias_map:
#b_src = d_alias_map[b] # b_src < a by construction
d_alias_map[a] = d_alias_map[b]
clusters[d_alias_map[b]].add(a)
labels[d_alias_map[b]].append(rule)
else:
# First time I see this pair (guaranteed sorted)
d_alias_map[a] = a
d_alias_map[b] = a
clusters[a] = set([a,b])
labels[a] = [rule]
for (a,b), list_clues in sorted(clues.items(), key=lambda e:(int(e[0][0]),int(e[0][1]))):
if print_flag:
print (((a,b), list_clues))
aa = aliases[a]
ab = aliases[b]
if EMAIL in list_clues:
merge(a,b,EMAIL)
elif len(set(list_clues)) >= 2:
for clue in set(list_clues):
merge(a,b,clue)
elif FULL_NAME in list_clues:
merge(a,b,FULL_NAME)
elif NAME_APPENDED in list_clues:
merge(a, b, NAME_APPENDED)
elif NAME_PARTS in list_clues:
merge(a, b, NAME_PARTS)
elif COMP_EMAIL_PREFIX in list_clues:
merge(a,b,COMP_EMAIL_PREFIX)
elif SIMPLE_NAME in list_clues:
merge(a,b,SIMPLE_NAME)
elif PREFIX_NAME in list_clues:
merge(a,b,PREFIX_NAME)
# print 'Done: clusters'
for uid, member_uids in clusters.items():
# print ((uid, member_uids))
members = [aliases[m] for m in member_uids]
# Count fake/real
c = Counter([m.record_type for m in members])
real = [m for m in members if m.record_type==USR_REAL]
fake = [m for m in members if m.record_type==USR_FAKE]
# Count rules that fired
cl = Counter(labels[uid])
if print_flag:
print (cl)
is_valid = False
# If all have the same email there is no doubt
if cl.get(EMAIL,0) >= (len(members)-1):
is_valid = True
# If all the REALs have the same email, assume all the FAKEs are this REAL
elif len(Counter([m.email for m in real]).keys()) == 1:
is_valid = True
# If there is at most one real, at least two rules fired, and each rule applied to each pair
elif len(cl.keys()) > 1 and min(cl.values()) >= (len(members)-1):
is_valid = True
# At most one real, the only rule that fired is COMP_EMAIL_PREFIX or FULL_NAME
elif len(cl.keys()) == 1 and \
(cl.get(COMP_EMAIL_PREFIX,0) or cl.get(FULL_NAME,0) or \
cl.get(NAME_PARTS,0) or cl.get(NAME_APPENDED,0)):
is_valid = True
# All with same full name and same full name and email domain
elif cl.get(FULL_NAME,0) >= (len(members)-1) and \
cl.get(DOMAIN,0) >= (len(members)-1):
is_valid = True
# All same composite email prefix / same full name
elif (cl.get(COMP_EMAIL_PREFIX,0) >= (len(members)-1) or cl.get(FULL_NAME,0) >= (len(members)-1)):
is_valid = True
elif cl.get(NAME_APPENDED,0) >= (len(members)-1):
is_valid = True
elif cl.get(FULL_NAME,0) >= (len(members)-1):
is_valid = True
# The only two rules that fired are full name and email, in some combination
elif len(cl.keys()) == 2 and cl.get(FULL_NAME,0) > 0 and cl.get(EMAIL,0) > 0:
is_valid = True
elif len(cl.keys()) == 3 and cl.get(FULL_NAME,0) > 0 and cl.get(EMAIL,0) > 0 and cl.get(SIMPLE_NAME,0) > 0:
is_valid = True
elif len(cl.keys()) == 2 and cl.get(EMAIL,0) > 0 and cl.get(SIMPLE_NAME,0) > 0:
is_valid = True
elif cl.get(PREFIX_NAME,0) > 0:
is_valid = True
elif cl.get(SIMPLE_NAME,0) > 0 and cl.get(FULL_NAME,0) > 0 \
and cl.get(SIMPLE_EMAIL_PREFIX,0) > 0 and cl.get(EMAIL,0) > 0:
is_valid = True
elif cl.get(SIMPLE_NAME,0) > 0:
is_valid = True
elif cl.get(NAME_PARTS,0) >= (len(members)-1):
is_valid = True
else:
# is_valid = True
# continue
# Split by email address if at least 2 share one
if cl.get(EMAIL,0):
ce = [e for e,c in Counter([m.email for m in members]).items() if c > 1]
for e in ce:
extra_members = [m for m in members if m.email==e]
extra_real = [m for m in extra_members if m.record_type==USR_REAL]
if len(extra_real):
rep = sorted(extra_real, key=lambda m:int(m.uid))[0]
else:
rep = sorted(extra_members, key=lambda m:int(m.uid))[0]
for a in extra_members:
if a.uid != rep.uid:
unmask[a.uid] = rep.uid
if print_flag:
print (str(cl.items()))
for m in members:
print ([m.uid, m.name, m.email])
if is_valid:
# Determine group representative
if len(real):
rep = sorted(real, key=lambda m:int(m.uid))[0]
else:
rep = sorted(members, key=lambda m:int(m.uid))[0]
for a in members:
if a.uid != rep.uid:
unmask[a.uid] = rep.uid
if print_flag:
print ('Mapped:' + str((a.uid, rep.uid)))
return unmask