mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
9.7 KiB
9.7 KiB
In [ ]:
!pip install -qU semantica
In [ ]:
from semantica.kg import GraphBuilder
from semantica.reasoning import Reasoner, ExplanationGenerator
In [ ]:
builder = GraphBuilder()
entities = [
{"id": "alice", "type": "Person", "name": "Alice"},
{"id": "bob", "type": "Person", "name": "Bob"},
{"id": "charlie", "type": "Person", "name": "Charlie"},
{"id": "sf", "type": "Location", "name": "San Francisco"},
{"id": "california", "type": "Location", "name": "California"},
]
relationships = [
{"source": "alice", "target": "bob", "type": "parent_of"},
{"source": "bob", "target": "charlie", "type": "parent_of"},
{"source": "sf", "target": "california", "type": "located_in"},
{"source": "alice", "target": "sf", "type": "lives_in"},
]
knowledge_graph = builder.build([{"entities": entities, "relationships": relationships}])
In [ ]:
# Initialize Reasoner
reasoner = Reasoner()
# Define rules using logic syntax
rules = [
"IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)",
"IF lives_in(?x, ?y) AND located_in(?y, ?z) THEN lives_in(?x, ?z)"
]
for rule in rules:
reasoner.add_rule(rule)
In [ ]:
# Perform forward chaining to derive new facts
# The Reasoner can infer facts directly from the knowledge graph or a list of facts
inferred_facts = reasoner.infer_facts(knowledge_graph)
print(f"Inferred {len(inferred_facts)} new facts:")
for fact in inferred_facts:
print(f" - {fact}")
In [ ]:
# Define a goal to prove
goal = "grandparent_of(alice, charlie)"
# Perform backward chaining
proof = reasoner.backward_chain(goal)
if proof:
print(f"Goal '{goal}' proven successfully!")
else:
print(f"Could not prove goal '{goal}'.")
In [ ]:
generator = ExplanationGenerator()
# If we have a proof from backward chaining, explain it
if proof:
proof_explanation = generator.generate_explanation(proof)
print("Explanation for backward chaining proof:")
print(proof_explanation.natural_language)
In [ ]:
from semantica.kg import GraphBuilder
from semantica.reasoning import Reasoner, ExplanationGenerator
builder = GraphBuilder()
reasoner = Reasoner()
explainer = ExplanationGenerator()
In [ ]:
entities = [
{"id": "alice", "type": "Person", "name": "Alice"},
{"id": "bob", "type": "Person", "name": "Bob"},
{"id": "charlie", "type": "Person", "name": "Charlie"},
{"id": "sf", "type": "Location", "name": "San Francisco"},
{"id": "california", "type": "Location", "name": "California"}
]
relationships = [
{"source": "alice", "target": "bob", "type": "parent_of"},
{"source": "bob", "target": "charlie", "type": "parent_of"},
{"source": "sf", "target": "california", "type": "located_in"},
{"source": "alice", "target": "sf", "type": "lives_in"}
]
knowledge_graph = builder.build([{"entities": entities, "relationships": relationships}])
print(len(knowledge_graph.get("entities", [])))
print(len(knowledge_graph.get("relationships", [])))
In [ ]:
rules = [
"IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)",
"IF lives_in(?x, ?y) AND located_in(?y, ?z) THEN lives_in(?x, ?z)"
]
for r in rules:
reasoner.add_rule(r)
In [ ]:
for rel in relationships:
fact = f"{rel['type']}({rel['source']}, {rel['target']})"
reasoner.add_fact(fact)
derived = reasoner.forward_chain()
print(len(derived))
for d in derived:
print(d.conclusion)
In [ ]:
goals = [
"grandparent_of(alice, charlie)",
"lives_in(alice, california)"
]
for g in goals:
proof = reasoner.backward_chain(g)
print(g)
print(bool(proof))
In [ ]:
if derived:
exp = explainer.generate_explanation(derived[0])
print(exp.natural_language)
goal = "grandparent_of(alice, charlie)"
proof = reasoner.backward_chain(goal)
if proof:
pexp = explainer.generate_explanation(proof)
print(pexp.natural_language)