[英]Crafting custom ontology/RDF graph from Dbpedia
我已使用Construct Sparql查询从dbpedia提取RDF图。 现在的问题是,提取的图存储与个人(例如Maria Sharapova)相关联的属性(例如出生地)作为对个人的注释。 构造图形时如何指定属性? 我的代码:
String service="http://dbpedia.org/sparql";String queryString =
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
"PREFIX dbp: <http://dbpedia.org/property/> "+
"PREFIX dbpedia: <http://dbpedia.org/resource/> "+
"PREFIX dbo: <http://dbpedia.org/ontology/> " +
"CONSTRUCT { ?entity rdfs:label ?label . ?entity rdf:type dbo:TennisPlayer. ?entity dbp:birthPlace ?b.} " +
"where { "+
"?entity rdfs:label ?label ."+
"?entity rdf:type dbo:TennisPlayer."+
"?entity dbp:birthPlace ?b." +
"FILTER (lang(?label) = 'en')."+
"}\n"
Query query = QueryFactory.create(queryString);
System.out.println("Query Result Sheet");
QueryExecution qe =QueryExecutionFactory.sparqlService(service, query);
Model results = qe.execConstruct();
results.write(System.out, "RDF/XML");
String fileName = "D:/tayybah/GATE data/GATE ontologies/dbpedia domain.owl";
FileWriter outfile = new FileWriter( fileName );
results.write(outfile, "RDF/XML");
我过去通常这样做的方式(例如,是否将RDFS词汇表的数据格式转换为SKOS中的RDFS词汇表转换为SKOS )是使用一个或多个值块来指定每个DBpedia我自己的本体中的类型或属性及其对应的类型或属性。 例如,类似以下的构造查询:
construct {
?entity a ?mytype ; ?myprop ?value
}
where {
values (?dbtype ?mytype) {
(dbpedia-owl:TennisPlayer my:PlayerOfTennis)
}
values (?dbprop ?myprop) {
(rdfs:label my:label)
(dbpprop:birthPlace my:placeOfBirth)
}
?entity a ?dbtype ; ?dbprop ?value .
}
这将捕获每个类型为?dbtype的实体及其每个值?dbprop ,并构造一个图,该图具有类型为?mytype的对应实体, 并具有相同的属性?myprop 。 正如Rob Hall指出的那样 ,Protege仍然会认为my:label和my:placeOfBirth是注释属性,除非您为它们添加了声明。 您实际上也可以在构造查询中执行此操作。 如果您只添加值块中的内容,这可以很简单,例如,
construct {
?entity a ?type ; ?myprop ?value .
?myprop a ?proptype
}
where {
values (?dbprop ?myprop ?proptype) {
(dbpprop:birthPlace my:placeOfBirth owl:ObjectProperty)
#-- ...
}
}
您也可以使用相同的方法添加语言过滤器。 请记住,您可以在值块中使用关键字undef ,并且可以使用unbound检入过滤器。 因此,作为最后一个示例,您可以执行以下操作:
prefix my: <http://example.org/vocab/>
construct {
?entity a ?mytype ; ?myprop ?value .
?mytype a owl:Class .
?myprop a ?proptype .
}
where {
values (?dbtype ?mytype) {
(dbpedia-owl:TennisPlayer my:PlayerOfTennis)
}
values (?dbprop ?myprop ?lang ?proptype) {
(dbpedia-owl:birthPlace my:placeOfBirth undef owl:ObjectProperty)
(rdfs:label my:label 'en' owl:AnnotationProperty)
}
?entity a ?dbtype ; ?dbprop ?value .
filter ( !bound(?lang) || langMatches(lang(?value),'en') )
}
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ns1: <http://example.org/vocab/> .
@prefix dbpedia: <http://dbpedia.org/resource/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
dbpedia:Maaike_Smit rdf:type ns1:PlayerOfTennis ;
ns1:label "Maaike Smit"@en ;
ns1:placeOfBirth dbpedia:Netherlands ,
dbpedia:Emmeloord .
dbpedia:Alexander_Kudryavtsev rdf:type ns1:PlayerOfTennis ;
ns1:label "Alexander Kudryavtsev"@en ;
ns1:placeOfBirth dbpedia:Yekaterinburg .
dbpedia:Alexander_Zverev rdf:type ns1:PlayerOfTennis ;
ns1:label "Alexander Zverev"@en ;
ns1:placeOfBirth dbpedia:Sochi .
dbpedia:Alina_Jidkova rdf:type ns1:PlayerOfTennis ;
ns1:label "Alina Jidkova"@en ;
ns1:placeOfBirth dbpedia:Soviet_Union ,
dbpedia:Moscow .
# ...
ns1:PlayerOfTennis rdf:type owl:Class .
ns1:label rdf:type owl:AnnotationProperty .
ns1:placeOfBirth rdf:type owl:ObjectProperty .
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
public class DBpediaOntologyMappingExample {
public static void main(String[] args) {
String query = "\n"
+ "prefix owl: <http://www.w3.org/2002/07/owl#>\n"
+ "prefix dbpedia-owl: <http://dbpedia.org/ontology/>\n"
+ "prefix my: <http://example.org/vocab/>\n"
+ "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "\n"
+ "construct {\n"
+ " ?entity a ?mytype ; ?myprop ?value .\n"
+ " ?mytype a owl:Class .\n"
+ " ?myprop a ?proptype .\n"
+ "}\n"
+ "where {\n"
+ " values (?dbtype ?mytype) {\n"
+ " (dbpedia-owl:TennisPlayer my:PlayerOfTennis)\n"
+ " }\n"
+ " values (?dbprop ?myprop ?lang ?proptype) {\n"
+ " (dbpedia-owl:birthPlace my:placeOfBirth undef owl:ObjectProperty)\n"
+ " (rdfs:label my:label 'en' owl:AnnotationProperty)\n"
+ " }\n"
+ " ?entity a ?dbtype ; ?dbprop ?value .\n"
+ " filter ( !bound(?lang) || langMatches(lang(?value),'en') )\n"
+ "}\n"
+ "limit 100";
String dbpedia = "http://dbpedia.org/sparql";
QueryExecution exec = QueryExecutionFactory.sparqlService( dbpedia, query );
RDFDataMgr.write( System.out, exec.execConstruct(), Lang.N3 );
}
}
图形在创建时不应以任何方式修改属性。 但是,当您自由查看图形时,它不知道如何解释属性,并且默认情况下将其称为注释。
您有两个选择。 在解释提取的三元组(以便蛋白质可以看到描述属性的三元组)或在构造查询中提取三元组时,请包括DBPedia的架构。
您似乎不需要定义dbp:bithPlace
属性以外的任何内容,因此最简单的解决方案是手动添加缺少的三元组:
final Model results = qe.execConstruct();
results.add(results.getResource("http://dbpedia.org/property/birthPlace"),
RDF.type, OWL.DatatypeProperty);
如果要使用许多属性,或者不知道要检索的属性,则应该直接包含模式(将它们加载到Jena模型中),或者运行经过修改的查询以获取有关属性的属性。
编辑
以下示例显示了仅在必要时将架构数据直接添加到结果中。
final Model schema = ModelFactory.createDefaultModel();
// TODO populate schema
// ...
final Model results = qe.execConstruct();
results.add(schema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.