Ejemplo n.º 1
0
def new_weight():
    if request.method == 'POST':
        get_data = json.loads(request.get_data(as_text=True))
        param = {
            'user_id': get_data['u_id'],
            'setq_id': get_data['q_id'],
            'weight1': get_data['w1'],
            'weight2': get_data['w2']
        }
        w = Weight()
        data = w.new_weight(param)
        if data == True:
            resData = {"resCode": 0, "data": data, "message": '权重已更新'}
            return jsonify(resData)
        resData = {"resCode": 1, "data": [], "message": '更新失败'}
        return jsonify(resData)
    else:
        resData = {"resCode": 1, "data": [], "message": '请求方式错误'}
        return jsonify(resData)
Ejemplo n.º 2
0
def project():
    if request.method == 'POST':
        get_data = json.loads(request.get_data(as_text=True))
        # 1.准备数据
        # 具体来说,获取数据库中所有该问题集合的问题,和所有用户的回答
        # q_list[]
        # ans1[]
        # ans2[]
        # ans1[i][j] 问题i 用户j
        # weight[i] 用户i的权重
        user_list = []
        q_list = []
        ans1 = []
        ans2 = []
        # 1.1找出所有问题id
        param = {'id_setq': get_data['setq_id']}
        q = Q()
        data = q.allq_id(param)
        for i in data:
            q_list.append(i['idq'])
            ans1.append([])
            ans2.append([])
        print('q_list = ', q_list)
        # 1.2根据问题id确定回答问题的用户id
        # for i in range(len(q_list)):
        param = {'id_q': q_list[0]}
        a = Ans()
        data = a.allans_userid(param)

        for item in data:
            if item['user_id'] not in user_list:
                user_list.append(item['user_id'])
        print('user_list = ', data)
        # 1.3对应问题id和用户id的所有回答,ans1和ans2
        for i in range(len(q_list)):
            for j in user_list:
                param = {'id_q': q_list[i], 'id_user': j}
                a = Ans()
                data = a.allans_q_user(param)
                for item in data:
                    ans1[i].append(item['ans_one'])
                    ans2[i].append(item['ans_two'])
        print('ans1 = ', ans1)
        print('ans2 = ', ans2)

        # 2.聚合结果
        # MV-one
        # MV-two
        Data = []

        q_MV_one = MV(ans1)
        Data.append(q_MV_one)

        for i in range(len(q_list)):
            param = {'id_q': q_list[i], 'ans': q_MV_one[i]}
            q = Q()
            q.set_q_MV_one(param)

        q_MV_two = MV(ans2)
        Data.append(q_MV_two)

        for i in range(len(q_list)):
            param = {'id_q': q_list[i], 'ans': q_MV_two[i]}
            q = Q()
            q.set_q_MV_two(param)
        # TD-one
        # TD-two
        # TD要返回对应的weight列表
        q_TD_one, weight_one = TD(user_list, q_list, ans1)
        Data.append(q_TD_one)

        for i in range(len(q_list)):
            param = {'id_q': q_list[i], 'ans': q_TD_one[i]}
            q = Q()
            q.set_q_TD_one(param)

        q_TD_two, weight_two = TD(user_list, q_list, ans2)
        Data.append(q_TD_two)

        for i in range(len(q_list)):
            param = {'id_q': q_list[i], 'ans': q_TD_two[i]}
            q = Q()
            q.set_q_TD_two(param)
        # 更新权重
        for i in range(len(user_list)):
            param = {
                'user_id': user_list[i],
                'setq_id': get_data['setq_id'],
                'weight1': weight_one[i],
                'weight2': weight_two[i]
            }
            w = Weight()
            w.new_weight(param)

        resData = {"resCode": 0, "data": Data, "message": '返回聚合结果'}
        return jsonify(resData)