← 返回 Semantica 专题首页 🚀 SEMANTICA · COOKBOOK · 进阶系列

语义层构建

Semantica 官方 Cookbook 中文翻译 · 第 28 / 37 篇

📦 semantica 🕸️ 知识图谱 🔎 GraphRAG

1概览

构建企业级语义层:构建知识图谱、生成本体、创建语义层、导出 RDF,并存储到三元组存储中。

文档API 参考

2安装

从 PyPI 安装 Semantica:

pip install semantica
# 或安装所有可选依赖:
pip install semantica[all]

3工作流:构建 KG → 生成本体 → 创建语义层 → 导出 RDF

# 安装并升级 Semantica
!pip install -qU semantica
# 导入语义层构建所需的组件
from semantica.kg import GraphBuilder
from semantica.ontology import OntologyGenerator
from semantica.export import RDFExporter
from semantica.triplet_store import TripletStore

4步骤 1:构建知识图谱

# 步骤 1:构建知识图谱
builder = GraphBuilder()

# 定义实体(人员、组织、项目)
entities = [
    {"id": "e1", "type": "Person", "name": "Alice", "properties": {"age": 30, "role": "Engineer"}},
    {"id": "e2", "type": "Person", "name": "Bob", "properties": {"age": 35, "role": "Manager"}},
    {"id": "e3", "type": "Organization", "name": "Tech Corp", "properties": {"founded": 2010}},
    {"id": "e4", "type": "Project", "name": "Project Alpha", "properties": {"status": "active"}},
]

# 定义实体间的关系
relationships = [
    {"source": "e1", "target": "e2", "type": "reports_to"},
    {"source": "e1", "target": "e3", "type": "works_for"},
    {"source": "e2", "target": "e3", "type": "works_for"},
    {"source": "e1", "target": "e4", "type": "works_on"},
]

# 构建知识图谱对象
knowledge_graph = builder.build(entities, relationships)

5步骤 2:生成本体

# 步骤 2:从知识图谱生成本体
generator = OntologyGenerator()
ontology = generator.generate_from_graph(knowledge_graph)

6步骤 3:创建语义层

# 步骤 3:创建语义层——将图实体/关系映射到本体类/属性
def create_mappings(kg, ontology):
    mappings = {
        "entity_type_mappings": {},
        "relationship_type_mappings": {},
        "property_mappings": {}
    }

    # 将实体类型映射到本体类 URI
    entity_types = set(e.get("type") for e in entities)
    ontology_classes = ontology.get("classes", [])

    for entity_type in entity_types:
        matching_class = next((cls for cls in ontology_classes if cls.get("name") == entity_type), None)
        if matching_class:
            mappings["entity_type_mappings"][entity_type] = matching_class.get("uri", entity_type)

    # 将关系类型映射到本体属性 URI
    relationship_types = set(r.get("type") for r in relationships)
    ontology_properties = ontology.get("properties", [])

    for rel_type in relationship_types:
        matching_prop = next((prop for prop in ontology_properties if prop.get("name") == rel_type), None)
        if matching_prop:
            mappings["relationship_type_mappings"][rel_type] = matching_prop.get("uri", rel_type)

    return mappings

# 生成映射并组装语义层对象
mappings = create_mappings(knowledge_graph, ontology)

semantic_layer = {
    "graph": knowledge_graph,
    "ontology": ontology,
    "mappings": mappings,
    "metadata": {
        "version": "1.0",
        "created_at": "2024-01-01",
        "description": "Enterprise semantic layer"
    }
}

7步骤 4:导出 RDF

exporter = RDFExporter()
# 导出知识图谱
exporter.export(knowledge_graph, "knowledge_graph.ttl", format="turtle")
print("Exported knowledge graph to knowledge_graph.ttl")

8小结

企业级语义层构建: - 知识图谱已构建 - 本体已生成 - 已创建带映射的语义层 - RDF 导出已完成