无法在owlready python中使用搜索方法从本体中获取我的个人

问题描述

我有一个本体:https://raw.githubusercontent.com/amiraelsayed/lumiere/master/lumiere3.owl

我想获得名为CS-Java的特定课程的所有课程

我尝试使用owlready搜索方法,并在其中添加了具有课程名称和对象属性的过滤条件,但始终给出0,而它应该检索大约19条内容

这是我的个人

<owl:Namedindividual rdf:about="http://www.smacrs.com/lumiere.owl#CS-Java">
        <rdf:type rdf:resource="http://www.smacrs.com/lumiere.owl#Course"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Arrays"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Do_..._While"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Final"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#For_Loops"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Getting_User_Input"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Hello_World_Program"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#If_conditions"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Introduction_and_Installation"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Method_Parameters"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Methods"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Multi-Dimensional_Arrays"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Static"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#String_Builder_and_String_Formatting"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Switch"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Using_Variables"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#What_Java_Is_and_How_It_Works"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#While_Loops"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Strings:_Working_With_Text"/>
        <Code rdf:datatype="http://www.w3.org/2001/XMLSchema#string">CS102</Code>
        <Description rdf:datatype="http://www.w3.org/2000/01/rdf-schema#Literal">This course of study builds on the skills gained by students in Java Fundamentals or Java Foundations to help advance Java programming skills. Students will design object-oriented applications with Java and will create Java programs using hands-on,engaging activities.</Description>
        <Name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Introduction to Java Programming Language</Name>
    </owl:Namedindividual>

这是课程,它包含课程个人列表

onto.search(part_of="*")包含所有课程的所有课程,而当我使用onto.search(part_of="CS-Java")时,它返回0,而我只需要返回本课程的课程即可

解决方法

我想您的搜索返回0个结果,因为search方法只能用于对注释的内容执行Full Text Search

似乎您需要对本体中包含的知识进行查询,所以也许使用SPARQL是一个主意?

Owlready提供了一种perform SPARQL queries on an ontology的访问方式,因此您可以表达诸如“选择CS-Java包含的所有内容”之类的内容:

# convert the ontology to a RDFLib graph
graph = onto.world.as_rdflib_graph()

# perform the query
result = list(graph.query_owlready("""
prefix : <http://www.smacrs.com/lumiere.owl#>
SELECT DISTINCT ?c WHERE {
  :CS-Java :contains ?c .
}"""))

另一种方法可能只是读取contains类上CS-Java属性的内容:

onto['CS-Java'].contains

这两个方法均返回一个Owlready2对象的列表(在本示例中为18个对象)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...