lambdamusic / Ontospy

Python library and command-line interface for inspecting and visualizing RDF models aka ontologies.
http://lambdamusic.github.io/Ontospy/
MIT License
219 stars 52 forks source link

Get names of individuals #19

Closed cthoyt closed 6 years ago

cthoyt commented 7 years ago

I would like to get the name of all of the instances of a given class, but without the URI prefix. I think this is more of an RDFLib specific issue dealing with URIRef objects, but maybe there's a solution inside ontospy that I haven't found yet

import ontospy
test_ontology_url = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine"
ont = ontospy.Ontospy(test_ontology_url)
for clz in o.classes:
    print(clz)
    for instance in clz.instances():
        print(instance)
        print(dir(instance))

Output:

<Class *http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#AlsatianWine*>
<Class *http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#AmericanWine*>
<Class *http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#Anjou*>
http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#RoseDAnjou
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__invert__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__truediv__', 'capitalize', 'casefold', 'center', 'count', 'de_skolemize', 'defrag', 'encode', 'endswith', 'eq', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'md5_term_hash', 'n3', 'neq', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'toPython', 'translate', 'upper', 'zfill']

...

None of the functions listed with dir return what I want (I iterated through all them and ran them) What I'd like is simply a way to retrieve:

RoseDAnjou

And make it a bit more general than splitting on the # character, since not all ontologies have to use a # to differentiate the base IRI and the members, correct? Thanks so much for the help!

jo-tud commented 7 years ago

Getting the individuals in an ontology would be really great! It would help me to improve my ontology spec generator a lot.

lambdamusic commented 7 years ago

Ok I'll try to add that with the next release.

jo-tud commented 7 years ago

Great! Looking forward to it.

lambdamusic commented 6 years ago

Added with v1.8.8. Sorry it's taken so long! You can now do this:

for instance in class.instances:
    print instance.uri, instance.qname

Note: instances are extracted on demand (once the method is called) so to avoid unnecessary calculations due to metaclasses and the same object being at the same time an instance and a class.

cthoyt commented 6 years ago

That's wonderful, thanks so much!