0% found this document useful (0 votes)
6 views4 pages

Chatbot Response Generation Code

The document contains a Python program that implements a simple chatbot using natural language processing techniques. It utilizes the NLTK library for text processing and the sklearn library for calculating cosine similarity to generate responses based on user input. The chatbot can greet users and respond to queries about chatbots, with an option to exit the conversation.

Uploaded by

Yash Chhaproo
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)
6 views4 pages

Chatbot Response Generation Code

The document contains a Python program that implements a simple chatbot using natural language processing techniques. It utilizes the NLTK library for text processing and the sklearn library for calculating cosine similarity to generate responses based on user input. The chatbot can greet users and respond to queries about chatbots, with an option to exit the conversation.

Uploaded by

Yash Chhaproo
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

Program :

import io
import random
import string # to process standard python strings
import warnings
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from [Link] import cosine_similarity
import warnings
[Link]('ignore')
import nltk
from [Link] import WordNetLemmatizer
[Link]('popular', quiet=True) # for downloading packages
with open('[Link]','r', encoding='utf8', errors ='ignore') as fin:
raw = [Link]().lower()

sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences


word_tokens = nltk.word_tokenize(raw)# converts to list of words

lemmer = WordNetLemmatizer()
def LemTokens(tokens):
return [[Link](token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in [Link])
def LemNormalize(text):
return LemTokens(nltk.word_tokenize([Link]().translate(remove_punct_dict)))

GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey",)


GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to
me"]

def greeting(sentence):
"""If user's input is a greeting, return a greeting response"""
for word in [Link]():
if [Link]() in GREETING_INPUTS:
return [Link](GREETING_RESPONSES)

def response(user_response):
robo_response=''
sent_tokens.append(user_response)
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
tfidf = TfidfVec.fit_transform(sent_tokens)
vals = cosine_similarity(tfidf[-1], tfidf)
idx=[Link]()[0][-2]
flat = [Link]()
[Link]()
req_tfidf = flat[-2]
if(req_tfidf==0):
robo_response=robo_response+"I am sorry! I don't understand you"
return robo_response
else:
robo_response = robo_response+sent_tokens[idx]
return robo_response
flag=True
print("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type
Bye!")
while(flag==True):
user_response = input()
user_response=user_response.lower()
if(user_response!='bye'):
if(user_response=='thanks' or user_response=='thank you' ):
flag=False
print("ROBO: You are welcome..")
else:
if(greeting(user_response)!=None):
print("ROBO: "+greeting(user_response))
else:
print("ROBO: ",end="")
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag=False
print("ROBO: Bye! take care..")

OUTPUT :

You might also like