[英]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.