Python Project: ELIZA (In 13 Lines of Code)

What is ELIZA?

To understand the Python project on ELIZA, we need a bit of background.

ELIZA is a computer program designed to simulate human-like conversation by using natural language processing techniques. It was created by Joseph Weizenbaum in 1966 at the Massachusetts Institute of Technology (MIT) and is considered one of the earliest examples of a chatbot.

The program was named after the character Eliza Doolittle from the play “Pygmalion,” who undergoes a transformation from a working-class girl to a refined lady through language training.

ELIZA works by analyzing the user’s input, searching for keywords, and using pre-programmed rules to generate responses that mimic those of a therapist. It was designed to simulate a Rogerian psychotherapist, a type of therapist who emphasizes empathy and reflection rather than offering direct advice or solutions.

ELIZA became famous in the late 1960s and early 1970s, as it was one of the first programs to create the illusion of a human-like conversation. It has since inspired numerous chatbots and conversational agents, and its legacy can still be seen in the development of modern AI-powered chatbots and virtual assistants.

Implement ELIZA in Python

ELIZA can be a fun programming project to make because it allows you to explore and experiment with natural language processing techniques and simulate human-like conversation. The project can also be a great opportunity to learn about rule-based programming and develop your programming skills.

Creating an ELIZA program can be challenging, as it requires analyzing and interpreting user input, identifying keywords and patterns, and generating appropriate responses. However, it can also be rewarding to see your program engage in convincing conversations with users.

Moreover, because ELIZA is a well-known and widely studied program, there are many resources and examples available online to help guide you through the process of creating your own version of the program. This can make the project more accessible and enjoyable for beginners and experienced programmers alike.

Overall, ELIZA can be a fun programming project to make because it allows you to explore the fascinating world of natural language processing while creating a program that can simulate a human-like conversation.

How ELIZA works?

It looks for simple patterns and substitutes to give the illusion of understanding from the computer (wiki).


  • You write I need cake it can look for a pattern I need *
  • Then it can ask Why do you need cake?

This can be done simply as follows.

eliza_response = 'What do you need?'

while True:
    s = input(eliza_response + ' ')

    if 'I need ' in s:
        your_need = s.split('I need ')[-1]
        eliza_response = 'Why do you need ' + your_need + '?'
    elif 'because' in s:
        your_cause = s.split()[-1]
        eliza_response = 'Why are you ' + your_cause + '?'
    elif s == 'quit':
        eliza_response = 'Can you tell me why?'

This can generate the following dialog.

What do you need? I need cake
Why do you need cake? because I am hungry
Why are you hungry? because it makes me less irritated
Why are you irritated? because I need to avoid low blood sugar
Why do you need to avoid low blood sugar? because I need a reward
Why do you need a reward? because I was good
Why are you good? because I need to learn Python
Why do you need to learn Python? because I need a job
Why do you need a job? quit

You can expand on this idea to make the conversation more real.

Want more Python projects?

This is part of 19 Python Projects and you can find the Fibonacci sequence project where you will learn 4 aspects of programming.


Recent Posts

Build and Deploy an AI App

Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…

5 days ago

Building Python REST APIs with gcloud Serverless

Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…

5 days ago

Accelerate Your Web App Development Journey with Python and Docker

App Development with Python using Docker Are you an aspiring app developer looking to level…

6 days ago

Data Science Course Made Easy: Unlocking the Path to Success

Why Value-driven Data Science is the Key to Your Success In the world of data…

2 weeks ago

15 Machine Learning Projects: From Beginner to Pro

Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…

2 weeks ago

Unlock the Power of Python: 17 Project-Based Lessons from Zero to Machine Learning

Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…

2 weeks ago