What Is Semantic Extraction in Semantica? A Deep Dive into Knowledge Graph Construction
Semantic extraction is the core capability of the Semantica library that transforms unstructured natural language into structured knowledge representations including entities, relations, events, and RDF triplets.
Semantic extraction in Semantica operates through a modular pipeline architecture that converts raw text into queryable knowledge graphs. Developed by the semantica-agi/semantica project, this system combines traditional NLP techniques with modern LLM capabilities through an extensible provider interface. The implementation emphasizes lazy loading and runtime method dispatch to minimize dependencies while maximizing flexibility for production deployments.
Core Components of Semantic Extraction
The semantic extraction engine organizes functionality into distinct layers within the semantica/semantic_extract/ package. Each layer abstracts specific concerns while exposing clean public APIs.
Public API Layer
The entry point resides in semantica/semantic_extract/__init__.py, which implements lazy exports via __getattr__ to defer heavy imports. Users interact with high-level classes such as NamedEntityRecognizer, RelationExtractor, TripletExtractor, and SemanticNetworkExtractor without loading spaCy or transformers until actually needed.
Method Dispatch and Registry
semantica/semantic_extract/methods.py houses the extraction function registry, implementing pattern-based, rule-based, ML-based, HuggingFace, and LLM extraction strategies. The get_*_method helpers select the appropriate technique at runtime based on configuration strings like ["llm", "pattern"].
Provider Abstraction
semantica/semantic_extract/providers.py encapsulates concrete LLM back-ends including OpenAI, Gemini, and Anthropic. The BaseProvider interface handles credential-safe loading and request formatting, allowing seamless swapping of underlying models without changing extraction logic.
Core Extraction Engines
The heavy lifting occurs in specialized modules:
ner_extractor.py– Implements spaCy-based entity detection with fallback pattern matchingrelation_extractor.py– Detects relationships via co-occurrence, dependency parsing, and semantic patternsevent_detector.py– Extracts temporal markers and participant rolestriplet_extractor.py– Converts entities and relations into validated RDF triples with serialization support
Semantic Analysis and Network Building
semantica/semantic_extract/semantic_analyzer.py performs role labeling and similarity clustering, while semantica/semantic_extract/semantic_network_extractor.py assembles primitives into graph structures using SemanticNode and SemanticEdge objects defined in semantica/semantic_extract/types.py.
The Semantic Extraction Pipeline
The canonical workflow processes text through eight distinct stages, each corresponding to specific classes in the codebase:
- Named Entity Recognition (
NamedEntityRecognizer) generatesEntityobjects from raw text - Relation Extraction (
RelationExtractor) identifiesRelationinstances linking detected entities - Event Detection (
EventDetector) captures temporal contexts and event participants - Coreference Resolution merges pronouns and repeated mentions across the document
- Triplet Extraction (
TripletExtractor) emits validatedTripletobjects (subject-predicate-object) - Semantic Analysis (
SemanticAnalyzer) computes similarity scores and assigns semantic roles - Network Construction (
SemanticNetworkExtractor) builds aSemanticNetworkgraph instance - Export serializes to GraphML, Neo4j, or other graph formats
Extensibility and Configuration
The architecture supports runtime extension through two primary registries defined in semantica/semantic_extract/registry.py.
Method Registry allows registration of custom extraction functions without modifying core code. New techniques integrate via the MethodRegistry class and become available to all extractor classes immediately.
Provider Registry accepts new LLM implementations by subclassing BaseProvider. Registered providers automatically appear in configuration options across all LLM-dependent extractors.
Config-Driven Fallback Chains enable resilient extraction by specifying method arrays. When TripletExtractor receives method=["llm", "pattern"], it attempts LLM extraction first, automatically falling back to pattern matching if the primary method fails or returns low confidence.
Quality Assurance Mechanisms
semantica/semantic_extract/extraction_validator.py implements TripletValidator and ExtractionValidator classes that enforce schema compliance and confidence thresholds. semantica/semantic_extract/cache.py provides TTL-based caching for LLM calls through the ExtractionCache class, preventing redundant API requests during batch operations. Progress tracking integrates with the pipeline for real-time feedback on long-running extraction jobs.
Practical Implementation Examples
Basic Entity and Relation Extraction
This example demonstrates the fundamental extraction workflow using pattern-based fallback:
from semantica.semantic_extract import (
NamedEntityRecognizer,
RelationExtractor,
TripletExtractor,
)
text = "Ada Lovelace wrote the first algorithm for Charles Babbage's analytical engine in 1843."
# Named Entity Recognition
ner = NamedEntityRecognizer(confidence_threshold=0.75)
entities = ner.extract_entities(text)
# Relation Extraction
rel_extractor = RelationExtractor(confidence_threshold=0.6)
relations = rel_extractor.extract_relations(text, entities=entities)
# Triplet Extraction with LLM fallback to pattern matching
triplet_extractor = TripletExtractor(method=["llm", "pattern"])
triplets = triplet_extractor.extract_triplets(
text,
entities=entities,
relations=relations
)
print(triplets) # List[Triplet] with subject, predicate, object fields
Batch Processing with Provenance
For production workloads, extract provenance metadata alongside triplets:
from semantica.semantic_extract import TripletExtractor
documents = [
{"id": "doc1", "text": "Tesla unveiled the Model S in 2012."},
{"id": "doc2", "text": "SpaceX launched the Falcon Heavy in 2018."},
]
extractor = TripletExtractor(
method="llm",
provider="openai",
llm_model="gpt-4",
include_provenance=True,
)
batch_triplets = extractor.extract(
[doc["text"] for doc in documents],
pipeline_id="batch_001",
)
# Access source sentences via Triplet.metadata
for doc_idx, trips in enumerate(batch_triplets):
print(f"Document {documents[doc_idx]['id']}:")
for t in trips:
source = t.metadata.get('sentence')
print(f" {t.subject} –{t.predicate}→ {t.object} (src: {source})")
Building Semantic Networks
Construct knowledge graphs directly from extracted primitives:
from semantica.semantic_extract import SemanticNetworkExtractor
network_extractor = SemanticNetworkExtractor()
network = network_extractor.build_network(
text="Google acquired Fitbit for $2.1B in 2020.",
entities=entities,
relations=relations,
)
# Export to standard graph formats
network.export(format="graphml", path="knowledge.graphml")
Summary
- Semantic extraction transforms unstructured text into structured knowledge primitives (entities, relations, events, triplets) within the semantica-agi/semantica framework
- The architecture implements lazy loading via
semantica/semantic_extract/__init__.pyto minimize import overhead and dependency conflicts - Method dispatch in
methods.pysupports hybrid extraction strategies combining LLMs, patterns, and rule-based approaches with automatic fallback chains - Provider abstraction in
providers.pyenables seamless integration of OpenAI, Gemini, Anthropic, and custom LLM services - The pipeline progresses through NER, relation extraction, coreference resolution, and triplet generation before final network construction in
semantic_network_extractor.py - Quality assurance includes schema validation, confidence scoring, and TTL-based caching to optimize API costs
Frequently Asked Questions
What is the difference between semantic extraction and NER in Semantica?
Named Entity Recognition (NER) represents only the initial stage of semantic extraction. While NamedEntityRecognizer in ner_extractor.py produces isolated Entity objects, the full semantic extraction pipeline incorporates RelationExtractor for identifying connections, TripletExtractor for generating RDF-compliant subject-predicate-object structures, and SemanticNetworkExtractor for building traversable knowledge graphs. NER provides the raw nodes; semantic extraction constructs the complete graph topology.
How does Semantica handle extraction method failures?
The framework implements automatic fallback through config-driven method chains. When instantiating extractors like TripletExtractor, users pass ordered method lists such as ["llm", "pattern", "rule"]. If the LLM provider returns errors or sub-threshold confidence scores, the dispatcher in methods.py automatically attempts the next method in sequence. This resilience pattern ensures extraction continuity even when external API services encounter downtime.
Can I use custom LLM providers with Semantica's semantic extraction?
Yes, the provider system supports custom implementations through semantica/semantic_extract/providers.py. Developers subclass BaseProvider to implement authentication, request formatting, and response parsing for proprietary or self-hosted models. After registering the new provider via the provider registry, it becomes available to all extraction classes through the standard provider parameter without requiring changes to extraction logic.
What output formats does semantic extraction in Semantica support?
The SemanticNetwork class generated by SemanticNetworkExtractor exports to multiple graph serialization formats including GraphML for visualization tools, Neo4j-compatible CSV for graph databases, and JSON-LD for linked data applications. Individual extractors like TripletExtractor return native Python dataclasses (Triplet, Entity, Relation) defined in types.py, enabling programmatic access to structured fields while supporting custom serialization pipelines.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →