Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
4
Indexable
Never
from aima.logic import *


def main():
    print("Welcome to the Expert System")
    print("Enter facts and rules in the format 'Predicate(arg1, arg2, ...)'")
    print("Enter 'query' to query the knowledge base")
    print("Enter 'exit' to quit")

    facts = []
    rules = []

    while True:
        user_input = input(">> ")

        if user_input.lower() == 'exit':
            break
        elif user_input.lower() == 'query':
            query_kb(facts, rules)
        else:
            try:
                expr = parse_input(user_input)
                if is_fact(expr):
                    facts.append(expr)
                elif is_rule(expr):
                    rules.append(expr)
                else:
                    print("Invalid input: Must be a fact or rule")
            except Exception as e:
                print("Error:", e)


def parse_input(user_input):
    return expr(user_input)


def is_fact(expr):
    return expr.op


def is_rule(expr):
    return not expr.op


def query_kb(facts, rules):
    kb = FolKB(facts, rules)
    query = input("Enter query in the format 'Predicate(arg1, arg2, ...)': ")
    try:
        result = kb.ask(parse_input(query))
        print("Query result:", result)
    except Exception as e:
        print("Error:", e)


if __name__ == "__main__":
    main()
Leave a Comment