您可以使用其中一种用于处理 RDF 的 API 以编程方式执行此操作,也可以使用 RDF 查询工具(例如 SPARQL)来执行此操作。在这两种情况下,您都需要下载DBpedia 本体(以 RDF/XML 序列化的 OWL 本体)。
使用 Jena API 遍历三元组
使用Jena,您可以将数据加载到Model中,然后使用listStatements选择具有rdfs:subClassOf
作为谓词的语句。请注意,null
它用作通配符。
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDFS;
public class IterateRDFSSubclassTriples {
public static void main(String[] args) {
final Model dbpedia = ModelFactory.createDefaultModel();
dbpedia.read( "/home/taylorj/Downloads/dbpedia_3.9.owl", "RDF/XML" );
final StmtIterator stmts = dbpedia.listStatements(null, RDFS.subClassOf, (RDFNode) null);
while ( stmts.hasNext() ) {
final Statement stmt = stmts.next();
System.out.println( stmt.getSubject() + " is a subclass of " + stmt.getObject() );
}
}
}
这会产生如下输出:
http://dbpedia.org/ontology/FilmFestival is a subclass of http://schema.org/Festival
http://dbpedia.org/ontology/Embryology is a subclass of http://dbpedia.org/ontology/AnatomicalStructure
http://dbpedia.org/ontology/Canal is a subclass of http://dbpedia.org/ontology/Stream
http://dbpedia.org/ontology/Fern is a subclass of http://dbpedia.org/ontology/Plant
http://dbpedia.org/ontology/Architect is a subclass of http://dbpedia.org/ontology/Person
…
使用 SPARQL
您可以运行以下 SPARQL 查询以从rdfs:subClassOf
三元组中提取所有子类和超类:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?subclass ?superclass where {
?subclass rdfs:subClassOf ?superclass
}
使用 Jena 的命令行sparql
工具,我们找到了 541 个这样的三元组:
$ sparql --query query.rq --data dbpedia_3.9.owl | head
-----------------------------------------------------------------------------------------------------------------------------------------------
| subclass | superclass |
===============================================================================================================================================
| <http://dbpedia.org/ontology/FilmFestival> | <http://schema.org/Festival> |
| <http://dbpedia.org/ontology/Embryology> | <http://dbpedia.org/ontology/AnatomicalStructure> |
| <http://dbpedia.org/ontology/Canal> | <http://dbpedia.org/ontology/Stream> |
| <http://dbpedia.org/ontology/Fern> | <http://dbpedia.org/ontology/Plant> |
| <http://dbpedia.org/ontology/Architect> | <http://dbpedia.org/ontology/Person> |
...
| <http://dbpedia.org/ontology/LegalCase> | <http://dbpedia.org/ontology/Case> |
| <http://dbpedia.org/ontology/Lymph> | <http://dbpedia.org/ontology/AnatomicalStructure> |
| <http://dbpedia.org/ontology/City> | <http://dbpedia.org/ontology/Settlement> |
-----------------------------------------------------------------------------------------------------------------------------------------------
如果您希望将此数据作为图表,则可以使用construct
查询,并且您将获得多种可用的输出格式:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
construct where {
?subclass rdfs:subClassOf ?superclass
}
$ sparql --query query.rq --data dbpedia_3.9.owl | head -20
@prefix : <http://dbpedia.org/ontology/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:PoloLeague rdfs:subClassOf :SportsLeague .
:RacingDriver rdfs:subClassOf :Athlete .
:ResearchProject rdfs:subClassOf :Project .
# ...
$ sparql -out NT --query query.rq --data dbpedia_3.9.owl | head -20
<http://dbpedia.org/ontology/PoloLeague> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://dbpedia.org/ontology/SportsLeague> .
<http://dbpedia.org/ontology/RacingDriver> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://dbpedia.org/ontology/Athlete> .
<http://dbpedia.org/ontology/ResearchProject> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://dbpedia.org/ontology/Project> .
<http://dbpedia.org/ontology/Song> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://dbpedia.org/ontology/MusicalWork> .
<http://dbpedia.org/ontology/NetballPlayer> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://dbpedia.org/ontology/Athlete> .
<http://dbpedia.org/ontology/Guitar> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://dbpedia.org/ontology/Instrument> .
$ sparql -out RDF/XML --query query.rq --data dbpedia_3.9.owl | head -28
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns="http://dbpedia.org/ontology/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdf:Description rdf:about="http://dbpedia.org/ontology/SpeedwayTeam">
<rdfs:subClassOf>
<rdf:Description rdf:about="http://dbpedia.org/ontology/SportsTeam">
<rdfs:subClassOf>
<rdf:Description rdf:about="http://dbpedia.org/ontology/Organisation">
<rdfs:subClassOf>
<rdf:Description rdf:about="http://dbpedia.org/ontology/Agent">
<rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</rdf:Description>
</rdfs:subClassOf>
</rdf:Description>
</rdfs:subClassOf>
</rdf:Description>
</rdfs:subClassOf>
</rdf:Description>
<rdf:Description rdf:about="http://dbpedia.org/ontology/NoteworthyPartOfBuilding">
<rdfs:subClassOf>
<rdf:Description rdf:about="http://dbpedia.org/ontology/ArchitecturalStructure">
<rdfs:subClassOf>
<rdf:Description rdf:about="http://dbpedia.org/ontology/Place">
<rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</rdf:Description>
<!-- ... -->