Files
semantica/cookbook/advanced/12_Unstructured_to_Ontology.ipynb
T
KaifAhmad1 7c5c9d9117 docs: organize cookbook notebooks and add Colab integration
- Add numbering to all notebooks for better sorting
  - Introduction: 01-19
  - Advanced: 01-12
  - Use cases: numbered within each category
- Add Google Colab badges to all 72 notebooks
- Clean up Welcome notebook with proper code cells
- Remove unnecessary print statements and verbose content
2025-12-03 13:00:36 +05:30

4.6 KiB

Open In Colab

Unstructured to Ontology

Overview

Transform unstructured text into a formal ontology: extract concepts, generate ontology, validate, and export to OWL.

Documentation: API Reference

Workflow: Unstructured Text → Extract Concepts → Generate Ontology → Validate → Export OWL

In [ ]:
from semantica.semantic_extract import NERExtractor, RelationExtractor
from semantica.ontology import OntologyGenerator, OntologyValidator
from semantica.export import OWLExporter

Step 1: Extract Concepts from Unstructured Text

In [ ]:
unstructured_text = """
Apple Inc. is a technology company founded by Steve Jobs in 1976. 
The company is headquartered in Cupertino, California. 
Tim Cook is the current CEO of Apple. 
Apple develops products like iPhone, iPad, and MacBook.
The company has offices in multiple countries including the United States, China, and Japan.
"""

extractor = NERExtractor()
entities = extractor.extract(unstructured_text)

relation_extractor = RelationExtractor()
relationships = relation_extractor.extract(unstructured_text, entities)

for entity in entities[:5]:
    print(f"{entity.get('text', entity)} ({entity.get('type', 'Unknown')})")

Step 2: Generate Ontology

In [ ]:
generator = OntologyGenerator()
ontology = generator.generate(entities, relationships)

if ontology.get('classes'):
    for cls in ontology.get('classes', [])[:5]:
        print(f"{cls.get('name', cls)}")

Step 3: Validate Ontology

In [ ]:
validator = OntologyValidator()
validation_result = validator.validate_ontology(ontology)

print(f"Valid: {validation_result.valid}")
print(f"Consistent: {validation_result.consistent}")
print(f"Errors: {len(validation_result.errors)}")
print(f"Warnings: {len(validation_result.warnings)}")

if validation_result.errors:
    for error in validation_result.errors:
        print(f"  - {error}")

if validation_result.warnings:
    for warning in validation_result.warnings:
        print(f"  - {warning}")

if validation_result.metrics:
    print(f"  Classes: {validation_result.metrics.get('class_count', 0)}")
    print(f"  Properties: {validation_result.metrics.get('property_count', 0)}")
    print(f"  Object Properties: {validation_result.metrics.get('object_property_count', 0)}")
    print(f"  Data Properties: {validation_result.metrics.get('data_property_count', 0)}")
    print(f"  Hierarchy Depth: {validation_result.metrics.get('hierarchy_depth', 0)}")

Step 4: Export to OWL

In [ ]:
exporter = OWLExporter()
exporter.export(ontology, "output.owl")

Summary

Unstructured to ontology transformation:

  • Concepts Extracted from Text
  • Ontology Generated
  • Ontology Validated
  • OWL Export Completed