-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareTwoLists.py
309 lines (277 loc) · 10.6 KB
/
CompareTwoLists.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
from fuzzywuzzy import fuzz
import sys
import csv
import os
import argparse
from tqdm import tqdm
import pandas as pd
from tabulate import tabulate
# Test with csv load
def LoadCSV(filepath):
with open(filepath, "rb") as f:
reader = csv.reader(f)
l = []
for s in reader:
l.append(s[0])
# l = list(reader)
f.close()
return l
def LoadManual(string):
l = []
for s in string.lower().split(","):
l.append(s)
return l
# fileLocation is intentionally not being used. Just wanted a quick hack to be able to call it after assigning it to the outputMethod (needed 3 params to match the same method)
def printResults(matchList, SaveFileLocation=None):
if len(matchList) > 0:
# print('\n' + 'List for ' + item)
print(tabulate(pd.DataFrame(matchList), headers="keys", tablefmt="psql"))
input("Press Enter go to next")
def saveResults(matchList, SaveFileLocation):
if os.path.exists(SaveFileLocation):
try:
os.remove(SaveFileLocation)
except Exception as e:
print(
"\nFile" + SaveFileLocation + ' in use, please close and hit "Enter"\n'
)
input()
saveResults(matchList, SaveFileLocation)
matchList.to_csv(
SaveFileLocation,
header=True,
columns=["text_Key", "word1", "word2", "rFull", "rPartial", "rToken", "rSum"],
index=False,
)
# with open(SaveFileLocation, 'wb') as csvFile:
# writer = csv.writer(csvFile)
# if len(matchList) > 0:
# writer.writerow(['Item', 'Matched Item', 'Full Match Percent', 'Partial Match Percent', 'Token Match Percent', 'Match Sum'])
# for i in matchList:
# writer.writerow([i[0], i[1], i[2], i[3], i[4], i[5]])
def getMatches(
List1,
List2,
ignoreCase,
fullOrPartialOrToken,
ratioPercent,
SaveFileLocation,
matchLimit,
sameList=False,
):
matchList = pd.DataFrame()
for item in tqdm(List1):
tempMatchList = []
word1 = str(item).strip()
# sys.stdout.write('.')
for word in List2:
word2 = str(word).strip()
if ignoreCase == "yes":
word1 = word1.lower()
word2 = word2.lower()
if sameList:
if word1 != word2:
tempMatchList = compareWords(
fullOrPartialOrToken,
ignoreCase,
ratioPercent,
tempMatchList,
word1,
word2,
)
else:
tempMatchList = compareWords(
fullOrPartialOrToken,
ignoreCase,
ratioPercent,
tempMatchList,
word1,
word2,
)
# Compare length of list to match limit to account for when comparing a list to itself
# print tempMatchList
if len(tempMatchList) > matchLimit:
# Adding a my_list because directly appending to the matchList df was really slow
my_list = []
# print tempMatchList
for i in tempMatchList:
if len(matchList) == 0:
matchList = matchList.append(i, ignore_index=True)
else:
# if not any(d['text_Key'] == i['text_Key'] for d in matchList.iterrows()):
if not (matchList["text_Key"] == i["text_Key"]).any():
my_list.append(i)
# print i
# print my_list
# input('Press Enter')
# # print len(matchList)
# if len(matchList) == 0:
# matchList = matchList.append(i, ignore_index=True)
# if any(matchList.text_Key == i['text_Key']):
# pass
# else:
# matchList = matchList.append(i, ignore_index=True)
# input('Press Enter to continue')
my_list_DataFrame = pd.DataFrame(my_list)
# print l_DataFrame.head()
matchList = matchList.append(my_list_DataFrame)
# print matchList.head()
if (
input(
"\nWould you like the data sorted by the sum of the match percentages? (Yes/No)\n"
).lower()
== "yes"
):
# Order matchList
def getKey(item):
return item[5]
# matchList = sorted(matchList, key=getKey, reverse=True)
matchList = matchList.sort_values(by=["rSum"], ascending=0)
return matchList
def compareWords(
fullOrPartialOrToken, ignoreCase, ratioPercent, tempMatchList, word1, word2
):
rFull = 0
rPartial = 0
rToken = 0
# Create a key to be able to find duplicate comparisons and remove them in the matchList
text_Key = ""
if word1 >= word2:
text_Key = word1 + word2
else:
text_Key = word2 + word1
if fullOrPartialOrToken == "full":
rFull = fuzz.ratio(word1, word2)
elif fullOrPartialOrToken == "partial":
rPartial = fuzz.partial_ratio(word1, word2)
elif fullOrPartialOrToken == "token":
rToken = fuzz.token_set_ratio(word1, word2)
elif fullOrPartialOrToken == "all":
rFull = fuzz.ratio(word1, word2)
rPartial = fuzz.partial_ratio(word1, word2)
rToken = fuzz.token_set_ratio(word1, word2)
rSum = rFull + rPartial + rToken
else:
raise ("\nI don't recognize that command")
if (
rFull > int(ratioPercent)
or rPartial > int(ratioPercent)
or rToken > int(ratioPercent)
):
tempMatchList.append(
{
"text_Key": text_Key,
"word1": word1,
"word2": word2,
"rFull": rFull,
"rPartial": rPartial,
"rToken": rToken,
"rSum": rSum,
}
)
return tempMatchList
def main(argDict):
# loadType = 'csv'
# fullOrPartialOrToken = 'all'
# ignoreCase = True
# output = 'print'
# ratioPercent = 80
# matchLimit = 0
# SaveFileLocation = 'null'
List1 = []
List2 = []
if argDict["loadType"] == None:
argDict["loadType"] = input(
'Would you like to load from CSV or manual input? (Import "CSV" or "Manual")\n\n'
).lower()
if argDict["loadType"] == "csv":
if argDict["fileLocation_List1"] == None:
argDict["fileLocation_List1"] = input(
"Please enter the absolute file path of the 1st file you would like to load\n"
)
List1 = LoadCSV(argDict["fileLocation_List1"])
if argDict["fileLocation_List2"] == None:
argDict["fileLocation_List2"] = input(
"Please enter the absolute file path of the 2nd file you would like to load\n"
)
List2 = LoadCSV(argDict["fileLocation_List2"])
elif argDict["loadType"] == "manual":
List1 = LoadManual(input("Please enter a comma separated list for list 1\n\n"))
List2 = LoadManual(input("Please enter a comma separated list for list 2\n\n"))
else:
List1 = LoadManual(input("Please enter a comma separated list for list 1\n\n"))
List2 = LoadManual(input("Please enter a comma separated list for list 2\n\n"))
if argDict["fullOrPartialOrToken"] == None:
argDict["fullOrPartialOrToken"] = input(
"\nWould you like to do a full, partial, token or all comparison?\n"
).lower()
if argDict["ignoreCase"] == None:
argDict["ignoreCase"] = input("Would you like to ignore case? Yes/No\n").lower()
# If there is a save location set the output option to save automatically
if argDict["saveFileLocation"] != None:
argDict["output"] = "save"
if argDict["output"] == None:
argDict["output"] = input(
"How would you like to output the results? Print to console or Save to file (Print or Save)\n"
).lower()
if argDict["output"] == "print":
outputMethod = printResults
argDict["saveFileLocation"] = None
elif argDict["output"] == "save":
outputMethod = saveResults
if argDict["saveFileLocation"] == None:
argDict["saveFileLocation"] = input(
"Please give the location of the csv file (including the name of the file) that you would like to save\n"
).replace('"', "")
else:
print("Did not recognize output command")
if argDict["matchLimit"] == None:
matchLimit = 0
if argDict["ratioPercent"] == None:
argDict["ratioPercent"] = int(
input("What percentage match? 0-100 (100 means exact match)\n\n")
)
# Reset match limit to only return results greater than 1 when the list is being compared to itself
sameList = False
if List1 == List2:
sameList = True
print(
"The lists are the same. Will return results where matches are greater than 1"
)
argDict["matchLimit"] = 1
matchList = getMatches(
List1,
List2,
argDict["ignoreCase"],
argDict["fullOrPartialOrToken"],
argDict["ratioPercent"],
argDict["saveFileLocation"],
argDict["matchLimit"],
sameList=sameList,
)
if len(matchList) > 0:
outputMethod(matchList, argDict["saveFileLocation"])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--loadType")
parser.add_argument("--fullOrPartialOrToken")
parser.add_argument("--ignoreCase")
parser.add_argument("--output")
parser.add_argument("--ratioPercent")
parser.add_argument("--matchLimit")
parser.add_argument("--saveFileLocation")
parser.add_argument("--fileLocation_List1")
parser.add_argument("--fileLocation_List2")
args = parser.parse_args()
argDict = {
"loadType": args.loadType,
"fullOrPartialOrToken": args.fullOrPartialOrToken,
"ignoreCase": args.ignoreCase,
"output": args.output,
"ratioPercent": args.ratioPercent,
"matchLimit": args.matchLimit,
"saveFileLocation": args.saveFileLocation,
"fileLocation_List1": args.fileLocation_List1,
"fileLocation_List2": args.fileLocation_List2,
}
main(argDict)