Building a Simple Text-Driven Assistant in Python
Introduction: In this digital age, virtual assistants have become an integral part of our lives, helping us perform various tasks with just a few words. In this blog, we’ll explore the creation of a basic text-driven assistant using Python. We’ll walk through the code and discuss its functionalities, demonstrating how you can interact with this assistant to open applications and perform simple tasks using natural language commands.
Understanding the Code: The provided Python code showcases a straightforward implementation of a text-driven assistant. It presents a menu of actions that can be triggered by specific phrases. The assistant allows users to interact with it through a text-based interface, making it easy to understand and use.
//The code defines several functions to perform different actions, such as opening the calendar, Chrome, File Explorer, Notepad, Command Prompt, Control Panel, Task Manager, and System Settings.
import os
print("Hey, I am your assistant.")
def open_calendar():
print("Opening calendar...")
os.system("start outlookcal:")
def open_chrome():
print("Opening Chrome...")
os.system("start chrome")
def open_file_explorer():
print("Opening File Explorer...")
os.system("start explorer")
def open_notepad():
print("Opening Notepad...")
os.system("start notepad")
def open_command_prompt():
print("Opening Command Prompt...")
os.system("start cmd")
def open_control_panel():
print("Opening Control Panel...")
os.system("start control")
def open_task_manager():
print("Opening Task Manager...")
os.system("start taskmgr")
def open_system_settings():
print("Opening System Settings...")
os.system("start ms-settings:")
def assistance():
ch = input("How can I help you: ").strip()
return ch
while True:
ch = assistance()
if ("run" in ch or "execute" in ch) and "cal" in ch:
open_calendar()
elif ("run" in ch or "open" in ch or "execute" in ch) and "chrome" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_chrome()
else:
print("Okay, I won't open Chrome.")
elif ("run" in ch or "execute" in ch) and "file explorer" in ch:
open_file_explorer()
elif ("run" in ch or "open" in ch) and "file explorer" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_file_explorer()
else:
print("Okay, I won't open explorer.")
elif ("run" in ch or "execute" in ch) and "notepad" in ch:
open_notepad()
elif ("run" in ch or "open" in ch) and "notepad" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_notepad()
else:
print("Okay, I won't open notepad.")
elif ("run" in ch or "execute" in ch) and ("cmd" in ch or "command prompt" in ch) :
open_command_prompt()
elif ("run" in ch or "open" in ch) and "cmd" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_command_prompt()
else:
print("Okay, I won't open command prompt.")
elif ("run" in ch or "execute" in ch) and "control panel" in ch :
open_control_panel()
elif ("run" in ch or "open" in ch) and "control panel" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_control_panel()
else:
print("Okay, I won't open controlpanel.")
elif ("run" in ch or "execute" in ch) and "task manager" in ch :
open_task_manager()
elif ("run" in ch or "open" in ch) and "task manager" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_task_manager()
else:
print("Okay, I won't open task manager.")
elif ("run" in ch or "execute" in ch) and "setting" in ch :
open_system_settings()
elif ("run" in ch or "open" in ch) and "setting" in ch:
if "don't" not in ch and "not" not in ch and "do not" not in ch:
open_system_settings()
else:
print("Okay, I won't open system setting.")
elif ch:
print("Please type valid text....")
else:
print("Thank you, bye.")
break
Features of the Assistant:
- Opening Applications: The assistant can open various applications like the calendar, Chrome, File Explorer, Notepad, Command Prompt, Control Panel, Task Manager, and System Settings. Users can simply type commands like “open Chrome,” “run Notepad,” or “execute Task Manager” to trigger the corresponding actions.
- Handling User Preferences: The assistant is designed to consider negative statements like “don’t open” or “not” to respect user preferences. For instance, if you command the assistant to “open Chrome,” it will comply. However, if you say “don’t open Chrome,” it will acknowledge your request and refrain from opening the application.
How the Assistant Works:
- The program starts by defining functions for each action, like opening Chrome or Notepad, using the
os.system()
command to execute the respective applications. - The main function,
assistance()
, takes user input and returns it for processing. - The code then enters an infinite loop using
while True
, ensuring the assistant remains active and responsive. - The user’s input is analyzed through a series of conditions using
if
andelif
statements. The assistant matches the input against predefined phrases to determine which action to perform. - To ensure a user-friendly experience, the assistant considers multiple variations of commands. For example, users can type “run Chrome,” “execute Chrome,” or “open Chrome,” and the assistant will recognize them all.
Expanding the Assistant: While the current implementation is simple, there is immense potential to expand the assistant’s capabilities. Developers can add new actions, such as sending emails, searching the web, or providing weather updates. Integrating voice recognition and natural language processing can further enhance the user experience, making the assistant more intuitive and interactive.
Conclusion: In this blog, we explored a basic text-driven assistant built using Python. The code allows users to interact with the assistant using natural language commands, providing a glimpse into the fascinating world of virtual assistants. From opening applications to handling user preferences, this assistant lays the foundation for more sophisticated AI-driven solutions.
Creating a text-driven assistant is a small step towards building intelligent systems that can understand and respond to human interactions. With continuous advancements in AI and machine learning, virtual assistants will undoubtedly become even more powerful and ubiquitous, revolutionizing how we interact with technology in our daily lives.
Github Link:-https://github.com/mdshamsfiroz/TextdrivenMenu.git