From a4b9dae35255df7380b984f22d53b5e9f3d4a491 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Lamercerie?=
 <aurelien.lamercerie@tetras-libre.fr>
Date: Thu, 21 Sep 2023 17:55:30 +0200
Subject: [PATCH] Preprocessing AMR Rule to fix some bug on SSC-04 graph

---
 tenet/scheme/amr_master_rule/__init__.py      |  2 +
 .../preprocessing/amr_bug_fixing_2.py         | 88 +++++++++++++++++
 .../preprocessing/amr_bug_fixing_3.py         | 94 +++++++++++++++++++
 tenet/scheme/owl_amr_scheme_1.py              |  4 +-
 tests/main_tests/test_main_owl_extraction.py  |  2 +-
 5 files changed, 188 insertions(+), 2 deletions(-)
 create mode 100644 tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_2.py
 create mode 100644 tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_3.py

diff --git a/tenet/scheme/amr_master_rule/__init__.py b/tenet/scheme/amr_master_rule/__init__.py
index da108ca3..ef2bd7ce 100644
--- a/tenet/scheme/amr_master_rule/__init__.py
+++ b/tenet/scheme/amr_master_rule/__init__.py
@@ -5,6 +5,8 @@
 # -- Preprocessing Rules
 
 from scheme.amr_master_rule.preprocessing.amr_bug_fixing_1 import * 
+from scheme.amr_master_rule.preprocessing.amr_bug_fixing_2 import * 
+from scheme.amr_master_rule.preprocessing.amr_bug_fixing_3 import * 
 
 from scheme.amr_master_rule.preprocessing.amr_reification_1_concept import * 
 from scheme.amr_master_rule.preprocessing.amr_reification_2_concept import * 
diff --git a/tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_2.py b/tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_2.py
new file mode 100644
index 00000000..929d0614
--- /dev/null
+++ b/tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_2.py
@@ -0,0 +1,88 @@
+#!/usr/bin/python3.10
+# -*- coding: Utf-8 -*-
+
+#==============================================================================
+# TENET: Rule to fix AMR bug
+#------------------------------------------------------------------------------
+# Fix AMR bug about planet named system solar
+#==============================================================================
+
+from rdflib import Graph, Literal
+
+import transduction
+from transduction import net
+from transduction.query_builder import generate_select_query
+from transduction.rdfterm_computer import ( produce_uriref, produce_literal )
+
+
+#==============================================================================
+# Pattern Search: bug about planet named system solar
+#==============================================================================
+
+def __search_pattern(graph):
+    select_data_list = ['?originNode', '?originConcept']
+    clause_list = ['?originConcept a ns3:Concept.',
+                   'FILTER ( CONTAINS(str(?originConcept), str(ns2:intercontinental)) ).',
+                    '?originNode a ?originConcept.',
+                    '?cloudNode ns2:mod ?originNode.', 
+                    '?cloudNode a ns2:cloud.'
+                   ]
+    query_code = generate_select_query(graph, select_data_list, clause_list)
+    result_set = graph.query(query_code)
+    return query_code, result_set
+            
+
+#==============================================================================
+# Construct Method(s)
+#==============================================================================
+
+def __add_new_concept(graph, originNode, originConcept, new_label='interstellar'):
+    triple_list = []
+    
+    new_concept_uri = produce_uriref(graph, f'ns2:{new_label}')
+    
+    # -- New concept
+    relation = produce_uriref(graph, 'rdf:type')
+    value = produce_uriref(graph, 'ns3:Concept')
+    triple_list.append((new_concept_uri, relation, value))
+    
+    # # -- Label
+    # relation = produce_uriref(graph, 'rdfs:label')
+    # value = Literal(new_label)
+    # triple_list.append((new_concept_uri, relation, value))
+    
+    # -- Linking between individual and new concept
+    relation = produce_uriref(graph, 'rdf:type')
+    triple_list.append((originNode, relation, new_concept_uri))
+    
+    # -- Classification of the original concept as LinkedData (for tracing)
+    relation = produce_uriref(graph, 'rdfs:subClassOf')
+    value = produce_uriref(graph, 'amr:AMR_Linked_Data')
+    triple_list.append((originConcept, relation, value))
+    
+    # -- Comment the origin concept as 'bug'
+    relation = produce_uriref(graph, 'rdfs:comment')
+    value = Literal('bug')
+    triple_list.append((originConcept, relation, value))
+    
+    return triple_list
+
+
+#==============================================================================
+# Main Method
+#==============================================================================
+
+def fix_amr_bug_2(graph):
+    
+    # -- Rule Initialization
+    rule_label = 'fix AMR bug (2)'
+    
+    # -- Search for patterns
+    _, pattern_set = __search_pattern(graph)
+    
+    # -- Selection Analyzing (1)
+    rule_triple_list = []
+    for pattern in pattern_set:  
+        rule_triple_list += __add_new_concept(graph, pattern.originNode, pattern.originConcept)
+    
+    return rule_label, rule_triple_list
diff --git a/tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_3.py b/tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_3.py
new file mode 100644
index 00000000..5849b4fa
--- /dev/null
+++ b/tenet/scheme/amr_master_rule/preprocessing/amr_bug_fixing_3.py
@@ -0,0 +1,94 @@
+#!/usr/bin/python3.10
+# -*- coding: Utf-8 -*-
+
+#==============================================================================
+# TENET: Rule to fix AMR bug
+#------------------------------------------------------------------------------
+# Fix AMR bug about planet named system solar
+#==============================================================================
+
+from rdflib import Graph, Literal
+
+import transduction
+from transduction import net
+from transduction.query_builder import generate_select_query
+from transduction.rdfterm_computer import ( produce_uriref, produce_literal )
+
+
+#==============================================================================
+# Pattern Search: bug about planet named system solar
+#==============================================================================
+
+def __search_pattern(graph):
+    select_data_list = ['?originNode', '?originConcept', '?cloudNode']
+    clause_list = ['?originConcept a ns3:Concept.',
+                   'FILTER ( CONTAINS(str(?originConcept), str(ns2:molecule)) ).',
+                    '?originNode a ?originConcept.',
+                    '?originNode ns2:consist ?cloudNode.',
+                    '?cloudNode a ns2:cloud.'
+                   ]
+    query_code = generate_select_query(graph, select_data_list, clause_list)
+    result_set = graph.query(query_code)
+    return query_code, result_set
+            
+
+#==============================================================================
+# Construct Method(s)
+#==============================================================================
+
+def __add_new_concept(
+        graph, originNode, originConcept, cloudNode, new_label='molecular'):
+    triple_list = []
+    
+    new_concept_uri = produce_uriref(graph, f'ns2:{new_label}')
+    
+    # -- New concept
+    relation = produce_uriref(graph, 'rdf:type')
+    value = produce_uriref(graph, 'ns3:Concept')
+    triple_list.append((new_concept_uri, relation, value))
+    
+    # # -- Label
+    # relation = produce_uriref(graph, 'rdfs:label')
+    # value = Literal(new_label)
+    # triple_list.append((new_concept_uri, relation, value))
+    
+    # -- Linking between individual and new concept
+    relation = produce_uriref(graph, 'rdf:type')
+    triple_list.append((originNode, relation, new_concept_uri))
+    
+    # -- Linking between individual and linked node
+    relation = produce_uriref(graph, 'ns2:mod')
+    triple_list.append((cloudNode, relation, originNode))
+    
+    # -- Classification of the original concept as LinkedData (for tracing)
+    relation = produce_uriref(graph, 'rdfs:subClassOf')
+    value = produce_uriref(graph, 'amr:AMR_Linked_Data')
+    triple_list.append((originConcept, relation, value))
+    
+    # -- Comment the origin concept as 'bug'
+    relation = produce_uriref(graph, 'rdfs:comment')
+    value = Literal('bug')
+    triple_list.append((originConcept, relation, value))
+    
+    return triple_list
+
+
+#==============================================================================
+# Main Method
+#==============================================================================
+
+def fix_amr_bug_3(graph):
+    
+    # -- Rule Initialization
+    rule_label = 'fix AMR bug (3)'
+    
+    # -- Search for patterns
+    _, pattern_set = __search_pattern(graph)
+    
+    # -- Selection Analyzing (1)
+    rule_triple_list = []
+    for pattern in pattern_set:  
+        rule_triple_list += __add_new_concept(
+            graph, pattern.originNode, pattern.originConcept, pattern.cloudNode)
+    
+    return rule_label, rule_triple_list
diff --git a/tenet/scheme/owl_amr_scheme_1.py b/tenet/scheme/owl_amr_scheme_1.py
index d461b2e9..5a8e2f02 100644
--- a/tenet/scheme/owl_amr_scheme_1.py
+++ b/tenet/scheme/owl_amr_scheme_1.py
@@ -48,7 +48,9 @@ prefix_list = [('owl', '<http://www.w3.org/2002/07/owl#>'),
 # ---------------------------------------------
 
 amr_bug_fixing_sequence = ['Bug fixing for some known anomalies of AMR-LD data',
-                           rule.fix_amr_bug_1
+                           rule.fix_amr_bug_1,
+                           rule.fix_amr_bug_2,
+                           rule.fix_amr_bug_3
                            ]
 
 amr_reification_sequence = ['AMR reification from AMR-Linked-Data to AMR (tenet) structure',
diff --git a/tests/main_tests/test_main_owl_extraction.py b/tests/main_tests/test_main_owl_extraction.py
index a89785c4..ef595bb1 100644
--- a/tests/main_tests/test_main_owl_extraction.py
+++ b/tests/main_tests/test_main_owl_extraction.py
@@ -56,7 +56,7 @@ test_data_dir = f'{INPUT_DIR_PATH}amrDocuments/'
 # onto_prefix = f'SimpleTest'
 # base_output_name = f'SimpleTest'
 
-uuid_num = '02'
+uuid_num = '04'
 amrld_dir_path = f'{test_data_dir}dev/solar-system-{uuid_num}/'
 amrld_file_path = f'{amrld_dir_path}SSC-{uuid_num}-01.stog.amr.ttl'
 base_output_name = f'SolarSystemDev{uuid_num}'
-- 
GitLab