Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get names of individuals #19

Closed
cthoyt opened this issue Dec 14, 2016 · 5 comments
Closed

Get names of individuals #19

cthoyt opened this issue Dec 14, 2016 · 5 comments

Comments

@cthoyt
Copy link
Contributor

cthoyt commented Dec 14, 2016

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
Copy link

jo-tud commented Mar 11, 2017

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

@lambdamusic
Copy link
Owner

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

@jo-tud
Copy link

jo-tud commented Apr 27, 2017

Great! Looking forward to it.

@lambdamusic
Copy link
Owner

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
Copy link
Contributor Author

cthoyt commented Mar 23, 2018

That's wonderful, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants