In [1]:
import janus_swi as janus
janus.query_once("writeln('Hello world!')")
Out[1]:
{'truth': True}
Prolog example¶
In [2]:
janus.consult("trains","""
train('Amsterdam', 'Haarlem').
train('Amsterdam', 'Schiphol').
""")
print([d['Tuple'] for d in janus.query("train(_From,_To),Tuple=_From-_To")])
[('Amsterdam', 'Haarlem'), ('Amsterdam', 'Schiphol')]
Pygol example¶
In this directory, three files BK.pl , pos_example.f and neg_example.n are needed, see https://github.com/danyvarghese/PyGol/tree/main/examples/Michalski_trains .
In [22]:
# Import package from root folder
import sys
#sys.path.insert(0, '../../')
from PyGol import *
#Define the constants
const=["elipse", "hexagon","rectangle","u_shaped","triangle","hexagon","circle","nil"]
#Generate the bottom clauses
P, N = bottom_clause_generation(constant_set = const, container = "memory")
# Split examples into train and test subsets
Train_P, Test_P, Train_N, Test_N=pygol_train_test_split(test_size=0, positive_file_dictionary=P,
negative_file_dictionary=N)
#Learning Phase/Training Phase using Python
model= pygol_learn(Train_P, Train_N, max_neg=0, max_literals=3, key_size=1)
100%|██████████| 5/5 [00:00<00:00, 102.72it/s] 100%|██████████| 5/5 [00:00<00:00, 725.11it/s] 100%|██████████| 5/5 [00:00<00:00, 71.46it/s] 100%|██████████| 5/5 [00:00<00:00, 128.90it/s]
+----------+ Hypothesis +----------+ ['eastbound(A):-has_car(A,B),closed(B),short(B)'] +----------+ Training +----------+ +---------------------+------------------+------------------+ | n = 10 | Positive(Actual) | Negative(Actual) | +=====================+==================+==================+ | Positive(Predicted) | 5 | 0 | +---------------------+------------------+------------------+ | Negative(Predicted) | 0 | 5 | +---------------------+------------------+------------------+ +-------------+---+ | Metric | # | +=============+===+ | Accuracy | 1 | +-------------+---+ | Precision | 1 | +-------------+---+ | Sensitivity | 1 | +-------------+---+ | Specificity | 1 | +-------------+---+ | F1 Score | 1 | +-------------+---+
In [23]:
model.hypothesis
Out[23]:
['eastbound(A):-has_car(A,B),closed(B),short(B)']
In [24]:
Train_N
Out[24]:
{'eastbound(west6).': ['has_car(A,B)',
'has_car(A,C)',
'long(B)',
'short(C)',
'shape(B,rectangle)',
'shape(C,rectangle)',
'closed(B)',
'open(C)',
'load(B,circle,3)',
'load(C,triangle,1)',
'wheels(B,2)',
'wheels(C,2)'],
'eastbound(west7).': ['has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'short(B)',
'short(C)',
'long(D)',
'shape(B,rectangle)',
'shape(C,u_shaped)',
'shape(D,rectangle)',
'double(B)',
'open(B)',
'open(C)',
'jagged(D)',
'load(B,circle,1)',
'load(C,triangle,1)',
'load(D,nil,0)',
'wheels(B,2)',
'wheels(C,2)',
'wheels(D,2)'],
'eastbound(west8).': ['has_car(A,B)',
'has_car(A,C)',
'long(B)',
'short(C)',
'shape(B,rectangle)',
'shape(C,u_shaped)',
'closed(B)',
'open(C)',
'load(B,rectangle,1)',
'load(C,circle,1)',
'wheels(B,3)',
'wheels(C,2)'],
'eastbound(west9).': ['has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'has_car(A,E)',
'short(B)',
'long(C)',
'short(D)',
'short(E)',
'shape(B,u_shaped)',
'shape(C,rectangle)',
'shape(D,rectangle)',
'shape(E,u_shaped)',
'open(B)',
'jagged(C)',
'open(D)',
'open(E)',
'load(B,circle,1)',
'load(C,rectangle,1)',
'load(D,rectangle,1)',
'load(D,circle,1)',
'wheels(B,2)',
'wheels(C,2)',
'wheels(D,2)',
'wheels(E,2)'],
'eastbound(west10).': ['has_car(A,B)',
'has_car(A,C)',
'short(B)',
'long(C)',
'shape(B,u_shaped)',
'shape(C,rectangle)',
'open(B)',
'open(C)',
'load(B,rectangle,1)',
'load(C,rectangle,2)',
'wheels(B,2)',
'wheels(C,2)']}
In [25]:
Train_P
Out[25]:
{'eastbound(east1).': ['has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'has_car(A,E)',
'short(C)',
'closed(C)',
'long(B)',
'long(D)',
'short(E)',
'open(B)',
'open(D)',
'open(E)',
'shape(B,rectangle)',
'shape(C,rectangle)',
'shape(D,rectangle)',
'shape(E,rectangle)',
'load(B,rectangle,3)',
'load(C,triangle,1)',
'load(D,hexagon,1)',
'load(E,circle,1)',
'wheels(B,2)',
'wheels(C,2)',
'wheels(D,3)',
'wheels(E,2)'],
'eastbound(east2).': ['has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'short(B)',
'short(C)',
'short(D)',
'shape(B,u_shaped)',
'shape(C,u_shaped)',
'shape(D,rectangle)',
'open(B)',
'open(C)',
'closed(D)',
'load(B,triangle,1)',
'load(C,rectangle,1)',
'load(D,circle,2)',
'wheels(B,2)',
'wheels(C,2)',
'wheels(D,2)'],
'eastbound(east3).': ['closed(C)',
'closed(D)',
'has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'load(B,circle,1)',
'load(C,triangle,1)',
'load(D,triangle,1)',
'long(D)',
'open(B)',
'shape(B,rectangle)',
'shape(C,hexagon)',
'shape(D,rectangle)',
'short(B)',
'short(C)',
'wheels(B,2)',
'wheels(C,2)',
'wheels(D,3)'],
'eastbound(east4).': ['has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'has_car(A,E)',
'short(B)',
'short(C)',
'short(D)',
'short(E)',
'shape(B,u_shaped)',
'shape(C,rectangle)',
'shape(D,elipse)',
'shape(E,rectangle)',
'double(C)',
'open(B)',
'open(C)',
'closed(D)',
'open(E)',
'load(B,triangle,1)',
'load(C,triangle,1)',
'load(D,rectangle,1)',
'load(E,rectangle,1)',
'wheels(B,2)',
'wheels(C,2)',
'wheels(D,2)',
'wheels(E,2)'],
'eastbound(east5).': ['has_car(A,B)',
'has_car(A,C)',
'has_car(A,D)',
'short(B)',
'long(C)',
'short(D)',
'shape(B,rectangle)',
'shape(C,rectangle)',
'shape(D,rectangle)',
'double(B)',
'open(B)',
'closed(C)',
'closed(D)',
'load(B,triangle,1)',
'load(C,rectangle,1)',
'load(D,circle,1)',
'wheels(B,2)',
'wheels(C,3)',
'wheels(D,2)']}