{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNCZyT2Cwhm58Z84c/3fVJD", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Build a Rule-Based System with Python\n", "\n", "## Learn how to simulate expert systems using Python dictionaries and logic\n", "\n", "| ![space-1.jpg](https://github.com/Tanu-N-Prabhu/Python/blob/master/Img/rule_based.png?raw=true) |\n", "|:--:|\n", "| Image Generated Using Canva|\n", "\n", "### Introduction\n", "Rule-based systems are at the heart of many decision-making engines; from medical diagnosis to recommendation engines. These systems apply a set of predefined \"if-then\" rules to derive conclusions. In this article, you'll learn how to build a basic rule-based engine in Python that mimics real-world decision-making.\n", "\n", "---\n", "\n", "### What's Covered\n", "* What is a rule-based system\n", "\n", "* How to represent rules in Python\n", "\n", "* Building a mini inference engine\n", "\n", "* Testing it on a real-world example\n", "\n", "\n", "---\n", "\n", "### Code Implementation\n", "\n", "\n" ], "metadata": { "id": "AM4JNYqhSdqd" } }, { "cell_type": "code", "source": [ "# Step 1: Define rules as functions\n", "def is_fever(temp):\n", " return temp > 99.5\n", "\n", "def is_cough(symptoms):\n", " return 'cough' in symptoms\n", "\n", "def is_sore_throat(symptoms):\n", " return 'sore throat' in symptoms\n", "\n", "# Step 2: Define the rule engine\n", "def diagnose(symptoms, temperature):\n", " rules = {\n", " \"Flu\": is_fever(temperature) and is_cough(symptoms),\n", " \"Common Cold\": is_cough(symptoms) and is_sore_throat(symptoms),\n", " \"Healthy\": not is_fever(temperature) and not is_cough(symptoms)\n", " }\n", "\n", " for condition, result in rules.items():\n", " if result:\n", " return condition\n", " return \"Unknown Condition\"\n", "\n", "# Step 3: Run the engine\n", "symptoms = ['cough', 'sore throat']\n", "temperature = 100.2\n", "\n", "diagnosis = diagnose(symptoms, temperature)\n", "print(\"Diagnosis:\", diagnosis)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7dG0e5AAaYp_", "outputId": "816b73cb-9ae4-47ee-aa28-e755a1268eef" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Diagnosis: Flu\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Conclusion\n", "You’ve just built a basic rule-based system, the backbone of many early AI applications. While this example is simple, the principles can be extended with complex logic, probabilistic reasoning, or even natural language processing for intelligent decision-making systems. Thanks for reading my article, let me know if you have any suggestions or similar implementations via the comment section. Until then, see you next time. Happy coding!\n", "\n", "---\n", "\n", "### Before you go\n", "* Be sure to Like and Connect Me\n", "* Follow Me : [Medium](https://medium.com/@tanunprabhu95) | [GitHub](https://github.com/Tanu-N-Prabhu) | [LinkedIn](https://ca.linkedin.com/in/tanu-nanda-prabhu-a15a091b5) | [Python Hub](https://github.com/Tanu-N-Prabhu/Python)\n", "* [Check out my latest articles on Programming](https://medium.com/@tanunprabhu95)\n", "* Check out my [GitHub](https://github.com/Tanu-N-Prabhu) for code and [Medium](https://medium.com/@tanunprabhu95) for deep dives!" ], "metadata": { "id": "s0kL_-OpagpT" } } ] }