Creating a basic chatbot using Python in Jupyter Notebook. This chatbot interacts with the user using the hardcoded inputs and outputs which are fed into the Python code.
Requirements:
You need to install the NLTK (Natural Language Toolkit), it provides libraries and programs for symbolic and statistical natural language processing for English written in the Python programming language. To install this module type the below command in the terminal.
pip install nltk
Below is the implementation
from nltk.chat.util import Chat, reflections pairs =[ ['my name is (.*)', ['Hello ! % 1']], ['(hi|hello|hey|holla|hola)', ['Hey there !', 'Hi there !', 'Hey !']], ['(.*) your name ?', ['My name is Geeky']], ['(.*) do you do ?', ['We provide a platform for tech enthusiasts, a wide range of options !']], ['(.*) created you ?', ['Geeksforgeeks created me using python and NLTK']] ] chat = Chat(pairs, reflections) chat.converse() |
Output:
Explanation of the above code:
In the first line of code we have imported the Chat class and Reflections dictionary from the Natural Language Toolkit’s chatbot utilities. Chat class which will process the conversation between the user and your chatbot. Reflections is a dictionary that when a value in a regular expression group matches a key in the dictionary it will output the value in the response. So for the first item of list pairs if we input my name is geeky where geeky corresponds to the regex “(.*)” it will output “Hello ! Geeky”, that is it replaces the regex in response to “%1” that was referred to as “(.*)” with “Geeky”. Now we initialize the chatbot using pairs and reflections dictionary. Then after initialization we call the converse method of Chat class that automates the chatbot.
Recommended Posts:
- Creating child process using fork() in Python
- Python | Creating tensors using different functions in Tensorflow
- Creating a dataframe using Excel files
- Creating a Pandas dataframe using list of tuples
- Creating Pandas dataframe using list of lists
- Creating and Using Serializers - Django REST Framework
- Creating a multiple Selection using Tkinter
- Creating a tabbed browser using PyQt5
- Creating a simple browser using PyQt5
- Creating NotePad using PyQt5 - Python
- Creating a Camera Application using Pyqt5
- Creating a dataframe using CSV files
- Creating Golden Ratio Calculator using PyQt5
- Creating and updating PowerPoint Presentations in Python using python - pptx
- MoviePy – Creating Animation Using Matplotlib
- Basic calculator program using Python
- How to make a basic Scatterplot using Python-Plotly?
- Python | Basic Gantt chart using Matplotlib
- How to Create a Basic Project using MVT in Django ?
- How to Create a basic API using Django Rest Framework ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

