Skip to content
Snippets Groups Projects
Commit 00a53237 authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

New Heuristic Rule to refine mother relation

parent e705feee
No related branches found
No related tags found
No related merge requests found
......@@ -59,6 +59,7 @@ from scheme.amr_master_rule.transduction.phenomena_analyzer.degree_analyzer_1 im
from scheme.amr_master_rule.transduction.heuristic_deducer.relation_deducer_1 import *
from scheme.amr_master_rule.transduction.heuristic_deducer.refine_restriction_1 import *
from scheme.amr_master_rule.transduction.heuristic_deducer.refine_mother_relation_1 import *
# -- Generation Rules
......
......@@ -275,6 +275,7 @@ def __generate_owl_taxonomic_relation(graph, new_class_uri, net):
if mother_class_net_uri_list is None:
predicat_list.append(__compute_class_type_uri(net))
else :
# TODO: add method to filter some mother class
for mother_class_net_uri in mother_class_net_uri_list:
predicat_list.append(__get_mother_class_uri(graph, mother_class_net_uri))
......
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# TENET: Rule to refine mother relation between classes
#------------------------------------------------------------------------------
# AMR rule to refine mother relations for parent classes related to the same
# node
#==============================================================================
from rdflib import Graph
import transduction
from transduction import net
from transduction.query_builder import generate_select_query
from transduction.rdfterm_computer import produce_uriref, produce_literal
ENTITY_CLASS_TYPE = 'base-out:Entity'
#==============================================================================
# Pattern Search: domain
#==============================================================================
def __search_pattern(graph):
query_code = ''
result_set = []
for arg_relation in ['amr:role_domain']:
select_data_list = ['?target_class_net', '?mother_class_net']
clause_list = ['?target_class_net a [rdfs:subClassOf* net:Class_Net].',
'?mother_class_net a [rdfs:subClassOf* net:Class_Net].',
'FILTER ( ?target_class_net != ?mother_class_net ).',
'?target_class_net net:hasMotherClassNet ?mother_class_net.'
]
query_code = generate_select_query(graph, select_data_list, clause_list)
result_set += graph.query(query_code)
return query_code, result_set
#==============================================================================
# Useful Additional Search
#==============================================================================
def __search_parent_class_related_to_same_node(graph, class_net_uri):
select_data_list = ['?parent_class_net']
clause_list = [
'?parent_class_net a [rdfs:subClassOf* net:Class_Net].',
f'?parent_class_net net:hasMotherClassNet <{class_net_uri}>.',
'?parent_class_net net:coverBaseNode ?sameBaseNode.',
f'<{class_net_uri}> net:coverBaseNode ?sameBaseNode.'
]
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 __classify_net_as_entity(graph, net_uri):
class_net = net.ClassNet(graph, uri=net_uri)
class_net.class_type = ENTITY_CLASS_TYPE
return class_net.generate_triple_definition()
def __associate_mother_relation(graph, left_net_uri, right_net_uri):
left_class_net = net.ClassNet(graph, uri=left_net_uri)
right_class_net = net.ClassNet(graph, uri=right_net_uri)
left_class_net.mother_class_net = right_class_net.uri
return left_class_net.generate_triple_definition()
#==============================================================================
# Main Rule Method: classify-net-from-domain
#==============================================================================
def refine_mother_relation(graph):
# -- Rule Initialization
rule_label = "Refine mother relations for parent classes related to the same node"
rule_triple_list = []
query_code, pattern_set = __search_pattern(graph)
for pattern in pattern_set:
_, parent_class_set = __search_parent_class_related_to_same_node(graph, pattern.mother_class_net)
for row in parent_class_set:
if pattern.target_class_net != row.parent_class_net:
# Associate mother relation between class nets
new_triples = __associate_mother_relation(graph, pattern.target_class_net, row.parent_class_net)
rule_triple_list.extend(new_triples)
return rule_label, rule_triple_list
......@@ -121,6 +121,7 @@ classification_sequence_2 = ['classification sequence (2)',
heuristic_deduction_sequence = ['heuristic deduction sequence',
rule.refine_composite_class,
rule.refine_mother_relation,
rule.deduce_individual_and_relation_from_restriction_Recursively,
]
......
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# RELATION DEDUCER Test
#------------------------------------------------------------------------------
# Script to test the relation deducer under development
#==============================================================================
import subprocess, os
from rdflib import Graph, Namespace
from rdflib.namespace import NamespaceManager, FOAF, RDF
from rdflib import URIRef, Literal, BNode
FILE_PATH = f'{os.path.dirname(os.path.abspath(__file__))}'
INPUT_DIR_PATH = f'{FILE_PATH}/test_data/'
OUTPUT_DIR_PATH = f'{FILE_PATH}/test_data/'
TEST_FILE_NAME_1 = 'deducer-devGraph-5'
from context import tenet
from tenet.scheme.amr_master_rule.transduction.heuristic_deducer import refine_mother_relation_1 as test_rule_1
from tenet.scheme import amr_master_rule
from tenet.transduction.rdfterm_computer import __update_uri_with_prefix
from tenet.transduction import rdfterm_computer, prefix_handle
from tenet.transduction import net
#==============================================================================
# Useful Methods
#==============================================================================
def load_test_graph(test_file_name):
print(f'\n -- Test Graph Loading')
graph = Graph()
prefix_handle.update_graph_namespacemanager(graph)
graph_path = f'{INPUT_DIR_PATH}{test_file_name}.ttl'
graph.parse(graph_path)
print(f" ----- Graph Loaded ({len(graph)})")
return graph
def print_triple(graph, triple, num=-1):
num_str = f'[{num}]' if num > -1 else '[-]'
(s, p, o) = triple
s = __update_uri_with_prefix(graph, s)
p = __update_uri_with_prefix(graph, p)
o = __update_uri_with_prefix(graph, o)
print(f' {num_str} {s} {p} {o}')
def add_triples_in_graph(test_file_name, graph, triple_list):
print(f'\n -- Adding triple(s) in graph')
print(f" ----- Graph length before update: {len(graph)}")
print(f" ----- Number of triples to add: {len(triple_list)}")
print(f" ----- Added triples:")
n = 0
graph_length = len(graph)
for triple in triple_list:
graph.add(triple)
if graph_length < len(graph):
n += 1
graph_length = len(graph)
print_triple(graph, triple, num=n)
print(f" ----- Graph length after update: {len(graph)}")
output_graph_path = f'{OUTPUT_DIR_PATH}{test_file_name}.result.ttl'
output_graph_uri = f'https://amr.tetras-libre.fr/rdf/{test_file_name}/result'
print(f'\n -- Serialize test graph to {output_graph_path}')
graph.serialize(destination=output_graph_path,
format='turtle',
base=output_graph_uri)
#==============================================================================
# Development Test
#==============================================================================
def test_search_pattern_deducer(graph):
_, pattern_set = test_rule_1.__search_pattern(graph)
print(f'\n ----- number of selection found: {len(pattern_set)}')
for row in pattern_set:
result_str = f'>>> '
result_str += f'{row.target_class_net.n3(graph.namespace_manager)}'
result_str += f' {row.mother_class_net.n3(graph.namespace_manager)}'
print(result_str)
return pattern_set
def test_search_parent_class_related_to_same_node(graph, class_net_uri):
print(f'\n ----- search parent for following class: {class_net_uri}')
_, pattern_set = test_rule_1.__search_parent_class_related_to_same_node(graph, class_net_uri)
print(f'\n ----- number of selection found: {len(pattern_set)}')
for row in pattern_set:
result_str = f'>>> '
result_str += f'{row.parent_class_net.n3(graph.namespace_manager)}'
print(result_str)
return pattern_set
#==============================================================================
# Unit Test
#==============================================================================
def test_rule_application_deducer(test_file_name, graph, rule):
print('\n -- Rule Test for Relation Deduction')
rule_label, new_triple_list = rule(graph)
print(f' ----- label: {rule_label}')
add_triples_in_graph(test_file_name, graph, new_triple_list)
#==============================================================================
# Test Script
#==============================================================================
if __name__ == '__main__':
print('\n *** Test Preparation ***')
graph_1 = load_test_graph(TEST_FILE_NAME_1)
print('\n \n')
print('\n ///////////////////// Relation Deduction Rule')
print('\n *** Step Test ***')
print('\n -- Step 1: Search Pattern for Deducer')
pattern_set = test_search_pattern_deducer(graph_1)
print('\n \n')
print('\n -- Step 2: Search Parent Class Related to Same Node')
class_net_uri = 'https://tenet.tetras-libre.fr/semantic-net#atomClass_object_o'
pattern_set = test_search_parent_class_related_to_same_node(graph_1, class_net_uri)
print('\n \n')
print('\n *** Unit Test ***')
test_rule_application_deducer(TEST_FILE_NAME_1, graph_1, test_rule_1.refine_mother_relation)
print('\n \n')
print('\n *** - ***')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment