Skip to content

bast-ai-build-owl

Version: 0.1.4 Module: bast_ai_buildowl Path: src/python/build_owl

Overview

Library for building OWL (Web Ontology Language) ontologies programmatically. Provides entity extraction, ontology construction, and knowledge graph generation capabilities.

Features

Entity Extraction

  • NLP-based entity recognition
  • Custom entity patterns
  • Entity deduplication
  • Relationship extraction

Ontology Building

  • Class hierarchy creation
  • Property definitions
  • Annotation support
  • OWL/RDF serialization

Knowledge Graph

  • Triple generation
  • Graph visualization
  • SPARQL query support
  • Import/export utilities

Dependencies

Internal Dependencies

Package Purpose
bast-ai-config Configuration management

External Dependencies

Uses base library dependencies (rdflib, spacy)

Usage Examples

Initialize OWL Builder

from bast_ai_buildowl import OWLBuilder

builder = OWLBuilder(
    namespace="http://example.org/ontology#",
    ontology_iri="http://example.org/myontology"
)

Add Classes

# Add class
builder.add_class("Person")
builder.add_class("Student", parent="Person")
builder.add_class("Professor", parent="Person")

# Add with annotations
builder.add_class(
    "Course",
    annotations={
        "label": "Academic Course",
        "description": "A course offered by the university"
    }
)

Add Properties

# Object property
builder.add_object_property(
    name="teaches",
    domain="Professor",
    range="Course"
)

# Data property
builder.add_data_property(
    name="hasName",
    domain="Person",
    range="string"
)

Add Individuals

builder.add_individual(
    name="john_doe",
    class_type="Student",
    properties={
        "hasName": "John Doe",
        "enrolledIn": "CS101"
    }
)

Entity Extraction API

from bast_ai_buildowl.bp import EntitiesExtractAPI

extractor = EntitiesExtractAPI(catname="mycat")

# Extract entities from text
entities = extractor.extract(
    text="John Smith is a professor at MIT teaching machine learning.",
    entity_types=["PERSON", "ORG", "TOPIC"]
)

for entity in entities:
    print(f"{entity['text']}: {entity['type']}")

Build from JSON

# Input JSON structure
data = {
    "classes": [
        {"name": "Animal"},
        {"name": "Dog", "parent": "Animal"},
        {"name": "Cat", "parent": "Animal"}
    ],
    "properties": [
        {"name": "hasOwner", "domain": "Animal", "range": "Person"}
    ],
    "individuals": [
        {"name": "fido", "type": "Dog", "hasOwner": "john"}
    ]
}

builder.build_from_json(data)

Export Ontology

# Export to OWL/RDF
builder.save("output/ontology.owl", format="xml")
builder.save("output/ontology.ttl", format="turtle")
builder.save("output/ontology.json-ld", format="json-ld")

# Get as string
owl_string = builder.serialize(format="xml")

Testing

Unit Tests (3 files)

  • test_add_entities.py - Entity addition tests
  • test_init_owl.py - Initialization tests
  • test_svc_api.py - Service API tests

Integration Tests (5 files)

  • entities_extract_api_test.py
  • entities_extract_example.py
  • install_spacy_model.py
  • test_model_installation.py
  • test_spacy_model.py

Running Tests

pants test src/python/build_owl:: -- -s --no-header

Configuration

# resources/config/{catname}.yaml
build_owl:
  namespace: "http://example.org/ontology#"
  default_language: "en"

  entity_extraction:
    model: "en_core_web_sm"
    confidence_threshold: 0.8

  export:
    default_format: "xml"
    include_annotations: true

Issues & Improvements

Current Issues

  1. Limited inference: No OWL reasoner integration
  2. Performance: Large ontologies can be slow
  3. Validation: No ontology validation

Suggested Improvements

  1. Add reasoner: Integrate HermiT or Pellet
  2. Streaming: Stream large ontologies
  3. Validation: Add SHACL validation
  4. Visualization: Add graph visualization
  5. Versioning: Track ontology versions

Changelog

v0.1.4 (Current)

  • Improved entity extraction
  • Better relationship detection

v0.1.3

  • Added JSON-LD export
  • Performance improvements