How to return class labels as string

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to return class labels as string

fahadhassan
Hi,

I am using owlready2 to process my ontology(loaded in.owl format) in python. I am facing a few difficulties as discussed below.

1. I want to get my class labels in the ontology as string. When I used onto.class() and after changing the rendering of entities, it returns the output as a class 'owlready2.entity.ThingClass', however, I am interested in getting output as a string type. Is there any way to access the labels as a string or a way to convert this to a string so that I can use it for my desired task which need string variable.

2. I know that we can access the class by onto.class and subclasses by onto.class.ancestors() or by onto.class.subclasses(), but for that, I need to know the class label. But in my case I just know the level of the hierarchy, for example, I want to know the class labels of classes at level 6 of hierarchy or last level or any other level. Can I do this using owlready2. I mean is there any method of indexing through which I can directly get access to a specific level of hierarchy/ontology.

3. Also, is there any way to print the hierarchy of classes since the list(onto.classes()) prints only the complete list of classes in ontology and ignores the hierarchy or hierarchical relationships between them.

I am using Owlready2 for the first time. Any kind of help is highly appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: How to return class labels as string

Jiba
Administrator
Hi,

1) you can use Class.label (Class.label.first() if you want only one label)

2) there is no depth-based function in Owlready; you have to implement it using e.g. subclasses()

3) similarly, you can implement it easily using a recursive function.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: How to return class labels as string

Joe S.
Hello. I tried 1) to return label of a class, but the results is None. Tried this:

print(onto.class.label.first())

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to return class labels as string

Joe S.
Figured it out. So, to return the label of a class or variable, etc., as a string:
print(onto.Class.name)