Repository navigation
Expand file tree
/
Copy pathchatbot.py
More file actions
84 lines (68 loc) · 2.64 KB
/
Copy pathchatbot.py
File metadata and controls
84 lines (68 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import nltk
import random
import string
import warnings
warnings.filterwarnings('ignore')
f = open('C:\\Users\\User\\Downloads\\Act1Scene1.txt', 'r', errors='ignore')
raw = f.read()
raw = raw.lower()
sent_tokens = nltk.sent_tokenize(raw) #converts to list of scentences
word_tokens = nltk.word_tokenize(raw) #converts to list of words
sentToken = sent_tokens[:4]
#print(sentToken)
wordToken = word_tokens[:4]
#print(wordToken)
#preprocessing
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)))
#Greetings
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(scentence):
for word in scentence.split():
if word.lower() in GREETING_INPUTS:
return random.choice(GREETING_RESPONSES)
#Vectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
def response(user_response):
chatbot_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 = vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
req_tfidf = flat[-2]
if(req_tfidf == 0):
chatbot_response = chatbot_response + "I am sorry! I don't understand you"
return chatbot_response
else:
chatbot_response=chatbot_response+sent_tokens[idx]
return chatbot_response
if __name__ == "__main__":
flag = True
print("Hello, there my name is Aneka. I will answer your queries. 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("Aneka: You're welcome!")
else:
if(greeting(user_response)!=None):
print("Aneka:" +greeting(user_response))
else:
print("Aneka:", end='')
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag = False
print("Aneka: Bye! Have a great time!" )