def recommend(self, rec_mode, section, since, dupes): content_type, profile_mode = rec_mode get_int = profile_mode == 'interests' or profile_mode == 'both' get_exp = profile_mode == 'expertise' or profile_mode == 'both' # Get top 5 tags and top 3 topics tags = self.user.get_tags(5, get_int, get_exp) topics = self.user.get_topics(3, get_int, get_exp) # Construct query based on section duplicates = dupes.get( 'question' if content_type == 'questions' else 'answer', [0]) query = queries.build_section(section) query_params = { 'site_id': config.site_id, 'since': since, 'dupes': tuple(duplicates if len(duplicates) > 0 else [0]), 'tags': tuple(tags), 'topics': tuple(topics), } # Run query with db.connection() as conn: cur = conn.cursor() cur.execute(query, query_params) results = [res[0] for res in cur] # If questions, match to user and return if content_type == 'questions': return self.user.match_questions( [profiles.QuestionProfile(qid) for qid in results], get_int, get_exp) # If answers, we have some more work elif content_type == 'answers': # Construct question-answer index with db.connection() as conn: cur = conn.cursor() cur.execute(queries.question_answer_index, {'answers': tuple(results)}) qa_index = {qid: aid for qid, aid in cur} # Match questions to user qlist = [profiles.QuestionProfile(qid) for qid in qa_index.keys()] qmatches = self.user.match_questions(qlist, get_int, get_exp) # Return answers belonging to the matched questions return [(qa_index[qid], score) for qid, score in qmatches] # Something else? Better return nothing at all else: return []
def __init__(self, id): self.id = id with db.connection() as conn: cur = conn.cursor() cur.execute(queries.user_profile['question_get_content'], (self.id, )) _, self.title, self.body, self.creation_date = cur.fetchone()
def run_daily_cron(logger): logger.info('Creating question profiles from last 2 days') cnt = create_question_profiles(queries.all_questions_since, (config.site_id, 2, 2)) logger.debug('Created %d profiles', cnt) logger.info('Creating profiles for all user activities') cnt = create_question_profiles(queries.all_user_activity, (config.site_id, )) logger.debug('Created %d profiles', cnt) logger.info('Retraining daily subscriber user profiles') from recommender.profiles import UserProfile, CommunityProfile with db.connection() as conn: cur = conn.cursor() cur.execute(queries.daily_subscribers, (config.site_id, )) i = 0 for uid in cur: user = UserProfile.load(uid[0]) user.retrain() user.save() utils.archive_user_profile(user) i += 1 logger.debug('Retrained %d user profiles', i) # 4) Retrain community user profile logger.info('Retraining community profile') community = CommunityProfile.load() community.retrain() community.save() utils.archive_user_profile(community) logger.info('Community profile saved') db.close()
def persist_question_topics(question, topics): qid = question[0] with db.connection() as conn: execute_values( conn.cursor(), 'INSERT INTO mls_question_topics (question_id, topic_id, site_id, weight, created_at, updated_at) VALUES %s', ((qid, topic, config.site_id, weight) for weight, topic in topics), '(%s, %s, %s, %s, NOW(), NOW())')
def _get_topics(self): try: return self.__topics except AttributeError: with db.connection() as conn: cur = conn.cursor() cur.execute(queries.user_profile['get_topics'], (config.site_id, )) self.__topics = [topic[0] for topic in cur] return self.__topics
def topics(self): try: return self.__topics except AttributeError: with db.connection() as conn: cur = conn.cursor() cur.execute(queries.user_profile['question_get_topics'], (self.id, )) self.__topics = cur.fetchall() return self.__topics
def tags(self): try: return self.__tags except AttributeError: with db.connection() as conn: cur = conn.cursor() cur.execute(queries.user_profile['question_get_tags'], (self.id, )) self.__tags = [tag[0] for tag in cur] return self.__tags
def create_question_profiles(query, args): with db.connection() as conn: cur = conn.cursor() cur.execute(query, args) i = 0 for question in cur: topics = train.get_question_topics(question) train.persist_question_topics(question, topics) i += 1 return i
def _get_question_profiles(self, question_query, since=None, since_table=''): params = {'user_id': self.id} since_query = 'AND {}created_at > %(since)s'.format( since_table) if since else '' if since: params['since'] = since with db.connection() as conn: cur = conn.cursor() cur.execute(question_query.format(since=since_query), params) return [QuestionProfile(question[0]) for question in cur]
def run_weekly_cron(logger): logger.info('Retraining weekly subscriber user profiles') from recommender.profiles import UserProfile with db.connection() as conn: cur = conn.cursor() cur.execute(queries.weekly_subscribers, (config.site_id, )) i = 0 for uid in cur: user = UserProfile.load(uid[0]) user.retrain() user.save() utils.archive_user_profile(user) i += 1 logger.debug('Retrained %d user profiles', i) db.close()
#!env/bin/python from recommender.profiles import UserProfile from recommender import db, config with db.connection() as conn: cur = conn.cursor() cur.execute( 'SELECT id FROM users WHERE account_id IS NOT NULL AND site_id = %s', (config.site_id, )) for uid in cur: user = UserProfile.load(uid[0]) print(user.id) user.retrain() user.save() db.close()