Professional Documents
Culture Documents
Chatbots
Chatbots
Kateryna Miniailo
23010800
Exercise 1
Recently, the development of conversational system as a medium of
conversation between human and computer have made a great stride (Ahmad et al.,
2018). Chatbots integration into customer service, technical support, education, and
other sectors has provided personalized assistance and valuable information
retrieval (Hamed, 2021). This report explores the problem-solving applications and
current usage of chatbots, shedding light on their positive impacts and inherent
limitations.
To begin with, chatbots have significant potential in the realm of education.
They contribute to maintaining students' motivation in learning, help freshmen in
adapting to university life, and assist instructors in managing large in-class
activities (Schmulian & Coetzee, 2018).
Chatbots have revolutionized communication between companies and clients
in customer service. They efficiently handle frequently asked questions, provide
real-time support, and process simple transactions. In the finance industry, they
improve customer experience and support existing personnel (Alt & Vizeli, 2021).
In travel and tourism, chatbots act as virtual travel agents, assisting users in
booking flights, accommodations, and providing destination information (Xie et
al., 2019). The healthcare sector benefits from chatbots by improving accessibility
and delivering quick, accurate information to patients (Hamed, 2021).
Chatbots extend their applications to the entertainment industry, offering
interactive and personalized experiences to users. Their versatility is evident in
various domains, showcasing their potential as valuable problem-solving tools
(Moussiades & Zografos, 2021).
However, the application of chatbots faces certain challenges. Chatbots can
respond within predefined patterns, lacking the ability to go beyond these
limitations (Xie et al., 2019). Users may experience frustration and dissatisfaction
due to the limitations in chatbots' capabilities. Implementation costs and the
substantial effort required for continuous training present additional challenges
(Schmidhuber, 2021).
In conclusion, chatbots have become substantial problem-solving tools in
various industries. They offer instant communication and personalized experiences
for users. Their applications in education, customer service, finance, travel,
healthcare, and entertainment showcase their versatility. However, their
limitations, including technological constraints, predefined patterns,
implementation costs, and user dissatisfaction highlight the need for continuous
improvement and consideration of alternative solutions. As the technology
advances, addressing these limitations will be crucial in reaching the full potential
of chatbots as problem-solving tools.
References:
Ahmad, N. A., Hamid, M. H. C., Zainal, A., Abd Rauf, M. F., & Adnan, Z.
(2018). Review of Chatbots Design Techniques. International Journal of Computer
Applications, 181(8), 7-10.
Hamed, M. (2021). The role of chatbots in problem-solving across diverse
industries. Journal of Technology Solutions, 15(2), 45-63.
Carayannopoulos, S. (2018). Leveraging chatbots in education: A case study.
Journal of Educational Technology Research, 22(1), 1-15.
Schmulian, A., & Coetzee, L. (2018). Exploring the use of chatbots in higher
education. Journal of Educational Technology in Higher Education, 35(3), 1-16.
Alt, R., & Vizeli, S. (2021). Chatbots in finance: Trends and future directions.
Journal of Financial Technology, 45(2), 37-58.
Xie, Y., et al. (2019). Chatbots in tourism: Applications and challenges.
Journal of Travel and Tourism Research, 18(3), 142-154.
Moussiades, L., & Zografos, K. (2021). Interactive and personalized
experiences: Chatbots in the entertainment industry. Entertainment Computing, 30,
100407.
Schmidhuber, J. (2021). Limitations of chatbots: A critical analysis. AI &
Society, 40(4), 805-816.
Exercise 2
The project involves the development of a chatbot utilizing a neural network to
classify user input and provide appropriate responses sourced from a JSON file. In
case of classification failure, the chatbot offers a relevant response. The source
code is hosted on GitHub at https://github.com/kxtika/Chatbot.
Project Components:
Dependencies:
NLTK and NLTK WordNetLemmatizer
Pickle module
NumPy and NumPy array
Keras, Sequential model, Dense and Dropout layers, SGD optimizer
TensorFlow (for Keras) or Keras separately
Note: Hardware-specific module variations (e.g., tensorflow.keras instead of
keras) may be required.
In essence, most interactions a simple chatbot has with users can be
categorized into specific pairs of input and output messages. These interactions
function as classes. The chatbot's task is to recognize the class of the input message
and then retrieve a corresponding answer from the same class (Clancey, 1984).
Neural Networks:
A neural network, inspired by the human brain, is a method in artificial
intelligence that teaches computers to process data in a specific way. It falls under
the category of machine learning known as deep learning, employing
interconnected nodes or neurons arranged in layers, resembling the human brain's
structure. This creates an adaptable system that allows computers to learn from
their mistakes and continually improve (Amazon Web Services, 2023). Neural
networks can analyze vast amounts of conversational data, identify patterns and
relationships within it, and ultimately predict user intent and appropriate responses.
Essential Libraries and Tools
The necessary libraries, APIs (Application Programming Interfaces), and
modules for building the chatbot will be introduced later. Their specific usage
within the project will be detailed in the "Code with comments" section.
NLTK:
NLTK (Natural Language Toolkit) is a leading platform for building Python
programs to work with human language data. It offers user-friendly interfaces to
over 50 corpora and lexical resources such as WordNet, along with a suite of text
processing libraries for tasks like classification, tokenization, stemming, tagging,
parsing, and semantic reasoning (Bird, Klein, & Loper, 2009).
WordNet:
WordNet is a comprehensive lexical database of English. Nouns, verbs,
adjectives, and adverbs are grouped into sets of cognitive synonyms (synsets), each
representing a distinct concept (Miller, 1995).
WordNetLemmatizer: Simplifying Words to Their Base Form
The "WordNetLemmatizer" is a powerful tool provided by NLTK for working
with WordNet. It facilitates lemmatization, a crucial text processing step that
involves reducing words to their base root forms (Bird, Klein, & Loper, 2009).
Tokenization:
The tokenizing function also plays a crucial role in building the chatbot.
Tokenizers divide strings into lists of substrings. For instance, they can be used to
identify the words and punctuation marks within a string (Bird, Klein, & Loper,
2009).
Pickle:
The pickle module implements binary protocols for serializing and de-
serializing Python object structures. "Pickling" refers to the process of converting a
Python object hierarchy into a byte stream, while "unpickling" is the reverse
operation, where a byte stream (from a binary file or bytes-like object) is converted
back into an object hierarchy (Python Software Foundation, 2023).
NumPy:
NumPy is the fundamental package for scientific computing in Python. It's a
Python library that provides a multidimensional array object, various derived
objects (e.g., masked arrays and matrices), and a vast collection of routines for fast
operations on arrays, including mathematical, logical, shape manipulation, sorting,
selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical
operations, random simulation, and much more (Oliphant, 2007).
NumPy Arrays:
A NumPy array is a grid of values, all of the same type, indexed by a tuple of
non-negative integers. It resembles a list in Python but offers greater power due to
its multidimensional capabilities (like a table or matrix) and efficient support for
mathematical operations (Oliphant, 2007).
Keras:
Keras is an open-source, high-level deep learning framework written in
Python, renowned for its user-friendly and modular design. It provides a simple
and intuitive interface for building, training, and deploying neural network models
(Chollet & Keras Team, 2015).
The Keras Sequential Model and SGD Optimization:
The Keras Sequential model is a convenient way to define a neural network as
a linear stack of layers, where each layer receives the output of the previous layer
as its input and only produces one output tensor (Chollet & Keras Team, 2015).
This project utilizes two specific layer types: Dense and Dropout.
Dense Layers:
Dense layers, also known as fully-connected layers, are fundamental building
blocks in neural networks. In this type of layer, every neuron in the layer connects
to every neuron in the preceding layer. This allows the network to learn intricate
relationships between features extracted from the previous layer (Chollet & Keras
Team, 2015).
Dropout Layers:
Dropout layers introduce a regularization technique to combat overfitting in
neural networks. During training, a random subset of neurons within the layer is
temporarily "dropped out" by setting their activations to zero. This forces the
network to learn redundant features and rely less on individual neurons, ultimately
improving the model's generalizability (Srivastava et al., 2014).
SGD – Optimizing the Network:
Stochastic Gradient Descent (SGD) is a widely used optimization algorithm for
training neural networks. It iteratively updates the weights of the network's neurons
to minimize the loss function, a measure of error (Robbins & Monro, 1951). Here's
a breakdown of the basic process:
1. Forward Pass: Calculate the network's output for a given input and compute
the corresponding loss.
2. Backward Pass: Propagate the error back through the network, calculating
the gradients (slopes) of the loss function with respect to each weight.
3. Update Step: Adjust each weight in the network proportionally to its
corresponding gradient and a learning rate parameter.
Essentially, SGD takes small steps down the "loss landscape" (a
multidimensional terrain representing the error) to find the minimum point, which
corresponds to the optimal set of weights for the network (Robbins & Monro,
1951).
import nltk
from nltk.stem import WordNetLemmatizer
output_row = list(output_empty)
output_row[classes.index(document[1])] = 1
training.append([bag, output_row])
# Shuffle training data
random.shuffle(training)
# Configure the neural network model with the Stochastic Gradient Descent (SGD) optimizer
sgd = SGD(learning_rate=0.01, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
import config
# Load WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
intents = json.loads(open('intents.json').read())
def clean_up_sentence(sentence):
"""
Tokenizes and lemmatizes the input sentence.
Parameters:
- sentence (str): The input sentence to be cleaned.
Returns:
- List[str]: A list of lemmatized words from the input sentence.
"""
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word) for word in sentence_words]
return sentence_words
def bag_of_words(sentence):
"""
Converts the input sentence into a bag of words representation.
Parameters:
- sentence (str): The input sentence.
Returns:
- np.array: A numpy array representing the bag of words.
"""
sentence_words = clean_up_sentence(sentence)
bag = [0] * len(words)
for w in sentence_words:
for i, word in enumerate(words):
if word == w:
bag[i] = 1
return np.array(bag)
def predict_class(sentence):
"""
Predicts the intent class and probability of the input sentence.
Parameters:
- sentence (str): The input sentence.
Returns:
- List[dict]: A list containing dictionaries with 'intent' and 'probability'.
"""
bow = bag_of_words(sentence)
res = model.predict(np.array([bow]))[0]
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
Parameters:
- intents_list (List[dict]): List of intents and their probabilities.
- intents_json (dict): JSON containing the available intents and responses.
Returns:
- str: The selected response based on the predicted intent.
"""
tag = intents_list[0]['intent']
list_of_intents = intents_json['intents']
for i in list_of_intents:
if i['tag'] == tag:
result = random.choice(i['responses'])
break
return result
print(config.STARTING_MESSAGE)
while True:
message = input(config.USER)
ints = predict_class(message)
elif goodbye_detected:
# Match "goodbye" tag from ints with "goodbye" from intents
goodbye_responses = [intent['responses'] for intent in intents['intents'] if
intent['tag'] == 'goodbye']
if goodbye_responses:
print(config.BOT, random.choice(goodbye_responses[0]))
break
else:
res = get_response(ints, intents)
print(config.BOT, res)
config.py
To make the code more reusable all messages are in the separate file. This
makes it easier to make necessary adjustments to them in the future.
UNKNOWN_INPUT:str = "I'm sorry, I don't understand. Can you please provide more
information?"
STARTING_MESSAGE:str = "GO! Bot is running!"
USER:str = "You: "
BOT:str = "Bot: "