Beispiel #1
0
    def test_property_merge_config_closest_inherit(self):
        rdf_content = rdf_prefix + '''
:had_participant a owl:ObjectProperty ; .
:carried_out_by a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
:custody_received_by a owl:ObjectProperty ;
    owl:subPropertyOf :carried_out_by ; .
        '''
        ontology = Ontology(rdf_content)
        config = ontology.merge_with_master_config(
            '{"fields":{"had_participant":{"color":"red"},'
            '"carried_out_by":{"color":"blue"}}}')
        self.assertTrue('fields' in config)
        fields = config['fields']
        self.assertIn('had_participant', fields)
        property_ = fields['had_participant']
        self.assertIn('color', property_)
        self.assertEqual('red', property_['color'])
        self.assertIn('carried_out_by', fields)
        sub_property = fields['carried_out_by']
        self.assertIn('color', sub_property)
        self.assertEqual('blue', sub_property['color'])
        self.assertIn('custody_received_by', fields)
        subsub = fields['custody_received_by']
        self.assertIn('color', subsub)
        self.assertEqual(sub_property['color'], subsub['color'])
Beispiel #2
0
 def setUp(self):
     ontology_content = '''
             @prefix : <http://dig.isi.edu/ontologies/dig/> .
             @prefix owl: <http://www.w3.org/2002/07/owl#> .
             @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
             @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
             @prefix schema: <http://schema.org/> .
             @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
             :Person a owl:Class ;
                 rdfs:subClassOf :Actor, :Biological_Object ;
                 :common_properties :label, :title, :religion ; .
             :has_name a owl:DatatypeProperty ;
                 schema:domainIncludes :Person ;
                 schema:rangeIncludes xsd:string ; .
             :has_child a owl:ObjectProperty ;
                 schema:domainIncludes :Person ;
                 schema:rangeIncludes :Person ; .
         '''
     ontology = Ontology(ontology_content,
                         validation=False,
                         include_undefined_class=True,
                         quiet=True)
     kg_schema = KGSchema(ontology.merge_with_master_config(dict()))
     etk = ETK(kg_schema=kg_schema,
               ontology=ontology,
               generate_json_ld=True)
     etk2 = ETK(kg_schema=kg_schema,
                ontology=ontology,
                generate_json_ld=False)
     self.doc = etk.create_document(dict(),
                                    doc_id='http://xxx/1',
                                    type_=[DIG.Person.toPython()])
     self.doc2 = etk2.create_document(dict(),
                                      doc_id='http://xxx/2',
                                      type_=[DIG.Person.toPython()])
Beispiel #3
0
 def test_property_merge_master_config(self):
     rdf_content = rdf_prefix + ':had_participant a owl:ObjectProperty . '
     ontology = Ontology(rdf_content)
     d_color = 'red'
     config = ontology.merge_with_master_config('{}', {'color': d_color})
     self.assertIn('fields', config)
     fields = config['fields']
     self.assertIn('had_participant', fields)
     property_ = fields['had_participant']
     self.assertIn('color', property_)
     self.assertEqual(property_['color'], d_color)
Beispiel #4
0
    def test_property_merge_config_delete_orphan(self):
        rdf_content = rdf_prefix + '''
:had_participant a owl:ObjectProperty ; .
:carried_out_by a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
        '''
        ontology = Ontology(rdf_content)
        config = ontology.merge_with_master_config('{"fields":{"had_participant":{"color":"red"},'
                                                   '"custody_received_by":{"color":"blue"}}}',
                                                   delete_orphan_fields=True)
        self.assertTrue('fields' in config)
        fields = config['fields']
        self.assertIn('had_participant', fields)
        self.assertIn('carried_out_by', fields)
        self.assertNotIn('custody_received_by', fields)
Beispiel #5
0
    def test_property_merge_config_inherit(self):
        rdf_content = rdf_prefix + '''
:had_participant a owl:ObjectProperty ; .
:transferred_custody_of a owl:ObjectProperty ;
    owl:subPropertyOf :had_participant ; .
        '''
        ontology = Ontology(rdf_content)
        config = ontology.merge_with_master_config('{"fields":{"had_participant":{"color":"red"}}}')
        self.assertIn('fields', config)
        fields = config['fields']
        self.assertIn('had_participant', fields)
        property_ = fields['had_participant']
        self.assertIn('color', property_)
        self.assertEqual(property_['color'], 'red')
        self.assertIn('transferred_custody_of', fields)
        sub_property = fields['transferred_custody_of']
        self.assertIn('color', sub_property)
        self.assertEqual(property_['color'], sub_property['color'])
Beispiel #6
0
    def test_ontology_schema_org(self):
        import json

        rdf_content = rdf_prefix + '''
        schema:Person a owl:Class ; .
        schema:relatedTo
              a rdf:Property ;
              schema:domainIncludes schema:Person ;
              schema:rangeIncludes schema:Person .
        '''
        kg = '''
        {
          "@type": ["http://schema.org/Person"],
          "@id": "person1",
          "http://schema.org/relatedTo": {
            "@type": ["http://schema.org/Person"],
            "@id": "person2"
          }
        }
        '''

        ontology = Ontology(rdf_content)
        for entity in ontology.all_classes():
            self.assertIsInstance(entity, OntologyEntity)
            self.assertIsInstance(entity, OntologyClass)
        self.assertEqual(len(ontology.all_properties()), 1)

        kg_json = json.loads(kg)
        config = ontology.merge_with_master_config(kg_json,
                                                   delete_orphan_fields=True)
        self.assertTrue('fields' in config)
        fields = config['fields']
        self.assertIn('relatedTo', fields)
        #
        self.assertTrue(
            ontology.is_valid('schema:relatedTo',
                              kg_json["http://schema.org/relatedTo"], kg_json))
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix schema: <http://schema.org/> .
:Event a owl:Class ; .
:Entity a owl:Class ; .
:Organization a owl:Class ; .
:MOVEMENT_TRANSPORT a owl:Class ; .
:GeopoliticalEntity a owl:Class ; .
skos:prefLabel a owl:DatatypeProperty ; 
    schema:domainIncludes :Entity, :Event ;
    rdfs:range xsd:string ; .
:conflict_attack_place a owl:ObjectProperty ;
    schema:domainIncludes :Entity, :Event ;
    schema:rangeIncludes :GeopoliticalEntity ; .
    '''

    ontology = Ontology(ontology_content,
                        validation=False,
                        include_undefined_class=True,
                        quiet=True)
    kg_schema = KGSchema(ontology.merge_with_master_config(dict()))
    etk = ETK(modules=ExampleETKModule, kg_schema=kg_schema, ontology=ontology)
    input_data = {'doc_id': '1', 'data': json.loads(sample_input)}
    doc = etk.create_document(input_data)
    docs = etk.process_ems(doc)
    kgs = [json.dumps(doc.kg.value) for doc in docs[1:]]
    with open('output.jsonl', 'w') as f:
        f.write('\n'.join(kgs))
    with open('output.nt', 'w') as f:
        f.writelines(map(rdf_generation, kgs))