[知识图谱构建]-读取MySQL数据导入Neo4J(去重)

1、背景

简单记录下,

最近要做一些有关知识图谱融合研究的相关工作,需要自己构建一些能实际落地应用的知识图谱来验证知识图谱融合研究的有效性,本文程序完成的工作是读取MySQL中的题库数据并导入Neo4J中,同时对节点和关系进行去重,避免重复创建节点和关系,如有错误或可优化的地方请指正。

2、数据来源

在本篇博客程序运行之前,笔者已经爬取了1w8左右的高中历史题库数据,爬取的是某在线题库网站。数据:高中历史题库-18000条-机器学习文档类资源-CSDN下载

3、代码

# coding:utf-8
from py2neo import Graph, Node, Relationship, NodeMatcher, RelationshipMatcher
import web
import hashlib

# 连接Neo4j
url = "http://localhost:7474"
username = "neo4j"
password = "test"
graph = Graph(url, auth=(username, password))
node_matcher = NodeMatcher(graph)
relation_matcher = RelationshipMatcher(graph)
print("neo4j info: {}".format(str(graph)))
hl = hashlib.md5(b'KG')

# 连接数据库
db = web.database(
    dbn='mysql',
    user='root',
    pw='xxxxxxx',
    db='learn_kg',
)


# 得到md5字符串
def get_md5_string(input_str):
    hl.update(input_str.encode(encoding='utf-8'))
    return hl.hexdigest()


# 从数据库中查询数据并处理
def query_data():
    results = list(db.query("select * from t_questions_yixuela"))
    return results


# 处理数据
def handle_data():
    root_node = {'label': 'Period', 'name': '初中'}
    history_node = {'label': 'subject', 'name': '初中历史'}
    add_to_neo(root_node, history_node, '子学科')
    results = query_data()
    for res in results:
        question = res['question_info']
        answer = res['answer']
        points = res['point']
        input_string_split = points.split("@@")
        first_point = input_string_split[0]
        second_point = input_string_split[1]
        third_point = input_string_split[2]
        add_to_neo(history_node, {'label': 'point', 'name': first_point}, '子知识点')
        add_to_neo({'label': 'point', 'name': first_point}, {'label': 'point', 'name': second_point}, '子知识点')
        add_to_neo({'label': 'point', 'name': second_point}, {'label': 'point', 'name': third_point}, '子知识点')
        print(f'{first_point}-> {second_point}-> {third_point}')
        # 插入题目
        question_md5 = get_md5_string(question)
        add_to_neo({'label': 'point', 'name': third_point},
                   {'label': 'question', 'name': question_md5, 'info': question}, '题目')
        # 插入答案
        add_to_neo({'label': 'question', 'name': question_md5}, {'label': 'point', 'name': answer}, '答案')


# 添加关系到neo4j
def add_relation_to_neo(start_node_item, end_node_item, relation_str):
    start_node = node_matcher.match(start_node_item['label'], name=start_node_item['name']).first()
    end_node = node_matcher.match(end_node_item['label'], name=end_node_item['name']).first()
    if start_node is not None and end_node is not None:
        r_type = relation_matcher.match([start_node, end_node], r_type=relation_str).first()
        if r_type is None:
            graph.create(Relationship(start_node, relation_str, end_node))
            print(f"create  relations.{relation_str}, start_node:{str(start_node)}, end_node:{str(end_node)}")


# 添加节点和关系到neo4j
def add_to_neo(start_node, end_node, relation):
    add_node_to_neo(start_node)
    add_node_to_neo(end_node)
    add_relation_to_neo(start_node, end_node, relation)


# 添加节点到neo4j
def add_node_to_neo(node_items):
    label = node_items['label']
    name = node_items['name']
    find_node = node_matcher.match(label, name=name).first()
    if find_node is None:
        attrs = {k: v for k, v in node_items.items() if k != "label"}
        node = Node(label, **attrs)
        graph.create(node)


if __name__ == '__main__':
    handle_data()

4、效果

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值