11
2- # coding: utf-8
3-
4- # # Meet Robo: your friend
2+ #Meet Robo: your friend
53
4+ #import necessary libraries
65import io
76import random
87import string # to process standard python strings
98import warnings
10-
9+ import numpy as np
1110from sklearn .feature_extraction .text import TfidfVectorizer
1211from sklearn .metrics .pairwise import cosine_similarity
1312
14- import numpy as np
15-
1613import nltk
1714from nltk .stem import WordNetLemmatizer
18-
19- warnings .filterwarnings ("ignore" )
20-
2115nltk .download ('popular' , quiet = True ) # for downloading packages
22- # Includes the following already.
16+
17+ # uncomment the following only the first time
2318#nltk.download('punkt') # first-time use only
2419#nltk.download('wordnet') # first-time use only
2520
21+
22+ #Reading in the corpus
2623with open ('chatbot.txt' ,'r' , encoding = 'utf8' , errors = 'ignore' ) as fin :
2724 raw = fin .read ().lower ()
2825
26+ #Calculating the tokens
2927sent_tokens = nltk .sent_tokenize (raw )# converts to list of sentences
3028word_tokens = nltk .word_tokenize (raw )# converts to list of words
3129
32-
3330sent_tokens [:2 ]
34-
35-
3631word_tokens [:5 ]
3732
38-
3933lemmer = WordNetLemmatizer ()
4034def LemTokens (tokens ):
4135 return [lemmer .lemmatize (token ) for token in tokens ]
4236remove_punct_dict = dict ((ord (punct ), None ) for punct in string .punctuation )
4337def LemNormalize (text ):
4438 return LemTokens (nltk .word_tokenize (text .lower ().translate (remove_punct_dict )))
4539
46-
4740GREETING_INPUTS = ("hello" , "hi" , "greetings" , "sup" , "what's up" ,"hey" ,)
4841GREETING_RESPONSES = ["hi" , "hey" , "*nods*" , "hi there" , "hello" , "I am glad! You are talking to me" ]
4942
@@ -57,8 +50,6 @@ def greeting(sentence):
5750 return random .choice (GREETING_RESPONSES )
5851
5952
60-
61-
6253# Generating response
6354def response (user_response ):
6455 robo_response = ''
@@ -80,7 +71,6 @@ def response(user_response):
8071
8172flag = True
8273print ("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!" )
83-
8474while (flag == True ):
8575 user_response = input ()
8676 user_response = user_response .lower ()
0 commit comments