pwin / owlready2

GNU Lesser General Public License v3.0
132 stars 22 forks source link

AttributeError: 'str' object has no attribute 'storid' #28

Open tommycarstensen opened 1 year ago

tommycarstensen commented 1 year ago

This code:

import os
from owlready2 import onto_path
from owlready2 import get_ontology
onto_path.append(os.getcwd())
onto = get_ontology('http://purl.obolibrary.org/obo/bto.owl')
print(onto.search(subclass_of = 'obo.BTO_0000042'))

Gives this error: AttributeError: 'str' object has no attribute 'storid'

It seems like a bug to me.

Hannah-Doerpholz commented 1 year ago

Not a dev here, but I think I can help you out. The problem is with the IRI. You are loading an OBO ontology. When you check out the original ontology file you will find that the class BTO_0000042 can only be accessed through http://purl.obolibrary.org/obo/BTO_0000042. However, when you print the variable onto you get http://purl.obolibrary.org/obo/bto.owl#. If you attach the class id after that, there will naturally be an error. You have to properly define the obo namespace as instructed in the "namspaces" section in the documentation. The following should work for you:

onto = get_ontology('http://purl.obolibrary.org/obo/bto.owl')
obo = get_namespace('http://purl.obolibrary.org/obo/')
print(onto.search(subclass_of = obo.BTO_0000042))

I hope I could help with your problem (if it is still relevant for you).

B-Gendron commented 8 months ago

@tommycarstensen I got the exact same problem when I try to apply an object property to an individual. The funny thing is that it works perfectly for data properties but not for object properties. I finally figured out what happened and I think it might be useful for others that would encouter this problem. The error I made is that I forgot to declare the class of the object to be used as the object property output. Formally, instead of assigning a token to an utterance like this:

token = dialog_dict['TokenID'][current_token_index:current_token_index+n_tokens][0]
utt.containsToken = [token]

I declare that token is of class Token by adding an intermediate step, which gives:

token = dialog_dict['TokenID'][current_token_index:current_token_index+n_tokens][0]
tok = onto.Token(token)
utt.containsToken = [tok]

Now, the assignation works perfectly.