Creating Chatbot Using Python NLTK
What is a chatbot?
A chatbot is an intelligent piece of software that is capable of communicating and performing actions similar to a human. Chatbots are used a lot in customer interaction, marketing on social network sites and instant messaging the client.
First import all the packages related to chatbot
Importing all the related libraries
#import all libraries import nltk from nltk.stem import WordNetLemmatizer lemmatizer = WordNetLemmatizer() import json import pickle import numpy as np from keras.models import Sequential from keras.layers import Dense, Activation, Dropout from keras.optimizers import SGD import random
Install all packages related to this
#install packages pip install tensorflow, keras, pickle, nltk
Load the datasets
#load datasets words= classes =  documents =  ignore_words = ['?', '!'] data_file = open('intents.json').read() intents = json.loads(data_file)
for intent in intents['intents']: for pattern in intent['patterns']: #tokenize each word w = nltk.word_tokenize(pattern) words.extend(w) #add documents in the corpus documents.append((w, intent['tag'])) # add to our classes list if intent['tag'] not in classes: classes.append(intent['tag'])
Now we will lemmatize each word and remove duplicate words from the list
# lemmatize, lower each word and remove duplicates words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words] words = sorted(list(set(words))) # sort classes classes = sorted(list(set(classes))) # documents = combination between patterns and intents print (len(documents), "documents") # classes = intentsprint (len(classes), "classes", classes) # words = all words, vocabulary print (len(words), "unique lemmatized words", words) pickle.dump(words,open('words.pkl','wb')) pickle.dump(classes,open('classes.pkl','wb'))
Creating the training and testing data
# create our training data training =  # create an empty array for our output output_empty =  * len(classes) for doc in documents: # initialize our bag of words bag =  # list of tokenized words for the pattern pattern_words = doc pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words] for w in words: bag.append(1) if w in pattern_words else bag.append(0) # output is a '0' for each tag and '1' for current tag (for each pattern) output_row = list(output_empty) output_row[classes.index(doc)] = 1 training.append([bag, output_row]) # shuffle our features and turn into np.array random.shuffle(training) training = np.array(training) # create train and test lists. X - patterns, Y - intent strain_x = list(training[:,0]) train_y = list(training[:,1]) print("Training data created")
If you learn more about then visit here - Reference
Get your project or assignment completed by Deep learning expert and experienced developers and researchers.
If you have project files, You can send at email@example.com directly