AI project

Tram Ho


Chatbot is a software program designed to interact with users through a chat interface. Chatbots can help with a variety of problems, including customer support, answering questions, making transactions, and providing information. Chatbots can be used on a variety of platforms, including websites, mobile apps, messaging apps, and social media channels. In this article, I will make a demo chat bot for readers to understand how the chat bot works. In which we will mainly use libraries for data preprocessing like NLP and to train the chat bot model we will use TFLearn’s model.

Here we will come to the first step of creating a chat bot.

The above code will include the libraries that will be used in the chatbot. The chatbot will first try to open the pre-trained file from the previous times to save time.

If the file does not exist it will create a new file by iterating over the data set of predefined “intents”. This code also generates a list of input “documents” and output “labels” by associating each template with its respective intent tag.

Next, the chat bot will apply the algorithms of the nltk library such as word_tokenize. It is the process of splitting a phrase, sentence, paragraph, one or more text documents into smaller units. Each of these smaller units is called Tokens, the list of resulting words is sorted and duplicates removed. The list of labels is also sorted.

In this step, we will process the data to train the chatbot model. We will create 2 empty lists and an empty out_empty which is a list of zeros of length equal to the number of unique labels in the dataset.

Then I loop through each pattern in my data and encode it using the nltk library. I also started each word in the template using the LancasterStemmer word starter. I create a bag of words for each sample by checking if each word in my vocabulary is included in the pattern. If there is a word, I will add 1 to the bag list, if not, I will add 0. Then I will put the data in the empty list of training and output and use the np library to process the data for them and save them to 1 file named data.pkl.

And here is a neural network model using the tflearn library.

The function encodes user input into individual words, applies a start to each word (reduces the word to its original form), and creates a bag of word vectors representing which words in the feature list appear in user input. For each word in the feature list, the function sets the corresponding element in the bag of word vectors to 1 if the word appears in the user input, and sets it to 0 otherwise. Then the function returns an array using the numpy library.

And this is the main running function in our chat bot program. Results will use the predict function and the bag_of_words function above to clean the data and then feed it to the neural network model for prediction and then use np.argmax to calculate the prediction to see which data is correct. The most correct output, then will find the corresponding tags in the data base and will get the answer corresponding to the question and return it to us.

Above is all that in the past time I have tried based on internet-based search and development to make a simple chat bot. If the article has many errors because it’s my first time writing, please ignore and comment below so I can know and correct it. Thank you everyone for reading.

Share the news now

Source : Viblo