#!/usr/bin/python ## C463 Artificial Intelligence ## Dana Vrajitoru ## An expert system that will help the user choose a car appropriate ## to their needs and preferences. # declare these to be able to answer without the quotes yes = "yes" no = "no" y = "yes" n = "no" ## Organizing the database for a car expert system in a dictionary so ## we can make the process more automatic. ## The list associates with each state contains the question to be ## asked when we are in that state, the state to go to from there if ## the answer is "yes",a nd the state to go to from there if the ## answer is "no". def make_car_db(): car_db = {} car_db["initial"] = ["Will you have more than 5 passengers?", "bus_state", "carry_state"] car_db["carry_state"] = ["Do you need to cary a lot of stuff?", "suv_state", "size_state"] car_db["size_state"] = ["Do you want a big car?", "speed_state", "vwb_state"] car_db["speed_state"] = ["Do you want a fast car?", "", ""] car_db["bus_state"] = ["I suggest a bus."] car_db["suv_state"] = ["I sugest a SUV."] car_db["vwb_state"] = ["I suggest a VW Beetle."] return car_db ## to be completed by the student ## A simple general purpose expert system that executes a loop doing ## the following: ## (1) ask the user a question ## (2) reading the asnwer and transforming it into a standard form ## (3) going from the current state to another one based on the answer ## starting from the initial state and until we reach a terminal state ## or leaf in the decision tree. def expert_system (db): state = "initial" question = "" answer = "" res = "" while state: question = get_text(db, state) print question if is_terminal(db, state): res = state state = "" else: user_input = input() answer = trans_answer(user_input) ## if answer: ## print "yes\n" ## else: ## print "no\n" state = process_answer(db, state, answer) return res ## Transforming a text into something simpler to test: true or false. ## The argument must be of type text or character, anything else will ## be discarded. If the text starts with "y" or "Y" then the result ## will be true (t), everything else is interpreted as nil. def trans_answer (text): if not text: return False elif type(text) == type(" ") and (text[0] == "y" or text[0] == "Y"): return True else: return False ## Checks if a state is terminal. We're making the convention that ## states with associated text ending with a question mark are not ## terminal, all of the others are terminal. def is_terminal (db, state): text = get_text(db, state) if text[len(text)-1] == "?": return False else: return True ## Returns the text to be displayed for each of the possible states of ## the system. def get_text (db, state): if not db or not state or not (state in db.keys()): return "" elif not len(db[state]): return "" else: return db[state][0] ## Returns a new state for every state of the system based on the ## answer. def process_answer (db, state, answer): if not db or not state or not (state in db.keys()): return "" elif len(db[state]) < 3: return "" elif answer: return db[state][1] else: return db[state][2] if __name__ == '__main__': db = make_car_db() expert_system(db) ## Will you have more than 5 passengers? ## yes ## I suggest a bus. ## bus_state ## Will you have more than 5 passengers? ## no ## Do you need to cary a lot of stuff? ## yes ## I sugest a SUV. ## suv_state