1
1
2
- # coding: utf-8
3
-
4
- # # Meet Robo: your friend
2
+ #Meet Robo: your friend
5
3
4
+ #import necessary libraries
6
5
import io
7
6
import random
8
7
import string # to process standard python strings
9
8
import warnings
10
-
9
+ import numpy as np
11
10
from sklearn .feature_extraction .text import TfidfVectorizer
12
11
from sklearn .metrics .pairwise import cosine_similarity
13
12
14
- import numpy as np
15
-
16
13
import nltk
17
14
from nltk .stem import WordNetLemmatizer
18
-
19
- warnings .filterwarnings ("ignore" )
20
-
21
15
nltk .download ('popular' , quiet = True ) # for downloading packages
22
- # Includes the following already.
16
+
17
+ # uncomment the following only the first time
23
18
#nltk.download('punkt') # first-time use only
24
19
#nltk.download('wordnet') # first-time use only
25
20
21
+
22
+ #Reading in the corpus
26
23
with open ('chatbot.txt' ,'r' , encoding = 'utf8' , errors = 'ignore' ) as fin :
27
24
raw = fin .read ().lower ()
28
25
26
+ #Calculating the tokens
29
27
sent_tokens = nltk .sent_tokenize (raw )# converts to list of sentences
30
28
word_tokens = nltk .word_tokenize (raw )# converts to list of words
31
29
32
-
33
30
sent_tokens [:2 ]
34
-
35
-
36
31
word_tokens [:5 ]
37
32
38
-
39
33
lemmer = WordNetLemmatizer ()
40
34
def LemTokens (tokens ):
41
35
return [lemmer .lemmatize (token ) for token in tokens ]
42
36
remove_punct_dict = dict ((ord (punct ), None ) for punct in string .punctuation )
43
37
def LemNormalize (text ):
44
38
return LemTokens (nltk .word_tokenize (text .lower ().translate (remove_punct_dict )))
45
39
46
-
47
40
GREETING_INPUTS = ("hello" , "hi" , "greetings" , "sup" , "what's up" ,"hey" ,)
48
41
GREETING_RESPONSES = ["hi" , "hey" , "*nods*" , "hi there" , "hello" , "I am glad! You are talking to me" ]
49
42
@@ -57,8 +50,6 @@ def greeting(sentence):
57
50
return random .choice (GREETING_RESPONSES )
58
51
59
52
60
-
61
-
62
53
# Generating response
63
54
def response (user_response ):
64
55
robo_response = ''
@@ -80,7 +71,6 @@ def response(user_response):
80
71
81
72
flag = True
82
73
print ("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!" )
83
-
84
74
while (flag == True ):
85
75
user_response = input ()
86
76
user_response = user_response .lower ()
0 commit comments