Example #1
0
def get_user_gmp(user_id):
    gmp = gae_cache.cache_get(user_id + 'gmp')
    if gmp:
        return gmp
    gmp = GMPoints.gql('WHERE user_id = :1', user_id).get()
    gae_cache.cache_put(user_id + 'gmp', gmp)
    return gmp
Example #2
0
def get_user_gmp(user_id):
    gmp = gae_cache.cache_get(user_id + 'gmp')
    if gmp:
        return gmp
    gmp = GMPoints.gql('WHERE user_id = :1', user_id).get()
    gae_cache.cache_put(user_id + 'gmp', gmp)
    return gmp
Example #3
0
def boredRecommend(user_id, num=DEF_REC_NUM):
    recommendations = gae_cache.cache_get(user_id + 'boredrmd')
    if recommendations:
        return recommendations

    expertise = ExUserInfo.gql('WHERE user_id = :1', user_id).get().expertise
    diff_expertise = set(data_models.EXPERTISES.keys())
    diff_expertise.difference_update(set(expertise))

    if not diff_expertise:
        return None
    query1 = URLStorage.gql('WHERE topic IN :1', list(diff_expertise))
    #    url_recs = [item for item in query1]
    #    logging.info('number of bored rmds: ' + str(len(url_recs)))
    #    li = [random.randint(0, len(url_recs)-1) for i in range(2*num)]
    query2 = Rating.gql('WHERE user_id = :1', user_id)
    rated = [item.url for item in query2]
    recommendations = []
    for url_rec in query1:
        if url_rec.url not in rated:
            recommendations.append(url_rec)
            if len(recommendations) >= num:
                break

    gae_cache.cache_put(user_id + 'boredrmd', recommendations)
    return recommendations
Example #4
0
def worth_points(url, user, update_cache = False):
    points = gae_cache.cache_get(user.user_id+url+'points')
    if points and not update_cache:
        return points
    points = None
    
    m_rating = Rating.gql('WHERE user_id = :1 AND url = :2', user.user_id, url).get()
    #if less than one hour between two attempts, no points are awarded
    current_time = datetime.datetime.now()
    if m_rating and (current_time - m_rating.time).total_seconds() < 3600:
        points = 0
    
    if points is None:
        points =  BASIC_RATING_POINTS
        #punish multiple attempts to a specific url
        punish_coefficient = 1
        if m_rating:
            punish_coefficient = math.pow(0.5, float(m_rating.attempts))
        sparse_points = 0
        diff_coefficient = 1
        url_stat = URLStorage.gql('WHERE url = :1', url).get()
        if url_stat:
            for i in range(6):
                if url_stat.total < SPARSITY_LEVEL[i]:
                    sparse_points = SPARSITY_POINTS[i]
                    break
            r = url_stat.ratings
            diff_coefficient = 1.0 / (((abs(r[4] + r[3] - r[1] - r[0])) / (r[4] + r[3] + r[1] + r[0] + 10.0)) + 1)
            points = int(math.ceil(sparse_points * diff_coefficient * punish_coefficient))
    
    gae_cache.cache_put(user.user_id+url+'points', points)
    return points
Example #5
0
def boredRecommend(user_id, num = DEF_REC_NUM):
    recommendations = gae_cache.cache_get(user_id + 'boredrmd')
    if recommendations:
        return recommendations
    
    expertise = ExUserInfo.gql('WHERE user_id = :1', user_id).get().expertise
    diff_expertise = set(data_models.EXPERTISES.keys())
    diff_expertise.difference_update(set(expertise))
    
    if not diff_expertise:
        return None
    query1 = URLStorage.gql('WHERE topic IN :1', list(diff_expertise))
#    url_recs = [item for item in query1]
#    logging.info('number of bored rmds: ' + str(len(url_recs)))
#    li = [random.randint(0, len(url_recs)-1) for i in range(2*num)]
    query2 = Rating.gql('WHERE user_id = :1', user_id)
    rated = [item.url for item in query2]
    recommendations = []
    for url_rec in query1:
        if url_rec.url not in rated:
            recommendations.append(url_rec)
            if len(recommendations) >= num:
                break
    
    gae_cache.cache_put(user_id+'boredrmd', recommendations)
    return recommendations
Example #6
0
def sparseRecommend(user_id, num=DEF_REC_NUM):
    recommendations = gae_cache.cache_get(user_id + 'sparsermd')
    if recommendations:
        return recommendations

    query1 = URLStorage.gql('ORDER BY total ASC')
    query2 = Rating.gql('WHERE user_id = :1', user_id)
    rated = [item.url for item in query2]
    recommendations = []
    tmp = 0
    for url_rec in query1:
        tmp += 1
        if url_rec.url not in rated:
            recommendations.append(url_rec)
            if len(recommendations) >= num:
                break

    gae_cache.cache_put(user_id + 'sparsermd', recommendations)
    return recommendations
Example #7
0
def sparseRecommend(user_id, num = DEF_REC_NUM):
    recommendations = gae_cache.cache_get(user_id + 'sparsermd')
    if recommendations:
        return recommendations
    
    query1 = URLStorage.gql('ORDER BY total ASC')
    query2 = Rating.gql('WHERE user_id = :1', user_id)
    rated = [item.url for item in query2]
    recommendations = []
    tmp = 0
    for url_rec in query1:
        tmp += 1
        if url_rec.url not in rated:
            recommendations.append(url_rec)
            if len(recommendations) >= num:
                break
    
    gae_cache.cache_put(user_id+'sparsermd', recommendations)
    return recommendations
Example #8
0
def worth_points(url, user, update_cache=False):
    points = gae_cache.cache_get(user.user_id + url + 'points')
    if points and not update_cache:
        return points
    points = None

    m_rating = Rating.gql('WHERE user_id = :1 AND url = :2', user.user_id,
                          url).get()
    #if less than one hour between two attempts, no points are awarded
    current_time = datetime.datetime.now()
    if m_rating and (current_time - m_rating.time).total_seconds() < 3600:
        points = 0

    if points is None:
        points = BASIC_RATING_POINTS
        #punish multiple attempts to a specific url
        punish_coefficient = 1
        if m_rating:
            punish_coefficient = math.pow(0.5, float(m_rating.attempts))
        sparse_points = 0
        diff_coefficient = 1
        url_stat = URLStorage.gql('WHERE url = :1', url).get()
        if url_stat:
            for i in range(6):
                if url_stat.total < SPARSITY_LEVEL[i]:
                    sparse_points = SPARSITY_POINTS[i]
                    break
            r = url_stat.ratings
            diff_coefficient = 1.0 / (((abs(r[4] + r[3] - r[1] - r[0])) /
                                       (r[4] + r[3] + r[1] + r[0] + 10.0)) + 1)
            points = int(
                math.ceil(sparse_points * diff_coefficient *
                          punish_coefficient))

    gae_cache.cache_put(user.user_id + url + 'points', points)
    return points
Example #9
0
def expertiseRecommend(user_id, num=DEF_REC_NUM):
    recommendations = gae_cache.cache_get(user_id + 'expertisermd')
    if recommendations:
        return recommendations

    expertise = ExUserInfo.gql('WHERE user_id = :1', user_id).get().expertise
    if not expertise:
        return None
    query1 = URLStorage.gql('WHERE topic IN :1', expertise)
    #    url_keys = [item for item in query1.run(keys_only=True)]
    #    logging.info('number of expertise rmds: ' + str(len(url_keys)))
    #    li = [random.randint(0, len(url_keys)-1) for i in range(2*num)]
    query2 = db.GqlQuery('SELECT url from Rating WHERE user_id = :1', user_id)
    rated = [item.url for item in query2]
    recommendations = []
    for url_rec in query1:
        #        url_rec = URLStorage.get(url_keys[i])
        if url_rec.url not in rated:
            recommendations.append(url_rec)
            if len(recommendations) >= num:
                break

    gae_cache.cache_put(user_id + 'expertisermd', recommendations)
    return recommendations
Example #10
0
def expertiseRecommend(user_id, num = DEF_REC_NUM):
    recommendations = gae_cache.cache_get(user_id + 'expertisermd')
    if recommendations:
        return recommendations
    
    expertise = ExUserInfo.gql('WHERE user_id = :1', user_id).get().expertise
    if not expertise:
        return None
    query1 = URLStorage.gql('WHERE topic IN :1', expertise)
#    url_keys = [item for item in query1.run(keys_only=True)]
#    logging.info('number of expertise rmds: ' + str(len(url_keys)))
#    li = [random.randint(0, len(url_keys)-1) for i in range(2*num)]
    query2 = db.GqlQuery('SELECT url from Rating WHERE user_id = :1', user_id)
    rated = [item.url for item in query2]
    recommendations = []
    for url_rec in query1:
#        url_rec = URLStorage.get(url_keys[i])
        if url_rec.url not in rated:
            recommendations.append(url_rec)
            if len(recommendations) >= num:
                break
            
    gae_cache.cache_put(user_id+'expertisermd', recommendations)
    return recommendations