0% found this document useful (0 votes)
19 views2 pages

Null

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Null

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import nltk

from nltk.sentiment.vader import SentimentIntensityAnalyzer


from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
import random

nltk.download('vader_lexicon')
nltk.download('punkt')
nltk.download('stopwords')

# Initialize sentiment analyzer


sia = SentimentIntensityAnalyzer()

# Responses for different sentiment levels


positive_responses = ["That's great!", "I'm happy to hear that.", "Awesome!", "Good to kno
neutral_responses = ["I see.", "Okay.", "Tell me more.", "Interesting."]
negative_responses = ["I'm sorry to hear that.", "That's unfortunate.", "Is there anything

# Define a function to analyze and respond to user input


def analyze_sentiment(user_input):
# Tokenize and remove stopwords from the input
words = word_tokenize(user_input)
words = [word.lower() for word in words if word.isalnum() and word not in stopwords.wo

# Calculate sentiment scores


sentiment_scores = sia.polarity_scores(user_input)

# Determine sentiment level


if sentiment_scores['compound'] >= 0.05:
response = random.choice(positive_responses)
elif sentiment_scores['compound'] <= -0.05:
response = random.choice(negative_responses)
else:
response = random.choice(neutral_responses)

return response

# Main chat loop


while True:
user_input = input("You: ")

if user_input.lower() == 'exit':
print("Chatbot: Goodbye!")
break

response = analyze_sentiment(user_input)
print("Chatbot:", response)

You might also like