これは、RDF を操作するための API の 1 つを使用してプログラムで行うか、SPARQL などの RDF クエリ ツールを使用して行うことができます。どちらの場合も、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>
<!-- ... -->