def rerank_results(self, results, user_vector, user_gender, user_location, user_sentiment): """ reranks the results of a query by using the similarity between the user thematic vector and the vector from the tweets :param results: the documents resulting from a query :param user_vector: the thematic vector of a user :param user_gender: the gender of a user :param user_location: the location of a user :param user_sentiment: the sentiment of a user :return: the reranked list of documents """ reranked = [] user_vec = ProfileOneHotEncoder.add_info_to_vec( user_vector, user_gender, user_location, user_sentiment).reshape(1, -1) for i in range(len(results)): doc_infos = Tweet.load(int(results[i]['TweetID'])) if doc_infos is None: reranked.append({'doc': results[i], 'sim': 0.}) else: doc_vector = ProfileOneHotEncoder.add_info_to_vec( doc_infos.vector, doc_infos.gender, doc_infos.country, doc_infos.sentiment).reshape(1, -1) sim = cosine_similarity(user_vec, doc_vector) reranked.append({'doc': doc_infos, 'sim': sim[0][0]}) reranked = sorted(reranked, key=lambda k: k['sim'], reverse=True) return [x['doc'] for x in reranked]
def mark_view(): user = User.load(user_name=session.get('username')) tweet = Tweet.load(int(request.form.get('tweet_id'))) state = '' if tweet.is_faved(user): state = 'removed' # user.remove_favorite(tweet) else: view = Favorite(user_id=user.id, tweet_id=tweet.id) # user.update_profile(np.array(tweet.vector)) DB.get_instance().add(view) state = 'added' DB.get_instance().commit() return state
def link_tweets(self, results): return [Tweet.load(r['TweetID']) for r in results]