mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
8.4 KiB
8.4 KiB
In [ ]:
# !pip install semantica[all]
from semantica.utils.logging import get_logger
logger = get_logger("unstructured_guide")
print("Environment setup complete.")In [ ]:
text_corpus = """
QuantumDynamics is a leading AI research lab founded by Dr. Elena Rostova in 2018.
The lab is headquartered in Zurich, Switzerland, and focuses on quantum computing algorithms.
Dr. Rostova serves as the Chief Scientist.
The lab has released products like the Q-1 Processor and the NeuralBridge SDK.
QuantumDynamics collaborates with major universities such as MIT and ETH Zurich.
"""In [ ]:
from semantica.semantic_extract import NERExtractor, RelationExtractor
from semantica.ontology import OntologyGenerator, OntologyOptimizer
# 1. Initialize Extractors
ner = NERExtractor()
re = RelationExtractor()
# 2. Extract Entities
print("Extracting entities...")
entities = ner.extract(text_corpus)
print(f"Found {len(entities)} entities: {[e['text'] for e in entities]}")
# 3. Extract Relationships
print("Extracting relationships...")
relationships = re.extract(text_corpus, entities)
for r in relationships:
print(f" - {r['source']} -> {r['type']} -> {r['target']}")
# 4. Generate Structure
generator = OntologyGenerator()
nlp_ontology = generator.generate_ontology({"entities": entities, "relationships": relationships}, name="QuantumOntologyNLP")
# 5. Optimize (Clean up)
optimizer = OntologyOptimizer()
nlp_ontology = optimizer.optimize_ontology(nlp_ontology, remove_redundancy=True)
print(f"\nGenerated NLP Ontology with {len(nlp_ontology['classes'])} classes and {len(nlp_ontology['properties'])} properties.")In [ ]:
from semantica.ontology import LLMOntologyGenerator
try:
# Initialize LLM Generator (ensure OPENAI_API_KEY is set in env)
llm_gen = LLMOntologyGenerator(provider="openai", model="gpt-4")
print("Generating ontology with LLM...")
llm_ontology = llm_gen.generate_ontology_from_text(
text=text_corpus,
name="QuantumOntologyLLM"
)
print(f"Generated LLM Ontology with {len(llm_ontology['classes'])} classes and {len(llm_ontology['properties'])} properties.")
print("Classes detected:", [c['name'] for c in llm_ontology['classes']])
except Exception as e:
print(f"Skipping LLM generation: {e}")
llm_ontology = NoneIn [ ]:
from semantica.visualization import OntologyVisualizer
visualizer = OntologyVisualizer()
print("--- NLP Approach Visualization ---")
fig_nlp = visualizer.visualize_structure(nlp_ontology, output="interactive")
if fig_nlp: fig_nlp.show()
if llm_ontology:
print("--- LLM Approach Visualization ---")
fig_llm = visualizer.visualize_structure(llm_ontology, output="interactive")
if fig_llm: fig_llm.show()In [ ]:
from semantica.export import OWLExporter
exporter = OWLExporter()
# Export the NLP ontology by default, or the LLM one if preferred
target_ontology = llm_ontology if llm_ontology else nlp_ontology
output_file = "quantum_ontology.ttl"
exporter.export(target_ontology, output_file, format="turtle")
print(f"Successfully exported ontology to {output_file}")