def select(self): sf = var.get() win.destroy() if (rORg == "recommend"): recommend(me, sf) else: graphPlot(me, sf)
def search(): #request为全局变量可得到用户输入信息 n=request.args.get('user') #调用推荐函数 dic=recommend.recommend(n) #用返回结果进行模板渲染 return render_template('search.html',Data=dic)
def search(): #request为全局变量可得到用户输入信息 n = request.args.get('user') dic = recommend.recommend(n) return render_template('search.html', Data=dic)
def get(self, movie_id): movie = get_movie_or_404(movie_id) ml_dir = os.path.abspath( os.path.join(get_this_dir(__file__), "../../ml")) clusters_path = os.path.abspath(os.path.join(ml_dir, "clusters.pk")) cluster_numbers_path = os.path.abspath( os.path.join(ml_dir, "cluster-numbers.pk")) cluster_data = [clusters_path, cluster_numbers_path] # cluster_data = [os.path.join('..', 'ml', 'clusters.pk'), os.path.join('..', 'ml', 'cluster-numbers.pk')] recommendation_data = recommend.recommend( movie['Title'], cluster_data)['cluster_movies'] recommendations = list(map(lambda x: x['id'], recommendation_data)) recommendations.remove(movie_id) args = movie_recom_req_parser.parse_args() limit = args.get('limit') if limit is None: limit = len(recommendations) limited_recom = recommendations[:limit] return { 'movie_id': movie_id, 'movie': movie, 'num_recommendations': len(limited_recom), 'recommendations': get_movies_info(limited_recom) }
def test_recom(self): myMat = mat(svd_rec.loadExData()) print("\n myMat == %s" % (myMat)) myMat[0, 1] = myMat[0, 0] = myMat[1, 0] = myMat[2, 0] = 4 myMat[3, 3] = 2 print("\n myMat == %s" % (myMat)) recommend_mat = recommend.recommend(myMat, 2) # [(2, 2.5), (1, 2.0243290220056256)] # 表面用户2对物品2的评分为2.5分,对物品1的预测评分值为2.024 print("\n recommend_mat == %s" % (recommend_mat)) recommend_mat = recommend.recommend(myMat, 2, simMeas=sim.ecludSim) print("\n recommend_mat == %s" % (recommend_mat)) recommend_mat = recommend.recommend(myMat, 2, simMeas=sim.pearsSim) print("\n recommend_mat == %s" % (recommend_mat))
def evaluation_per_user(line): number_of_matches = 0 number_of_mismatches = 0 elems = line.split(';') test_user_input = dict(eval(elems[1])) if not len(test_user_input) == 0: most_preferred_user_genre = max(test_user_input, key=test_user_input.get) else: most_preferred_user_genre = None preferred_genres = [ genre for genre in test_user_input if test_user_input[genre] > 5 ] recommendations = recommend(evaluate(test_user_input)) for book in books_with_genres_dict: if book in recommendations: if most_preferred_user_genre in books_with_genres_dict[book]: number_of_matches += 1 elif most_preferred_user_genre == None: pass elif len([ val for val in preferred_genres if val in books_with_genres_dict[book] ]) > 0: number_of_matches += 1 else: number_of_mismatches += 1 return (number_of_matches, number_of_mismatches)
def MovieDetail(response, **kwargs): title = kwargs.get('title') present = "FALSE" if response.user.is_authenticated: profile = Profile.objects.get(user = response.user) movie = Movie.objects.get(title = title) history_model = ThroughModel.objects.filter(profile=profile).order_by('date_viewed') ThroughModel.objects.create(profile=profile, movie=movie) if history_model.count() > 5: history_model.first().delete() watchlist = profile.watchlist.filter(title = title) if watchlist.count() == 0: present = 'FALSE' else: present = 'TRUE' if response.method == 'POST': if response.POST.get("save") == "add": profile.watchlist.add(movie) present = "TRUE" elif response.POST.get("save") == "remove": profile.watchlist.remove(movie) present = "FALSE" else: pass movies = recommend.recommend(title) movies = Movie.objects.filter(title__in = movies).order_by('-rating') current = Movie.objects.filter(title = title).first() context = { 'movies' : movies, 'current' : current, 'present' : present } return render(response, 'movie/detail.html', context)
def search(): if request.method == 'GET': lyrics = genius.get_lyrics(request.args['query']) else: lyrics = genius.get_lyrics(request.form['query']) data = recommend.recommend(lyrics) return render_template('similar.html', data=data)
def handle_message(event): query = event.message.text result_indexes = recommend.recommend(query) result_index = result_indexes[0].astype('U13') line_bot_api.reply_message(event.reply_token, TextSendMessage(text=result_index))
def next_yes(): opt = parse_cmdline() logging.basicConfig(level=logging.DEBUG if opt.verbose else logging.INFO) conn = psycopg2.connect(opt.dsn) rows = None data = request.json.get('data', None) if not data: return 'No data', 400 with conn.cursor() as cur: cur.execute("SELECT * FROM movies ORDER BY RANDOM() LIMIT 500") rows = cur.fetchall() logging.debug("print_content(): status message: %s", cur.statusmessage) conn.commit() row = recommend(data, rows) return_val = { row[0]: { 'title': row[1], 'run_time': row[2], 'year': row[3], 'imdb_rating': row[4], 'rt_rating': row[5], 'rated': row[6], 'img': row[7], 'description': row[8], 'imdb_votes': row[9], 'genres': row[10] } } return jsonify(return_val)
def _recommend(): topic = request.get_json()['topic'] from_date = request.get_json()["from_date"] to_date = request.get_json()["to_date"] author_id = request.get_json()['author_id'] model_name = request.get_json()['model_name'] return recommend(topic, from_date, to_date, author_id, model_name)
def rec(): user_id = str(request.args.get("user_id", None)) rest_list = request.args.get("rest_list", None) review_cut = int(request.args.get("review_cut", None)) user_cut = int(request.args.get("user_cut", None)) list = recommend.recommend(user_id, rest_list, review_cut, user_cut) return jsonify(list)
def bookclass(index): if(index=='novel'): result = allnovelbookdict hot=recommend.recommend(allnovelbooklist,3) tupian=picture[0] elif(index=='success'): result = allsuccessdict hot=recommend.recommend(allsuccesslist,1) tupian=picture[2] elif(index=='economy'): result=alleconomydict hot=recommend.recommend(alleconomylist,1) tupian=picture[3] elif(index=='technology'): result=alltechnologydict hot=recommend.recommend(alltechnologylist,0) tupian=picture[1] return render_template('bookclass.html', novel_info=result,hot=hot,tupian=tupian)
def recommend_stuff(profile_id): #gets the json result from recommend.py recommends = recommend.recommend(profile_id) games = [] for rec in recommends: rec_dict = {"appId" : rec[2], "title": rec[3], "fromAppId": rec[0], "fromTitle":rec[1]} games.append(rec_dict) global gamesJSON gamesJSON = ({"games": games}) #json of recommendations writeJSONToDirectory(gamesJSON)
def results(): data = request.form['exampleTextarea'] scores = request.form['exampleTextarea2'] minprice = request.form['minprice'] maxprice = request.form['maxprice'] type_ = request.form['type'] if not rec.isfloat(minprice): minprice = 0.0 else: minprice = float(minprice) if not rec.isfloat(maxprice): maxprice = 100000.0 else: maxprice = float(maxprice) url_list = data.split('\n') score_list = scores.split('\n') score_list = [int(score) for score in score_list if rec.isfloat(score)] item_list = [rec.get_item_id_from_url(url) for url in url_list] # if len(url_list) > 0: # first = url_list[0] # if ('www.whiskybase.' not in first) and (not rec.isfloat(first)): # # Profile page, scrape to get ratings info # item_list, score_list = rec.profile_input(first) # pred_url_list = predict(url_list) guess = rec.recommend(model, item_list, score_list, rdf=rdf, similarity_matrix=similarity_matrix) results = rec.filter_results(guess, rdf, type_, minprice, maxprice, num_to_show=20) # res = [] # for whisk in guess: # res.append('https://www.whiskybase.com/whisky/{}'.format(whisk)) cols_needed_for_display = [ 'id', 'url', 'category', 'brand', 'name', 'price', 'photo_url', 'null_photo_url', 'brand_and_name' ] results2 = results[results['null_photo_url'] == 0] results3 = results2[cols_needed_for_display] # results3 = results[cols_needed_for_display] rec_list = results3.T.to_dict().values() return render_template('results.html', items=rec_list)
def recommender_system(path_to_data, path_to_item, person, distance_metric): """ This is the main file to combine all the other functions in order to recommend products to a user. """ data = input_data(path_to_data) item_name = item_name_map(path_to_item) recommend_items = recommend(data, person, distance_metric) result = match_item(recommend_items, item_name) return result
def index(userid): print(request.get_header('X-Forwarded-For')); easy,medium,hard = recommend.recommend(userid) shareText = userid+'さんのお勧め問題は' problems = [] #for p in easy: # print(type(p['title'])) # problems.append('%s'%p['title']) #shareText+=','.join(problems) return template('frame', userid=userid,easy=easy,medium=medium,hard=hard,shareText=shareText)
def handle_message(event): query = event.message.text result_indexes = recommend.recommend(query) result_index = df.values[result_indexes[0]] song = result_index[0] artist = result_index[1] line_bot_api.reply_message( event.reply_token, TextSendMessage(text="「" + song + "」" + "\n" + artist))
def dispatch(messageJson=None): """ dispatch is the microservice dispatcher for IndigoGirls, a 2048-like game. It routes requests for game state transformations to the appropriate functions :param messageJson: JSON string that describes the state of the game needed for the requested transformation :return: A JSON string that describes the state of the game after the requested transformation has taken place. """ def buildErrorString(diagnostic=None): """ returns a dictionary containing the specified key and accompanying diagnostic information :param diagnostic: A string that describes the error :return: A dictionary that contains the specified error key having a value that consists of the specfied error string followed by a free-form diagnostic message """ ERROR_PROPERTY = u'gameStatus' ERROR_PREFIX = u'error: ' return {ERROR_PROPERTY: ERROR_PREFIX + diagnostic} #Validate JSONness of input be converting the string to an equivalent dictionary try: messageDictionary = json.loads(messageJson) except: resultDictionary = json.dumps( buildErrorString('input JSON string is invalid')) return resultDictionary #Validate presence of dispatching code if (u"op" not in messageDictionary): resultDictionary = json.dumps(buildErrorString('op is missing')) return resultDictionary #Perform the game transformation as directed by the value of the "op" key # input to each function: a dictionary containing the name-value pairs of the input JSON string # output of each function: a dictionary containing name-value pairs to be encoded as a JSON string if (messageDictionary[u"op"] == u"initializeGame"): resultDictionary = initializeGame( messageDictionary) #<---------- Added my implementation elif (messageDictionary[u"op"] == u"swipe"): resultDictionary = swipe(messageDictionary) elif (messageDictionary[u"op"] == u"recommend"): resultDictionary = recommend(messageDictionary) elif (messageDictionary[u"op"] == u"status"): resultDictionary = status(messageDictionary) elif (messageDictionary[u"op"] == u"predict"): resultDictionary = predict(messageDictionary) else: resultDictionary = buildErrorString('op is invalid') #Covert the dictionary back to a string in JSON format resultJson = json.dumps(resultDictionary) return resultJson
def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) profile = Profile.objects.get(user=user) history_model = ThroughModel.objects.filter(profile=profile).order_by('-date_viewed') recommend_list = [] for obj in history_model: title = obj.movie.title temp_list = recommend.recommend(title) recommend_list.extend(temp_list) recommend_list = recommend.remove_duplicates(recommend_list) return Movie.objects.filter(title__in=recommend_list).order_by('-rating')
def new_p(n): predicted = {} each = '479' predicted[each] = recommend(dataset, each, n) ordered = {} for each in predicted: ordered.setdefault(each, {}) for movie in predicted[each]: ordered[each][movie[1]] = movie[0] print ordered[each]['Harry Potter and the Deathly Hallows: Part 2 (2011)'] print test[each]['Harry Potter and the Deathly Hallows: Part 2 (2011)']
def recommendRoute(): tier = request.get_json().get('tier') id = request.get_json().get('id') tier = int(tier) # myTeam = [3,5,12,34,64] # opponentTeam = [87,3,34,2,14] print(tier) print(id) outcome = recommend(tier, id) print outcome res = json.dumps({'res': outcome}) return res
def semester(request, year, semester): context = {} user = request.user context['courses'] = get_depts() context['user'] = user semester = Semester.objects.get(owner=user, year=year, semester=semester) context['semester'] = semester context['courses'] = unpack([s.course_id for s in semester.courses.all()]) context['recommended'] = unpack(recommend(user)[:5]) context = RequestContext(request, context) return render_to_response('semester.html', context)
def test100_010_AcceptanceTest1(self): messageDictionary = { "board": { "columnCount": 4, "rowCount": 4, "grid": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1] } } resultDictionary = recommend.recommend(messageDictionary) result = resultDictionary["score"] expected = 16 self.assertEquals(expected, result)
def recommend(self): recoms = recommend.recommend(N=4) # Get recommendations & load to playlist for song in recoms: button = SongButton(text=song + ' - ' + recoms[song][0], title=song, artist=recoms[song][0], img_link=recoms[song][1] ) button.bind(on_release=self.download_song) self.h = self.h + 20 if self.num_buttons > 8 else self.h self.playlist.height = self.h self.playlist.add_widget(button) self.num_buttons += 1
def main(): # TODO: revise to receive interest from a foreign source. interest = [ 'I want to predict mascots\' popularity from its photo using machine learning methodologies.' ] args = get_commandline_args() scraper = RSSScraper() _papers = [] descriptions = [] for field in args.fields: scraper.fetch_rss(field=field) for _abs in scraper.extract_paper_abstract(): _papers.append(_abs) descriptions.append(_abs['description']) papers = pd.DataFrame(data=_papers) similarity = pd.Series(data=calculate_similarity_of_interest( interest=interest, descriptions=descriptions)) papers.loc[:, 'similarity'] = similarity recommend(papers, interest)
def test200_010_AcceptanceTest2(self): messageDictionary = { "moves": "abc", "board": { "columnCount": 4, "rowCount": 4, "grid": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1] } } resultDictionary = recommend.recommend(messageDictionary) result = resultDictionary["gameStatus"] expected = "error: improper value for moves" self.assertEquals(expected, result)
def recommend_stuff(profile_id): #gets the json result from recommend.py recommends = recommend.recommend(profile_id) games = [] for rec in recommends: rec_dict = { "appId": rec[2], "title": rec[3], "fromAppId": rec[0], "fromTitle": rec[1] } games.append(rec_dict) global gamesJSON gamesJSON = ({"games": games}) #json of recommendations writeJSONToDirectory(gamesJSON)
def bookdetail(kind,index): get_books() strindex=index index=int(index) showbook={} if(kind=='novel'): for item in allnovelbookdict: if item==index: showbook[index]=allnovelbookdict[item] print(showbook) result = recommend.recommend(allnovelbooklist, index) like = SingleProductCF.SingleProductCF().recommend("data/information.txt", strindex, allnovelbookdict) elif(kind=='success'): for item in allsuccessdict: if item==index: showbook[index]=allsuccessdict[item] print(showbook) result = recommend.recommend(allsuccesslist, index) like = SingleProductCF.SingleProductCF().recommend("data/information.txt", strindex, allsuccessdict) elif(kind=='economy'): for item in alleconomydict: if item==index: showbook[index]=alleconomydict[item] print(showbook) result = recommend.recommend(alleconomylist, index) like = SingleProductCF.SingleProductCF().recommend("data/information.txt", strindex, alleconomydict) elif(kind=='technology'): for item in alltechnologydict: if item == index: showbook[index] = alltechnologydict[item] print(showbook) result = recommend.recommend(alltechnologylist, index) like = SingleProductCF.SingleProductCF().recommend("data/information.txt", strindex, alltechnologydict) for item in like: for key in item: print(item[key][1]) return render_template('bookdetail.html', novel_info=result,showbook=showbook,index=index,like=like)
def home(): # Check if user is loggedin if 'loggedin' in session: if request.method == 'POST': language = request.form['language'] cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor) cursor.execute('SELECT * FROM accounts WHERE username = %s', (session['username'],)) account = cursor.fetchone() recommendation = recommend(account['id'], language) return render_template('home.html', username=session['username'], language=language, recommendation=recommendation.to_html(columns=['song','artist_1','album','record_name','release_month','rating'],index=False,classes='table',border=None)) else: # User is loggedin show them the home page return render_template('home.html', username=session['username']) # User is not loggedin redirect to login page return redirect(url_for('login'))
def driver(n): predicted = {} start = time.clock() for each in test: predicted[each] = recommend(dataset, each, n) end = time.clock() print end - start ordered = {} for each in predicted: ordered.setdefault(each, {}) for movie in predicted[each]: ordered[each][movie[1]] = movie[0] mae_error = mae(ordered, test) print mae_error rmse_error = rmse(ordered, test) print rmse_error
def test100_020_AcceptanceTest1(self): messageDictionary = { "moves": 2, "board": { "columnCount": 4, "rowCount": 4, "grid": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1] } } resultDictionary = recommend.recommend(messageDictionary) result = resultDictionary["score"] if (result == 16 or result == 18 or result == 20 or result == 22 or result == 24 or result == 26 or result == 28 or result == 30 or result == 32 or result == 34): #all possible values given the board with two moves self.assertEquals(1, 1) else: self.assertEquals(0, 1)
def on_click(self): tuser = self.textEdit.toPlainText() genre = pickle.load(open("genre.pickle", "rb")) cf_pred = pickle.load(open("cf.pickle", "rb")) rec = recommend.recommend(int(tuser), cf_pred, genre) srec = "" first = 0 for i in rec: if (first == 0): srec = srec + str(i) first = 1 else: srec = srec + ", " + str(i) if (len(srec) > 0): text = "The movies recommended to user " + tuser + " are: " + srec else: text = "User not found!" self.textBrowser.setText(text)
def course(request, *args): try: id = int(arg[1]) except: #must be a name try: name = " ".join(request.path[1:-1].split("/")[1:]) print name course = Course.objects.get(name=name) print course except: return HttpResponse("Error!") id = course.course_id context = RequestContext(request, api('course', str(id))) user = request.user context['courses'] = get_depts() context['user'] = user context['recommended'] = unpack(recommend(user)[:5]) context['reviews'] = api('course', str(id), 'reviews') return render_to_response('course.html', context)
def run(): log.info("user_cart_recommend delete ...") sql_del_cart_recommend = "delete from user_cart_recommend" cur.execute(sql_del_cart_recommend) conn.commit() log.info("user_cart_recommend insert ...") sql_similarity = "select pid,spid,similarity from product_similarity" cur.execute(sql_similarity) rdd_product_similarity = sc.parallelize(cur.fetchall()) sql_cart = "select uid,pid,8 as rating from data_cart" cur.execute(sql_cart) rdd_cart = sc.parallelize(cur.fetchall()) import recommend #rdd_cart1 = rdd_cart.map(lambda x:(x[0],x[1],8)) rdd_user_cart_recommend = recommend.recommend(rdd_product_similarity,rdd_cart) sql_values = "" list_user_cart_recommend = rdd_user_cart_recommend.collect() for i in range(len(list_user_cart_recommend)): row = list_user_cart_recommend[i] sql_values += "(%s,%s,%s)," % row if (i != 0 and i % 1000 == 0) or i == len(list_user_cart_recommend) - 1: sql_insert = "insert into user_cart_recommend (uid,pid,rating) values %s"\ % sql_values.rstrip(',') sql_values = "" cur.execute(sql_insert) conn.commit() log.info("user_cart_recommend rdd map...") list_user_cart_recommend1 = rdd_user_cart_recommend.map(lambda x:(x[0],(x)))\ .groupByKey().mapValues(list).collect() log.info("user_cart_recommend write redis...") redis_prefix = "rec.user_cart_recommend.uid" for i in range(len(list_user_cart_recommend1)): row = list_user_cart_recommend1[i] json_value = json.dumps(row[1]) pipe.set("%s:%s" % (redis_prefix,row[0]),json_value) pipe.execute()
def index(request): context = RequestContext(request, {}) user = request.user context['courses'] = get_depts() context['recommended'] = unpack(recommend(user)[:5]) return render_to_response('index.html', context)
def recommend(method,userId): response = {} response['code'] = 200 response['data'] = rec.recommend(method,userId) return json.dumps(response)
def recommended(request): context = {} context['courses'] = unpack(recommend(request.user)[:5]) return render_to_response('recommended.html', context)
__author__ = 'Sylvanus' import pre import init import search import recommend pre.prepare() (hashtable, similarity) = init.loadData() scores = init.login(2297) text = ['小说', '十字'] booklist = search.doSearch(text, hashtable) print('list: ', booklist) recommender = recommend.recommend(booklist, similarity, scores) print('recommender: ', recommender)