Exemplo n.º 1
0
def advogato_data_KG_target(kind, entity):
    '''
    这个暂时没什么用
    :param kind:
    :param entity:
    :return:
    '''
    conn, cursor = init()
    if kind == 'source':
        sql = source_template.format(entity)
    else:
        sql = target_template.format(entity)
    cursor.execute(sql)
    results = cursor.fetchall()
    print(len(results))
    nodes = []  # 最终结果
    seconds = []
    cnt = 0
    for row in results:
        node = {'source': row[1], 'target': row[2], 'type': "resolved", 'rela': row[3]}
        nodes.append(node)
        seconds.append(row[2])
        cnt += 1
        if cnt > max_nodes_per_level:
            break
    thirds = []
    cnt = 0
    for source in seconds:
        sql = source_template.format(source)
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            node = {'source': row[1], 'target': row[2], 'type': "resolved", 'rela': row[3]}
            nodes.append(node)
            thirds.append(row[2])
            cnt += 1
            if cnt > max_nodes_per_level:
                break
    cnt = 0
    for source in thirds:
        sql = source_template.format(source)
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            node = {'source': row[1], 'target': row[2], 'type': "resolved", 'rela': row[3]}
            nodes.append(node)
            cnt += 1
            if cnt > max_nodes_per_level:
                break
    print(nodes)

    return nodes
Exemplo n.º 2
0
def helper(entity01, nodes, level, conn, cursor):
    if level <= 0:
        return
    per_level_nodes = 0
    for entity in list(entity01.keys()):
        print(entity)
        used = entity01[entity]
        if used == 1:
            continue
        sql = source_template.format(entity)
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            entity01[entity] = 1
            entity01[row[2]] = 0
            # entity01.update({entity:1, row[2]:0})
            node = {
                'source': row[1],
                'target': row[2],
                'type': "resolved",
                'rela': row[3]
            }
            nodes.append(node)
            per_level_nodes += 1
            if per_level_nodes > max_nodes_per_level:
                break
    helper(entity01, nodes, level - 1, conn, cursor)
Exemplo n.º 3
0
def advogato_data_KG_source(kind, entity, level):
    conn, cursor = init()
    sql = source_template.format(entity)
    entity01 = {}
    nodes = []
    cursor.execute(sql)
    results = cursor.fetchall()
    per_level_nodes = 0
    for row in results:
        source_node = row[1]
        target_node = row[2]
        entity01[source_node] = 1  # 表示已经查询过了
        entity01[target_node] = 0  # 表示还未查询
        node = {
            'source': row[1],
            'target': row[2],
            'type': "resolved",
            'rela': row[3]
        }
        nodes.append(node)
        per_level_nodes += 1
        if per_level_nodes > max_nodes_per_level:
            break
    helper(entity01, nodes, level - 1, conn, cursor)
    return nodes
Exemplo n.º 4
0
def helper(entity01, nodes, level, conn, cursor, links, category_to_int,real_nodes, source_template):
    if level <= 0:
        return
    per_level_nodes = 0
    for entity in list(entity01.keys()):
        print(entity)
        used = entity01[entity]
        if used == 1:
            continue
        sql = source_template.format(entity)
        cursor.execute(sql)
        results = cursor.fetchall()

        for row in results:
            source_node = row[1]
            target_node = row[2]
            entity01[source_node] = 1  # 表示已经查询过了
            entity01[target_node] = 0  # 表示还未查询
            if source_node not in real_nodes:
                real_nodes.append(source_node)
                node = {'name': source_node, 'value': str(source_node), 'category': category_to_int['关联实体']}
                nodes.append(node)
            if target_node not in real_nodes:
                real_nodes.append(target_node)
                node = {'name': target_node, 'value': str(target_node), 'category': category_to_int['关联实体']}
                nodes.append(node)
            link = {'source': real_nodes.index(source_node), 'target': real_nodes.index(target_node), 'value': row[3]}
            links.append(link)
            per_level_nodes += 1
            if per_level_nodes > max_nodes_per_level:
                break
    helper(entity01, nodes, level - 1, conn, cursor, links, category_to_int, real_nodes,source_template)
Exemplo n.º 5
0
def advogato_data_KG_source(kind, entity, level):
    conn, cursor = init()
    sql = source_template.format(entity)
    entity01 = {}
    cursor.execute(sql)
    results = cursor.fetchall()
    per_level_nodes = 0
    contents = {}
    contents["type"] = "force"  # 属性 type
    no_repeat_categories = ['指定实体', '关联实体']
    category_to_int = {"指定实体": 0, "关联实体": 1}
    print(len(results))
    print(no_repeat_categories)
    categories = []
    for category in no_repeat_categories:
        node = {'name': category, 'keyword': {}, 'base': category}
        categories.append(node)
    contents["categories"] = categories
    print(contents)
    nodes = []
    links = []
    real_nodes = []
    for row in results:
        source_node = row[1]
        target_node = row[2]
        entity01[source_node] = 1  # 表示已经查询过了
        entity01[target_node] = 0  # 表示还未查询
        if row[1] not in real_nodes:
            real_nodes.append(row[1])
            if row[1] == entity:
                category = "指定实体"
            else:
                category = "关联实体"
            node = {
                'name': row[1],
                'value': str(row[1]),
                'category': category_to_int[category]
            }
            nodes.append(node)
        if row[2] not in real_nodes:
            real_nodes.append(row[2])
            if row[2] == entity:
                category = "指定实体"
            else:
                category = "关联实体"
            node = {
                'name': row[2],
                'value': str(row[2]),
                'category': category_to_int[category]
            }
            nodes.append(node)
        link = {
            'source': real_nodes.index(row[1]),
            'target': real_nodes.index(row[2]),
            'value': row[3]
        }
        links.append(link)
        per_level_nodes += 1
        if per_level_nodes > max_nodes_per_level:
            break
    helper(entity01, nodes, level - 1, conn, cursor, links, category_to_int,
           real_nodes)
    contents['nodes'] = list(nodes)
    contents['links'] = list(links)
    return contents