我应该如何为自定义数据集构建知识图?

数据挖掘 机器学习 Python 神经网络 知识库
2022-03-11 14:21:33

我是机器学习的新手,我正在尝试为类似于 Google 的类项目的搜索目的创建一个小型知识图。

好的,所以我几天来一直在搜索这个主题,这是我从网络和研究论文中发现的。

  1. 创建 RDF 三元组或使用已经存在的数据库,如 Freebase、Wikidata 等。
  2. 然后使用 ComplEx、TransE 等算法训练模型。
  3. 最后将其用于查询。

我的问题是我不想使用已经存在的数据库。我有一套文件。有没有什么好的库可以从自定义数据中制作三元组?

同样在训练我的模型之后,我应该使用哪个数据库来存储我的模型以及如何查询它以获取答案。

1个回答

您也可以使用RML从现有数据源构建知识图 RML 代表 RDF 映射语言,允许您将异构数据源转换为 RDF 并扩展R2RML我创建了一个使用 JSON 数据作为数据源的示例,但也可以使用其他格式,例如 CSV、XML、关系数据库等。

RML(RDF 映射语言)

将现有数据源转换为 RDF 遵循 RML:

  1. 编写 RML 映射规则,指示 RML 处理器如何将数据转换为 RDF。您可以在RML 文档中找到许多如何为数据源编写映射规则的示例。

RML 规则由Triples Maps组成, 它们本身又包含以下部分:

逻辑源

rml:logicalSource [
    rml:source "people.json" ;
    rml:referenceFormulation ql:JSONPath ;
    rml:iterator "$.people.[*]" ; 
] ;

people.json使用定义为的 JSONPath 表达式访问 数据源$.people.[*]该表达式允许 RML 处理器迭代 JSON 数据。

主题图

rr:subjectMap [
    rr:template "http://ex.com/Person/{firstname}_{lastname}" ;
    rr:class foaf:Person ; 
] ;

此 SubjectMap 创建的每个主题都将看起来像 http://ex.com/Person/{firstname}_{lastname}wherefirstnamelastname 在 RML 处理器执行期间被相应的 JSON 值替换。科目有班foaf:Person

谓词-对象映射

rr:predicateObjectMap [
        rr:predicate foaf:givenName ;
        rr:objectMap [ 
            rml:reference "firstname" ; 
        ] 
    ] ;

此映射生成一个谓词,对象将在映射过程中foaf:givenName接收 JSON 值。firstname

  1. 使用 RML 处理器执行您的映射规则。RML 处理器的一个示例是 RML MapperRML Streamer也可以使用其他 RML 处理器,只要它们符合 RML 规范

RML 处理器将根据前面显示的映射规则从 JSON 数据生成以下三元组:

<http://ex.com/Person/John_Doe> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>.
<http://ex.com/Person/John_Doe> <http://xmlns.com/foaf/0.1/givenName> "John".
<http://ex.com/Person/John_Doe> <http://xmlns.com/foaf/0.1/familyName> "Doe".
<http://ex.com/Person/Jane_Smith> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>.
<http://ex.com/Person/Jane_Smith> <http://xmlns.com/foaf/0.1/givenName> "Jane".
<http://ex.com/Person/Jane_Smith> <http://xmlns.com/foaf/0.1/familyName> "Smith".
<http://ex.com/Person/Sarah_Bladinck> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>.
<http://ex.com/Person/Sarah_Bladinck> <http://xmlns.com/foaf/0.1/givenName> "Sarah".
<http://ex.com/Person/Sarah_Bladinck> <http://xmlns.com/foaf/0.1/familyName> "Bladinck".

完整示例

我创建了一个小演示来创建一个带有名字和姓氏的 FOAF 人:

RML 映射规则

@base <http://example.com> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<#PersonMapping>
    a rr:TriplesMap ;
    rml:logicalSource [
        rml:source "people.json" ;
        rml:referenceFormulation ql:JSONPath ;
        rml:iterator "$.people.[*]" ; 
    ] ;

    rr:subjectMap [
        rr:template "http://ex.com/Person/{firstname}_{lastname}" ;
        rr:class foaf:Person ; 
    ] ;

    rr:predicateObjectMap [
        rr:predicate foaf:givenName ;
        rr:objectMap [ 
            rml:reference "firstname" ; 
        ] 
    ] ;

    rr:predicateObjectMap [
        rr:predicate foaf:familyName ;
        rr:objectMap [ 
            rml:reference "lastname" ; 
        ] 
    ] .

JSON数据

{
    "people": [
        {
            "firstname": "John",
            "lastname": "Doe"
        },
        {
            "firstname": "Jane",
            "lastname": "Smith"
        },
        {
            "firstname": "Sarah",
            "lastname": "Bladinck"
        }
    ]
}

注意:我为 RML 及其技术做出了贡献。