changeset 33:7e2e344c3b87

make name of type attribute configurable. default '_type' for nodes.
author casties
date Tue, 02 Feb 2016 14:41:08 +0100
parents ce12475d2109
children 74dfaed3600b
files importFromOpenMind/importer/ismi2model.py importFromOpenMind/importer/model2model.py importFromOpenMind/importer/model2neo4j.py
diffstat 3 files changed, 26 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/importFromOpenMind/importer/ismi2model.py	Fri Jan 22 20:41:25 2016 +0100
+++ b/importFromOpenMind/importer/ismi2model.py	Tue Feb 02 14:41:08 2016 +0100
@@ -23,6 +23,9 @@
     'notes_old'
 ]
 
+# name of type attribute
+node_type_attribute = '_type'
+rel_type_attribute = 'type'
 
 entsURL=baseURL+"method=get_ents&oc=%s"
 
@@ -149,16 +152,8 @@
         print("ERROR: entity type doesn't match!")
         return None
             
-    # rename if type attr exists
-    if 'type' in attrs:
-        attrs['type2'] = attrs['type']
-        if '_n_type' in attrs:
-            # rename normalized attribute
-            attrs['_n_type2'] = attrs['_n_type']
-            del attrs['n_type']
-        
     # set type
-    attrs['type'] = fixName(oc)
+    attrs[node_type_attribute] = fixName(oc)
                 
     ismi_id = ent['id']
     # rename id to ismi_id
@@ -234,7 +229,8 @@
             continue
         
         # create relation with type
-        nx_rel = nx_graph.add_edge(src_id, tar_id, type=fixName(rel_name), ismi_id=rel_id)
+        rel_atts = {rel_type_attribute: fixName(rel_name), 'ismi_id': rel_id}
+        nx_rel = nx_graph.add_edge(src_id, tar_id, rel_atts)
         
         nx_relations[rel_id] = nx_rel
         
--- a/importFromOpenMind/importer/model2model.py	Fri Jan 22 20:41:25 2016 +0100
+++ b/importFromOpenMind/importer/model2model.py	Tue Feb 02 14:41:08 2016 +0100
@@ -9,7 +9,8 @@
 output_fn = 'ismi_graph_mod.gpickle'
 
 # operations
-ops = ['locate', 'contract', 'inv_rels', 'add_links']
+#ops = ['locate', 'contract', 'inv_rels', 'add_links']
+ops = ['locate', 'add_links']
 
 # types of object to locate
 locate_objects_of_type = ['PLACE']
@@ -29,15 +30,12 @@
 #add_link_attributes = {'ismi_id': 'https://ismi-dev.mpiwg-berlin.mpg.de/drupal-ismi/entity/%s'}
 add_link_attributes = {'ismi_id': 'https://ismi-dev.mpiwg-berlin.mpg.de/om4-ismi/browse/entityDetails.xhtml?eid=%s'}
 
+# name of type attribute
+node_type_attribute = '_type'
+rel_type_attribute = 'type'
+
 
 def fixName(name, is_src_rel=False, is_tar_rel=False, att_from_rel=False):
-    # these are too embarrassing...
-    if 'FLORUIT' in name:
-        name = name.replace('FLORUIT', 'FLOURISH')
-        
-    elif 'floruit' in name:
-        name = name.replace('floruit', 'flourish')
-        
     if is_src_rel:
         #name = name + '>'
         pass
@@ -75,7 +73,7 @@
     # iterate all nodes
     for n in nx.nodes_iter(nx_graph):
         attrs = nx_graph.node[n]
-        if attrs['type'] in locate_objects_of_type:
+        if attrs[node_type_attribute] in locate_objects_of_type:
             # locatable object
             name = attrs['label']
             if name in locations:
@@ -115,13 +113,13 @@
         (nx_src, nx_tar) = nx_edge
         # get attributes of edge
         rel_attrs = nx_graph.edge[nx_src][nx_tar][0]
-        rel_type = rel_attrs['type']
+        rel_type = rel_attrs[rel_type_attribute]
         # get attributes of source and target nodes
         src_attrs = nx_graph.node[nx_src]
         tar_attrs = nx_graph.node[nx_tar]
         
         # contract source relations
-        tar_type = tar_attrs['type']
+        tar_type = tar_attrs[node_type_attribute]
         if tar_type in contract_relations_into_attributes:
             # get list of attributes to transfer
             transfer_atts = contract_relations_into_attributes[tar_type]
@@ -145,7 +143,7 @@
                     src_attrs['_n_'+att_name] = tar_attrs.get('_n_'+transfer_att)
             
         # contract target relations
-        src_type = src_attrs['type']
+        src_type = src_attrs[node_type_attribute]
         if src_type in contract_relations_into_attributes:
             # get list of attributes to transfer
             transfer_atts = contract_relations_into_attributes[src_type]
@@ -185,10 +183,11 @@
         (nx_src, nx_tar) = nx_edge
         # get attributes of edge
         rel_attrs = nx_graph.edge[nx_src][nx_tar][0]
-        rel_type = rel_attrs['type']
+        rel_type = rel_attrs[rel_type_attribute]
         rel_id = rel_attrs['ismi_id']
         # create new relation
-        nx_graph.add_edge(nx_tar, nx_src, type=fixName(rel_type, is_tar_rel=True), ismi_id=-rel_id)
+        invrel_atts = {rel_type_attribute: fixName(rel_type, is_tar_rel=True), 'ismi_id': -rel_id}
+        nx_graph.add_edge(nx_tar, nx_src, invrel_atts)
 
         cnt += 1
         if cnt % 100 == 0:
--- a/importFromOpenMind/importer/model2neo4j.py	Fri Jan 22 20:41:25 2016 +0100
+++ b/importFromOpenMind/importer/model2neo4j.py	Tue Feb 02 14:41:08 2016 +0100
@@ -13,6 +13,10 @@
 # neo4j base URL
 neo4jBaseURL = "http://localhost:7474/db/data/"
 
+# name of type attribute
+node_type_attribute = '_type'
+rel_type_attribute = 'type'
+
 
 ## setup
 
@@ -27,7 +31,7 @@
     cnt = 0
     for node_id in nx.nodes_iter(nx_graph):
         attrs = nx_graph.node[node_id]
-        ntype = attrs['type']
+        ntype = attrs[node_type_attribute]
         ismi_id = attrs['ismi_id']
         # create node with attributes
         n4j_node = n4j_graph.nodes.create(**attrs)
@@ -50,7 +54,7 @@
         (nx_src, nx_tar) = nx_edge
         # get attributes of edge
         attrs = nx_graph.edge[nx_src][nx_tar][0]
-        ntype = attrs['type']
+        etype = attrs[rel_type_attribute]
         # get ismi_id of source and target nodes
         src_id = nx_graph.node[nx_src]['ismi_id']
         tar_id = nx_graph.node[nx_tar]['ismi_id']
@@ -66,7 +70,7 @@
             break
         
         # create Neo4J relation
-        n4j_rel = n4j_graph.relationships.create(src, ntype, tar)
+        n4j_rel = n4j_graph.relationships.create(src, etype, tar)
         # add attributes
         n4j_rel.properties = attrs