示例#1
0
def entity_sort(entity_info):  # 返回得分最大的两个实体id。[name, entity_Id, one_relation, link_relation_num,
    # link_relation_type_num, be_included, relative_position, have_quesition_word, min_distance,have_alpha_or_digit, entity_length]
    result = []
    print("entity_info", entity_info)
    if len(entity_info) == 1:  # 返回name和keyid
        result = entity_info[:2]
        return result
    else:
        result = []
        flag = 0
        entity_scores = LambdaRank.lambdarank(entity_info, flag)
        print(entity_scores)
        temp_score = list(set(entity_scores))
        a = sorted(temp_score, key=lambda x: x * 1000 if x < 0 else x, reverse=True)
        print(a)
        if len(a) >= 5:
            m = 5
        else:
            m = len(a)
        for j in range(0, m):
            res = [idx for idx, i in enumerate(entity_scores) if i == a[j]]
            for i in res:
                # print(i)
                temp = []
                temp = entity_info[i][:2]
                result.append(temp)
        print(result)
        # time.sleep(10)
        return result
        '''
示例#2
0
def relation_sort(
    relation_info
):  # [entity_name, relation, target_entity, target_entity_keyid, jaccard, edit, w2v]
    # print(relation_info)
    if len(relation_info) == 1:
        return relation_info
    else:
        temp = []
        flag = 1
        relation_scores = LambdaRank.lambdarank(relation_info, flag)
        # print(relation_scores)
        temp_score = list(set(relation_scores))
        # entity_scores.sort()
        a = sorted(temp_score,
                   key=lambda x: x * 1000 if x < 0 else x,
                   reverse=True)
        # print(a)

        result10 = []
        result1 = []
        res = [idx for idx, i in enumerate(relation_scores) if i == a[0]]
        for i in res:
            # print(i)
            temp = []
            temp = relation_info[i]
            result1.append(temp)
        if len(a) >= 10:
            m = 10
        else:
            m = len(a)
        for j in range(0, m):
            res = [idx for idx, i in enumerate(relation_scores) if i == a[j]]
            for i in res:
                # print(i)
                temp = []
                temp = relation_info[i]
                result10.append(temp)
        '''
        res = [idx for idx, i in enumerate(relation_scores) if i == a[1]]
        for j in res:
            # print(i)
            temp = []
            temp = relation_info[j]
            result.append(temp)
        '''
        print(result1)
        ##time.sleep(10)
        return result1, result10