Exemple #1
0
def get_similar_post_comment():
    post_ids = []
    for post in Post.where():
        post_ids.append(post.id)
        idf.append(post.id, post.content.encode('U8'))
    length = len(post_ids)
    similar = np.zeros(length**2, np.float)
    similar = similar.reshape(length, length)
    
    cache = {}
    for numi, i in enumerate(post_ids):
        for numj, j in enumerate(post_ids):
            key = '%s-%s'%(i, j)
            if key not in cache and '%s-%s'%(j, i) not in cache:
                cache[key] = idf.similar(i, j)
            else: 
                key = '%s-%s'%(j, i)
            similar[numi][numj] = cache[key]


    comments = Comment.where().order_by('updated').col_list(col='id,content')
    com_length = len(comments)
    comment_rela = np.zeros(com_length*length, np.float)
    comment_rela = comment_rela.reshape(com_length, length)
    for num, (i, j) in enumerate(comments):
        idf.append(i, j.encode('U8'))
        for x, p in enumerate(post_ids):
            comment_rela[num][x] = idf.similar(i, p)
    return post_ids, similar, [i[0] for i in comments], comment_rela
def comment_by_group_user(group_id, user_id):
    author_id = get_author_id_by_user_id(user_id)
    users = get_user_by_group(group_id)
    comments = []
    for user in users:
        for post in Post.where(user_id=user):
            for comment in Comment.where(post_id=post.id):
                if comment.author_id == author_id:
                    comments.append(comment)
    return comments
def comment_by_users(users):
    for post in posts_by_users(users):
        for comment in Comment.where(post_id=post.id):
            yield comment