二、知识图谱的基本概念
2.1知识图谱的基本单位:
2.2知识图谱的构建过程:
三、知识图谱的搭建
3.1语料库展示:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3.2 Neo4j页面展示:
3.3 知识图谱搭建:
3.3.1 使用Cypher语法在Neo4j页面搭建知识图谱:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CREATE (n:Anatomylevel2) RETURN n;
MATCH (n:Anatomylevel2) SET n.Unique_ID='D001829' RETURN n;
MATCH (n:Anatomylevel2) SET n.Enlish_name='Body Regions', n.Chinese_Translation='身体部位' RETURN n;
CREATE(n:Anatomylevel2{Unique_ID:'D001829',Enlish_name:'Body Regions',Chinese_Translation:'身体部位',Level_ID:'A01'}) RETURN n
MATCH (n:Anatomylevel2) WITH collect(n) AS nodes CALL apoc.refactor.mergeNodes(nodes) yield node RETURN *;
MATCH(a:Anatomylevel2),(b:Anatomylevel3) CREATE (a)-[r:link]->(b) RETURN r
MATCH(a:Anatomylevel2)-[r:link]->(b:Anatomylevel3) DELETE r
MATCH(b:Anatomylevel3) DELETE b
MATCH(n) DETACH DELETE n
3.3.2 使用Python中py2neo包搭建知识图谱:
from py2neo import Node,Graph,NodeMatcher
import pandas as pd
import re
def __init__(self,url,username,password,data_path,sheet_name):
# 打开数据库
self.url = url
self.username = username
self.password = password
self.graph = Graph(self.url,auth = (self.username,self.password))
# 导入数据
self.data = pd.read_excel(data_path,sheet_name=sheet_name)
# 使用level_one构成实体类
self.level_one = re.search(r'(.*) [',self.data['level1_text'].tolist()[0], re.DOTALL).group(1)
# 创建实体
def Create_Node(self):
# level_rest
Max_columns = int((len(self.data.columns.tolist())+1)/2 + 1)
for i in range(2, Max_columns):
print(f'正在创建第{i}层的node')
column_url = 'level' + str(i) + '_url'
column_text = 'level' + str(i) + '_text'
import_urls_list = list(set(self.data[column_url].tolist()))
import_urls_list = [text_ for text_ in import_urls_list if text_ == text_]
for url in import_urls_list:
Unique_ID = url
English_text_list = []
Chinese_text_list = []
level_ID_list = []
import_texts_list = list(set(self.data[self.data[column_url]==url][column_text].tolist()))
for text in import_texts_list:
English_text = re.search(r'(.*)(', text, re.DOTALL).group(1)
English_text_list.append(English_text)
Chinese_text = re.search(r'((.*))', text, re.DOTALL).group(1)
Chinese_text_list.append(Chinese_text)
level_ID = re.search(r'[(.*)]', text, re.DOTALL).group(1)
level_ID_list.append(level_ID)
label = self.level_one + 'level' + str(i)
node = Node(label,Unique_ID = Unique_ID, English_name = English_text_list, Chinese_Translation= Chinese_text_list,level_ID = level_ID_list)
self.graph.create(node)
def Create_Relationship(self):
Max_columns = int((len(self.data.columns.tolist()) + 1) / 2 + 1)
for i in range(2, Max_columns -1):
print(f'正在创建第{i}层的Relationship')
current_column_url = 'level' + str(i) + '_url'
next_column_url = 'level' + str(i+1) + '_url'
import_urls_list = list(set(self.data[current_column_url].tolist()))
import_urls_list = [text_ for text_ in import_urls_list if text_ == text_]
for url in import_urls_list:
print(url)
# NodeMatcher获取匹配的结点
next_urls_list = list(set(self.data[self.data[current_column_url]==url][next_column_url].tolist()))
next_urls_list = [text_ for text_ in next_urls_list if text_ == text_]
for next_url in next_urls_list:
print(next_url)
current_label = self.level_one + 'level' + str(i)
next_label = self.level_one + 'level' + str(i+1)
Relation = "MATCH (from:"+current_label+"{Unique_ID:"+"""+url+"""+"}),(to:"+next_label +"{Unique_ID:"+"""+next_url+"""+"}) MERGE (from)-[r:link]->(to)"
self.graph.run(Relation)
3.3.3 使用py2neo快速搭建知识图谱:
四、知识图谱的查询
4.0 查询语法:
4.1 查询结点:
MATCH (a:Anatomylevel2)RETURN a
MATCH (a:Anatomylevel2{Unique_ID:"D001829"})-->(b) RETURN a,b
MATCH (a:Anatomylevel2{Unique_ID:"D001829"})-->(b)RETURN b
MATCH (a:Anatomylevel2{Unique_ID:"D001829"})-[r:link]->(b)RETURN b
4.2 查询属性:
MATCH (a:Anatomylevel2)RETURN a.Chinese_Translation
4.3 查询关系:
MATCH (a:Anatomylevel2)-[r]->(b) RETURN type(r)
4.4 查询路径:
MATCH p =shortestPath((a:Anatomylevel2{Unique_ID:"D001829"})-[*1..2]->(b:Anatomylevel3{Unique_ID:"D059925"})) RETURN p
MATCH p =(a:Anatomylevel2{Unique_ID:"D001829"})-[*1..2]->(b) RETURN p
发表评论