使用pandas构造neo4j电影知识谱图
实现效果
电影知识图谱构造过程
实现过程
import pandas as pd
from tqdm import tqdmfrom config import graph, cnndef delete_all():graph.run("MATCH(n) DETACH DELETE(n)")# 创建带属性的节点
def createNode(node, row):try:graph.run("MERGE (a:%s {name: '%s', type: '%s', release_date:'%s', ""actors: '%s' }) "% (node, row['name'], row['type'],row['release_date'], row['actors']))except Exception as e:print(e)print('出错,但是继续')# 创建节点和关系
def create(a, b, aname, bname, relation):try:graph.run("MERGE (a:%s {name: '%s'}) ""MERGE (b:%s {name: '%s'}) ""MERGE (a)-[:%s]->(b)" % (a, aname, b, bname, relation))except Exception as e:print(e)print('出错,但是继续')def import_neo():sql = "select * from tb_movies"df = pd.read_sql(sql, con=cnn)# 使用 tqdm 包装 df.iterrows()for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="构造电影知识图谱"):# print(row['name'])createNode('电影', row)if row['directors'] != '':create('电影', '导演', row['name'], row['directors'], '执导')if row['year'] != '':create('电影', '年份', row['name'], row['year'], '所属年份')if row['regions'] != '':create('电影', '国家或地区', row['name'], row['regions'], '出品国家或地区')if row['genres'] != '':create('电影', '类型', row['name'], row['genres'], '所属类型')if row['languages'] != '':create('电影', '语言', row['name'], row['languages'], '语言')if __name__ == '__main__':delete_all()import_neo()