-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_to_grakn.py
52 lines (45 loc) · 1.93 KB
/
insert_to_grakn.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
import os
import json
import primal_grakn as grakn
from pprint import pprint
CWD = os.path.abspath(os.path.dirname(__file__))
DATA_DIR = os.path.join(CWD, 'data')
PATH = os.path.join(DATA_DIR, 'output.json')
KEYSPACE = 'luscient_grakn_demo'
with open(PATH) as f:
results = json.load(f)
with grakn.Graph(keyspace=KEYSPACE) as graph:
for result in results:
for func_rel in result['functional_relationships']['entity_level']:
concept_ids = {}
for component in {'antecedent', 'consequent'}:
named_entity = func_rel[component]['text']
valence = func_rel[component]['valence']
# Get or insert the drive-concept
response = graph.match_or_insert('$driven_concept isa driven-concept has name \"{0}\" has valence \"{1}\";'.format(named_entity, valence))
ent_id = response[0]['driven_concept']['id']
concept_ids[component] = ent_id
# Insert the triggering-relationship
query = '''
match
$triggering isa driven-concept id {ant_id};
$triggered isa driven-concept id {con_id};
insert
$trig_rel
(
triggering: $triggering,
triggered: $triggered
)
has source-text \"{source_text}\"
has source-name \"{source_name}\"
has source-id \"{source_id}\"
isa triggering-relationship;
'''.format(
ant_id=concept_ids['antecedent'],
con_id=concept_ids['consequent'],
source_text=result['original_text'],
source_name=result['reference']['source'],
source_id=result['reference']['id'],
)
response = graph.execute(query)
graph.commit()