-
Notifications
You must be signed in to change notification settings - Fork 2
/
dedupe_bulk.py
173 lines (141 loc) · 6.51 KB
/
dedupe_bulk.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
from pymongo import MongoClient
import Levenshtein as lev
import pandas as pd
# from fuzzywuzzy import fuzz as lev
demoClient = MongoClient()
myClient = MongoClient("localhost", 27017)
myDatabase = myClient["De-dupe"]
myCollection = myDatabase["Data"]
# DECLARE THRESHOLDS - SLIDER CONCEPT IN UI
# mrnWeight = 0.8
# fnameWeight = 0.6
# lnameWeight = 0.6
# dobWeight = 0.65
# phoneWeight = 0.7
# s_phoneWeight = 0.7
# emailWeight = 0.65
# pincodeWeight = 0.5
# stateWeight = 0.5
# yoeWeight = 0.5
# spezWeight = 0.65
# eduWeight = 0.6
flag = 0
data = []
# USER INPUTS - BULK DATA
def dedupe_bulk(filename,MRNscale,fNamescale,lNamescale,DOBscale,Statescale,Pincodescale,Phonescale,YOEscale,Specializationscale,Educationscale):
# print(filename)
# print(df.head)
mrnWeight = float(MRNscale)
fnameWeight = float(fNamescale)
lnameWeight = float(lNamescale)
dobWeight = float(DOBscale)
phoneWeight = float(Phonescale)
pincodeWeight = float(Pincodescale)
stateWeight = float(Statescale)
yoeWeight = float(YOEscale)
spezWeight = float(Specializationscale)
eduWeight = float(Educationscale)
df=pd.read_csv(filename)
print(df.head)
df = pd.read_csv(filename)
for index, row in df.iterrows():
mrn_inp = row['MRN Number']
fname_inp = row['First Name']
lname_inp = row['Last Name']
dob_inp = row['DOB']
phone_inp = str(row['Phone Number'])
pincode_inp = str(row['Pincode'])
state_inp = row['State']
yoe_inp = str(row['Years of Exp.'])
spez_inp = row['Specialization']
edu_inp = row['Education']
maxSS = 0
for document in myCollection.find():
score = 0
mrnSimilarityScore = lev.ratio(document.get('MRN').lower(), mrn_inp.lower())
if mrnSimilarityScore >= mrnWeight:
score = score + 1
fnameSimilarityScore = lev.ratio(document.get('First Name').lower(), fname_inp.lower())
if fnameSimilarityScore >= fnameWeight:
score = score + 1
lnameSimilarityScore = lev.ratio(document.get('Last Name').lower(), lname_inp.lower())
if lnameSimilarityScore >= lnameWeight:
score = score + 1
dobSimilarityScore = lev.ratio(document.get('DOB'), dob_inp)
if dobSimilarityScore >= dobWeight:
score = score + 1
phoneSimilarityScore = lev.ratio(str(document.get('Phone Number')), phone_inp)
if phoneSimilarityScore >= phoneWeight:
score = score + 1
# s_phoneSimilarityScore = lev.ratio(str(document.get('Secondary Phone Number')), p_phone_inp)
# if s_phoneSimilarityScore >= s_phoneWeight:
# score = score + 1
# emailSimilarityScore = lev.ratio(document.get('Email'), email_inp)
# if emailSimilarityScore >= emailWeight:
# score = score + 1
pincodeSimilarityScore = lev.ratio(str(document.get('Pincode')), pincode_inp)
if pincodeSimilarityScore >= pincodeWeight:
score = score + 1
stateSimilarityScore = lev.ratio(document.get('State'), state_inp)
if stateSimilarityScore >= stateWeight:
score = score + 1
spezSimilarityScore = lev.ratio(document.get('Specialization'), spez_inp)
if spezSimilarityScore >= spezWeight:
score = score + 1
eduSimilarityScore = lev.ratio(document.get('Education'), edu_inp)
if spezSimilarityScore >= spezWeight:
score = score + 1
tsimilarityScore = (mrnSimilarityScore * mrnWeight + fnameSimilarityScore * fnameWeight +
lnameSimilarityScore * lnameWeight + dobSimilarityScore * dobWeight +
phoneSimilarityScore * phoneWeight +
pincodeSimilarityScore * pincodeWeight + stateSimilarityScore * stateWeight + spezSimilarityScore * spezWeight +
eduSimilarityScore * eduWeight) / (
mrnWeight + fnameWeight + lnameWeight + dobWeight + phoneWeight + pincodeWeight + stateWeight + spezWeight +
eduWeight)
if tsimilarityScore>maxSS:
maxSS = tsimilarityScore
if maxSS> 0.7:
DUP = 'D'
data.append(
[mrn_inp, fname_inp, lname_inp, dob_inp, phone_inp, pincode_inp, state_inp, yoe_inp, spez_inp,
edu_inp, maxSS, DUP])
maxSS = 0
elif maxSS > 0.5:
DUP = 'P'
data.append(
[mrn_inp, fname_inp, lname_inp, dob_inp, phone_inp, pincode_inp, state_inp, yoe_inp, spez_inp,
edu_inp, maxSS, DUP])
maxSS = 0
else:
DUP = 'U'
data.append(
[mrn_inp, fname_inp, lname_inp, dob_inp, phone_inp, pincode_inp, state_inp, yoe_inp, spez_inp,
edu_inp, maxSS, DUP])
maxSS = 0
data_summary = pd.DataFrame(data, columns=['MRN', 'First Name', 'Last Name', 'DOB', 'Phone Number',
'Pincode', 'State', 'Years of Exp.', 'Specialization', 'Education',
'SimilarityScore', 'DUP'])
data_summary.to_csv("Output.csv")
print("Output file generated. Check directory.")
# REPORT GENERATION - WRITING IT TO A FILE.
size = len(data_summary.index)
unique_count = (data_summary['DUP'].values == 'U').sum()
partial_count = (data_summary['DUP'].values == 'P').sum()
duplicate_count = (data_summary['DUP'].values == 'D').sum()
unique_count_perc = round(unique_count/size * 100, 2)
partial_count_perc = round(partial_count/size * 100, 2)
duplicate_count_perc = round(duplicate_count/size * 100, 2)
strlist = []
line1 = "--------Summary of Input Data----------\n"
line2 = "\n"
line3 = f"Total numbers of rows in the input data set: {size}."
line4 = f"\nUnique Entry Count: {unique_count} ({unique_count_perc} %)\n"
line5 = f"Duplicate Entry Count: {duplicate_count} ({duplicate_count_perc} %)\n"
line6 = f"Partial Similarity Entry Count: {partial_count} ({partial_count_perc} %)\n"
strlist = [line1, line2, line3, line4, line5, line6]
f= open("Report_Bulk.txt", "w+")
for line in strlist:
f.write(str(line))
print("Report Generated! Check file in directory")
response = {'check': flag}
return response