Creating a Command-Based Automation System with Fuzzy Matching in Python

In this article, we will explore how to create a basic command-based automation system using Python, which listens for user input, processes the input with fuzzy matching to determine the closest matching command, and triggers actions based on that input. We will walk through the steps to install the required libraries and set up a Python environment to run the program.

Steps Involved:

  1. Checking Python Version
  2. Creating a Virtual Environment
  3. Installing the Required Library
  4. Implementing the Fuzzy Matching Logic
  5. Automating Command Execution

Step 1: Checking Python Version

First, let’s verify which version of Python is installed on your system. You can check this by running the following command in your terminal or command prompt:

python --version

Make sure you have Python 3.x installed, as this code requires Python 3 to run properly.

Step 2: Creating a Virtual Environment

It is always a good practice to work within a virtual environment when developing Python applications. This helps you keep your dependencies isolated from the global Python environment, avoiding potential conflicts. To create a virtual environment, run the following command:

python -m venv venv

This command creates a new virtual environment named venv.

Step 3: Activating the Virtual Environment

Now that the virtual environment is created, you need to activate it before installing any packages. Run the following command to activate the virtual environment:

On Windows:

venv\Scripts\activate

On macOS/Linux:

source venv/bin/activate

Once activated, your terminal prompt should indicate that you are working inside the venv environment.

Step 4: Installing Required Libraries

For this project, we will use the fuzzywuzzy library to perform fuzzy matching on user input. Fuzzy matching allows the program to intelligently recognize inputs that are close to predefined phrases, even if they are not exact matches.

To install fuzzywuzzy with speedup capabilities, run the following command:

pip install fuzzywuzzy[speedup] --cache-dir "D:/internship/sentence_similarity/.cache"

The --cache-dir option specifies where the downloaded files will be cached.

Step 5: Writing the Python Code

Now that we have the environment set up, let’s look at the Python code that handles the command matching and executes actions.

Python Code:

from fuzzywuzzy import process

# List of predefined phrases
predefined_phrases = ["powerpoint presentation", "mobile app", "mobile", "web application", "data science project"]

def getCommand(user_input):
    # Find the best match
    best_match, similarity_score = process.extractOne(user_input, predefined_phrases)
    return best_match
    # print(f"Best match: {best_match}, Similarity score: {similarity_score}")

def createPPT():
    f = open('present.ppt', 'w')
    f.write("data")
    f.close()

user_input = input("What do you want me to do:")

user_input = getCommand(user_input)

if(user_input == 'powerpoint presentation'):
    createPPT()
    print('ppt created')
else:
    print('unable to understand')

ode Breakdown:

  1. Importing the Fuzzy Matching Library:
    We import process from the fuzzywuzzy library, which provides various string-matching utilities.
from fuzzywuzzy import process

Defining Predefined Phrases:
We define a list of commands or tasks that our system can recognize. These are stored in the predefined_phrases list.

predefined_phrases = ["powerpoint presentation", "mobile app", "mobile", "web application", "data science project"]

Matching User Input to Commands:
The getCommand function uses fuzzy matching to find the most similar phrase from the predefined_phrases list based on the user input. The process.extractOne method returns the closest match and its similarity score. For simplicity, we only return the best match.

def getCommand(user_input):
    best_match, similarity_score = process.extractOne(user_input, predefined_phrases)
    return best_match

Creating a PowerPoint File:
If the matched command is "powerpoint presentation", the program creates a new PowerPoint file (present.ppt) and writes some dummy content to it using the createPPT function.

def createPPT():
    f = open('present.ppt', 'w')
    f.write("data")
    f.close()

Handling User Input:
The program prompts the user to input a task or command. The input is passed to the getCommand function, and the best-matched command is compared against predefined options. If the command matches "powerpoint presentation", the PowerPoint file is created. Otherwise, the program displays an error message.

user_input = input("What do you want me to do:")

user_input = getCommand(user_input)

if(user_input == 'powerpoint presentation'):
    createPPT()
    print('ppt created')
else:
    print('unable to understand')

Conclusion

This simple automation system demonstrates the power of fuzzy matching for recognizing user commands. With the fuzzywuzzy library, even if the user input is not an exact match to predefined phrases, the system can still identify the closest match and trigger the appropriate action.

Feel free to expand this program by adding more commands and actions to suit your needs. You could also enhance the error handling or provide more feedback to users if their input is ambiguous.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x