class Bot(object): def __init__(self): self.recommendation = Recommendation() def respond_to(self, sender, message): # Enregistre l'utilisateur s'il n'existe pas déjà user = self.recommendation.register_user(sender) # Donne le message pour que l'utilisateur l'utilise user.give_message(message) # Si le chatbot doit faire une recommandation ou pas self.recommendation.make_recommendation(user) if user.should_make_recommendation(): return self.recommendation.make_recommendation(user) else: intro = "" print("bon,bad, neutre") print(user.good_ratings) print(user.bad_ratings) print(user.neutral_ratings) # Si l'utilisateur parle pour la première fois, affiche un message d'intro if not user.has_been_asked_a_question(): intro = "Bonjour ! Je vais vous poser des questions puis vous faire une recommandation.\n" else: if (message == "oui"): user.answer_yes() elif (message == "non"): user.answer_no() else: user.answer_neutral() message = self.recommendation.ask_question(user) return intro + message
def home(): if "recommend" in request.args: try: title = request.args["recommend"] rec = ContentBased() did_you_mean = False df = rec.recommend(title, DEFAULT_LIMIT, full_search=True, keywords_and_desc=False, critics=False) poster_paths = get_poster_paths(df["id"].tolist(), df["original_title"].tolist()) if rec.changed_title != title and rec.changed_title != str(): did_you_mean = True else: rec.changed_title = title rec_title_meta = get_meta(rec.changed_title, None) rec_id = rec_title_meta[0]["id"] return render_template('recommendations.html', titles=df["original_title"].tolist(), images=poster_paths, votes=df["vote_average"].tolist(), m_id=df["id"].tolist(), rec_title=rec.changed_title, rec_id=rec_id, did_you_mean=did_you_mean) except: abort(404) elif "genres" in request.args: genre = request.args["genres"] if genre == "All": genre = None offset = int(request.args["offset"]) gen_rec = Recommendation() gen_rec.filter_genres() df = gen_rec.top_movies(gen_rec.md, percentile=0.85, limit=DEFAULT_LIMIT, offset=offset, genre=genre) poster_paths = get_poster_paths(df["id"].tolist(), df["original_title"].tolist()) return render_template('recommendations.html', titles=df["original_title"].tolist(), images=poster_paths, votes=df["vote_average"].tolist(), m_id=df["id"].tolist(), rec_title=request.args["genres"], offset=offset, next_offset=offset + DEFAULT_LIMIT, prev_offset=offset - DEFAULT_LIMIT, rec_id=None, did_you_mean=None) else: return render_template('homepage.html')
def menu(): recommendation = Recommendation() name_movie = None while name_movie != "": print("#################################") print("RECOMMENDATION CONTENT BASED IMDB") name_movie = input("Enter name movie: ") if name_movie != "": recommendation.recommendation_movies(name_movie)
def compute_recommendation(user): """ Compute the recommendation for a user. Return the recommended events """ recommendation = Recommendation() recommendation.init_basic_matrix() recommendation.init_frequency_matrix() recommendation.compute_matrix() return get_features_event(recommendation.compute_recommended_events(user))
def main(): if (sys.argv[-1] == '--collect'): url_pois = 'https://pt.foursquare.com/explore?mode=url&ne=-29.358988%2C-50.837817&q=Sele%C3%A7%C3%B5es%20principais&sw=-29.41889%2C-50.887942' url_city = 'http://www.dataviva.info/pt/location/5rs020102' e = Extraction(url_pois, url_city) e.poi_data_extraction() e.city_data_extraction() # Gera relatório do dataset file = 'foursquare_data.csv' df = pd.read_csv(file, parse_dates=True, encoding='UTF-8') profile = pandas_profiling.ProfileReport(df) profile.to_file(outputfile='dataset_report.html') P = Process(file) df = P.method() df_report = pd.read_csv('preprocessed.csv', parse_dates=True, encoding='UTF-8') profile = pandas_profiling.ProfileReport(df_report) profile.to_file(outputfile='preprocessed_dataset_report.html') R = Recommendation(df) R.pattern_recommendation() R.new_recommendation() R.compare() # R.test_rec() ont = Ontology() ont.write_owl()
def run(self, category=None, problems_solved=[]): solutions_df = self.load_solutions(category) thetas = Recommendation.get_thetas(solutions_df, nr_features=self.NR_FEATURES, nr_epochs=self.NR_EPOCHS, nr_iterations=self.NR_ITERATIONS) # Thetas already calculated for math category(5) # thetas = pd.read_csv('data_files/thetas.csv') # thetas = thetas.to_dict('list') next_problem = Recommendation.get_next_problem( problems_solved, thetas, nr_features=self.NR_FEATURES, aggressivity_radius=self.AGGRESSIVITY_RADIUS) return next_problem
def setUp(self): clean() c = Category(name="C", external_id="1") w = Weight(name="t", weight=1.0) w.save() c.save() for i in range(0, 3): User(name="U"+str(i), external_id=str(i), token="t"+str(i)).save() users = User.objects.all() for i in range(0, 5): Feature(name="F"+str(i)).save() features = Feature.objects.all() for i in range(0, 5): Event(category=c, external_id=str(i), name='E'+str(i), website='', description='').save() events = Event.objects.all() Rating(event=events[0], user=users[0], rating=1).save() Rating(event=events[1], user=users[0], rating=2).save() Rating(event=events[2], user=users[0], rating=4).save() Rating(event=events[3], user=users[0], rating=5).save() Rating(event=events[4], user=users[0], rating=3).save() Rating(event=events[0], user=users[1], rating=3).save() Rating(event=events[1], user=users[1], rating=2).save() Rating(event=events[2], user=users[1], rating=5).save() Rating(event=events[0], user=users[2], rating=1).save() Rating(event=events[1], user=users[2], rating=4).save() Rating(event=events[4], user=users[2], rating=3).save() EventFeature(event=events[0], feature=features[0], tf_idf=0.2, weight=w).save() EventFeature(event=events[0], feature=features[2], tf_idf=0.8, weight=w).save() EventFeature(event=events[1], feature=features[1], tf_idf=0.8, weight=w).save() EventFeature(event=events[1], feature=features[2], tf_idf=1.0, weight=w).save() EventFeature(event=events[1], feature=features[4], tf_idf=0.3, weight=w).save() EventFeature(event=events[2], feature=features[0], tf_idf=1.0, weight=w).save() EventFeature(event=events[2], feature=features[2], tf_idf=0.3, weight=w).save() EventFeature(event=events[3], feature=features[1], tf_idf=0.2, weight=w).save() EventFeature(event=events[3], feature=features[3], tf_idf=0.4, weight=w).save() EventFeature(event=events[4], feature=features[3], tf_idf=0.8, weight=w).save() FeatureUser(user=users[0], feature=features[1], weight=0.4).save() FeatureUser(user=users[0], feature=features[3], weight=0.8).save() FeatureUser(user=users[0], feature=features[4], weight=0.3).save() FeatureUser(user=users[1], feature=features[0], weight=1.0).save() FeatureUser(user=users[1], feature=features[2], weight=0.5).save() FeatureUser(user=users[2], feature=features[1], weight=0.2).save() FeatureUser(user=users[2], feature=features[4], weight=0.6).save() from recommendation import Recommendation self.recommendation = Recommendation()
def get_meta(title, m_id): rec = Recommendation() rec.filter_genres() rec.filter_productions() df_movies = rec.md df_credits = pd.read_csv(PATH_CREDITS) df_imdb_link = pd.read_csv(PATH_MOVIELENS_TO_TMDB) attributes = [ "id", "original_title", "genres", "homepage", "overview", "release_date", "production_companies", "runtime", "tagline", "vote_average", "vote_count" ] df_title = df_movies.iloc[df_movies.index[df_movies["original_title"] == title][0]][attributes] df_crew = df_credits.iloc[df_credits.index[df_credits["title"] == title] [0]][["cast", "crew"]] cast = [cast["name"] for cast in literal_eval(df_crew["cast"])[0:5]] crew = [ crew["name"] for crew in literal_eval(df_crew["crew"]) if crew["job"] in ["Director"] ] try: imdb_link = str(df_imdb_link.iloc[df_imdb_link.index[ df_imdb_link["tmdbId"] == int(m_id)][0]]["imdbId"])[:-2] imdb_link = ("https://www.imdb.com/title/tt" + "0" * (IMDB_ID_LEN - len(imdb_link)) + imdb_link) except: imdb_link = "https://www.imdb.com/search/title/?title=" + title return df_title, cast, crew, imdb_link
class Bot(object): def __init__(self): self.recommendation = Recommendation() def respond_to(self, sender, message): # Enregistre l'utilisateur s'il n'existe pas déjà user = self.recommendation.register_user(sender) # Donne le message pour que l'utilisateur l'utilise user.give_message(message) # Si le chatbot doit faire une recommandation ou pas if user.should_make_recommendation(): return self.recommendation.make_recommendation(user) else: intro = "" # Si l'utilisateur parle pour la première fois, affiche un message d'intro if not user.has_been_asked_a_question(): intro = "Bonjour ! Je vais vous poser des questions puis vous faire une recommandation.\n" message = self.recommendation.ask_question(user) return intro + message
def compare_list(l1, l2): if len(l1) != len(l2): return False from recommendation import Recommendation try: isSameSize = True for ll1 in l1: for ll2 in l2: isSameSize &= len(ll1) == len(ll2) if not isSameSize: return False for i in range(0, len(l1)): for j in range(0, len(l1[0])): if not Recommendation.compare_float(l1[i][j], l2[i][j]): print '(', i, ';', j, ') ', l1[i][j], ' not eq ', l2[i][j] return False except: for i in range(0, len(l1)): if not Recommendation.compare_float(l1[i], l2[i]): print '(', i, ') ', l1[i], ' not eq ', l2[i] return False return True
def recommend(data): recs = Recommendation(data, 5, 5) return (recs.allRecommendations())
class RecommendationTest1: def setUp(self): clean() c = Category(name="C", external_id="1") w = Weight(name="t", weight=1.0) w.save() c.save() for i in range(0, 3): User(name="U"+str(i), external_id=str(i), token="t"+str(i)).save() users = User.objects.all() for i in range(0, 5): Feature(name="F"+str(i)).save() features = Feature.objects.all() for i in range(0, 5): Event(category=c, external_id=str(i), name='E'+str(i), website='', description='').save() events = Event.objects.all() Rating(event=events[0], user=users[0], rating=1).save() Rating(event=events[1], user=users[0], rating=2).save() Rating(event=events[2], user=users[0], rating=4).save() Rating(event=events[3], user=users[0], rating=5).save() Rating(event=events[4], user=users[0], rating=3).save() Rating(event=events[0], user=users[1], rating=3).save() Rating(event=events[1], user=users[1], rating=2).save() Rating(event=events[2], user=users[1], rating=5).save() Rating(event=events[0], user=users[2], rating=1).save() Rating(event=events[1], user=users[2], rating=4).save() Rating(event=events[4], user=users[2], rating=3).save() EventFeature(event=events[0], feature=features[0], tf_idf=0.2, weight=w).save() EventFeature(event=events[0], feature=features[2], tf_idf=0.8, weight=w).save() EventFeature(event=events[1], feature=features[1], tf_idf=0.8, weight=w).save() EventFeature(event=events[1], feature=features[2], tf_idf=1.0, weight=w).save() EventFeature(event=events[1], feature=features[4], tf_idf=0.3, weight=w).save() EventFeature(event=events[2], feature=features[0], tf_idf=1.0, weight=w).save() EventFeature(event=events[2], feature=features[2], tf_idf=0.3, weight=w).save() EventFeature(event=events[3], feature=features[1], tf_idf=0.2, weight=w).save() EventFeature(event=events[3], feature=features[3], tf_idf=0.4, weight=w).save() EventFeature(event=events[4], feature=features[3], tf_idf=0.8, weight=w).save() FeatureUser(user=users[0], feature=features[1], weight=0.4).save() FeatureUser(user=users[0], feature=features[3], weight=0.8).save() FeatureUser(user=users[0], feature=features[4], weight=0.3).save() FeatureUser(user=users[1], feature=features[0], weight=1.0).save() FeatureUser(user=users[1], feature=features[2], weight=0.5).save() FeatureUser(user=users[2], feature=features[1], weight=0.2).save() FeatureUser(user=users[2], feature=features[4], weight=0.6).save() from recommendation import Recommendation self.recommendation = Recommendation() def test_RFSP(self): RR = [[1, 2, 4, 5, 3],[3, 2, 5, -1, -1],[1, 4, -1, -1, 3]] FF = [[0.2, -1, 0.8, -1, -1],[-1, 0.8, 1, -1, 0.3],[1, -1, 0.3, -1, -1],[-1, 0.2, -1, 0.4, -1],[-1, -1, -1, 0.8, -1]] SS = [[0, 0.4, 0, 0.8, 0.3],[1, 0, 0.5, 0, 0],[0, 0.2, 0, 0, 0.6]] PP = [[1, 0.2, 0.3, 1.2, 0],[1.2, 0, 1.1, 0, 0],[0, 0.8, 1, 0.8, 0.3]] self.recommendation.init_basic_matrix() if not compare_list(RR, transform_matrix_list(self.recommendation.get_matrix_r())): print 'error RR test_RFSP' print RR, '\n', transform_matrix_list(self.recommendation.get_matrix_r()) if not compare_list(FF, transform_matrix_list(self.recommendation.get_matrix_f())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list(self.recommendation.get_matrix_f()) if not compare_list(SS, transform_matrix_list(self.recommendation.get_matrix_s())): print 'error SS test_RFSP' print SS, '\n', transform_matrix_list(self.recommendation.get_matrix_s()) if not compare_list(PP, transform_matrix_list(self.recommendation.get_matrix_p())): print 'error PP test_RFSP' print PP, '\n', transform_matrix_list(self.recommendation.get_matrix_p()) def test_FF_UF_IUF(self): FF = [[1, 0.6, 0.3, 2.0, 0.3],[2.2, 0, 1.6, 0, 0],[0, 1.0, 1, 0.8, 0.9]] UF = [2, 2, 3, 2, 2] IUF = [0.176091259056, 0.176091259056, 0.0, 0.176091259056, 0.176091259056] self.recommendation.init_frequency_matrix() if not compare_list(FF, transform_matrix_list(self.recommendation.get_matrix_ff())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list(self.recommendation.get_matrix_ff()) if not compare_list(UF, transform_matrix_list(self.recommendation.get_matrix_uf())): print 'error UF test_RFSP' print UF, '\n', transform_matrix_list(self.recommendation.get_matrix_uf()) if not compare_list(IUF, transform_matrix_list(self.recommendation.get_matrix_iuf())): print 'error IUF test_RFSP' print IUF, '\n', transform_matrix_list(self.recommendation.get_matrix_iuf()) def test_W_UU(self): W = [[0.176091259056, 0.105654755433, -1, 0.352182518111, 0.0528273777167],[0.387400769922, -1, -1, -1, -1],[-1, 0.176091259056, -1, 0.140873007245, 0.15848213315]] UU = [[-1, 1.0, 0.822730338093],[1.0, -1, -1],[0.822730338093, -1, -1]] self.recommendation.compute_matrix_w() self.recommendation.compute_matrix_uu() if not compare_list(W, transform_matrix_list(self.recommendation.get_matrix_w())): print 'error W test_RFSP' print W, '\n', transform_matrix_list(self.recommendation.get_matrix_w()) if not compare_list(UU, transform_matrix_list(self.recommendation.get_matrix_uu())): print 'error UU test_RFSP' print UU, '\n', transform_matrix_list(self.recommendation.get_matrix_uu()) def test_final(self): events = Event.objects.all() res = {events[3]: 3, events[4]: 2} res = OrderedDict(sorted(res.items(), key=lambda t: t[1], reverse=True)) if not compare_ordered_dict(self.recommendation.compute_recommended_events(User.objects.all()[1]), res): print 'error final result' print res print self.recommendation.compute_recommended_events(User.objects.all()[1])
class RecommendationTest2: def setUp(self): clean() c = Category(name="C", external_id="1") w = Weight(name="t", weight=1.0) w.save() c.save() for i in range(0, 4): User(name="U"+str(i), external_id=str(i), token="t"+str(i)).save() users = User.objects.all() for i in range(0, 4): Feature(name="F"+str(i)).save() features = Feature.objects.all() for i in range(0, 6): Event(category=c, external_id=str(i), name='E'+str(i), website='', description='').save() events = Event.objects.all() Rating(event=events[1], user=users[0], rating=4).save() Rating(event=events[4], user=users[0], rating=5).save() Rating(event=events[1], user=users[1], rating=3).save() Rating(event=events[3], user=users[1], rating=4).save() Rating(event=events[5], user=users[2], rating=4).save() Rating(event=events[0], user=users[3], rating=5).save() Rating(event=events[2], user=users[3], rating=3).save() EventFeature(event=events[0], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[1], feature=features[0], tf_idf=1, weight=w).save() EventFeature(event=events[1], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[2], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[2], feature=features[2], tf_idf=1, weight=w).save() EventFeature(event=events[3], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[0], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[2], tf_idf=1, weight=w).save() EventFeature(event=events[5], feature=features[3], tf_idf=1, weight=w).save() from recommendation import Recommendation self.recommendation = Recommendation() def test_RFSP(self): RR = [[-1,4,-1,-1,5,-1],[-1,3,-1,4,-1,-1],[-1,-1,-1,-1,-1,4],[5,-1,3,-1,-1,-1]] FF = [[-1,1,-1,-1],[1,1,-1,-1],[-1,1,1,-1],[-1,1,-1,-1],[1,1,1,-1],[-1,-1,-1,1]] SS = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] PP = [[2,2,1,0],[1,2,0,0],[0,0,0,1],[0,2,1,0]] self.recommendation.init_basic_matrix() if not compare_list(RR, transform_matrix_list(self.recommendation.get_matrix_r())): print 'error RR test_RFSP' print RR, '\n', transform_matrix_list(self.recommendation.get_matrix_r()) if not compare_list(FF, transform_matrix_list(self.recommendation.get_matrix_f())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list(self.recommendation.get_matrix_f()) if not compare_list(SS, transform_matrix_list(self.recommendation.get_matrix_s())): print 'error SS test_RFSP' print SS, '\n', transform_matrix_list(self.recommendation.get_matrix_s()) if not compare_list(PP, transform_matrix_list(self.recommendation.get_matrix_p())): print 'error PP test_RFSP' print PP, '\n', transform_matrix_list(self.recommendation.get_matrix_p()) def test_FF_UF_IUF(self): FF = [[2,2,1,0],[1,2,0,0],[0,0,0,1],[0,2,1,0]] UF = [2,3,2,1] IUF = [0.30102999566398114, 0.1249387366082999, 0.30102999566398114, 0.6020599913279623] self.recommendation.init_frequency_matrix() if not compare_list(FF, transform_matrix_list(self.recommendation.get_matrix_ff())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list(self.recommendation.get_matrix_ff()) if not compare_list(UF, transform_matrix_list(self.recommendation.get_matrix_uf())): print 'error UF test_RFSP' print UF, '\n', transform_matrix_list(self.recommendation.get_matrix_uf()) if not compare_list(IUF, transform_matrix_list(self.recommendation.get_matrix_iuf())): print 'error IUF test_RFSP' print IUF, '\n', transform_matrix_list(self.recommendation.get_matrix_iuf()) def test_W_UU(self): W = [[0.6020599913279623, 0.2498774732165998, 0.30102999566398114, -1], [0.30102999566398114, 0.2498774732165998, -1, -1], [-1, -1, -1, 0.6020599913279623], [-1, 0.2498774732165998, 0.30102999566398114, -1]] UU = [[-1, 0.95551065333456875, -1.0, 1.0], [0.95551065333456875, -1, -1.0, 1], [-1.0, -1.0, -1, -1.0], [1, 1, -1.0, -1]] self.recommendation.compute_matrix_w() self.recommendation.compute_matrix_uu() if not compare_list(W, transform_matrix_list(self.recommendation.get_matrix_w())): print 'error W test_RFSP' print W, '\n', transform_matrix_list(self.recommendation.get_matrix_w()) if not compare_list(UU, transform_matrix_list(self.recommendation.get_matrix_uu())): print 'error UU test_RFSP' print UU, '\n', transform_matrix_list(self.recommendation.get_matrix_uu()) def test_final(self): events = Event.objects.all() res = {events[0]: 3, events[2]: 5, events[4] : 6} res = OrderedDict(sorted(res.items(), key=lambda t: t[1], reverse=True)) if not compare_ordered_dict(self.recommendation.compute_recommended_events(User.objects.all()[1]), res): print 'error final result' print res print self.recommendation.compute_recommended_events(User.objects.all()[1])
def setUp(self): clean() c = Category(name="C", external_id="1") w = Weight(name="t", weight=1.0) w.save() c.save() for i in range(0, 4): User(name="U" + str(i), external_id=str(i), token="t" + str(i)).save() users = User.objects.all() for i in range(0, 4): Feature(name="F" + str(i)).save() features = Feature.objects.all() for i in range(0, 6): Event(category=c, external_id=str(i), name='E' + str(i), website='', description='').save() events = Event.objects.all() Rating(event=events[1], user=users[0], rating=4).save() Rating(event=events[4], user=users[0], rating=5).save() Rating(event=events[1], user=users[1], rating=3).save() Rating(event=events[3], user=users[1], rating=4).save() Rating(event=events[5], user=users[2], rating=4).save() Rating(event=events[0], user=users[3], rating=5).save() Rating(event=events[2], user=users[3], rating=3).save() EventFeature(event=events[0], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[1], feature=features[0], tf_idf=1, weight=w).save() EventFeature(event=events[1], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[2], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[2], feature=features[2], tf_idf=1, weight=w).save() EventFeature(event=events[3], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[0], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[2], tf_idf=1, weight=w).save() EventFeature(event=events[5], feature=features[3], tf_idf=1, weight=w).save() from recommendation import Recommendation self.recommendation = Recommendation()
class RecommendationTest2: def setUp(self): clean() c = Category(name="C", external_id="1") w = Weight(name="t", weight=1.0) w.save() c.save() for i in range(0, 4): User(name="U" + str(i), external_id=str(i), token="t" + str(i)).save() users = User.objects.all() for i in range(0, 4): Feature(name="F" + str(i)).save() features = Feature.objects.all() for i in range(0, 6): Event(category=c, external_id=str(i), name='E' + str(i), website='', description='').save() events = Event.objects.all() Rating(event=events[1], user=users[0], rating=4).save() Rating(event=events[4], user=users[0], rating=5).save() Rating(event=events[1], user=users[1], rating=3).save() Rating(event=events[3], user=users[1], rating=4).save() Rating(event=events[5], user=users[2], rating=4).save() Rating(event=events[0], user=users[3], rating=5).save() Rating(event=events[2], user=users[3], rating=3).save() EventFeature(event=events[0], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[1], feature=features[0], tf_idf=1, weight=w).save() EventFeature(event=events[1], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[2], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[2], feature=features[2], tf_idf=1, weight=w).save() EventFeature(event=events[3], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[0], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[1], tf_idf=1, weight=w).save() EventFeature(event=events[4], feature=features[2], tf_idf=1, weight=w).save() EventFeature(event=events[5], feature=features[3], tf_idf=1, weight=w).save() from recommendation import Recommendation self.recommendation = Recommendation() def test_RFSP(self): RR = [[-1, 4, -1, -1, 5, -1], [-1, 3, -1, 4, -1, -1], [-1, -1, -1, -1, -1, 4], [5, -1, 3, -1, -1, -1]] FF = [[-1, 1, -1, -1], [1, 1, -1, -1], [-1, 1, 1, -1], [-1, 1, -1, -1], [1, 1, 1, -1], [-1, -1, -1, 1]] SS = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] PP = [[2, 2, 1, 0], [1, 2, 0, 0], [0, 0, 0, 1], [0, 2, 1, 0]] self.recommendation.init_basic_matrix() if not compare_list( RR, transform_matrix_list(self.recommendation.get_matrix_r())): print 'error RR test_RFSP' print RR, '\n', transform_matrix_list( self.recommendation.get_matrix_r()) if not compare_list( FF, transform_matrix_list(self.recommendation.get_matrix_f())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list( self.recommendation.get_matrix_f()) if not compare_list( SS, transform_matrix_list(self.recommendation.get_matrix_s())): print 'error SS test_RFSP' print SS, '\n', transform_matrix_list( self.recommendation.get_matrix_s()) if not compare_list( PP, transform_matrix_list(self.recommendation.get_matrix_p())): print 'error PP test_RFSP' print PP, '\n', transform_matrix_list( self.recommendation.get_matrix_p()) def test_FF_UF_IUF(self): FF = [[2, 2, 1, 0], [1, 2, 0, 0], [0, 0, 0, 1], [0, 2, 1, 0]] UF = [2, 3, 2, 1] IUF = [ 0.30102999566398114, 0.1249387366082999, 0.30102999566398114, 0.6020599913279623 ] self.recommendation.init_frequency_matrix() if not compare_list( FF, transform_matrix_list( self.recommendation.get_matrix_ff())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list( self.recommendation.get_matrix_ff()) if not compare_list( UF, transform_matrix_list( self.recommendation.get_matrix_uf())): print 'error UF test_RFSP' print UF, '\n', transform_matrix_list( self.recommendation.get_matrix_uf()) if not compare_list( IUF, transform_matrix_list( self.recommendation.get_matrix_iuf())): print 'error IUF test_RFSP' print IUF, '\n', transform_matrix_list( self.recommendation.get_matrix_iuf()) def test_W_UU(self): W = [[0.6020599913279623, 0.2498774732165998, 0.30102999566398114, -1], [0.30102999566398114, 0.2498774732165998, -1, -1], [-1, -1, -1, 0.6020599913279623], [-1, 0.2498774732165998, 0.30102999566398114, -1]] UU = [[-1, 0.95551065333456875, -1.0, 1.0], [0.95551065333456875, -1, -1.0, 1], [-1.0, -1.0, -1, -1.0], [1, 1, -1.0, -1]] self.recommendation.compute_matrix_w() self.recommendation.compute_matrix_uu() if not compare_list( W, transform_matrix_list(self.recommendation.get_matrix_w())): print 'error W test_RFSP' print W, '\n', transform_matrix_list( self.recommendation.get_matrix_w()) if not compare_list( UU, transform_matrix_list( self.recommendation.get_matrix_uu())): print 'error UU test_RFSP' print UU, '\n', transform_matrix_list( self.recommendation.get_matrix_uu()) def test_final(self): events = Event.objects.all() res = {events[0]: 3, events[2]: 5, events[4]: 6} res = OrderedDict(sorted(res.items(), key=lambda t: t[1], reverse=True)) if not compare_ordered_dict( self.recommendation.compute_recommended_events( User.objects.all()[1]), res): print 'error final result' print res print self.recommendation.compute_recommended_events( User.objects.all()[1])
class RecommendationTest1: def setUp(self): clean() c = Category(name="C", external_id="1") w = Weight(name="t", weight=1.0) w.save() c.save() for i in range(0, 3): User(name="U" + str(i), external_id=str(i), token="t" + str(i)).save() users = User.objects.all() for i in range(0, 5): Feature(name="F" + str(i)).save() features = Feature.objects.all() for i in range(0, 5): Event(category=c, external_id=str(i), name='E' + str(i), website='', description='').save() events = Event.objects.all() Rating(event=events[0], user=users[0], rating=1).save() Rating(event=events[1], user=users[0], rating=2).save() Rating(event=events[2], user=users[0], rating=4).save() Rating(event=events[3], user=users[0], rating=5).save() Rating(event=events[4], user=users[0], rating=3).save() Rating(event=events[0], user=users[1], rating=3).save() Rating(event=events[1], user=users[1], rating=2).save() Rating(event=events[2], user=users[1], rating=5).save() Rating(event=events[0], user=users[2], rating=1).save() Rating(event=events[1], user=users[2], rating=4).save() Rating(event=events[4], user=users[2], rating=3).save() EventFeature(event=events[0], feature=features[0], tf_idf=0.2, weight=w).save() EventFeature(event=events[0], feature=features[2], tf_idf=0.8, weight=w).save() EventFeature(event=events[1], feature=features[1], tf_idf=0.8, weight=w).save() EventFeature(event=events[1], feature=features[2], tf_idf=1.0, weight=w).save() EventFeature(event=events[1], feature=features[4], tf_idf=0.3, weight=w).save() EventFeature(event=events[2], feature=features[0], tf_idf=1.0, weight=w).save() EventFeature(event=events[2], feature=features[2], tf_idf=0.3, weight=w).save() EventFeature(event=events[3], feature=features[1], tf_idf=0.2, weight=w).save() EventFeature(event=events[3], feature=features[3], tf_idf=0.4, weight=w).save() EventFeature(event=events[4], feature=features[3], tf_idf=0.8, weight=w).save() FeatureUser(user=users[0], feature=features[1], weight=0.4).save() FeatureUser(user=users[0], feature=features[3], weight=0.8).save() FeatureUser(user=users[0], feature=features[4], weight=0.3).save() FeatureUser(user=users[1], feature=features[0], weight=1.0).save() FeatureUser(user=users[1], feature=features[2], weight=0.5).save() FeatureUser(user=users[2], feature=features[1], weight=0.2).save() FeatureUser(user=users[2], feature=features[4], weight=0.6).save() from recommendation import Recommendation self.recommendation = Recommendation() def test_RFSP(self): RR = [[1, 2, 4, 5, 3], [3, 2, 5, -1, -1], [1, 4, -1, -1, 3]] FF = [[0.2, -1, 0.8, -1, -1], [-1, 0.8, 1, -1, 0.3], [1, -1, 0.3, -1, -1], [-1, 0.2, -1, 0.4, -1], [-1, -1, -1, 0.8, -1]] SS = [[0, 0.4, 0, 0.8, 0.3], [1, 0, 0.5, 0, 0], [0, 0.2, 0, 0, 0.6]] PP = [[1, 0.2, 0.3, 1.2, 0], [1.2, 0, 1.1, 0, 0], [0, 0.8, 1, 0.8, 0.3]] self.recommendation.init_basic_matrix() if not compare_list( RR, transform_matrix_list(self.recommendation.get_matrix_r())): print 'error RR test_RFSP' print RR, '\n', transform_matrix_list( self.recommendation.get_matrix_r()) if not compare_list( FF, transform_matrix_list(self.recommendation.get_matrix_f())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list( self.recommendation.get_matrix_f()) if not compare_list( SS, transform_matrix_list(self.recommendation.get_matrix_s())): print 'error SS test_RFSP' print SS, '\n', transform_matrix_list( self.recommendation.get_matrix_s()) if not compare_list( PP, transform_matrix_list(self.recommendation.get_matrix_p())): print 'error PP test_RFSP' print PP, '\n', transform_matrix_list( self.recommendation.get_matrix_p()) def test_FF_UF_IUF(self): FF = [[1, 0.6, 0.3, 2.0, 0.3], [2.2, 0, 1.6, 0, 0], [0, 1.0, 1, 0.8, 0.9]] UF = [2, 2, 3, 2, 2] IUF = [ 0.176091259056, 0.176091259056, 0.0, 0.176091259056, 0.176091259056 ] self.recommendation.init_frequency_matrix() if not compare_list( FF, transform_matrix_list( self.recommendation.get_matrix_ff())): print 'error FF test_RFSP' print FF, '\n', transform_matrix_list( self.recommendation.get_matrix_ff()) if not compare_list( UF, transform_matrix_list( self.recommendation.get_matrix_uf())): print 'error UF test_RFSP' print UF, '\n', transform_matrix_list( self.recommendation.get_matrix_uf()) if not compare_list( IUF, transform_matrix_list( self.recommendation.get_matrix_iuf())): print 'error IUF test_RFSP' print IUF, '\n', transform_matrix_list( self.recommendation.get_matrix_iuf()) def test_W_UU(self): W = [[ 0.176091259056, 0.105654755433, -1, 0.352182518111, 0.0528273777167 ], [0.387400769922, -1, -1, -1, -1], [-1, 0.176091259056, -1, 0.140873007245, 0.15848213315]] UU = [[-1, 1.0, 0.822730338093], [1.0, -1, -1], [0.822730338093, -1, -1]] self.recommendation.compute_matrix_w() self.recommendation.compute_matrix_uu() if not compare_list( W, transform_matrix_list(self.recommendation.get_matrix_w())): print 'error W test_RFSP' print W, '\n', transform_matrix_list( self.recommendation.get_matrix_w()) if not compare_list( UU, transform_matrix_list( self.recommendation.get_matrix_uu())): print 'error UU test_RFSP' print UU, '\n', transform_matrix_list( self.recommendation.get_matrix_uu()) def test_final(self): events = Event.objects.all() res = {events[3]: 3, events[4]: 2} res = OrderedDict(sorted(res.items(), key=lambda t: t[1], reverse=True)) if not compare_ordered_dict( self.recommendation.compute_recommended_events( User.objects.all()[1]), res): print 'error final result' print res print self.recommendation.compute_recommended_events( User.objects.all()[1])
def recommend(self, title, limit, critics=False, full_search=False, use_pickle=True, keywords_and_desc=False): """ param: title - movie title (as in TMDB dataset) limit - no. of movies to display critics - True - will display critically acclaimed movies False - will not sort movies on basis of their imdb rankings (DEFAULT - False) full_search - True - will search using cast, crew, keywords and genre as metadata False - will search using overview and tagline as metadata (DEFAULT - False) use_pickle - True - will use pickled results False - will compute the results from scratch (DEFAULT - True) keywords_and_desc - True - will merge results of keywords and description False - will not merge results of keywords and description return: pandas DataFrame object with attributes - original_title, id, vote_average, vote_count, popularity, release_date """ rec = Recommendation() rec.filter_genres() title_index = self.verify_title(rec.md, title) if keywords_and_desc: if isfile(PATH_PICKLE_KEYWORDS) and isfile( PATH_PICKLE_DESC) and use_pickle: df_keywords = pd.read_pickle(PATH_PICKLE_KEYWORDS) df_desc = pd.read_pickle(PATH_PICKLE_DESC) rec_matrix_keywords = self.countvectorize(df_keywords) rec_matrix_desc = self.tfidf(df_desc) rec_matrix = rec_matrix_keywords + rec_matrix_desc df = df_keywords elif full_search: if isfile(PATH_PICKLE_KEYWORDS) and use_pickle: df = pd.read_pickle(PATH_PICKLE_KEYWORDS) else: df = self.make_keywords(rec.md) df.to_pickle(PATH_PICKLE_KEYWORDS) rec_matrix = self.countvectorize(df) else: if isfile(PATH_PICKLE_DESC) and use_pickle: df = pd.read_pickle(PATH_PICKLE_DESC) else: df = self.make_desc(rec.md) df.to_pickle(PATH_PICKLE_DESC) rec_matrix = self.tfidf(df) rec_movie = rec_matrix[title_index] ids = rec_movie.argsort()[::-1][1:SCAN_SIZE + 1] if critics: return rec.top_movies(df.iloc[ids], percentile=0.50, limit=limit, offset=0) else: return df.iloc[ids[:limit]][[ "original_title", "id", "vote_average", "vote_count", "popularity", "release_date", ]]
def __init__(self): self.recommendation = Recommendation()
def test_recommendation(self): recommendation = Recommendation(self.client) recommendation.forward_to("book1", "book2") recommendation.forward_to("book1", "book2") recommendation.forward_to("book1", "book2") recommendation.forward_to("book1", "book3") recommendation.forward_to("book1", "book3") recommendation.forward_to("book1", "book4") recommendation.forward_to("book1", "book5") recommendation.forward_to("book1", "book6") recommendation.forward_to("book1", "book6") assert recommendation.get_recommendations("book1", 1, 5) == [ "book2", "book6", "book3", "book5", "book4", ] assert recommendation.get_recommendations("book1", 1, 5, with_time=True) == [ ("book2", 3), ("book6", 2), ("book3", 2), ("book5", 1), ("book4", 1), ]