简体   繁体   English

检索owl中特定子类的类名

[英]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. 我是rdflib初学者,我有一个包含类和子类的本体,我需要在子类中寻找一个特定的词,如果找到它,则返回其类名。

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. 当我print qres的结果时,它将返回完整的URL,并且我只需要子类和类的名称。

Can anyone help with this. 有人能帮忙吗。

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 (eg, http://www.semanticweb.org/raya/ontologies/test6#Good-behaviour ) as results, and you want just the string Good-behaviour . 您尚未显示数据,所以我无法使用您确切的查询或数据,但是根据您的评论,听起来您正在获得IRI(例如, http://www.semanticweb.org/raya/ontologies/test6#Good-behaviour )作为结果,而您只需要字符串Good-behaviour You can use strafter to do that. 您可以使用strafter来做到这一点。 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: 然后,这样的查询将返回具有完整IRI的结果:

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. 您可以使用strafter获取字符串在其他字符串之后的部分。 Eg, 例如,

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, eg, as a so: , then you can also use str(so:) instead of the string form. 如果您在查询中,如定义前缀,为so: ,那么你也可以使用str(so:)而不是字符串形式。 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" |
-------------------------------------------------------------

You can use RDFLib without SPARQL and Python string manipulation to get your answer. 您可以在没有SPARQL和Python字符串操纵的情况下使用RDFLib来获得答案。 If you prefer to use SPARQL, the Joshua Taylor answer to this question would be the way to go. 如果您更喜欢使用SPARQL,那么Joshua Taylor 对此问题的答案将是正确的选择。 You also don't need the SPARQL processor plugin with recent versions (4+) of RDFLib - see the "Querying with SPARQL" documentation . 您也不需要带有RDFLib的最新版本(4+)的SPARQL处理器插件-请参阅“使用SPARQL查询”文档

To get the answer you are looking for you can use the RDFLIB Graph method subject_objects to get a generator of subjects and objects with the predicate you are interested in, rdfs:subClassOf. 要获得所需的答案,可以使用RDFLIB Graph方法subject_objects来生成主题和对象的生成器,以及您感兴趣的谓词rdfs:subClassOf。 Each subject and object will be an RDFLib URIRef, which are also Python unicode objects that can be manipulated using standard Python methods . 每个主题和对象都是RDFLib URIRef,这也是可以使用标准Python方法操作的Python unicode对象。 To get the suffix of the IRI call the split method of the object and take the last item in the returned list. 要获取IRI的后缀,请调用对象的split方法,并获取返回列表中的最后一项。

Here is your code reworked to do as described. 这是您的代码,按说明进行了修改。 Without the data, I can't fully test it but this did work for me when using a different ontology. 没有数据,我无法完全测试它,但是当使用其他本体时,这确实对我有用。

from rdflib import Graph
from rdflib.namespace import RDFS

g = Graph()
g.parse("test.owl")

# n is a subclass name and its class name is good-behaviour
# which i want to be the result
n = "pity"

for subj, obj in g.subject_objects(predicate=RDFS.subClassOf):
    if n in subj:
        print obj.rsplit('#')[-1]
    else:
        print 'not found'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM