Basic Chatbot
Basic Chatbot
and spaCy
Submitted by
Abstract
This project focuses on building a simple text-based chatbot using Natural Language
Processing (NLP) libraries like NLTK and spaCy in Python. The chatbot interacts with
users by identifying intents based on user input and responding with relevant answers.
This demonstrates how basic NLP and rule-based logic can be combined to simulate
conversational AI effectively.
Introduction
Chatbots have become a popular tool for user interaction in various fields such as cus-
tomer support, education, and healthcare. This project presents a text-based chatbot
developed using Python’s NLTK and spaCy libraries. The bot is capable of greeting
users, answering simple queries, and handling basic conversation using natural language
processing techniques.
Code
Chatbot Python Code
1 import nltk
2 import spacy
3 from nltk . tokenize import word_tokenize
4 import random
5
6 nlp = spacy . load ( " en_core_web_sm " )
7
8 responses = {
9 " greeting " : [ " Hi there ! " , " Hello ! " , " Hey ! " , " Hi ! How can I help you
?"],
10 " how_are_you " : [ " I ’m doing great , thanks for asking ! " , " I ’m just a
bot , but I ’m doing fine ! " ] ,
11 " weather " : [ " The weather is always perfect in the cloud ! " , " Sunny
with a chance of code . " ] ,
12 " name " : [ " You can call me ChatBot ! " , " I ’m your friendly chatbot . " ] ,
13 " goodbye " : [ " Goodbye ! " , " See you later ! " , " Bye ! Have a great day ! "
],
Page 1
Amit Bikram Mishra Text-Based Chatbot
14 " default " : [ " I ’m not sure I understand . Can you rephrase ? " , "
Interesting ! Tell me more . " ]
15 }
16
17 def get_intent ( user_input ) :
18 doc = nlp ( user_input . lower () )
19 if any ( token . lemma_ in [ ’ hi ’ , ’ hello ’ , ’ hey ’] for token in doc ) :
20 return " greeting "
21 elif " how " in user_input and " you " in user_input :
22 return " how_are_you "
23 elif " weather " in user_input :
24 return " weather "
25 elif " your name " in user_input or " who are you " in user_input :
26 return " name "
27 elif any ( token . lemma_ in [ ’ bye ’ , ’ goodbye ’ , ’ see you ’] for token in
doc ) :
28 return " goodbye "
29 else :
30 return " default "
31
32 def chat () :
33 print ( " ChatBot : Hello ! Type ’ bye ’ to exit . " )
34 while True :
35 user_input = input ( " You : " )
36 if user_input . lower () in [ ’ bye ’ , ’ exit ’ , ’ quit ’ ]:
37 print ( " ChatBot : " , random . choice ( responses [ " goodbye "
]) )
38 break
39 intent = get_intent ( user_input )
40 reply = random . choice ( responses [ intent ])
41 print ( " ChatBot : " , reply )
42
43 if __name__ == " __main__ " :
44 chat ()
Conclusion
This project showcases how basic natural language processing techniques using NLTK
and spaCy can be applied to build a simple conversational chatbot. While the chatbot
is rule-based, it lays a strong foundation for further development using machine learn-
ing and deep learning for advanced conversational AI. This practical implementation
demonstrates the usefulness of Python NLP libraries in creating engaging and responsive
applications.
Page 2