Chatboat app

import nltk
import numpy as np
import random
import string

f = open(‘mahatmagandhi.txt’,‘r’,errors=‘ignore’)
raw = f.read()
raw = raw.lower()
nltk.download(‘punkt’)
nltk.download(‘woldnet’)
sent_tokens = nltk.sent_tokenize(raw)
world_tokens = nltk.word_tokenize(raw)

lemmer = nltk.stem.WordNetLemmatizer()
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.Word_tokenize(text.lower().translate(remove_punct_dict)))

GREETING_INPUTS = (“hello”,“hi”,“greeting”,“sup”,“what’s up”, “hey”)
GREETING_RESPONSES=[“hi”,“hey”,“nods”,“hi there”,“hello”,“I an glade! you are taking to me”]
def greeting(sentence):
for word in sentence.split():
if word.lower()in GREETING_INPUTS:
return random.choice(GREETING_RESPONSES)

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarit

def response(user_response):
robo_response=’’
sent_tokens.append(user_response)
TfidfVec = TfidVectorizer(tokenizer=LemNormalize, stop_words=‘english’)
tfidf = TfIdfVec,fit_transform(sent_tokens)
vals = consine_similarity(tfidf[-1],tfidf)
idx = Vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
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(“chatbot: My name is Unique Bot I will help you with chatbot. if you want to quit simply 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(“unique Bot: you are welcom”)
else:
if(greeting(user_response)!=None):
print(“UniqueBot:” + greeting(user_response))
else:
print(“UniqueBot:”,end="")
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag=False
print(“UniqueBot:thanks for taking bye bye”)

© 2017 GitHub, Inc.
with by
GitHub Education