-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphGenerator.py
148 lines (101 loc) · 4.16 KB
/
GraphGenerator.py
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
from neo4j import GraphDatabase
import xml.etree.ElementTree as ET
import sys
TagToNode = {
"article":"Article","inproceedings":"Inproceedings","proceedings":"Proceedings",
"book":"Book","incollection":"Incollection","phdthesis":"PHDThesis","mastersthesis":"MastersThesis",
"www":"WWW","person":"Person","data":"Data"}
def progress_bar(total, progress):
bar_length, status = 20, ""
progress = progress / total
if progress >= 1:
progress, status = 1, "\r\n"
block = int(round(bar_length * progress))
text = "\rProcessing of {} items [{}] {:.0f}% {}".format(total,
"#" * block + "-" * (bar_length - block),
round(progress * 100, 0), status)
sys.stdout.write(text)
sys.stdout.flush()
def createPublicationNode(publication):
query = "MERGE (a:Publication" + " {key: \"" + publication.attrib["key"] + "\"})\n"
query += "SET a:" + TagToNode[publication.tag] + "\n"
if "mdate" in publication.keys():
query += "SET a.mdate = \"" + publication.attrib["mdate"] + "\"\n"
if "publtype" in publication.keys():
query += "SET a.publtype = \"" + publication.attrib["publtype"] + "\"\n"
if "reviewid" in publication.keys():
query += "SET a.reviewid = \"" + publication.attrib["reviewid"] + "\"\n"
if "rating" in publication.keys():
query += "SET a.rating = \"" + publication.attrib["rating"] + "\"\n"
if "cdate" in publication.keys():
query += "SET a.cdate = \"" + publication.attrib["cdate"] + "\"\n"
return query
def createRelations(info):
text = info.text.replace("\\","\\\\")
text = "\"" + text.replace("\"", "\\\"") + "\""
if info.tag == "author":
return "MERGE (p:Person {name: " + text +"})\nMERGE (p)<-[:author]-(a)\n"
elif info.tag == "editor":
return "MERGE (p:Person {name: " + text +"})\nMERGE (p)<-[:editor]-(a)\n"
elif info.tag == "title":
return "SET a.title = " + text + "\n"
elif info.tag == "booktitle":
return "SET a.booktitle = " + text + "\n"
elif info.tag == "pages":
return "SET a.pages = " + text + "\n"
elif info.tag == "year":
return "MERGE (y:Year {year: " + text[1:-1] +"})\nMERGE (y)<-[:year]-(a)\n"
elif info.tag == "address":
return "SET a.address = " + text + "\n"
elif info.tag == "journal":
return "MERGE (j:Journal {name: " + text +"})\nMERGE (j)<-[:journal]-(a)\n"
elif info.tag == "volume":
return "SET a.volume = " + text + "\n"
elif info.tag == "number":
return "SET a.number = " + text + "\n"
elif info.tag == "month":
return "SET a.month = " + text + "\n"
elif info.tag == "url":
return "SET a.url = " + text + "\n"
elif info.tag == "ee":
return "SET a.ee = " + text + "\n"
elif info.tag == "cdrom":
return "SET a.cdrom = " + text + "\n"
elif info.tag == "cite" and text != "\"...\"":
return "MERGE (c:Publication {key: " + text +"})\nMERGE (c)<-[:cite]-(a)\n"
elif info.tag == "publisher":
return "MERGE (p:Publisher {name: " + text +"})\nMERGE (p)<-[:publisher]-(a)\n"
elif info.tag == "note":
return "SET a.note = " + text + "\n"
elif info.tag == "crossref":
return "MERGE (c:Publication {key: " + text +"})\nMERGE (c)<-[:crossref]-(a)\n"
elif info.tag == "isbn":
return "SET a.isbn = " + text + "\n"
elif info.tag == "series":
return "MERGE (s:Series {name: " + text +"})\nMERGE (s)<-[:series]-(a)\n"
elif info.tag == "school":
return "MERGE (s:School {name: " + text +"})\nMERGE (s)<-[:school]-(a)\n"
elif info.tag == "chapter":
return "SET a.chapter = " + text + "\n"
elif info.tag == "publnr":
return "SET a.publnr = " + text + "\n"
else:
return ""
def main(file, username, password):
driver = GraphDatabase.driver("neo4j://127.0.0.1:7687", auth=(username, password))
session = driver.session()
tree = ET.parse(file)
root = tree.getroot()
n = 15000
xml_list = [elem for elem in root][:n]
i = 0
for publication in xml_list:
progress_bar(len(xml_list), i + 1)
query = createPublicationNode(publication)
for info in publication:
query += createRelations(info) + "WITH a\n"
session.run(query+"RETURN NULL")
i += 1
session.close()
driver.close()
main(sys.argv[1], sys.argv[2], sys.argv[3])