基于RDF本体模型和图数据库实现知识查询与推理

基于RDF本体模型和图数据库实现知识查询与推理

  • 基于RDF本体模型和图数据库实现知识查询与推理
    • 一、案例本体模型解释
    • 二、数据构建与查询

Here’s the table of contents:

基于RDF本体模型和图数据库实现知识查询与推理

    本文主要使用ONgDB图数据库和Neosemantics组件,展示了一个快速接入RDF数据的案例,关于组件的使用可以查看Neosemantics-Docs。另外Neosemantics-1.x组件和ONgDB-1.x的适配,软件发布链接可以查看Neosemantics-Releases。

基于RDF本体模型和图数据库实现知识查询与推理_第1张图片

    Neosemantics是一个插件,可以在ONgDB中使用RDF。RDF是用于数据交换的W3C标准模型。Neosemantics以无损方式将RDF数据存储在ONgDB中(导入的RDF随后可以导出,而不会在此过程中丢失单个三元组)。也可以按需将属性图数据从 ONgDB 导出为 RDF。Neosemantics的其他功能包括模型映射和对ONgDB图的推理。

一、案例本体模型解释

    本次案例本体分为三个部分,自定义本体层【Custom Ontology】、领域本体层【Domain Ontology】、实例数据层【Instance Data】,数据是按照三层设计来构建的。其中自定义本体层是领域本体的自定义扩展。

  • 图片来源:QuickGraph#9 The fashion Knowledge Graph. Inferencing with Ontologies in Neo4j

基于RDF本体模型和图数据库实现知识查询与推理_第2张图片

    从下面的链接访问graphene.json文件可以加载该本体模型的设计到Graphene工具。

# 从graphene.json加载本体模型的设计
https://github.com/ongdb-contrib/neosemantics-python-examples/blob/master/inferencing/ontologies/graphene.json

基于RDF本体模型和图数据库实现知识查询与推理_第3张图片

二、数据构建与查询

    按照中的设计,现在可以进行数据的构建了,关于如何运行数据采集、数据构建、数据查询,可以参考neosemantics-python-examples。下面是完整运行的脚本【可以快速体验RDF和属性图结合的使用】:

// ongdb-enterprise-1.0.x\neosemantics-1.0.x
// Resource Index | 导入RDF时需要给`Resource`节点的`uri`创建索引
CREATE INDEX ON :Resource(uri);

// Index creation
// 加载数据时为节点创建索引可以提高检索性能
CREATE INDEX ON :Item(itemId);
CREATE INDEX ON :Department(deptName);
CREATE INDEX ON :Category(catName);
CREATE INDEX ON :Brand(brandName);

// Import Clothing Materials Ontology
// 导入服装材料本体
CALL semantics.importOntology("http://www.nsmntx.org/2019/10/clothingMaterials","Turtle", { keepLangTag: true, handleMultival: 'ARRAY'});

// Load data
// 加载数据
LOAD CSV WITH HEADERS FROM "file:///next_products.csv"  AS row
MERGE (b:Brand { brandName : row.brandName })
MERGE (dep:Department { deptName: row.itemDepartment })
MERGE (cat:Category { catName: row.itemCategory })
MERGE (i:Item { itemId: row.itemId }) ON CREATE set i.itemName = row.itemName, i.composition = row.itemComposition, i.url = row.url
MERGE (i)-[:IN_CAT]->(cat)
MERGE (i)-[:IN_DEPT]->(dep)
MERGE (i)-[:BY]->(b) ;

// Annotate your data with the ontology
// 将本体和数据实例连接
MATCH (c:Class) UNWIND c.label as langLabel
WITH collect( {key: toLower(semantics.getValue(langLabel)), classNode: c }) as termToClassMap
MATCH (i:Item)
FOREACH (material IN [x in termToClassMap where toLower(i.composition) contains x.key | x.classNode ] | MERGE (i)-[:CONTAINS]->(material)) ;

// Extend the ontology with custom categories
// 自定义扩展本体`AnimalBasedMaterial`
WITH '@prefix owl:  .
@prefix rdfs:  .
@prefix clmat:  .
@prefix ccat:  .

ccat:AnimalBasedMaterial
a owl:Class ;
rdfs:label "Animal-based material", "Materiales de origen animal"@es, "matière dorigine animale"@fr .

clmat:Leather
rdfs:subClassOf ccat:AnimalBasedMaterial .

clmat:Silk
rdfs:subClassOf ccat:AnimalBasedMaterial .

clmat:Wool
rdfs:subClassOf ccat:AnimalBasedMaterial .
'
AS onto
CALL semantics.importOntologySnippet(onto,"Turtle", { keepLangTag: true, handleMultival: 'ARRAY'}) YIELD terminationStatus, triplesLoaded
RETURN terminationStatus, triplesLoaded ;

// list synthetic materials  in different languages
// 列出不同语言的`合成材料`
UNWIND ['es','en','fr'] AS lang
MATCH (w:Class { name: 'SyntheticFibre'})<-[:SCO*]-(woolVariant)
RETURN lang, COLLECT(semantics.getLangValue(lang,woolVariant.label)) as syntheticMaterials;

// fleeces by Berghaus
// 使用贝格豪斯的羊毛制作的产品
MATCH (:Category { catName: "Fleeces"})<-[:IN_CAT]-(i:Item)-[:BY]->(:Brand { brandName: "Berghaus"})
RETURN i.itemId as id, i.itemName as name, i.url as url, i.composition as composition;

// Brands producing hoodies
// 哪些品牌生产连帽衫
MATCH (:Category { catName: "Hoodies"})<-[:IN_CAT]-(i:Item)-[:BY]->(b:Brand)
RETURN b.brandName as brand, count(i) as productCount ORDER BY productCount DESC LIMIT 5 ;

// All leather products (explicit and implicit)
// 查找所有皮革制品(显性使用皮革和隐性使用皮革)
MATCH (leather:Class { name: "Leather"})
CALL semantics.inference.nodesInCategory(leather, { inCatRel: "CONTAINS" }) yield node AS product
WITH product MATCH (product)-[:BY]->(b:Brand)
return product.itemName AS product, b.brandName AS brand, product.composition AS composition ;

// Vegan trainers
// 查找没有使用动物材料的产品
MATCH (:Category {catName:"Trainers"})<-[:IN_CAT]-(item:Item)-[:BY]->(b:Brand), (ab:Class { name: "AnimalBasedMaterial"})
WHERE b.brandName IN ["Converse","New Balance","Nike","ASICS"]
AND NOT semantics.inference.inCategory(item,ab,{ inCatRel: "CONTAINS" })
RETURN item.url, item.itemName, item.composition ;

基于RDF本体模型和图数据库实现知识查询与推理_第4张图片

你可能感兴趣的:(ONgDB,图数据库,知识图谱,neo4j,ONgDB,图数据库,RDF,知识图谱)