ChatBots

Shashanka Shekhar Sharma
DataX Journal
Published in
12 min readNov 17, 2023

I believe that at the end of the century the use of words and general educated opinion will have altered so much that one will be able to speak of machines thinking without expecting to be contradicted. ~Alan Turing

Chatbots, also known as Chatterbots, are computer programs or AI systems created to emulate human conversations with users. They engage with users through text or voice interfaces, mimicking natural human interactions. Currently, this technology has found widespread applications in customer service, e-commerce, healthcare, finance, education, and entertainment. Nevertheless, the chatbot industry has immense potential for further expansion, promising increased productivity across various sectors. As it continues to evolve, chatbots have the capacity to revolutionise industries, making them more efficient and user-friendly.

credits: stock.adobe.com

Types of Chatbots

  1. Rule Based Chatbots: Operates on predefined rules and patterns and works on a sets of predefined set of instructions. They are relatively simple
  2. AI powered Chatbots: Leverage AI and ML to learn and adapt from interactions providing more personalised content on every interaction
  3. Virtual Assistants: Advanced chatbots which have the ability not only to converse with the user but also solve some tasks like making calls, setting alarms, etc.

The Turing Test

Alan Turing’s article titled ‘Computing Machinery and Intelligence’ came up with a testimony to determine the intelligence of a machine named Turing test. According to this, intelligence of a machine is measured by its ability to impersonate human a human in real time so that a person feels he is talking to a human and not a machine

History of Chatbots

Chatbots predate the term itself, with “Chatbot” coined after Michael Loren Maulden’s creation, Julia, in 1994. Notable early chatbots, ELIZA (1966) and PARRY (1972), initially focused on text-based conversations but have since evolved into multifunctional tools. Recent chatbots, like ALICE, Jabberwackey, and DUDE, exhibit advanced capabilities. In 1984, Racter, a chatbot, authored “The Policeman’s Beard is Half Constructed.” The CYRUS project (1978–1983), led by Janet Kolodner, developed a chatbot emulating Cyrus Vance, employing case-based reasoning and daily updates from United Press International. Following Vance’s resignation, a chatbot mimicking successor Edmund Muskie was created. These developments showcase the continuous growth of chatbot technology.

Design of Various Chatbots

Weak AI chatbots are designed for specific, narrow functions and typically lack reasoning capabilities. ALICE and ELIZA are examples of weak AI chatbots that rely on pattern matching techniques but do not possess advanced reasoning abilities. They use markup languages like AIML to function within a predefined conversational scope. In contrast, strong AI chatbots like Jabberwackey employ real-time learning and adaptability based on user interactions, going beyond static databases. These advanced chatbots integrate real-time learning with algorithms to improve their conversational abilities dynamically.

Recent chatbots, including OpenAI’s ChatGPT, utilize neural networks, specifically the Generative Pre-Trained Transformer (GPT) architecture, which has become a common approach in developing new chatbots. While ChatGPT has faced criticism for knowledge accuracy, it has gained popularity for its historical knowledge and ability to provide detailed answers. Numerous projects, such as Microsoft’s BioGPT for answering biomedical questions and the 2017 GSOC DBpedia project, which built a chatbot capable of communication through Meta’s Messenger, continue to advance and improve chatbot technology in various domains.

ELIZA

The appellation “Eliza” is derived from the human persona in George Bernard Shaw’s theatrical production “Pygmalion,” denoting Eliza Doolittle, who underwent instruction to acquire an aristocratic accent.

Eliza, a pioneering natural language processing computer program, was developed by German Scientist Joseph Weizenbaum at MIT between 1964 and 1966 which was one of the earliest programs that could attempt the Turing’s Test. Its primary objective was to simulate conversations with human beings, employing a remarkable mastery of pattern recognition and specific response techniques, enabling it to provide human-like responses. Operating with a pattern-based analysis and response strategy, Eliza sought to stimulate self-reflection and introspection in human-machine interactions, despite its absence of genuine understanding. The academic community of its time recognized its human-like behavior and saw potential applications in the field of medicine, particularly in assisting doctors in treating patients with psychological disorders. Eliza’s approach involved identifying keywords in user statements and mirroring them back through simple phrases or questions. In situations where it couldn’t discern an appropriate response, it would revert to generic prompts like “Please go on” or “Tell me more,” effectively creating an illusion of empathy and engaging users in therapeutic-like conversations. Its script, named DOCTOR, emulated a Rogerian psychotherapist, responding to non-directional user questions based on predefined rules.

Regrettably, the original source code of Eliza has been lost due to the limited publishing practices of its era. However, the discovery of the MAD-SLIP source code in the MIT Archives, now available on various platforms, is of immense historical significance. It not only reflects the programming language and techniques of its time but also exemplifies the early stages of software layering and abstraction, laying the groundwork for advanced software engineering practices. Notably, Eliza participated in a computer-only conversation with another early AI program, Parry, at the International Conference on Computer Communication. Eliza assumed the role of a Rogerian psychologist, while Parry simulated a schizophrenic patient, highlighting the pioneering strides made in early AI research.

Credits: Emma Goldman

Working of Eliza:

Eliza’s operation involved keyword analysis, the assignment of values to these keywords, and the transformation of input into output. The DOCTOR script was developed within a psychotherapy context to circumvent the need for a real-world knowledge database. This algorithm produced deceptively intelligent responses that often led users, including its creator, to form emotional attachments and occasionally forget they were conversing with a machine. This remarkable ability to induce powerful delusional thinking in normal individuals left Joseph, the creator, astonished. Eliza stands as a milestone in the history of programming, marking the first attempt to achieve human-machine interaction with the goal of creating the illusion of a genuine human conversation.

Disadvantage of Eliza

Eliza is incapable of learning new pattens of speech or words through interactions alone. Edits must be made to Eliza’s active script in order to change the manner by which the program operates. Its replies are already scripted and thus there is no generation of any text. Even though it was a revolutionary innovation of that age, in present context it’s of no use.

Design

Eliza, built on the pattern matching and substitution methodology, was originally written in the MAD-SLIP programming language for the IBM 7094 time-sharing system, enabling natural language processing with a computer. Its language capabilities were provided through separate scripts written in a Lisp-like representation. Joseph Weizenbaum identified five key technical challenges that Eliza needed to address:

  • keyword identification
  • minimal context discovery
  • appropriate transformation selection
  • generating responses in the absence of keywords
  • providing editing capabilities for Eliza scripts.

To overcome these challenges, Eliza was designed without a built-in contextual framework, relying on scripts to instruct how it should respond to user input. Eliza’s response process begins with the examination of user input for keywords, with the identified keywords organized into a keystack, where the highest-ranking keyword sits at the top. The input sentence is then manipulated and transformed according to the rule associated with the highest-ranking keyword. For instance, when it encounters responses like “alike” or “same,” Eliza would reply with a prompt such as “In what way?” This approach allowed Eliza to engage in text-based conversations with users and offer responses that gave the illusion of understanding and empathy.

💡 A Keyword is a word designated as important by the acting Eliza script which assigns to each keyword a precedence number or rank designed by the programmer.

Eliza employs specific transformations as dictated by the script, even disregarding context. It can, for instance, switch first-person pronouns to second person and vice versa. Words with high precedence take precedence over contextual patterns, ensuring independent treatment. The transformation process involves a decomposition rule and a reassembly rule to manipulate and construct responses based on the recognized keywords.

Future Aspects

The Javascript version of Eliza was originally created by Michael Wallace and was significantly enhanced by George Dunlop.

Cloning ELIZA in Python with more features.

This code uses pattern matching techniques as used by ELIZA but some extra features which uses modern techniques. Sentiment Analysis is used in the program which is used to analyse the emotions of the people using the user input texts.

With the help of libraries and using the concept of lists, the predefined pattern matching technique ELIZA is being imitated. However, this is just an initial attempt. One cannot expect it to converse about anything. This can only be done after the analysis of the csv files (namely feedback_data and conversation_data). With more number of responses, the programmer will be able to bring up more personalised patterns and will make ELIZA more effective for the users.

The library textblob is used to analyse the sentiment of the text and will rate the polarity of the text. If it is greater than 0, it is considered that the user is postive enough but if it is the opposite, the user might not be in happy state of mind. Sentiment analysis will be shown to the user every 5 conversation so as to make them introspect themselves and make them share more of their feelings.

In order to keep record and make improvement in the code, the inputs of the users are saved. This step is taken so as to get more idea about what people are asking ELIZA and accordingly updates can be made on it. This questions the privacy of users but it can be considered as a sacrifice for the betterment of the code.

A conversation counter is being made as well as the time of conversation is also printed to the user in the end. Users are also given option to submit their feedback if they wish to. This will help the program come up with more public centric updates.

Code is also in this link along with documentation file

'''
Chatbot Implementation

This code represents a sophisticated implementation of a chatbot that combines pattern matching techniques akin to ELIZA with modern features, such as sentiment analysis.
It engages users in text-based conversations while providing emotional insights.
It harnesses the TextBlob library to gauge sentiment in user inputs, fostering self-reflection and sharing of feelings.
The code is designed for future enhancement through data analysis, particularly the content stored in CSV files (feedback_data and conversation_data).
This accumulation of user interactions is essential for refining the chatbot's responses and making it more personalized.
Furthermore, the code includes mechanisms to keep records, timestamps, and even invites user feedback, ensuring that it evolves to cater to the users' needs.
The professional and responsive nature of this code signifies a powerful tool for simulating human-like interactions with users, paving the way for potential advancements in the field of conversational AI. The code encompasses elements such as date and time retrieval, user data logging, sentiment analysis, and user feedback integration, offering a comprehensive conversational experience.

Key Features
- Pattern-based responses to user input
- Sentiment analysis of user's emotional state
- Date and time retrieval
- User feedback collection
- Conversation history recording

Usage:
- Input "bye" to exit the conversation.
- Input "date and time" to get the current date and time.
- The program saves user prompts for feedback and analysis.

Developed By
Shashanka Shekhar Sharma
12.10.2023
'''
#Importing all libraries
#These libraries are used to bring more functionality to the Chatbot
import re
import random
from textblob import TextBlob
import datetime
import time
import csv
import os

#Printing the Historic ELIZA Deisgn
print('''Welcome to
EEEEEEE LL IIIIIIII ZZZZZ AAAA
EEE LL II ZZ AA AA
EEEEEEE LL II ZZ AAAAAAAA
EEE LL II ZZ AA AA
EEEEEEE LLLLLLLLL IIIIIIII ZZZZZZ AA AA''')

#This code is implemented to record the time spent by the code
start_time = time.time()

#CSV Files are created to store the feedback and conversations given by the user
csv_filename = 'feedback_data.csv'
conversation_csv_filename = 'conversation_data.csv'

#Function to get current date and time
def get_date_and_time():
now = datetime.datetime.now()
return now.strftime("Current date and time: %Y-%m-%d %H:%M:%S")

#Creating a list of predefined responses similar to that of Eliza using dictionary
response={
"hello":["Hello how can I help you"],
"hi":["Hey there! How can I help you"],
"how do you do?":["I am fine. What about you?"],
"i am (sad|feeling sad|depressed|anxious)":["Do something you enjoy. When you're feeling sad, it can be helpful to do something that you enjoy and that makes you feel good. This could be anything from reading a book to taking a walk to spending time with loved ones. If you want to talk I am always here"],
"(i'm having problems with my partner|how can i improve my relationship with my family)": ["It's normal to have problems in relationships from time to time. All relationships require work and effort to maintain. Try talking to them about the problem to reduce communication gaps","Would you like to share more about it?"],
"what can you do(.*)":["I can do the follwing: 1. Be your Psychotherapist 2. Get you date and time 3. Give your sentiment analysis"],
"how are you":["I am fine. What about you?"],
"i feel (.*)":["Why do you feel {}?","How long have you been feeling {}"],
"i am (.*)":["Why do you say you are {}?","How long have you been {}"],
"i (.*) you":["Why do you {} me?","What makes you think you {} me"],
"i (.*) myself":["Why do you {} yourself?","What makes you think you {} yourself?"],
"(.*) sorry (.*)":["There no need to apologize.","What are you apologizing for?"],
"(.*) friend (.*)":["Tell me more about your friend","How do your friend make you feel?"],
"yes":["You seem quite sure. Can you elaborate?","Ok,but can you elaborate."],
"no":["Why not?","Ok, Can you elaborate a bit?"],
"i am happy":["I am glad that you are. What makes you feel so?"],
"i want to talk (.*)":["Sure just go on. I am listening."],
"i am sad":["I am sorry to hear that. What makes you feel so."],
"i like to (.*)":["Well that's great. What makes you like it."],
"i like (.*)":["Well I see, why do you like (.*)"],
"(.*)i failed (.*)":["I can imagine how disappointed you must be feeling right now.Do you want to talk about it"],
"i am not sure about (.*)":["It is okay to not have answers about some things","Okay, what's causing the uncertainity."],
"what should I do about (.*)":["It's important to conisder your own feelings and priority before taking a decision. What's bothering you about (.*)"],
"i love (.*)":["I get that. Love is a powerful emotion. Tell me more about (.*)"],
"i (don't know|can't decide) what to do with my life":["Many people feel that way at times. Even I did! All you need to do is divert your focus to your hobbies and passion. What are yours?","It's okay to feel like that. Many people do. What are your interests and passion?"],
"i want to (visit|go) (.*)":["I am glad that you have such plans","I wish I could go too"],
"my (boyfriend|girlfriend|father|mother|brothers|brother|sister|sister)":["Oh I see. Tell more about how the relationship was like"],
"i (dream to|want to|aspire to|will) (.*)":["It's good to have dreams","What motivates you to be that?"],
"i (love|hate) my (job|school)":["Many people (love|hate) their (school|jobs). What's your reason?"],
"i feel stressed":["Dealing with stress is a pretty diffcult task. Try taking some break","Why don't you take a break?"],
"what (do you think|are your thoughts) about (.*)":["We are here to talk about you. Not me.","I would like to focus on you rather than my opinions. Tell me how was your day?"],
"i am (frustrated|annoyed) with (.*)":["I am sorry for how you feel. What makes you feel so?"],
"i lost (.*)":["In am sorry for your loss. How are you coping up with your loss?"],
"i am struggling with (.*)":["I can understand that. It's a part of life.","I am sorry to hear that. I hope things will be alright soon"],
"what is your name":["My name is ELiza and I am a Rogerian Psychotherapist"],
" (.*) ":["Please tell me more","Can you share more about your feelings? It will help you to loosen up your mind.","Can you elaborate on that?"],
"":["Why do you think that?","Please tell me more.","Let's change the focus a bit...tell me more about your family","Can you elaborate on that?"],
}

#Function to analyze sentiment
def analyze_sentiment(text):
analysis = TextBlob(text)
sentiment_score = analysis.sentiment.polarity
if sentiment_score > 0:
return "Sentiment Analysis: You seem to be feeling positive."
elif sentiment_score <= 0:
return "Sentiment Analysis: Let's talk more. You will feel better."

#Creating a list that will store the user's history
conversation_history = []

#Function to match user's input to a pre-defined response
def match_response(input_text):
for pattern, response_list in response.items():
matches = re.match(pattern, input_text.lower())
if matches:
choosen_response = random.choice(response_list)
return choosen_response.format(*matches.groups())
return "I am sorry. I am unable to understand what you are saying."
print("Welcome. I am Eliza. A Rogerian psychotherapist")
print("Your prompts will be saved in order to make improvements in the code")
print("Type date and time to get date and time")
print("Type bye to exit")

#Counter to keep track of conversations
conversation_counter = 0
if not os.path.exists(csv_filename):
with open(csv_filename, 'w', newline='') as csvfile:
fieldnames = ['Type', 'Text']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
if not os.path.exists(conversation_csv_filename):
with open(conversation_csv_filename, 'w', newline='') as csvfile:
fieldnames = ['Type', 'Text']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
while True:
user_input = input("You: ")
conversation_history.append({"user": user_input})
with open(conversation_csv_filename, 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Type': 'User', 'Text': user_input})

#Increment the conversation counter
conversation_counter += 1
if conversation_counter % 5 == 0:

#Analyze sentiment after every 5 conversations
sentiment_response = analyze_sentiment(user_input)
print(f"Eliza: {sentiment_response}")
if user_input.lower() == "bye":
feedback = input("Would you like to provide feedback (yes/no)? ")
if feedback.lower() == "yes":
user_feedback = input("Eliza: Please provide your feedback: ")
feedback=[]
feedback.append({"user":user_feedback})
for_feedback=["Thank you for your valuable feedback","Thank you for your time","Thank you","Thanks for being here. We will meet again"]
print(random.choice(for_feedback))
end_time = time.time()
time_spent = end_time - start_time
print(f"Time spent with Eliza: {time_spent:.2f} seconds")
print(f"Number of lines of conversations: {conversation_counter}")

#Append feedback to the CSV file
with open(csv_filename, 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Type': 'Feedback', 'Text': user_feedback})
else:
print("Goodbye")
end_time = time.time()
time_spent = end_time - start_time
print(f"Time spent with Eliza: {time_spent:.2f} seconds")
print(f"Number of lines of conversations: {conversation_counter}")
break
elif "programmer's data" in user_input.lower():
print("Eliza: Here are your past conversations:")
if os.path.exists(conversation_csv_filename):
with open(conversation_csv_filename, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(f"Type: {row['Type']}, Text: {row['Text']}")
if os.path.exists(csv_filename):
with open(csv_filename, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
print("Eliza: Here are your feedbacks:")
for row in reader:
print(f"Type: {row['Type']}, Text: {row['Text']}")
elif "date and time" in user_input.lower():
date_time = get_date_and_time()
print(f"Eliza: {date_time}")
else:
print("Eliza: " + match_response(user_input))

--

--