Ejemplo n.º 1
0
def recommend_joke():
    '''
    session - user_pref, joke_num(0-19)
    '''
    #write code to get the first joke and compute the matrix
    #we have loaded the data
    data_raw = pd.read_csv('ratings.csv' ,index_col = 0)
    data_jokes = pd.read_csv('jokes.csv', index_col = 0)
    data_final = data_raw[:100000]

    #create an object of recommender
    reco = Recommender(data_final, data_jokes)
    
    res = reco.get_most_popular()
    joke = res[0]
    joke_num = res[1]


    
    if request.method == 'GET':
        
        #we will randomly select one of the highest rated joke and display it
        #insert code to get the joke

        session['joke_num'] = joke_num #this is default but you will need to get the joke number you are displaying
        session['prev_joke'] = joke
        return render_template('joke.html', joke= joke)
    
    else:

        #This is the value which the user gave to the previous joke which is represented by joke_num
        value = request.form["rating"]
        session.pop('rating',None)

        #get the joke_num
        last_joke = session['joke_num']
        session.pop('joke_num',None)

        prev_joke = session['prev_joke']
        session.pop('prev_joke',None)

        #RDS ADD JOKE FOR FUTURE ANALYSIS
        joke_add = Jokes(joke = prev_joke,rating = value)

        db.session.add(joke_add)
        db.session.commit()
    
    
        #now that we have the joke number we will create the svd with this information
        
        #check if session is set
        if 'user_pref' in session:

            curr_user_pref = list(session['user_pref'])
            
            session.pop('user_pref',None)
            
            curr_user_pref[last_joke] = int(value)
            #fetch the data
            #create the matrix
            #append the user_pref
            #calculate svd
            
            #first we will get the interaction matrix
            interaction_df = reco.get_interaction()
            #append the user_pref
            new_df = reco.append_new_user(interaction_df, curr_user_pref)

            #calculate svd using the user_pref
            preds_df = reco.get_svd(new_df)

            #get the prediction

            recommended_res = reco.recommend_joke(preds_df,curr_user_pref)
            joke = recommended_res[0]
            joke_num= recommended_res[1]



            #set the sessions
            session['joke_num'] = joke_num
            session['user_pref'] = curr_user_pref
            session['prev_joke'] = joke
            
            return render_template('recommended_jokes.html', joke= joke)

        else :

            
            #first we will get the interaction matrix
            interaction_df = reco.get_interaction()

            #for that we will set a user_pref
            user_pref = [0]*interaction_df.shape[1] #create a list with the length of the number of jokes

            user_pref[last_joke] = int(value)
            
            
            #append the user_pref
            new_df = reco.append_new_user(interaction_df, user_pref)

            #calculate svd using the user_pref
            preds_df = reco.get_svd(new_df)



            #get the prediction

            recommended_res = reco.recommend_joke(preds_df,user_pref)
            joke = recommended_res[0]
            joke_num= recommended_res[1]

            new_joke = joke
            new_joke_number = joke_num#needs to be the one that is being recommend_joke
            
            #set the sessions
            session['joke_num'] = new_joke_number
            session['user_pref'] = user_pref
            session['prev_joke'] = joke


            return render_template('recommended_jokes.html', joke= new_joke)