示例#1
0
def proposed_similarity():
    try:
        request_weights = feature_weights.copy()
        body_string = request.data.decode("utf-8")
        if len(body_string) == 0:
            return jsonify("Empty String")
        
        if 'weights' in request.headers: 
            header = json.loads(request.headers['weights'])
            print(header)
            for weight_key in header:
                if weight_key in request_weights:
                    request_weights[weight_key]['weight'] = float(header[weight_key])
        # pdb.set_trace()
        method_vector = util.vector_from_text(body_string)
        nearest = ml.proposed_kNearest(method_vector, solution_vectors, lang_dict, 
                lang_model, k=kNearest, weights=request_weights)
        # nearest contains score, solutions_index and intersections
        # print(kNearest)
        nearest_vectors = []
        for element in nearest:
            result = {}
            result['score'] = element[0]
            result['code'] = solution_vectors[element[1]]
            result['scores'], result['matches'] = extract_info(element[2])
            nearest_vectors.append(result)
        return jsonify(nearest_vectors)
    except Exception as ex:
        print(ex)
        return jsonify(str(ex))
示例#2
0
def mix_similarity():
    try:
        request_weights = feature_weights.copy()
        body_string = request.data.decode("utf-8")
        if len(body_string) == 0:
            return jsonify("Empty String")

        if 'weights' in request.headers:
            header = json.loads(request.headers['weights'])
            for weight_key in header:
                if weight_key in request_weights:
                    request_weights[weight_key]['weight'] = float(
                        header[weight_key])
        method_vector = util.vector_from_text(body_string)
        # import pdb; pdb.set_trace()
        proposed_nearest = ml.proposed_kNearest(method_vector,
                                                solution_vectors,
                                                lang_dict,
                                                lang_model,
                                                k=5,
                                                weights=request_weights)
        cosine_nearest = ml.cosine_kNearest(method_vector,
                                            solution_vectors,
                                            baseline_dict,
                                            baseline_model,
                                            k=5)
        proposed_nearest = [tup[1] for tup in proposed_nearest]
        cosine_nearest = [tup[1] for tup in cosine_nearest]
        results = []
        for i in range(len(proposed_nearest)):
            reco_index = proposed_nearest[i]
            reco = {}
            if reco_index in cosine_nearest:
                reco['source'] = 2
                reco['rank_1'] = i + 1
                reco['rank_0'] = cosine_nearest.index(reco_index) + 1
                cosine_nearest.remove(reco_index)
            else:
                reco['source'] = 1
            reco['rank'] = i + 1
            reco['text'] = solution_vectors[reco_index].raw_text
            results.append(reco)

        for i in range(len(cosine_nearest)):
            reco_index = cosine_nearest[i]
            reco = {}
            reco['rank'] = i + 1
            reco['source'] = 0
            reco['text'] = solution_vectors[reco_index].raw_text
            results.append(reco)

        random.shuffle(results)
        return jsonify(results)

    except Exception as ex:
        print(ex)
        return jsonify(str(ex))
示例#3
0
def concept_tag_kNearest():
    try:
        body_string = request.data.decode("utf-8")
        if len(body_string) == 0:
            return jsonify("Empty String")
        method_vector = util.vector_from_text(body_string)
        # first transform the given method_vector with our model
        nearest = ml.concept_tag_kNearest(method_vector, solution_vectors, lang_dict,
                lang_model, k=kNearest)
        # nearest contains score, solutions_index and intersections
        # print(kNearest)
        nearest_vectors = []
        for element in nearest:
            result = {}
            result['score'] = element[0]
            result['code'] = solution_vectors[element[1]]
            result['match'] = element[2]
            nearest_vectors.append(result)
        return jsonify(nearest_vectors)
    except Exception as ex:
        return jsonify(str(ex))