From 32c417967ba735a7f69e4fbc9e946448a6899ee2 Mon Sep 17 00:00:00 2001 From: Hossein Rajaby Faghihi Date: Wed, 20 Sep 2023 14:32:06 +0000 Subject: [PATCH] adding a function to retrieve the predicate form of graph concepts --- domiknows/graph/graph.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/domiknows/graph/graph.py b/domiknows/graph/graph.py index 5f7f3f84..f8808d28 100644 --- a/domiknows/graph/graph.py +++ b/domiknows/graph/graph.py @@ -371,5 +371,37 @@ def what(self): wht = BaseGraphTree.what(self) wht['concepts'] = dict(self.concepts) return wht + + def print_predicates(self,): + predicate_list = dict() + variable_list = ['x', 'y', 'z', 'a', 'b', 'r', 't', 'l', 'i'] + concepts = list(self.concepts.values()) + for subgraph in self.subgraphs.values(): + concepts.extend(list(subgraph.concepts.values())) + for concept in concepts: + predicate_name = concept.name + variables = variable_list[0] + check = False + if concept._out: + if 'has_a' in concept._out: + num_relations = len(concept._out['has_a']) + variables = ", ".join(variable_list[:num_relations]) + check = True + if not check: + new_node = concept + while 'is_a' in new_node._out: + if new_node._out['is_a'][0].dst.name in predicate_list: + variables = predicate_list[new_node._out['is_a'][0].dst.name][1] + break + else: + if 'has_a' in new_node.sup._out: + num_relations = len(new_node.sup._out['has_a']) + variables = ", ".join(variable_list[:num_relations]) + break + new_node = new_node._out['is_a'][0].dst + predicate_list[predicate_name] = [predicate_name, variables] + predicate_list = [f"{predicate[0]}({predicate[1]})" for predicate in predicate_list.values()] + return predicate_list + Ontology = namedtuple('Ontology', ['iri', 'local'], defaults=[None]) \ No newline at end of file