6

I have made 2 ontologies using owlready2 library

list(ontology1.classes())             

[old_dataset_ontology.weather,
 old_dataset_ontology.season]

list(ontology1.individuals())

[old_dataset_ontology.rainy,
 old_dataset_ontology.windy,
 old_dataset_ontology.cold,
 old_dataset_ontology.clouds]


list(ontology2.classes())             

[new_dataset_ontology.weather,
 new_dataset_ontology.season,
 new_dataset_ontology.season1]

list(ontology2.individuals())

[new_dataset_ontology.rainy,
 new_dataset_ontology.windy,
 new_dataset_ontology.cold1]

I want to merge them but I do not find a way with olwready2. There is not something in the documentation. I want just a simple string matching and to delete the duplicate classes and indiv

Any ideas?

xavi
  • 80
  • 1
  • 12

2 Answers2

0

ontology1.mergeonto(ontology2) will merge ontology2 into ontology1, with ontology1 taking priority for duplicate classes and individuals.

Additionally, you can use the Owlready2 search function to perform a string match for classes and individuals when merging the two ontologies, as well as delete duplicate classes and individuals.

To merge two ontologies using the owlready2 library, you can use the following code:

  from owlready2 import *


  ontology1 = getontology("http://example.org/ontology1.owl")


  ontology2 = getontology("http://example.org/ontology2.owl")


  ontology1.mergeonto(ontology2, search=True, deleteduplicates=True)

The .merge_onto() method can be used to merge two ontologies, and it takes 3 arguments: the ontology to be merged, a boolean for whether to perform a string match for classes and individuals and a boolean for whether to delete duplicate classes and individuals. The default values are False and True respectively.

More information about this can be found on the Owlready2 documenation.

(Note - the URI - http://example.org/ontology1.owl - is an example of a IRI ontology.)

Adding something like what i said in the code at the top line of the code the first line, should fix the issue.

godot
  • 3,422
  • 6
  • 25
  • 42
EAgamer
  • 9
  • 2
0

You can write own method like this example below where I compare Ontologies by names(like you said just string matching):

from owlready2 import *

def MergeOntologies(ontology1, ontology2):
    # Iterate through all classes in ontology1
    for cls1 in ontology1.classes():
        # Iterate through all classes in ontology2
        for cls2 in ontology2.classes():
            # Compare class names
            if cls1.name == cls2.name:
                # Remove duplicate class from ontology2
                ontology2.remove_class(cls2)
    # Do same for individuals
    for ind1 in ontology1.individuals():
        for ind2 in ontology2.individuals():
            if ind1.name == ind2.name:
                ontology2.remove_entity(ind2)
    return ontology2
Nomoos
  • 21
  • 4