Extend Graph Schema¶
In this notebook, we'll demonstrate how the graph schema extends to include new triple patterns even if they do not conform to the existing schema.
Import Resources¶
from whyhow import WhyHow, Triple, Node, Relation
import os
import json
from random import choice
Generate Triples¶
In this simple example, we'll randomly generate a set of triples which represent a list of students and the subjects they study in school.
person_names = [
"Chris", "Alex", "Jordan", "Taylor", "Morgan", "Casey", "Riley", "Jamie", "Avery", "Parker",
"Sam", "Charlie", "Dakota", "Skyler", "Quinn", "Reese", "Sawyer", "Rowan", "Finley", "Blake"
]
subjects = [
"Computer Science", "Mathematics", "Physics", "Chemistry", "Biology", "History", "Literature", "Art", "Music"]
triples = [
Triple(
head=Node(name=choice(person_names), label='Student'),
relation=Relation(name='studies'),
tail=Node(name=choice(subjects), label='Subject')
)
for _ in range(25)
]
Create Graph¶
Now, we'll create a graph of the students and the subjects they study. A simple schema will automatically be derived based on the entities and relations we're using.
whyhow_client = WhyHow(api_key='', base_url="https://api.whyhow.ai")
workspace = whyhow_client.workspaces.create(name="Extend graph from triples")
graph = whyhow_client.graphs.create_graph_from_triples(triples=triples, workspace_id=workspace.workspace_id, name="Graph from triples")
Generate a new set of triples¶
Now, we're going to make a set of triples which do not conform to the existing schema.
foods = [
"Pizza", "Burger", "Pasta", "Sushi", "Salad", "Steak", "Tacos", "Ice Cream", "Chocolate", "Sandwich",
"Fried Chicken", "Hot Dog", "Fries", "Dumplings", "Ramen", "Curry", "Burrito", "Pancakes", "Waffles", "Smoothie"
]
non_conforming_triples = [
Triple(
head=Node(name=choice(person_names), label='Student'),
relation=Relation(name='likes'),
tail=Node(name=choice(foods), label='Food')
)
for _ in range(25)
]
Extend the Graph¶
Once the graph has finished creating, we will extend it with this new set of triples which do not conform to the original schema. To do this, we will specify strict_mode
as False
, which tells the WhyHow platform to extend the schema accordingly. (If strict_mode
is set to True
, the graph will only accept triples that conform to the existing schema.)
update_graph_response = whyhow_client.graphs.add_triples(graph_id=graph.graph_id, triples=non_conforming_triples, strict_mode=False)
print(update_graph_response)