@@ -108,33 +108,43 @@ def from_schema_graph(graph: Graph):
108108 @staticmethod
109109 def from_kg_graph (graph : Graph , node_limit : int = 100 ,):
110110 """
111- Creates an Ontology object from a given Knowledge Graph.
111+ Constructs an Ontology object from a given Knowledge Graph.
112+
113+ This function queries the provided knowledge graph to extract:
114+ 1. Entities and their attributes.
115+ 2. Relationships between entities and their attributes.
112116
113117 Args:
114- graph (Graph): The graph object representing the ontology.
115-
118+ graph (Graph): The graph object representing the knowledge graph.
119+ node_limit (int): The maximum number of nodes to sample for attribute extraction.
120+
116121 Returns:
117- The Ontology object created from the Knowledge Graph.
122+ Ontology: The Ontology object constructed from the Knowledge Graph.
118123 """
119124 ontology = Ontology ()
120125
126+ # Retrieve all entity labels and relationship types from the graph.
121127 e_labels = graph .query ("call db.labels()" ).result_set
128+ r_labels = graph .query ("call db.relationshipTypes()" ).result_set
122129
130+ # Process each entity label to extract attributes, limited to the specified number of nodes
123131 for label in e_labels :
124132 attributes = graph .query (
125133 f"""MATCH (a:{ label [0 ]} ) call {{ with a return [k in keys(a) | [k, typeof(a[k])]] as types }}
126134 WITH types limit { node_limit } unwind types as kt RETURN kt, count(1)""" ).result_set
127135 ontology .add_entity (Entity (label [0 ], [Attribute (attr [0 ][0 ], attr [0 ][1 ], False , False ) for attr in attributes ]))
128136
129- r_labels = graph . query ( "call db.relationshipTypes()" ). result_set
137+ # Process each relationship type and extract attributes, limited to the specified number of nodes
130138 for label in r_labels :
131139 for label_s in e_labels :
132140 for label_t in e_labels :
141+ # Check if a relationship exists between the source and target entity labels
133142 if graph .query (f"MATCH (s:{ label_s [0 ]} )-[a:{ label [0 ]} ]->(t:{ label_t [0 ]} ) return a limit 1" ).result_set :
134143 attributes = graph .query (
135144 f"""MATCH ()-[a:{ label [0 ]} ]->() call {{ with a return [k in keys(a) | [k, typeof(a[k])]] as types }}
136145 WITH types limit { node_limit } unwind types as kt RETURN kt, count(1)""" ).result_set
137146 ontology .add_relation (Relation (label [0 ], label_s [0 ], label_t [0 ], [Attribute (attr [0 ][0 ], attr [0 ][1 ], False , False ) for attr in attributes ]))
147+
138148 return ontology
139149
140150 def add_entity (self , entity : Entity ):
0 commit comments