Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

python 2.7 - retrieving the class name of a specific subclass in owl

I am an rdflib beginner, i have an ontology with classes and sub-classes and I need to look for a specific word in a subclass and, if it is found, return its class name.

I have the following code:

import rdflib
from rdflib import plugin
from rdflib.graph import Graph

g = Graph()
g.parse("test.owl")
from rdflib.namespace import Namespace
plugin.register(
  'sparql', rdflib.query.Processor,
  'rdfextras.sparql.processor', 'Processor')
plugin.register(
  'sparql', rdflib.query.Result,
  'rdfextras.sparql.query', 'SPARQLQueryResult')

qres = g.query("""
  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  PREFIX owl: <http://www.w3.org/2002/07/owl#>
  PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

   SELECT  ?subject ?object
WHERE { ?subject rdfs:subClassOf ?object } 

  """)
# n is asubclass name and its class name is good-behaviour which i want to be the result
n="pity"
for (subj,pred,obj) in qres:
  if n in subj:
    print obj
  else:
    print "not found"

When I print the result of qres it returns a complete URL, and I need the name only of the sub-class and the class.

Can anyone help with this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You haven't shown your data, so I can't use your exact query or data, but based on your comments, it sounds like you're getting IRIs (e.g., http://www.semanticweb.org/raya/ontologies/test6#Good-behaviour) as results, and you want just the string Good-behaviour. You can use strafter to do that. For instance, if you had data like this:

@prefix : <http://stackoverflow.com/questions/20830056/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

:retrieving-the-class-name-of-a-specific-subclass-in-owl 
  rdfs:label "retrieving the class name of a specific subclass in owl"@en .

Then a query like this will return results that have full IRIs:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?question where { 
  ?question rdfs:label ?label .
}
---------------------------------------------------------------------------------------------------------
| question                                                                                              |
=========================================================================================================
| <http://stackoverflow.com/questions/20830056/retrieving-the-class-name-of-a-specific-subclass-in-owl> |
---------------------------------------------------------------------------------------------------------

You can use strafter to get the part of a string after some other string. E.g.,

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?q where { 
  ?question rdfs:label ?label .
  bind(strafter(str(?question),"http://stackoverflow.com/questions/20830056/") as ?q)
}
-------------------------------------------------------------
| q                                                         |
=============================================================
| "retrieving-the-class-name-of-a-specific-subclass-in-owl" |
-------------------------------------------------------------

If you define the prefix in the query, e.g., as a so:, then you can also use str(so:) instead of the string form. If you prefer, you can also do the string manipulation in the variable list rather than the graph pattern. That would look like this:

prefix so: <http://stackoverflow.com/questions/20830056/> 
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select (strafter(str(?question),str(so:)) as ?q) where { 
  ?question rdfs:label ?label .
}
-------------------------------------------------------------
| q                                                         |
=============================================================
| "retrieving-the-class-name-of-a-specific-subclass-in-owl" |
-------------------------------------------------------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...