diff --git a/structure/cts/amr_ctr/transduction/atomic_extraction.py b/structure/cts/amr_ctr/transduction/atomic_extraction.py index e143aadff2c9fbd31220ebb5d639dcc275723790..69b2bf76deb6747e2555782d50d07e1de9074bc8 100644 --- a/structure/cts/amr_ctr/transduction/atomic_extraction.py +++ b/structure/cts/amr_ctr/transduction/atomic_extraction.py @@ -12,7 +12,8 @@ # Query Builder Imports #============================================================================== -import query_builder.construct as construct +#import query_builder.construct as construct +from query_builder.builders import * #============================================================================== @@ -33,7 +34,7 @@ rule_set['create-atom-class-net'] = { # -- New Class Net ?newNet a net:Atom_Class_Net ; net:hasStructure ?sentenceRef ; - net:coverBaseNode ?baseLeaf ; + net:coverBaseNode ?leaf1 ; net:hasClassName ?conceptName. # -- Propagation of relations (from nodes to nets) @@ -44,12 +45,12 @@ rule_set['create-atom-class-net'] = { """, 'clause': """ # -- Identify Class covering a single leaf - ?baseLeaf a amr:AMR_Leaf ; - amr:hasConcept ?concept ; + ?leaf1 a amr:AMR_Leaf ; + amr:hasConcept ?leaf1Concept ; amr:hasVariable ?variable. ?variable amr:label ?varLabel. - ?concept rdfs:subClassOf amr:AMR_Term_Concept. - ?concept amr:label ?conceptName. + ?leaf1Concept rdfs:subClassOf amr:AMR_Term_Concept. + ?leaf1Concept amr:label ?conceptName. # -- Identify structure ?root a amr:AMR_Root ; @@ -58,13 +59,13 @@ rule_set['create-atom-class-net'] = { # -- Identify inbound relations linked to the base leaf (for propagation) OPTIONAL {?inNet a [rdfs:subClassOf* net:Net] ; net:coverBaseNode ?inLeaf. - ?inLeaf ?inRelationEdge ?baseLeaf. + ?inLeaf ?inRelationEdge ?leaf1. ?inRelationEdge amr:hasAmrRole ?inRelationRole.} # -- Identify outgoing relations linked to the base leaf (for propagation) OPTIONAL {?outNet a [rdfs:subClassOf* net:Net] ; net:coverBaseNode ?outLeaf. - ?baseLeaf ?outRelationEdge ?outLeaf. + ?leaf1 ?outRelationEdge ?outLeaf. ?outRelationEdge amr:hasAmrRole ?outRelationRole.} """, 'binding': """ @@ -481,35 +482,33 @@ rule_set['create-phenomena-net-1'] = { if __name__ == '__main__': - print('\n' + ' *** Development Test ***') + print('\n' + ' *** Development Test ***') - print('\n' + ' -- test: new_net') - test_str = construct.new_net(class_name='?conceptName') - print(test_str) + print('\n' + ' -- test: Atom Class Net') + print(atom_class_net) print('\n' + ' -- test: update a test query') test_query= f"""[...] CONSTRUCT {{ - {construct.new_net(net_id='?newNet1', - net_type='Atom_Class_Net', - sentence_ref='?sentenceRef', - class_name='?conceptName')} + {atom_class_net.construct('?node1', + structure='?structureRef', + class_name='?leaf1ConceptLabel')} - {construct.relation_propagation(net_id='?newNet1')} + {atom_class_net.propagate_relations()} }} WHERE {{ - {clause.identify_node('?concept', '?variable')} + {{node.identify()}} ?concept rdfs:subClassOf amr:AMR_Term_Concept. ?concept amr:label ?conceptName. ?variable amr:label ?varLabel. - {clause.identify_structure()} + {{structure.identify()}} - {clause.identify_relations_for_propagation(net_id='?newNet1')} + {{atom_class_net.identify_relations_for_propagation()}} - {binding.new_variable('?newNet1')} + {{atom_class_net.bind_uri()}} }} """ diff --git a/structure/cts/amr_ctr/transduction/query_builder/builders.py b/structure/cts/amr_ctr/transduction/query_builder/builders.py new file mode 100644 index 0000000000000000000000000000000000000000..34471634859468fd8cf1ac5d93bc9d6e79a40bc4 --- /dev/null +++ b/structure/cts/amr_ctr/transduction/query_builder/builders.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# TENET: Query Builders +#------------------------------------------------------------------------------ +# Module grouping query part builder for different element classes (net, node, +# ...) +#============================================================================== + +#============================================================================== +# Importing required modules +#============================================================================== + +from .element.net import * + + +#============================================================================== +# Net Builder(s) +#============================================================================== + +# -- Atom Class Net +atom_class_net = Net('atomClassNet', 'Atom_Class_Net') +atom_class_net_1 = Net('atomClassNet1', 'Atom_Class_Net') +atom_class_net_2 = Net('atomClassNet2', 'Atom_Class_Net') + + +#============================================================================== +# Node Builder(s) +#============================================================================== + +# TODO + + + + +#============================================================================== +# Development Test +#============================================================================== + +if __name__ == '__main__': + + print('\n' + ' *** Development Test ***') + + print('\n' + ' -- test: Atom Class Net') + print(atom_class_net) + + print('\n' + ' -- test: update a test query') + test_query= f"""[...] + CONSTRUCT {{ + {atom_class_net.construct('?node1', + structure='?structureRef', + class_name='?leaf1ConceptLabel')} + + {atom_class_net.propagate_relations()} + + }} + WHERE {{ + clause_1 + clause_2 + [...] + binding + }} + """ + print(test_query) + + print('\n' + ' *** - ***') \ No newline at end of file diff --git a/structure/cts/amr_ctr/transduction/query_builder/construct.py b/structure/cts/amr_ctr/transduction/query_builder/construct.py index e916fcd989fd2546b72b64622422f1ee24aa90a8..b110da1f20ca98ae28a586bbb63cb44f3bf461ef 100644 --- a/structure/cts/amr_ctr/transduction/query_builder/construct.py +++ b/structure/cts/amr_ctr/transduction/query_builder/construct.py @@ -112,6 +112,7 @@ if __name__ == '__main__': {new_net(net_type='Atom_Class_Net', sentence_ref='?sentenceRef', class_name='?conceptName')} + {relation_propagation()} }} diff --git a/structure/cts/amr_ctr/transduction/query_builder/element/net.py b/structure/cts/amr_ctr/transduction/query_builder/element/net.py new file mode 100644 index 0000000000000000000000000000000000000000..794f509e337d70f23d32f40e20c5fd6332c47558 --- /dev/null +++ b/structure/cts/amr_ctr/transduction/query_builder/element/net.py @@ -0,0 +1,157 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# TENET: Net Query Builder +#------------------------------------------------------------------------------ +# Class to generate SPARQL query parts related to semantic nets +#============================================================================== + +#============================================================================== +# Importing required modules +#============================================================================== + +# -- + + +#============================================================================== +# Data Repository +#============================================================================== + +# -- Default References + +DEFAULT_NET_ID = '?newNet' +DEFAULT_NET_TYPE = 'Net' +DEFAULT_SENTENCE_REF = '?sentenceRef' +DEFAULT_BASE_LEAF = '?baseLeaf' +DEFAULT_CLASS_NAME = '?className' + + +# -- Reference Table + +DATA_PROPERTY_TABLE = { + 'structure': 'hasStructure', + 'class_name': 'hasClassName', + 'mother_class_net': 'hasMotherClassNet', + 'individual_label': 'hasIndividualLabel' + } + + +#============================================================================== +# Class +#============================================================================== + +class Net: + """ Class to generate SPARQL query parts related to semantic nets. + """ + + #-------------------------------------------------------------------------- + # Constructor(s) + #-------------------------------------------------------------------------- + + def __init__(self, id='defaultNetId', type='Net'): + + # -- Net Signature + self.id = f'?{id}' + self.type = f'net:{type}' + self.base_node = f'?{id}BaseNode' + + # -- Net Data + self.structure = f'?{id}Structure' + self.class_name = f'?{id}ClassName' + self.mother_class_net = f'?{id}MotherClassNet' + self.individual_label = f'?{id}IndividualLabel' + + # -- Private elements (for relation propagation) + self._in_relation_role = f'?{id}InRelationRole' + self._in_net = f'?{id}InNet' + self._out_relation_role = f'?{id}OutRelationRole' + self._out_net = f'?{id}OutNet' + + + #-------------------------------------------------------------------------- + # Construct Method(s) + #-------------------------------------------------------------------------- + + def __get_data_property(self, data_ref): + property_name = DATA_PROPERTY_TABLE[f'{data_ref}'] + return f'net:{property_name}' + + + def __get_net_data(self, **net_data): + data_str = "" + for data_ref, data_val in net_data.items(): + data_property = self.__get_data_property(data_ref) + data_str += f"{self.id} {data_property} {data_val}.\n " + return data_str + + def construct(self, base_node, **net_data): + return f""" + # -- New Class Net + {self.id} a {self.type}. + {self.id} net:coverBaseNode {base_node}. + {self.__get_net_data(**net_data)}""" + + def propagate_relations(self): + return f""" + # -- Propagation of relations (from nodes to nets) + {self._in_relation_role} a net:Relation. + {self._in_net} {self._in_relation_role} {self.id}. + {self._out_relation_role} a net:Relation. + {self.id} {self._out_relation_role} {self._out_net}.""" + + + + #-------------------------------------------------------------------------- + # Method(s) to get the SPARQL query corresponding to the rule + #-------------------------------------------------------------------------- + + + + + #-------------------------------------------------------------------------- + # Method(s) to update a graph by running the rule SPARQL query + #-------------------------------------------------------------------------- + + + + +#============================================================================== +# Development Test +#============================================================================== + +if __name__ == '__main__': + + print('\n' + ' *** Development Test ***') + + print('\n' + ' -- test: Atom Class Net') + atom_class_net = Net('atomClassNet', 'Atom_Class_Net') + print(atom_class_net) + + + print('\n' + ' -- test: construct') + construct_ctr = atom_class_net.construct('?node1', + structure='?structureRef', + class_name='?leaf1ConceptLabel') + print(construct_ctr) + + print('\n' + ' -- test: update a test query') + test_query= f"""[...] + CONSTRUCT {{ + {atom_class_net.construct('?node1', + structure='?structureRef', + class_name='?leaf1ConceptLabel')} + + {atom_class_net.propagate_relations()} + + }} + WHERE {{ + clause_1 + clause_2 + [...] + binding + }} + """ + print(test_query) + + print('\n' + ' *** - ***') \ No newline at end of file diff --git a/tenet.log b/tenet.log index e12b85a07a6d17baaad01cfb56bc1c46e7dce727..64071c74e5bad82985916d07de06fea439a50113 100644 --- a/tenet.log +++ b/tenet.log @@ -23,8 +23,8 @@ ----- CTS directory: ./structure/cts/ ----- target frame directory: ./input/targetFrameStructure/ ----- input document directory: ./input/amrDocuments/ - ----- output directory: ./output/SolarSystemDev1-20221213/ - ----- sentence output directory: ./output/SolarSystemDev1-20221213/ + ----- output directory: ./output/SolarSystemDev1-20221214/ + ----- sentence output directory: ./output/SolarSystemDev1-20221214/ ----- SHACL binary directory: ./lib/shacl-1.3.2/bin -- Config File Definition ----- schema file: ./structure/amr-rdf-schema.ttl @@ -44,9 +44,9 @@ ----- frame ontology seed file: ./input/targetFrameStructure/base-ontology-seed.ttl -- Output ----- ontology namespace: https://tenet.tetras-libre.fr/base-ontology/ - ----- output file: ./output/SolarSystemDev1-20221213/SolarSystemDev1.ttl + ----- output file: ./output/SolarSystemDev1-20221214/SolarSystemDev1.ttl *** - *** -- INFO - -- Creating output target directory: ./output/SolarSystemDev1-20221213/ +- INFO - -- Creating output target directory: ./output/SolarSystemDev1-20221214/ - DEBUG - -- Counting number of graph files (sentences) - DEBUG - ----- Graph count: 1 - INFO - === Extraction Processing using New TENET Engine === @@ -64,9 +64,9 @@ - DEBUG - ----- Sentence Loading - DEBUG - -------- ./input/amrDocuments/dev/solar-system-1/SSC-01-01.stog.amr.ttl (614) - DEBUG - --- Export work graph as turtle -- DEBUG - ----- Work graph file: ./output/SolarSystemDev1-20221213/SolarSystemDev1-1/SolarSystemDev1.ttl +- DEBUG - ----- Work graph file: ./output/SolarSystemDev1-20221214/SolarSystemDev1-1/SolarSystemDev1.ttl - DEBUG - --- Ending Structure Preparation -- DEBUG - ----- Total Execution Time = 0:00:00.132065 +- DEBUG - ----- Total Execution Time = 0:00:00.116396 - INFO - -- Loading Extraction Scheme (amr_scheme_1) - DEBUG - ----- Step number: 3 - INFO - -- Loading Extraction Rules (amr_ctr/*) @@ -95,7 +95,7 @@ - DEBUG - --- Serializing graph to SolarSystemDev1_preprocessing - DEBUG - ----- step: preprocessing - DEBUG - ----- id: SolarSystemDev1 -- DEBUG - ----- work_file: ./output/SolarSystemDev1-20221213/SolarSystemDev1-1/SolarSystemDev1_preprocessing.ttl +- DEBUG - ----- work_file: ./output/SolarSystemDev1-20221214/SolarSystemDev1-1/SolarSystemDev1_preprocessing.ttl - DEBUG - ----- base: http://SolarSystemDev1/preprocessing - INFO - ----- 212 triples extracted during preprocessing step - INFO - -- Applying extraction step: transduction @@ -176,7 +176,7 @@ - DEBUG - --- Serializing graph to SolarSystemDev1_transduction - DEBUG - ----- step: transduction - DEBUG - ----- id: SolarSystemDev1 -- DEBUG - ----- work_file: ./output/SolarSystemDev1-20221213/SolarSystemDev1-1/SolarSystemDev1_transduction.ttl +- DEBUG - ----- work_file: ./output/SolarSystemDev1-20221214/SolarSystemDev1-1/SolarSystemDev1_transduction.ttl - DEBUG - ----- base: http://SolarSystemDev1/transduction - INFO - ----- 376 triples extracted during transduction step - INFO - -- Applying extraction step: generation @@ -205,23 +205,23 @@ - DEBUG - --- Serializing graph to SolarSystemDev1_generation - DEBUG - ----- step: generation - DEBUG - ----- id: SolarSystemDev1 -- DEBUG - ----- work_file: ./output/SolarSystemDev1-20221213/SolarSystemDev1-1/SolarSystemDev1_generation.ttl +- DEBUG - ----- work_file: ./output/SolarSystemDev1-20221214/SolarSystemDev1-1/SolarSystemDev1_generation.ttl - DEBUG - ----- base: http://SolarSystemDev1/generation - INFO - ----- 159 triples extracted during generation step - INFO - -- Result: file containing only the factoids - DEBUG - --- Making factoid graph with the last step result - DEBUG - ----- Number of factoids: 176 - DEBUG - ----- Graph base: http://SolarSystemDev1/factoid -- DEBUG - --- Serializing graph to factoid file (./output/SolarSystemDev1-20221213/SolarSystemDev1-1/SolarSystemDev1_factoid.ttl) +- DEBUG - --- Serializing graph to factoid file (./output/SolarSystemDev1-20221214/SolarSystemDev1-1/SolarSystemDev1_factoid.ttl) - INFO - *** Execution Time *** ----- Function: apply (lib.tenet_extraction) ------ Total Time: 0:00:07.356953 ------ Process Time: 0:00:07.333204 +----- Total Time: 0:00:06.149423 +----- Process Time: 0:00:06.139959 *** - *** - INFO - === Final Ontology Generation === - INFO - -- Making complete factoid graph by merging sentence factoid graphs - INFO - ----- Total factoid number: 176 - INFO - ----- Graph base: http://SolarSystemDev1/factoid -- INFO - -- Serializing graph to factoid file (./output/SolarSystemDev1-20221213/SolarSystemDev1_factoid.ttl) +- INFO - -- Serializing graph to factoid file (./output/SolarSystemDev1-20221214/SolarSystemDev1_factoid.ttl) - INFO - === Done ===