Skip to content

Chat bot #9

Description

@lalithapulapa81

import nltk
import random
import string

Download necessary NLTK data files

nltk.download('punkt')
nltk.download('wordnet')

from nltk.stem import WordNetLemmatizer

Initialize Lemmatizer

lemmatizer = WordNetLemmatizer()

Sample knowledge base

responses = {
"hello": ["Hello there!", "Hi! How can I help you today?", "Hey!"],
"how are you": ["I'm good, thank you!", "Doing great! How about you?"],
"your name": ["I'm a simple chatbot created using NLTK.", "Call me Chatbot!"],
"bye": ["Goodbye!", "See you soon!", "Bye! Have a nice day!"],
"help": ["You can ask me simple questions like 'hello', 'how are you', 'your name', or say 'bye' to exit."]
}

Preprocess user input

def preprocess(sentence):
tokens = nltk.word_tokenize(sentence.lower()) # Tokenize and lowercase
tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in string.punctuation]
return tokens

Get chatbot response

def get_response(user_input):
tokens = preprocess(user_input)
for key in responses:
if key in " ".join(tokens):
return random.choice(responses[key])
return "I didn't understand that. Can you rephrase?"

Main chatbot loop

def chatbot():
print("🤖 Chatbot: Hello! I'm here to help. Type 'bye' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print("🤖 Chatbot: Bye! Have a great day!")
break
print("🤖 Chatbot:", get_response(user_input))

if name == "main":
chatbot()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions