How to create a chatbot in python?

Natural language processing (NLP) is one of the most promising fields of artificial intelligence that uses natural languages to enable human interactions with machines.There are two main approaches to NLP: — rules-based methods, — statistical methods, i.e. methods related to learning automatic.

Also to discover : Is the HP PC a good brand?

There are several interesting Python libraries for NLP, such as Natural Language Toolkit (NLTK), Spacy, TextBlob, etc.

A chatbot is computer software capable of interacting with humans using natural language. They are usually based on machine learning, especially NLP. Apple’s Siri, Amazon Alexa, Google Assitant and Microsoft’s Cortana are some well-known examples of software capable of processing natural languages.

See also : How to Create a Chatbot?

This article shows how to create a simple chatbot in Python using the ChatterBot library. Our bot will be used for trivial talks, as well as to answer some math questions. Here, we will scratch the surface of what is possible in creating custom chatbots and NLP in general.

### Preparing DependenciesYou’re just going to install the ChatterBot library for now. I recommend creating and using a new Python virtual environment for this purpose. Run the following commands in your Python (or Anaconda) terminal:

pip install chatterbot pip install chatterbot_corpus You can also try updating them:

pip install —upgrade chatterbot_corpus pip install —upgrade chatterbot That’s it. We’re ready to start.

### Class importYou will need to import two classes for this purpose: Chatterbot ChatBot and chatterbot.trainers ListTrainer:

from chatterbot import ChatBot from chatterbot.trainers import ListTrainer We are now ready to create and train our bot mathematician.

### Creating and training unbot Our bot will be an instance of the ChatBot class:

my_bot = ChatBot (Name=’PyBot’, Read_Only=true, logic_adapters= ) The only mandatory argument corresponds to the name of the parameter. Represents the name of the bot. You can provide read_only = True if you want to disable the bot’s ability to learn after training (i.e. from real conversations). logic_adapters is the list of adapters used to train the bot. There are several of them provided by chatterbot, such as the two in our example. ChatterBot.Logic.MathematicalEvaluation allows the bot to solve mathematical problems, while ChatterBot.Logic.BestMatch chooses the best match from the answers already provided.

So, we have to give answers. We do this by specifying the string lists that will then be used to train the bot and find the best match for each question. Here’s what I want our bot to learn for now:

small_talk = math_talk_1 = math_talk_2 = We can create and train the bot by creating a ListTrainer instance and providing you with string lists:

list_trainer = ListTrainer (my_bot) for item in (small_talk, math_talk_1, math_talk_2): list_trainer.train (item) The bot should now be trained and ready to communicate.

### Communication with a botYou can communicate with your bot using its .get_response () method. Here’s an example of what that would look like:

> >> print (my_bot.get_response (“hi”)) how do you do? > >> print (my_bot.get_response (“i feel awesome today”)) excellent, glad to hear that. > >> print (my_bot.get_response (“what’s your name?”)) i’m pybot. ask me a math question, please. > >> print (my_bot.get_response (“show me the pythagorean theorem”)) a squared plus b squared equals c squared. > >>

print (my_bot.get_response (“do you know the law of cosines?”)) c**2 = a**2 b**2 – 2 * a * b * cos (gamma) Don’t expect the bot to answer every question well! His knowledge is limited to things similar to what he has learned. Many times, you will find you answering nonsense, especially if you don’t provide comprehensive training.

### Training a bot with a corpus of dataYou can use your own corpus of data or an existing one to train a bot. For example, you can use some corpus provided by chatterbot:

from chatterbot.trainers import ChatterBotCorpustrainer corpus_trainer = ChatterBotCorpustrainer (my_bot) corpus_trainer.train (‘chatterbot.corpus.english’) chatterbot offers this function in several languages. You can also specify a subset of a corpus you’d like to use. ### ConclusionNow you know how to create and use a simple chatbot.

This is just a small illustration of what you can do with natural language processing and chatbots. There are a lot more possibilities out there. If you’re interested in exploring them, you can start by familiarizing yourself with NLTK and ChatterBot.

This article was originally published at: