LorenzoFerriniCodes / pykb

A Python API to access a KB-API conformant knowledge base

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pykb: A Python API to access a KB-API conformant knowledge base

Documentation Status

pykb provides a 'pythonic' interface to KB-API conformant knowledge bases like minimalkb or ORO.

Installation

pykb is only tested with Python3!

$ pip3 install pykb

(or, of course, from the source: clone & python setup.py install)

Documentation

Head to readthedocs. Sparse for now.

Examples

import kb
from kb import KbError
import time

REASONING_DELAY = 0.2

def onevent(evt):
    print("Something happened! %s" % evt)

with kb.KB() as kb:

    kb += ["alfred rdf:type Human", "alfred likes icecream"]
    
    if 'alfred' in kb:
        print("Hello Alfred!")

    if 'alfred likes icecream' in kb:
        print("Oh, you like icrecreams?")

    kb -= ["alfred likes icecream"]

    if 'alfred likes *' not in kb:
        print("You don't like anything? what a pity...")

    kb += ["Human rdfs:subClassOf Animal"]
    time.sleep(REASONING_DELAY) # give some time to the reasoner
    
    if 'alfred rdf:type Animal' in kb:
        print("I knew it!")

    for facts in kb.about("Human"):
        print(facts)

    for known_human in kb["?human rdt:type Human"]:
        print(known_human)


    kb += ["alfred desires jump", "alfred desires oil"]
    kb += ["jump rdf:type Action"]

    try:
        for action_lover in kb["?agent desires ?obj", "?obj rdf:type Action"]:
            print(action_lover)
    except KbError as kbe:
        print("The request failed: %s\nMaybe the knowledge base you are using does not support this kind of request." % kbe)

    kb.subscribe(["?agent isIn ?loc", "?loc rdf:type Room"], onevent, var="?agent")
    kb += ["alfred isIn sleepingroom", "sleepingroom rdf:type Room"]

    time.sleep(1) # event should have been triggered!

About

A Python API to access a KB-API conformant knowledge base


Languages

Language:Python 100.0%