1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
from py2neo import Graph,Node,Relationship import os
classification = ['番剧名称','番剧类型','角色列表','声优列表','播放平台','制作公司']
class KG: name_file = open('data/name.txt', 'r', encoding='utf8') name_img_file = open('data/name_img.txt', 'r', encoding='utf8') time_file = open('data/time.txt', 'r', encoding='utf8') platform_file = open('data/platform.txt', 'r', encoding='utf8') author_file = open('data/author.txt', 'r', encoding='utf8') role_img_file = open('data/role_img.txt', 'r', encoding='utf8') role_name_file = open('data/role_name.txt', 'r', encoding='utf8') role_content_file = open('data/role_content.txt', 'r', encoding='utf8') seiyuu_file = open('data/seiyuu.txt', 'r', encoding='utf8') classify_file = open('data/classify.txt', 'r', encoding='utf8') content_file = open('data/content.txt', 'r', encoding='utf8')
def createEntity(self,graph): cql = '''CREATE (n:番剧数据库{id:'0', name:'番剧数据库'}) RETURN n''' graph.run(cql)
for i, c in enumerate(classification): cql = ''' MERGE (a:番剧数据库{id:'%d', name:'%s'}) MERGE (b {name: '番剧数据库'}) MERGE (b)-[:划分]->(a) ''' % (i+1, c) graph.run(cql)
name_list = self.name_file.readlines() name_img_list = self.name_img_file.readlines() author_list = self.author_file.readlines() platform_list = self.platform_file.readlines() content_list = self.content_file.readlines()
for i in range(len(name_list)): cql = """ MERGE (:番剧名称{id:'%d',名称:"%s",图片:'%s',内容:'%s'}) """ % (i, name_list[i].replace('\n',''), name_img_list[i].replace('\n',''), content_list[i].replace('\n','')) graph.run(cql)
print("step 1 down")
author_tmp_list = [] platform_tmp_list = [] for i in range(len(name_list)): if author_list[i] not in author_tmp_list: author_tmp_list.append(author_list[i]) cql = """ MERGE (:制作公司{id:'%d', 名称:"%s"}) """ % (i, author_list[i].replace('\n','')) graph.run(cql) if platform_list[i] not in platform_tmp_list: platform_tmp_list.append(platform_list[i]) cql = """ MERGE (:播放平台{id:'%d', 名称:"%s"}) """ % (i, platform_list[i].replace('\n', '')) graph.run(cql)
print("step 2 down")
classify_tmp_list = [] seiyuu_tmp_list = [] for i in range(len(name_list)): classify_list = self.classify_file.readline().split(' ') seiyuu_list = self.seiyuu_file.readline().split(' ') role_name_list = self.role_name_file.readline().split(' ') role_img_list = self.role_img_file.readline().split(' ') role_content_list = self.role_content_file.readline().split(' ')
for j in range(len(classify_list)): if classify_list[j] not in classify_tmp_list: classify_tmp_list.append(classify_list[j]) cql = """ MERGE (:番剧类型{类型:'%s'}) """ % (classify_list[j].replace('\n','')) graph.run(cql) for j in range(len(seiyuu_list)): if seiyuu_list[j] not in seiyuu_tmp_list: seiyuu_tmp_list.append(seiyuu_list[j]) cql = """ MERGE (:声优列表{名称:'%s'}) """ % (seiyuu_list[j].replace('\n','')) graph.run(cql) for j in range(len(role_name_list)): if (len(role_name_list)!=len(role_img_list)): print("wrong img:"+role_name_list[j]) if (len(role_content_list)<4): break cql = """ MERGE (:角色列表{名称:'%s',图片:'%s',性别:'%s',身份:'%s',特征:'%s',配音:'%s'}) """ % (role_name_list[j].replace('\n',''), role_img_list[j].replace('\n',''), role_content_list[j * 4 + 0].replace('\n', ''), role_content_list[j * 4 + 1].replace('\n', ''), role_content_list[j * 4 + 2].replace('\n', ''), role_content_list[j * 4 + 3].replace('\n', '')) graph.run(cql)
print("step 3 down")
def createreRationship(self,graph): self.name_file.seek(0) self.seiyuu_file.seek(0) self.platform_file.seek(0) self.role_name_file.seek(0) self.role_img_file.seek(0) self.author_file.seek(0) self.classify_file.seek(0)
name_list = self.name_file.readlines() author_list = self.author_file.readlines() platform_list = self.platform_file.readlines()
for i in range(len(name_list)): classify_list = self.classify_file.readline().split(' ') role_name_list = self.role_name_file.readline().split(' ') role_img_list = self.role_img_file.readline().split(' ') seiyuu_list = self.seiyuu_file.readline().split(' ')
for j in range(len(classify_list)-1): cql = """ MATCH (a:番剧名称{id:'%d', 名称:"%s"}), (b:番剧类型{类型:'%s'}) MERGE (b)-[:类型]->(a) """ % (i,name_list[i].replace('\n',''), classify_list[j].replace('\n','')) graph.run(cql)
for j in range(len(role_name_list)): cql = """ MATCH (a:番剧名称{id:'%d', 名称:"%s"}), (b:角色列表{名称:'%s',图片:'%s'}) MERGE (b)-[:属于]->(a) """ % (i,name_list[i].replace('\n',''), role_name_list[j].replace('\n',''), role_img_list[j].replace('\n','')) graph.run(cql)
cql = """ MATCH (a:声优列表{名称:"%s"}), (b:角色列表{名称:'%s',图片:'%s'}) MERGE (a)-[:配音]->(b) """ % (seiyuu_list[j].replace('\n', ''), role_name_list[j].replace('\n', ''), role_img_list[j].replace('\n', '')) graph.run(cql)
cql = """ MATCH (a:番剧名称{id:'%d', 名称:"%s"}), (b:制作公司{名称:"%s"}) MERGE (b)-[:制作]->(a) """ % (i,name_list[i].replace('\n',''), author_list[i].replace('\n','')) graph.run(cql)
cql = """ MATCH (a:番剧名称{id:'%d', 名称:"%s"}), (b:播放平台{名称:"%s"}) MERGE (b)-[:播放]->(a) """ % (i,name_list[i].replace('\n',''), platform_list[i].replace('\n','')) graph.run(cql)
print("step 4 down")
if __name__ == '__main__': test_graph = Graph("http://127.0.0.1:7474/browser/", auth=("neo4j", "123456789")) test_graph.run('match(n) detach delete n') kg = KG() kg.createEntity(test_graph) kg.createreRationship(test_graph)
|