【问题标题】:Importing DBpedia's class hierarchy导入 DBpedia 的类层次结构
【发布时间】:2013-10-18 15:12:44
【问题描述】:

我正在使用 DBPedia 信息框属性和信息框类型从 DBPedia 导入数据。但是我仍然错过了类之间的关系。如何以相对简单的方式有效地检索类型层次结构?我考虑过将 DBpedia 类型映射到 Yago 类型,然后使用

检索 yago 类型层次结构

有没有更简单或更直接的方法来解决这个问题?

【问题讨论】:

  • 你只想要the ontology吗?其中包含类之间的rdfs:subClassOf 关系。
  • 是的 - 你有关于如何提取数据的建议 - 特别是我只关心 Java / Scala 中的 subClassof 关系吗?
  • 如果您使用的是 Java,可以使用一些库。耶拿和芝麻是常用的。您可以运行一个 SPARQL 查询,使用 rdfs:subClass 查询三元组(并且您可能会在查询时查询具有rdf:type rdfs:Classall 事物)。您还可以通过 Jena 的模型和 listStatements 方法以编程方式获得这些结果,但 SPARQL 查询可能是最简单的方法。

标签: rdf owl dbpedia


【解决方案1】:

您可以使用其中一种用于处理 RDF 的 API 以编程方式执行此操作,也可以使用 SPARQL 等 RDF 查询工具来执行此操作。在这两种情况下,您都需要下载DBpedia ontology(以 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>
  <!-- ... -->

【讨论】:

  • 你是如何以编程方式做到的? :P
  • @StefanKunze 你到底想要怎样的结果?您是否正在寻找仅包含这些三元组的模型?还是一种遍历它们的方法?还是别的什么……?
  • 我想遍历那个本体文件,所以只处理子类关系。
  • @StefanKunze 好的,使用起来并不难,例如 Jena。我现在必须运行,但我应该能够在几个小时内发布代码示例。
  • @StefanKunze 我已经更新了我的答案,以展示您如何使用 Jena 以编程方式执行此操作。有很多 RDF API,Jena 只是其中之一。但是,大多数都会让您做类似的事情;即:将数据加载到模型中,从模型中选择某种形式的三元组,对这些三元组做一些事情。
猜你喜欢
  • 2016-03-26
  • 2012-09-30
  • 1970-01-01
  • 2011-08-03
  • 2018-09-25
  • 2011-05-19
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多