-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_name.py
More file actions
318 lines (246 loc) · 11 KB
/
clean_name.py
File metadata and controls
318 lines (246 loc) · 11 KB
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
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
import pandas as pd
import re
import os
suffix_file = "companysuffixfile.txt" # i still do not know why we need to complicate it with json
#"Auto(responder )Bot" a.k.a. AutoBot
class AutoBot: #Optimus Prime here we come!
def __init__(self,file_path): # Asking for variable in initializing hampers none file class functionality
self.df = pd.read_csv(file_path)
# Drop if full column/row empty
def emptycheck(self):
self.df = self.df.dropna(axis= 'columns', how='all')
self.df = self.df.dropna(axis= 'index', how='all')
# Make new columns if they do not exist
def makenamecols(self, namecol):
self.nameloc = self.df.columns.get_loc(namecol) # To Do: not define in init, error if file doesnt have Name column
try:
self.df.insert(self.nameloc+1, "FirstName", None)
self.df.insert(self.nameloc+2, "MiddleName", None)
self.df.insert(self.nameloc+3, "LastName", None)
except:
return
# Split original names into 3 columns and assign them to respective columns
def splitnames(self, namecol):
self.nameloc = self.df.columns.get_loc(namecol) # To Do: not define in init, error if file doesnt have Name column
for i in range(len(self.df["Name"])):
try:
a, *b, c = self.df.iat[i,self.nameloc].split()
s = " "
b = s.join(b)
except:
a = self.df.iat[i,self.nameloc]
b = None
c = None
self.df.iat[i,self.nameloc+1] = a
self.df.iat[i,self.nameloc+2] = b
self.df.iat[i,self.nameloc+3] = c
# self.main_df.append(self.df, ignore_index = True) #only works as intended if both csv files have the same columns in the same order (I think)
# Send data back to original file to make changes
def to_csv(self, spath,user_file):#, spath = None):
file_name = os.path.basename(user_file)
s1 = "/cleaned - "
self.df.to_csv(spath + s1 + file_name, index=False) # To Do: append date/time to keep unique file name
# output_file_path = "myfiles/user_file"
# file_path = "myfiles/cleaned_data"
# self.main_df.to_csv(output_file_path + '.csv', index=False) #tkinter will allow to pick filepath and file name
# self.main_df.to_csv(file_path + '.csv', index=False)
# Find and replace company names using a set list
def FindReplace(self, companycol):
with open(suffix_file) as f:
companysuffixlist = f.read().splitlines()
# Function that returns length of input, needed for sorting
def myFunc(e):
return len(e)
companysuffixlist.sort(reverse=True, key=myFunc)
for suffix in companysuffixlist:
self.df[companycol] = [re.sub("\ \,?\ ?"+suffix+"\.?$", '', idk) for idk in self.df[companycol]]
self.df[companycol] = [re.sub("\,\ ?"+suffix+"\.?$", '', idk) for idk in self.df[companycol]]
# "\ \,?\ ?"x "\.?" | "\,\ ?" x "\.?"
# "\ " + suffix + "\.?$"
# Run all functions above, edit original file()
def fullsplit(self):
self.emptycheck()
self.makenamecols()
self.splitnames()
self.to_csv()
def CompanySplit(self):
self.FindReplace()
self.to_csv()
# Create/Append to file with a list of Company Extensions
def append_suffix(suffix): #I propose this should be a seperate function, not in this class
# Open file in read n write mode
with open(suffix_file, "a+") as file_object:
lines = file_object.read().splitlines()
# Check if value exists, if not, then append to file
if suffix not in lines:
file_object.seek(0) # Move read cursor to the start of file.
data = file_object.read(10) # If file is not empty then append '\n'
if len(data) > 0:
file_object.write("\n") # Append text at the end of file
file_object.write(suffix)
# Create/Append to file with a list of Company Extensions
def drop_suffix(dropsuffix): #I propose this should be a seperate function, not in this class
# Open file in read n write mode
with open(suffix_file) as file_object:
lines = file_object.read().splitlines()
terms = len(lines)
lines.pop(terms - dropsuffix - 1)
# Empty file to prevent doubling
open(suffix_file, "w").close()
# Check if value exists, if not, then append to file
with open(suffix_file, "a+") as file_object:
for ex in lines:
file_object.seek(0) # Move read cursor to the start of file.
data = file_object.read(10) # If file is not empty then append '\n'
if len(data) > 0:
file_object.write("\n") # Append text at the end of file
file_object.write(ex)
# OMG WE CAN UNIRONICALLY NAME THE BOT OPTIMUS HOLY SHIT, IT'S LATIN FOR "BEST" FUCK YEAH BITCHES
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename, asksaveasfile, askdirectory
from tkinter import messagebox as mb
# from tkinter import Listbox
# Make/open a TKinter Window
window = tk.Tk()
window.title("SavAi Cleaner Bot")
window.resizable(0, 0) # Doesnt allow window resize
# list of used variables
name_col = None
Comp_col = None
user_sel_input_file_path = None
user_sel_output_file_path = None
name_col_dropdown_row = 10
comp_name_dropdown_row = 11
select_file_row = 12
save_file_row = select_file_row + 1
# Prettify!!
# fontStyle = tk.font(family="Lucida Grande", size=20)
tk.Label(window, text="SavAi",
fg = "black",
# bg = "yellow",
font = "Verdana 20 bold").grid(row=0, column=1)
##
# def sholist():
listbox = tk.Listbox(window)#, selectmode=SINGLE)
listbox.grid(row=3, column=1)
try:
with open(suffix_file, "r+") as f:
ext = f.read().splitlines()
for l in ext:
listbox.insert(-1, l)
except:
open(suffix_file, "a+").close()
# # Input button for keyword
def append():
listbox.delete(0, -1)
if not e1.get().rstrip(" "):
print ("empty")
pass
else:
append_suffix(e1.get().rstrip(" "))
listbox.insert(-1, e1.get().rstrip(" "))
print ("not empty")
e1 = tk.Entry(window) # Text box on window
e1.grid(row= 1, column= 1)
tk.Label(window, text="Company Extension").grid(row=1)
tk.Button(window, text="Add",command=append).grid(row=1,column=2)
# Drop from current list of keywords
def exlist():
sel = listbox.curselection()
print(sel)
drop_suffix(sel[0])
listbox.delete(tk.ACTIVE)
tk.Button(window, text="Delete",command=exlist).grid(row=1,column=3)
# Let User choose which is Name Column in a dropbox from file to split
tk.Label(window, text="Name Column :").grid(row=name_col_dropdown_row, sticky=W)
tk.Label(window, text="Company Column :", anchor="w").grid(row=comp_name_dropdown_row, sticky=W)
# Ask for file path
def filef():
global user_sel_input_file_path
user_sel_input_file_path = askopenfilename(title = "Select A File",
filetypes=(("csv","*.csv"),("all files","*.*")) )
myLabel = tk.Label(window, text=user_sel_input_file_path).grid(
row=select_file_row,
column=1
)
col_options = [
]
df2 = pd.read_csv(user_sel_input_file_path)
for head in df2.columns:
col_options.append(head)
def Name_click(event):
global name_col
name_col = None
name_col = Name_combo.get()
Name_combo = ttk.Combobox(window, value = col_options)
Name_combo.grid(row=name_col_dropdown_row, column = 1)
Name_combo.bind("<<ComboboxSelected>>", Name_click)
def Company_click(event):
global Comp_col
Comp_col = None
Comp_col = Company_box.get()
Company_box = ttk.Combobox(window, value = col_options)
Company_box.grid(row=comp_name_dropdown_row, column = 1)
# Company_box.current(0)
Company_box.bind("<<ComboboxSelected>>", Company_click)
# Pass the ask dialogue box through a button
tk.Button(window, text="Open",command=filef).grid(row=select_file_row, column=2)
tk.Label(window, text="File : ").grid(row=select_file_row, sticky=W)
# Ask for save path
def files():
global user_sel_output_file_path
user_sel_output_file_path = askdirectory(title = "Select Save Location")
print(user_sel_output_file_path)
mylabel = tk.Label(window, text=user_sel_output_file_path).grid(
row=save_file_row,
column=1
)
# Pass the ask dialogue box through a button
tk.Button(window, text="Save Location",command=files).grid( row=save_file_row,
column=2,
columnspan =2)
tk.Label(window, text="Save Path : ").grid(row=save_file_row, sticky=W)
# Clean/Run Autobot
def cleanf():
try:
if user_sel_input_file_path:
run = AutoBot(user_sel_input_file_path)
if name_col == None and Comp_col == None and user_sel_input_file_path != None:
tk.messagebox.showerror(title="Error", message="Neither Name nor Company column is selected.")
elif name_col == None:
tk.messagebox.showwarning(title="Error", message="No Name Column selected, Names will not be split")
elif Comp_col == None:
tk.messagebox.showwarning(title="Error", message="No Company Column selected, company extensions will not be dropped")
else:
pass
# tk.messagebox.showerror(title="Error", message="Unexpected Error")
if name_col:
run.emptycheck()
run.makenamecols(name_col)
run.splitnames(name_col)
else:
pass
if Comp_col:
run.FindReplace(Comp_col)
else:
pass
if user_sel_output_file_path:
if name_col == None and Comp_col == None:
pass
else:
run.to_csv(user_sel_output_file_path, user_sel_input_file_path)
else:
tk.messagebox.showerror(title="Error", message="Please select save file location")
else:
tk.messagebox.showerror(title="Error", message="No file selected. Please select a file to be cleaned")
# #No file selected dialogue box
except:
tk.messagebox.showerror(title="Error", message="Unknown Error: Please contact the developers at SavAI")
# Clean Button
tk.Button(text='Clean', command=cleanf).grid(row=select_file_row,column=3)
# Checkbox for new file or same file
tk.mainloop() #Needs this