/
cbrGuiv2.py
292 lines (269 loc) · 9.25 KB
/
cbrGuiv2.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
import fileOps,math
from Tkinter import *
from simMetrics import *
from collections import defaultdict
products = fileOps.openCsv("products.csv")
orders = fileOps.openCsvDebug("casesDebug.csv")
orderDebug = fileOps.openCsvDebug("casesDebug.csv")
newOrderDict = {}
customerProducts = []
orderSimilarity = {}
similarityThreshold = 0.3
#similarityThreshold = raw_input('Enter threshold:')
state = 1
#Iterate over the CSV.
def main():
forgetFrames()
global newOrderDict
newOrderDict = {}
customerProducts = []
orderSimilarity = {}
custID = custEntry.get()
for key, prods in orders.iteritems():
if "c" + custID == key:
#Once we've find the customer, we've got our list of products and can break the loop.
customerProducts = prods
print customerProducts
print '----------'
for key, prods in orders.iteritems():
if custID != key:
customerProducts = set(customerProducts)
prods = set(prods)
extraProducts = customerProducts ^ prods #Xor
extraProducts = prods & extraProducts
similarProducts = prods & customerProducts #interseciton
#Output
print ""
print "Checking customer " + str(key)
print " Number of extra products: " + str(len(extraProducts))
print " Number of similar products: " + str(len(similarProducts))
if(len(similarProducts)!= 0 and len(extraProducts) != 0):
temp1 = len(similarProducts)*len(similarProducts)
temp2 = len(prods)*len(prods)
sim = math.sqrt(float(temp1)/float(temp2))
orderSimilarity[key] = sim
print "-----"
#text.insert(INSERT,"Summary for customer c"+custID)
#text.insert(END,"-----")
summaryLabelVar.set("Summary for customer c" + custID)
summaryLabelVar2.set("Number of similar orders: " + str(len(orderSimilarity)))
mostSimOrder = 0
for key, sim in orderSimilarity.iteritems():
if sim > mostSimOrder:
mostSimOrder = sim
keyToSearch = key
summaryLabelVar3.set("The most Similar order is ["+keyToSearch+"] = " + str(mostSimOrder))
#print "probability of wanting the extra item:"
for key, prods in orders.iteritems():
if key == keyToSearch:
prods = set(prods)
extraProducts = prods ^ customerProducts
extraProducts = prods & extraProducts
print "Extra Products " + str(extraProducts)
similarProducts = prods & customerProducts
if len(extraProducts) != 0:
if v.get() == 0:
for extraProd in extraProducts:
extraProdCat = fileOps.getProductCat(extraProd)
extraProdSim = 0
for simProd in similarProducts:
simProdCat = fileOps.getProductCat(simProd)
extraProdSim += category[simProdCat][extraProdCat]
extraProdSim = extraProdSim / len(similarProducts)
print "sim for :" + extraProd + ":"+ str(extraProdSim)
if extraProdSim > similarityThreshold:
key = "n1"
newOrderDict.setdefault(key, [])
newOrderDict[key].append(extraProd)
#print "_______________"
#print newOrderDict
else:
for extraProd in extraProducts:
key = "n1"
newOrderDict.setdefault(key, [])
newOrderDict[key].append(extraProd)
print "========="
suggestLabelVar.set("Suggesting new order of:"+str(newOrderDict['n1']))
suggestLabelVar2.set("Order Ok ?:")
summaryFrame.pack()
ordOptionsFrame.pack()
# suggest the sim products
# save to new order
def forgetFrames():
summaryFrame.pack_forget()
ordOptionsFrame.pack_forget()
editFrame.pack_forget()
def writeOrd():
print "Writing new order to file"
print "-------------------------"
print newOrderDict
temp = int(fileOps.getNextOrderID())
temp = str(temp+1)
fileOps.writeRecord(newOrderDict,temp)
summaryLabelVar.set("New Order Saved")
newCustProductsVar.set("")
if(state == 0):
addOrder()
if(state == 1):
showMain()
def addProd():
global newOrderDict
print str(newOrderDict)
present = False
updatedOrder = {}
add = eVar.get()
print add
for key, values in newOrderDict.items():
updatedOrder.setdefault(key, [])
for value in values:
updatedOrder[key].append(value)
if value == add:
present = True
if present != True:
updatedOrder[key].append(add)
newOrderDict = updatedOrder
suggestLabelVar.set("Suggesting new order of:"+str(newOrderDict))
newCustProductsVar.set(str(newOrderDict))
print newOrderDict
def removeProd():
global newOrderDict
print "newOrder"+str(newOrderDict)
remove = eVar.get()
updatedOrder = {}
for key, values in newOrderDict.items():
updatedOrder.setdefault(key, [])
for value in values:
if value != remove:
updatedOrder[key].append(value)
newOrderDict = updatedOrder
suggestLabelVar.set("Suggesting new order of:"+str(newOrderDict))
newCustProductsVar.set(str(newOrderDict))
print "-----"
def editOrd():
removeLabelVar.set("Enter products to edit")
editFrame.pack()
def setSim():
similarityThreshold = similarityOptionVar.get()
print "sim changed "+ str(similarityThreshold)
def getNextCustID():
orders = fileOps.openCsvDebug("casesDebug.csv")
temp = 0
for key, prods in orders.iteritems():
if int(key[1:]) > temp:
temp = int(key[1:])
temp = temp + 1
temp = 'c' + str(temp)
return temp
#states
def addOrder():
global newOrderDict
global state
state = 0
newOrderFrame.pack()
configureFrame.pack_forget()
radioFrame.pack_forget()
mainFrame.pack_forget()
summaryFrame.pack_forget()
ordOptionsFrame.pack_forget()
editFrame.pack_forget()
newCustIdVar.set("New Customer Order :"+getNextCustID())
key = getNextCustID()
newOrderDict = {}
newOrderDict.setdefault(key, [])
print str(newOrderDict)
def showOptions():
print v.get()
newOrderFrame.pack_forget()
configureFrame.pack()
radioFrame.pack()
mainFrame.pack_forget()
summaryFrame.pack_forget()
ordOptionsFrame.pack_forget()
editFrame.pack_forget()
def showMain():
global state
state = 1
newOrderFrame.pack_forget()
configureFrame.pack_forget()
radioFrame.pack_forget()
mainFrame.pack()
summaryFrame.pack_forget()
ordOptionsFrame.pack_forget()
editFrame.pack_forget()
#Setup Gui
cbrGui = Tk()
cbrGui.title("Python CBR")
cbrGui.geometry("500x500")
#Setup Menu
menubar = Menu(cbrGui)
cbrGui.config(menu=menubar)
optionsMenu=Menu(menubar,tearoff=0)
menubar.add_cascade(label="Options", menu = optionsMenu)
optionsMenu.add_command(label = "Add new order",command=addOrder)
optionsMenu.add_command(label = "Suggest new order",command=showMain)
optionsMenu.add_command(label = "View orders",command=forgetFrames)
optionsMenu.add_separator()
optionsMenu.add_command(label = "Configure",command=showOptions)
#Add order frame
newCustIdVar = StringVar() #get the last var
newCustProductsVar = StringVar()
#Configure
v = IntVar()
v1 = IntVar()
similarityOptionVar = StringVar()
#Main Var
custEntry = StringVar()
summaryLabelVar = StringVar()
summaryLabelVar2 = StringVar()
summaryLabelVar3 = StringVar()
suggestLabelVar = StringVar()
suggestLabelVar2 = StringVar()
orderOk = StringVar()
output = StringVar()
#Edit Var
removeLabelVar = StringVar()
eVar = StringVar()
#Frames
#------
#Add order frame
newOrderFrame = Frame(cbrGui)
newOrderFrameLabel = Label(newOrderFrame, textvariable = newCustIdVar).pack()
newOrderFrameLabel1 = Label(newOrderFrame, text="Products:").pack()
newOrderFrameLabel2 = Label(newOrderFrame, textvariable = newCustProductsVar).pack()
addOrderProd = Entry(newOrderFrame,textvariable = eVar).pack(side=LEFT)
addOrderBut = Button(newOrderFrame,text = "Remove", command = removeProd).pack(side=LEFT)
addOrderBut2 = Button(newOrderFrame,text = "Add", command = addProd).pack(side=LEFT)
addOrderBut3 = Button(newOrderFrame,text ='Ok',command = writeOrd).pack(side=BOTTOM)
#Configure Frame
configureFrame = Frame(cbrGui)
similarityLabel = Label(configureFrame, text = "Set the minimum similarity (Def = 0.3): ").pack(side=LEFT)
similarityOption = Entry(configureFrame,textvariable = similarityOptionVar).pack(side=LEFT)
setSimButton = Button(configureFrame,text = "Set", command = setSim).pack(side=LEFT)
radioFrame = Frame(cbrGui)
test4=Radiobutton(radioFrame, text="Quantitive", variable=v, value=1).pack(anchor=W)
test5=Radiobutton(radioFrame, text="Qualitative + Quantitive", variable=v, value=0).pack(anchor=W)
#Main Frame
mainFrame = Frame(cbrGui)
mainFrame.pack()
custIdLab = Label(mainFrame,text = 'Enter Customer ID:').pack()
custE = Entry(mainFrame,textvariable = custEntry).pack()
custIdBut = Button(mainFrame,text ='Go',command = main).pack()
output = Text(mainFrame)
summaryLabel = Label(mainFrame, textvariable = summaryLabelVar).pack() #mainframe to show finished
#Summary Frame
summaryFrame = Frame(cbrGui)
summaryLabel2 = Label(summaryFrame, textvariable = summaryLabelVar2).pack()
summaryLabel3 = Label(summaryFrame, textvariable = summaryLabelVar3).pack()
suggestLabel = Label(summaryFrame, textvariable = suggestLabelVar).pack()
suggestLabel2 = Label(summaryFrame, textvariable = suggestLabelVar2).pack()
#Options Frame
ordOptionsFrame = Frame(cbrGui)
ordEditBut = Button(ordOptionsFrame,text ='Edit',command = editOrd).pack(side=LEFT)
ordOkBut = Button(ordOptionsFrame,text ='Ok',command = writeOrd).pack(side=LEFT)
#Edit Frame
editFrame = Frame(cbrGui)
removeLabel = Label(editFrame, textvariable = removeLabelVar).pack()
removeE = Entry(editFrame,textvariable = eVar).pack(side=LEFT)
removeButton = Button(editFrame,text = "Remove", command = removeProd).pack(side=LEFT)
addButton = Button(editFrame,text = "Add", command = addProd).pack(side=LEFT)
cbrGui.mainloop()