Building a Simple Knowledge-Based System (KBS) in Python
Building a Simple Knowledge-Based System (KBS) in Python
I recently took a knowledge-based systems course where I was required to learn how to build a simple knowledge-based system (KBS) from scratch using Python. This post contains the implementation I used for practice.
A KBS is a program designed to simulate human decision-making using stored knowledge and logical rules. It consists of three main components: the knowledge base, the inference engine, and the user interface. Understanding these components separately makes it easier to build more complex systems later.
Components of a Knowledge-Based System
- Knowledge Base (KB) - Stores facts, rules, and structured information used for decision-making.
- Inference Engine (IE) - Processes input data against the knowledge base and derives conclusions.
- User Interface (UI) - Allows users to interact with the system, input data, and receive output.
Breaking the implementation down into these sections keeps things manageable.
1. Knowledge Base
A knowledge base is essentially a structured repository of facts. For this example, a basic genre recommendation system stores common genres and their associated keywords.
1
2
3
4
5
6
7
8
# Create the knowledge base
knowledge_base = {
"fiction": ["drama", "action", "story", "adventure"],
"fantasy": ["magic", "dragons", "quests", "elves"],
"mystery": ["murder", "crime", "detective", "police"],
"non-fiction": ["biography", "politics", "history", "facts"]
}
Each genre is mapped to a list of associated keywords. The knowledge base doesn’t process anything on its own—it just holds the data.
2. Inference Engine
The inference engine is responsible for reasoning over the knowledge base. It takes user-provided keywords, compares them against stored genres, and determines the most suitable recommendation.
1
2
3
4
5
6
7
# Create the inference engine
def recommend_genre(preferences):
for genre, keywords in knowledge_base.items():
if any(preference in keywords for preference in preferences):
return f"Based on your preferences, you might enjoy {genre} books."
return "No matching genre found."
This function iterates through each genre, checking if any keyword matches the user’s input. If a match is found, the system recommends the corresponding genre.
3. User Interface
A simple text-based interface takes user input, processes it, and returns a result.
1
2
3
4
5
6
7
# User interface
user_input = input("Enter what you enjoy in books (e.g., adventure, magic, crime, biography) separated by commas: ").lower().split(",")
user_preferences = [preference.strip() for preference in user_input]
result = recommend_genre(user_preferences)
print(result)
Users enter book-related interests in plain text. The input is split into a list, converted to lowercase for consistency, and trimmed of extra spaces. The inference engine is then called to determine the most appropriate book genre.
4. Assembling the Full KBS
Combining all components results in a functional knowledge-based recommendation system.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Knowledge Base
knowledge_base = {
"fiction": ["drama", "action", "story", "adventure"],
"fantasy": ["magic", "dragons", "quests", "elves"],
"mystery": ["murder", "crime", "detective", "police"],
"non-fiction": ["biography", "politics", "history", "facts"]
}
# Inference Engine
def recommend_genre(preferences):
for genre, keywords in knowledge_base.items():
if any(preference in keywords for preference in preferences):
return f"Based on your preferences, you might enjoy {genre} books."
return "No matching genre found."
# User Interface
user_input = input("Enter what you enjoy in books (e.g., adventure, magic, crime, biography) separated by commas: ").lower().split(",")
user_preferences = [preference.strip() for preference in user_input]
print(recommend_genre(user_preferences))
Testing
Example interaction:
1
2
3
4
5
Enter what you enjoy in books (e.g., adventure, magic, crime, biography) separated by commas:
magic, quests
Based on your preferences, you might enjoy fantasy books.
If the input doesn’t match any genre, it returns:
1
No matching genre found.
Conclusion
This KBS is a starting point. More complexity can be added, such as:
- Allowing multiple genres to be suggested.
- Expanding the knowledge base. You can use ChatGPT to do the heavy lifting for you.
- Implementing a GUI. In my next iteration, I ended up using ipywidgets to create a very nice-looking widget for the program.
The structure remains the same: a knowledge base to store information, an inference engine to reason over it, and a user interface to interact with it. With these fundamentals in place, more advanced systems can be built from here.