Skip to content
Snippets Groups Projects
Select Git revision
  • f57a8856919ea30fbfb5fc74ca0e9368afa4bd5b
  • master default protected
  • multiprocessing
  • experiment/clara
  • experiment/spec2B-poc
  • experiment/qivalio-poc
  • experiment/ertms
  • MAY-2023
  • FEB-2023
  • EGC-2023
  • 0.2.1
  • v0.2.0
  • v0.1.2
13 results

amr_reification_9.py

Blame
  • amr_reification_9.py 2.91 KiB
    #!/usr/bin/python3.10
    # -*- coding: Utf-8 -*-
    
    #==============================================================================
    # TENET: Rule to add AMR leaf for reclassified concept
    #------------------------------------------------------------------------------
    # Add AMR leaf corresponding to the relation between concept and variable.
    #==============================================================================
    
    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 )
    
    
    #==============================================================================
    # Pattern Search: concept{variable, concept}
    #==============================================================================
    
    def __search_pattern(graph):
        select_data_list = ['?concept', '?cLabel', '?variable', '?vLabel']
        clause_list = [f'?concept rdfs:subClassOf* amr:AMR_Concept .',
                       f'?concept amr:label ?cLabel .',
                       f'?concept amr:fromAmrLk ?linkedC .',
                       f'?variable a amr:AMR_Variable .',
                       f'?variable amr:label ?vLabel .',
                       f'?variable amr:fromAmrLk ?v .',
                       f'?v a ?linkedC .']
        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_amr_leaf(graph, concept, cLabel, variable, vLabel):
        triple_list = []
        
        # -- New leaf
        new_leaf_uri = produce_uriref(graph, f'amr:leaf_{cLabel}_{vLabel}')
        relation = produce_uriref(graph, 'rdf:type')
        value = produce_uriref(graph, 'amr:AMR_Leaf')
        triple_list.append((new_leaf_uri, relation, value))
        
        # -- Relation: hasVariable
        relation = produce_uriref(graph, 'amr:hasVariable')
        triple_list.append((new_leaf_uri, relation, variable))
        
        # -- Relation: hasConcept
        relation = produce_uriref(graph, 'amr:hasConcept')
        triple_list.append((new_leaf_uri, relation, concept))
        
        return triple_list
    
    
    #==============================================================================
    # Main Method
    #==============================================================================
    
    def add_amr_leaf_for_reclassified_concept(graph):
        
        # -- Rule Initialization
        rule_label = 'add AMR leaf for reclassified concept'
        
        # -- Search for patterns
        _, pattern_set = __search_pattern(graph)
        
        # -- Selection Analyzing (1)
        rule_triple_list = []
        for pattern in pattern_set:
            rule_triple_list += __add_amr_leaf(
                graph, pattern.concept, pattern.cLabel, pattern.variable, pattern.vLabel)
        
        return rule_label, rule_triple_list