Python, with its robust libraries and ease of use, has become the go-to language for AI development. Here, we'll explore how to create a simple AI code sample, specifically a basic chatbot, using Python and the Natural Language Toolkit (NLTK).

The chatbot will be rule-based, responding to inputs based on predefined patterns. Let's dive right in!

Setting Up the Environment
First, ensure you have Python and pip installed. If not, download and install Python from the official site. Once Python is set up, install the necessary libraries using pip:

pip install nltk
Import Necessary Libraries

Start by importing the required libraries. We'll use NLTK for text processing and re for pattern matching.
import nltk
import re
from nltk import word_tokenize
Prepare the Corpus

NLTK already provides several corpuses (large text datasets). However, for this chatbot, we'll create our own simple intents and entities.
greet_entities = [(" cumprimentar", "greeting")]
greet_intents = [("olΓ‘", "cumprimentar"), ("hi", "cumprimentar")]
farewell_entities = [("despedir-se", "farewell")]
farewell_intents = [("adeus", "despedir-se"), ("tchau", "despedir-se")]
Creating the Chatbot Functionality

Now, let's create the function that processes the user's input and generates a response based on the intents and entities defined.
def respond_to(input_text: str) -> str:
input_text = input_text.lower()
if any(intent in input_text for intent in greet_intents):
return "OlΓ‘! Como posso ajudar?"
elif any(intent in input_text for intent in farewell_intents):
return "AtΓ© logo! Se precisar de alguma coisa, estou aqui."
else:
return "NΓ£o entendi. Poderia reformular sua pergunta?"



















Chatbot Interaction Loop
Finally, create an infinite loop that listens for user input and responds accordingly.
while True:
user_input = input("VocΓͺs: ")
response = respond_to(user_input)
print("Chatbot:", response)
This simple chatbot responds to greetings and farewells. It's a starting point for more complex AI systems, like those using machine learning and deep learning. Play around with the code, expand the intents, and see where your creativity takes you!