第四天学习进度--(KBQA)初接触知识图谱之构建知识图谱(一)

 知识图谱,在经过一个大体的网上资料的搜索之后,简单的来说它是由实体节点和实体之间的关系组成的一个图结构。

根据网上了解到的这种关系,在使用neo4j构建知识图谱之前,我决定利用python中的networkx工具来自己构建一个知识图谱,以此来加深自己对于知识图谱的关系之后,再学习neo4j构建知识图谱并将其应用到实际的例子中。

networkx是一个处理复杂图结构的一个python工具,一般用在网络拓扑结构的处理当中。在将其转化为知识图谱的过程中,我们需要添加节点之间的关系,经过上述的简单分析,我们开始一个最简单的知识图谱的构建过程。

在构建之前,简单了解networkx提供的函数。

因为networkx相对于neo4j的安装更加容易,且在使用的过程中更加方便。所以暂时先以networkx作为接触知识图谱的一个例子。networkx为我们处理复杂网络提供了特别简单的函数调用,且声明简介易懂。

导入networkx

import networkx as nx

创建有向图

DG = nx.DiGraph()

创建无向图

G = nx.Graph()

创建多重无向图

MG = nx.MultiGraph()

创建多重有向图

MDG = nx.MultiDigraph()

清空图

G.clear()

添加节点

G.add_node(node)

添加边

G.add_edge(node1,node2[,weight])

删除节点

G.remove_node(node)

获得前驱节点

G.predecessors(node)

获得后继节点

G.successors(node)

分配节点属性

G.node[node]['attribute'] = 'info'

添加图属性

G.graph['attribute'] = 'info'

添加边属性

G[node1][node2]['attribute'] = 'info'

迪杰斯特拉求最短路径

dijkstra_path(G, source, target, weight='weight')

求最短距离

dijkstra_path_length(G, source, target, weight='weight')

图的所有边,以元组的列表为结果

F.edges([,date=True])

图的所有节点

G.nodes([,data=True])

 获得邻居节点

G.neighbors(node)

了解了networkx的一些基本的函数之后,现在开始一个简单的知识图谱构建的过程。

在构建知识图谱之前,我们得准备一些预料或者知识网络来开始构建我们的知识图谱,那么就以一个简单的知识网络为例子。

已知张三是李四的朋友,张三现任职于贪心科技(其中张三和李四的关系),根据这种图中所展示的关系,我们对这个社交网络进行还原并构建为networkx中的网络拓扑结构。

那么我们开始构建一个图,由图可以分析得到这是一个有向图。(构建的网络是一个有向图而不是无向图,因为节点和节点之间的关系是有向的,即使通常我们在认为A和B是朋友的这种情况通常来说是双向的,但也有可能A认为B是自己的朋友,而B却不认为A是自己的朋友的这种情况)

并将展示可能用到的函数都构造成一个函数,以便以后能够通用。


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/7/11 13:32
# @Author: hdq
# @File : simple_knowledge.py
import networkx as nx

#添加双向关系
def add_double_edge(digraph: nx.DiGraph, node1, node2, info=None, relation='relation'):
digraph.add_edge(node1, node2)
digraph.add_edge(node2, node1)
digraph[node2][node1][relation] = info
digraph[node1][node2][relation] = info

#添加单向关系
def add_edge(digraph: nx.DiGraph, node1, node2, info=None, relation='relation'):
digraph.add_edge(node1, node2)
digraph[node1][node2][relation] = info

#获取边的全部relation
def get_edges_relation(digraph: nx.DiGraph,relation="relation"):
result = []
for one in digraph.edges(data=True):
result.append(one[2][relation])
return set(result)

#获取为relation为info的所有边
def get_relation_edges(digraph: nx.DiGraph,info=None,relation="relation"):
result = []
for one in digraph.edges(data=True):
if (one[2][relation] == info):
result.append((one[0],one[1]))
return set(result)

#添加节点属性
def add_node_attribute(digraph: nx.DiGraph,nodelist,info=None):
digraph.add_nodes_from(nodelist,attribute=info)

#获取全部节点的attribute
def get_nodes_attribute(digraph: nx.DiGraph):
result=[]
for one in digraph.nodes(data=True):
result.append(one[1]["attribute"])
return set(result)

#获取attribute为info的所有节点
def get_attribute_nodes(digraph: nx.DiGraph,info=None):
result=[]
for one in digraph.nodes(data=True):
if(one[1]["attribute"]==info):
result.append(one[0])
return set(result)


#创建有向图
G = nx.DiGraph()
#添加节点及其属性
add_node_attribute(G,['张三','李四','小五','小四'],'人')
add_node_attribute(G,['贪心科技','腾讯'],'公司')

print("所有的节点属性:",get_nodes_attribute(G))
print("所有属性为公司的节点:",get_attribute_nodes(G,"公司"))
print("所有属性为人的节点:",get_attribute_nodes(G,"人"))
print("当前所有节点信息:",G.nodes(data=True))

#添加属性间的关系
add_double_edge(G,'张三','李四','朋友')
add_edge(G,'张三','贪心科技','现任职于')
add_edge(G,'小五','贪心科技')
add_double_edge(G,'小五','小四','同事')
add_edge(G,'小五','腾讯','现任职于')
add_edge(G,'小四','腾讯','曾任职于')

print("所有边的属性:",get_edges_relation(G))
print("所有属性为朋友的边:",get_relation_edges(G,"朋友"))
print("当前所有边信息:",G.edges(data=True))

print("张三和李四的关系:",G['张三']['李四']['relation'])

 上述代码运行的结果:

原文地址:https://www.cnblogs.com/halone/p/13283284.html