简体   繁体   English

从Dbpedia制作自定义本体/ RDF图

[英]Crafting custom ontology/RDF graph from Dbpedia

I have used Construct Sparql query to extract an RDF graph from dbpedia. 我已使用Construct Sparql查询从dbpedia提取RDF图。 Now the problem is that the extracted graph stores the properties (eg birthplace) associated with the individuals (eg Maria Sharapova) as annotations to the individual. 现在的问题是,提取的图存储与个人(例如Maria Sharapova)相关联的属性(例如出生地)作为对个人的注释。 How can I specify properties while constructing the graph? 构造图形时如何指定属性? My code: 我的代码:

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");

The way I've typically done this in the past (eg, converting an RDFS vocabulary to SKOS in Is there a way to convert the data format of an RDF vocabulary to SKOS ) is to use one or more values blocks to specify the each DBpedia type or property and its corresponding type or property in my own ontology. 我过去通常这样做的方式(例如,是否将RDFS词汇表的数据格式转换为SKOS中的RDFS词汇表转换为SKOS )是使用一个或多个块来指定每个DBpedia我自己的本体中的类型或属性及其对应的类型或属性。 For instance, a construct query like: 例如,类似以下的构造查询:

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 .
}

This grabs each entity of type ?dbtype and each of its values for ?dbprop , and constructs a graph with a corresponding entity of type ?mytype with the same value for the proeprty ?myprop . 这将捕获每个类型为?dbtype的实体及其每个值?dbprop ,并构造一个图,该图具有类型为?mytype的对应实体, 具有相同的属性?myprop As Rob Hall pointed out , Protege will still think that my:label and my:placeOfBirth are annotation properties unless you include declarations for them. 正如Rob Hall指出的那样 ,Protege仍然会认为my:labelmy:placeOfBirth是注释属性,除非您为它们添加了声明。 You can actually do that, too in the construct query. 您实际上也可以在构造查询中执行此操作。 If you just add to what you have in your values block, this can be as easy as, eg, 如果您只添加块中的内容,这可以很简单,例如,

construct {
  ?entity a ?type ; ?myprop ?value .
  ?myprop a ?proptype
}
where {
  values (?dbprop ?myprop ?proptype) {
    (dbpprop:birthPlace my:placeOfBirth owl:ObjectProperty)
    #-- ... 
  }
}

You can use the same approach to add in language filters, too. 您也可以使用相同的方法添加语言过滤器。 Just remember that you can use the keyword undef in values blocks, and that you can check in filters with unbound . 请记住,您可以在块中使用关键字undef ,并且可以使用unbound检入过滤器。 Thus, as the final example, you can do: 因此,作为最后一个示例,您可以执行以下操作:

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') )
}

SPARQL results SPARQL结果

@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 .

With Jena in Java 在Java中使用Jena

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 );
    }
}

The graph shouldn't modify the properties in any way when created. 图形在创建时不应以任何方式修改属性。 When you view the graph in protege, however, it has no knowledge how to interpret the properties, and calls them annotations by default. 但是,当您自由查看图形时,它不知道如何解释属性,并且默认情况下将其称为注释。

You have two options. 您有两个选择。 Include a schema for DBPedia when interpreting the triples you extract (so protege can see triples describing the properties) or extract triples along with your construct query. 在解释提取的三元组(以便蛋白质可以看到描述属性的三元组)或在构造查询中提取三元组时,请包括DBPedia的架构。

You don't seem to require definitions for anything beyond the dbp:bithPlace property, so the most trivial solution is to just add the missing triple manually: 您似乎不需要定义dbp:bithPlace属性以外的任何内容,因此最简单的解决方案是手动添加缺少的三元组:

final Model results =  qe.execConstruct();
results.add(results.getResource("http://dbpedia.org/property/birthPlace"),
            RDF.type, OWL.DatatypeProperty);

If you were to use many properties, or if you didn't know what properties you were going to retrieve, then you should either include the schemas directly (loading them into Jena models) or run a modified query that can pull properties about the properties. 如果要使用许多属性,或者不知道要检索的属性,则应该直接包含模式(将它们加载到Jena模型中),或者运行经过修改的查询以获取有关属性的属性。

EDIT 编辑

The following example shows just adding schema data directly to the results when necessary. 以下示例显示了仅在必要时将架构数据直接添加到结果中。

final Model schema = ModelFactory.createDefaultModel();
// TODO populate schema
// ...
final Model results = qe.execConstruct();
results.add(schema);

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

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