-
Notifications
You must be signed in to change notification settings - Fork 0
/
realec_graphs.py
55 lines (50 loc) · 2.04 KB
/
realec_graphs.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
from neo4jrestclient import client
from neo4jrestclient.client import GraphDatabase
import json
db = GraphDatabase("http://localhost:7474", username="neo4j", password="mypassword")
def create():
texts = db.labels.create("Texts")
# Create some nodes with labels
identifiers = {'texts':{}, 'mistakes': {}}
with open('./elastic/texts.json', 'r', encoding='utf-8') as f:
i = 0
for line in f:
print('texts', i)
data = json.loads(line.strip())
name = data['t_name']
text = data['text']
mark = data['essay_mark']
year = data['year']
fac = data['faculty']
t_type = data['task_type']
t = db.nodes.create(name=name, text=text, mark=mark, year=year, faculty=fac, type=t_type, t_id=i)
texts.add(t)
identifiers['texts'][i] = t.id
i += 1
mistakes = {}
mist = db.labels.create("Mistake")
with open('./elastic/mistakes.json', 'r', encoding='utf-8') as f:
i = 0
for line in f:
print('mistakes', i)
data = json.loads(line.strip())
name = data['m_type']
if name not in mistakes:
m = db.nodes.create(name=name, id=i)
mist.add(m)
mistakes[name] = m.id
text = data['m_text']
corr = data['correction']
w_l = data['weight_language']
cause = data['cause']
t_id = int(data['t_id'])
db.node[identifiers['texts'][t_id]].relationships.create("contains", db.node[mistakes[name]], text=text,
correction=corr, weight_language=w_l, cause=cause)
i += 1
def query():
q = 'MATCH (t:Texts)-[r:contains]->(m:Mistake) WHERE m.name="%s" RETURN t, type(r), m' % 'Articles'
results = db.query(q, returns=(client.Node, str, client.Node))
for r in results:
print("(%s)-[%s]->(%s)" % (r[0]["name"], r[1], r[2]["name"]))
if __name__ == '__main__':
query()