Q & A About Chronic Kidney Disease Using an AI Chatbot

Nancy Shnoudeh
5 min readMar 27, 2021

--

Chatbots give businesses a new edge against their competitors, from customer service bots at your favourite store, or doctor bots being used at justanswer.com. Knowing this, I thought to myself, “how cool would it be to create my own?” and the answer was, “it would be very cool 😎”. So I did, and this article is going to give you all the steps to make one as well!

This chatbot answers questions regarding Chronic Kidney Disease (CKD). It can inform the user on what CKD is, the risks and symptoms of CKD, and tell you when to see a doctor, all based on this article.

I would let the bot tell you what CKD is after we finish creating it 😉, but here’s some backround information:

CKD also known as chronic kidney failure, is the loss of kidney function

What do kidneys do for us?

- Kidneys filter waste and excess fluid from our blood.

Causes:

- It’s can be caused by type 1 or 2 diabetes, high blood pressure, glomerulonephritis (inflammation of the kidneys filtering units), vesicoureteral (condition that causes urine to back up into the kidneys), and many more.

Treatment:

- Kidney disease does not usually become apparent until the kidneys function is significantly impaired. This can lead to the disease being fatal wihtout treatment such as dialysis (artificial filtering) or a kidney transplant.

Mindset

When working on this project, keep in mind the mindset, to help you complete the algorithm… FIO — Figure. It. Out. 🔑

If there is anything you are unsure about, find some way to get the answer.

Some FIO options 💡:

  • Commenting your question directly on this article
  • Finding a video
  • Googling it
  • Talking to different people
  • Searching on stack overflow
  • Checking your code to make sure it’s the same as my code

Let’s get started!

  1. Open up an IDE (I used google collaboratory).
  2. Write a description for your code
#description: This is a 'smart' chat bot program

3. We are going to install nltk and newspaper3k

pip install nltk

and…

pip install newspaper3k

4. We are now going to import our libraries.

What are libraries?📚

— Libraries are pre-compliled routines that the program uses. Each library serves a different purpose in creating the algorithm.

#import the libraries
from newspaper import Article
import random
import string
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import warnings
warnings.filterwarnings('ignore')

5. Download the punkt package

#Download the punkt package
nltk.download('punkt',quiet=True)

After running the code, your output should say, “True”.

6. We are going to get our article/data

#Get the article
article = Article('https://www.mayoclinic.org/diseases-conditions/chronic-kidney-disease/symptoms-causes/syc-20354521')
article.download()
article.parse()
article.nlp
corpus = article.text

For this section of code:

Now when we refer to “article”, we call on the actual article by including the link as you see here…

article = Article('https://www.mayoclinic.org/diseases-conditions/chronic-kidney-disease/symptoms-causes/syc-20354521')

(In this line of code, I linked the article, which I also downloaded.)

7. I then proceeded to print the text from the article

#print the articles text
print(corpus)
**This picture of the output does not include all of the text.

8. Tokenization

What is tokenization?

Tokenization in python refers to the splitting up of a large body of text into smaller words, or lines of text. The tokenization feature is built into the nltk module that we installed in step 3.

#tokenization
text = corpus
sentence_list = nltk.sent_tokenize(text) # A list of sentences

9. Print the list of sentences.

After splitting up or text or “tokenizing” it, we are now going to print the sentences.

#Print the list of sentences
print(sentence_list)
**Small portion of text from the output

10. Create a function to return a random greeting response to a users greeting.

Pro tip: Pay close attention to the indentation!

#A function to return a random greeting response to a users greeting
def greeting_response(text):
text = text.lower()
#Bots greeting response
bot_greetings = ['howdy', 'hi', 'hey','hello', 'hola']
#users greeting
user_greetings = ['hi', 'hey', 'hello', 'hola', 'greetings', 'wassup']
for word in text.split():
if word in user_greetings:
return random.choice(bot_greetings)

We can run the code, to ensure it works!

The bot randomizes its responses, so by saying one of the above greetings, the bot will also respond accordingly.

**Does not include the full picture

We now have to create the bots response (not just greeting response).

#create the bots response
def bot_response(user_input):
user_input = user_input.lower()
sentence_list.append(user_input)
bot_response = ''
cm = CountVectorizer().fit_transform(sentence_list)
similarity_scores = cosine_similarity(cm[-1], cm)
similarity_scores_list = similarity_scores.flatten()
index = index_sort(similarity_scores_list)
index = index[1:]
response_flag = 0
j = 0
for i in range(len(index)):
if similarity_scores_list[index[i]] > 0.0:
bot_response = bot_response+' '+sentence_list[index[i]]
response_flag = 1
j = j+1
if j > 2:
break
if response_flag == 0:
bot_response = bot_response+' '+"I apologize, I don't understand."
sentence_list.remove(user_input) return bot_response

Okie dokie! Lets start the chat:

#start the chat
print('Doc Bot: I am Doctor Bot or Doc Bot for short. I will answer your queries about Chronic Kidney Disease. If you want to exit, type bye')
exit_list = ['exit', 'see you later', 'bye', 'quit', 'break', 'stop']while(True):
user_input = input()
if user_input.lower() in exit_list:
print('Doc Bot: Chat with you later!')
break
else:
if greeting_response(user_input) != None:
print('Doc Bot: ' +greeting_response(user_input))
else:
print('Doc Bot:' +bot_response(user_input))

Overview:

This is the output I received once I asked my bot for the risks of CKD. As you may notice, it didn't directly answer the question. It would require more data cleansing and training to improve it’s accuracy.

**Only a portion of the output (some parts are cut-out for the frame of the picture)

Doc Bot report card:

  • Can it respond to a greeting? Yes.
  • Can it let the user know when it doesn’t understand a query? Yes.
  • Can it respond to something on the exit list? Yes.
  • How clear can it answer a question? 5/10

Overall, this chatbot was very simple and quick to create. It responds well to what we manually coded, but when it’s time for the machine learning aspect, it isn’t able to give us an accurate response.

In conclusion, these AI chatbots are being used all around us, from shopping, virtual doctors, customer service, and more! If you have questions or complications building your chatbot, let me know so we can work through it together!

Also, be sure to check out this video from ComputerScience, for further clarification or assistance!

Let’s connect 🤝

Newsletter

LinkedIn

--

--