Python Forum
reccomending, according to the selected listbox value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reccomending, according to the selected listbox value
#1
the code is a bit long, hope you understand it, i have this small app, where i have to implement data from 2 files, and then showing some of the values(of data) in list box. on the other side i have combined the values in nested dictionary. Now i have to recommend movie according the the value i will select from list box. Maybe the code i have written is wrong in some parts, though in the last part, when i have to write the code of recommendation, i have no clue how i have to write the code, i so it will recommend me movie, for further explanation, i will be writing in comments part if needed. (Data&recommendations file: https://www.dropbox.com/sh/b949hapk5lmm9...qw69a?dl=0)

from Tkinter import *
import tkFileDialog
import csv
from recommendations import *
class STproject:

def __init__(self): #1

    self.variableclasses()
    self.buttonsnlabels()
    self.dictionary()


def variableclasses(self):
    self.all_vars = [StringVar() for _ in range(2)]
    self.var1 = StringVar()
    self.var2 = StringVar()

    self.var1.set(0)
    self.var2.set(0)

def buttonsnlabels(self):

    self.rdbttn=Radiobutton(root,text='user based recommendation',variable = self.var1,value=1,command=lambda:self.dictionary())#, command=lambda :self.selec1())
    self.rdbttn.grid(row=0,column=0)
    self.rdbttn2=Radiobutton(root,text='movie based recommendation',variable = self.var1,value=2,command=lambda:self.dictionary())#,command=lambda :self.selec1())
    self.rdbttn2.grid(row=1,column=0)
    self.rdbttn3=Radiobutton(root,text='pearson',variable = self.var2,value=1)#,command=lambda :self.selec2())
    self.rdbttn3.grid(row=2,column=0)
    self.rdbttn4=Radiobutton(root,text='euclidean',variable = self.var2,value=2)#,command=lambda :self.selec2())
    self.rdbttn4.grid(row=3,column=0)

    self.ratingbutton=Button(root,text='Upload Rating',command=lambda :self.file(self.all_vars[0]))
    self.ratingbutton.grid()
    self.ratingbutton=Button(root,text='Upload Movies',command=lambda :self.file(self.all_vars[1]))
    self.ratingbutton.grid()

    self.lb1 = Listbox(root)
    self.lb1.grid(row=0,column=1)
    self.lb2 = Listbox(root)
    self.lb2.grid(row=0,column=1)
    self.lb3 = Listbox(root)
    self.lb3.grid(row=0,column=1)

def file(self, v):
    result = tkFileDialog.askopenfilename()
    if result:
        v.set(result)
    # self.dictionary()

def dictionary(self):
    # self.lb1.delete(END, 0)  #clear listbox
    # self.lb1.update_idletasks()
    if self.var1.get()==1:
        self.d = {}
        if all(i.get() for i in self.all_vars): #process only if all 2 files are selected
            with open(self.all_vars[0].get(),"r") as a, open(self.all_vars[1].get(),"r") as b:
                for line1,line2,line3 in zip(csv.reader(a),csv.reader(b),csv.reader(a)):
                    self.d[line1[0]]={line2[1]:line3[2]}
                    self.lb1.insert('end',line2[1])
                print self.d

    else:
        self.d = {}
        if all(i.get() for i in self.all_vars): #process only if all 2 files are selected
            with open(self.all_vars[0].get(),"r") as a, open(self.all_vars[1].get(),"r") as b:
                for line1,line2,line3 in zip(csv.reader(a),csv.reader(b),csv.reader(a)):
                    self.d[line1[0]]={line2[1]:line3[2]}
                    self.lb1.insert('end', line1[1])
                print self.d

def recoms(self):
    # selection=self.lb1.curselection()
    # print selection
    if self.var2.get()==1:
        getRecommendations()
    else:
        pass
root=Tk()
root.title('SteelBox Inc. Calculator')
application=STproject() #2
root.mainloop() #3
later i wrote this one, though i got error
def selected(self):
        selection=self.lb1.curselection()
        print selection

    def recoms(self):

        if self.var2.get()==1:
            a=getRecommendations(STproject.dictionary,STproject.selected)
            self.lb2.insert(a)
        else:
            b=getRecommendations(STproject.dictionary,STproject.selected,similarity=sim_distance)
            self.lb2.insert(b)
Error:
Traceback (most recent call last): File "C:/Users/Umer Selmani/Desktop/MP2/test panda.py", line 125, in <module> application=STproject(root) #2 File "C:/Users/Umer Selmani/Desktop/MP2/test panda.py", line 31, in __init__ self.recoms() File "C:/Users/Umer Selmani/Desktop/MP2/test panda.py", line 120, in recoms b=getRecommendations(STproject.dictionary,STproject.selected,similarity=sim_distance) File "C:\Users\Umer Selmani\Desktop\MP2\recommendations.py", line 82, in getRecommendations for other in prefs: TypeError: 'instancemethod' object is not iterable

the recommendation if it is user based it will recommend movies according to the ratings they gave, if it is movie based, it will recommend acorrding to the genre
Reply
#2
Inside the function def getRecommendations(prefs, person, similarity = sim_pearson):
its expecting the passed in prefs to be an object that can be iterated
for other in prefs:
you are calling this method with b=getRecommendations(STproject.dictionary,STproject.selected,similarity=sim_distance)
it looks like you want to be calling the instance instead of the class
b=getRecommendations(self.dictionary,self.selected,similarity=sim_distance)
The method dictionary does not return a iterable of prefs and the method selected does not return a person.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020