Section 06

The Code

Foundation of Artificial Intelligence Computing Machinery and Intelligence 1950

The Code

Build a tiny Turing Test yourself

This code creates the simplest possible Turing Test simulation: a rule-based chatbot (the “machine” player) that tries to pass as human, and a scoring function that measures how often it fools a simulated interrogator.

This is not a modern AI — it uses simple keyword matching, just like the early chatbots of the 1960s (ELIZA, the first famous chatbot, used exactly this approach). Running this code will help you feel the difference between a machine that follows rules and one that understands.

# What this code does: Simulates a basic Turing Test with a rule-based chatbot
# Paper: Computing Machinery and Intelligence — Alan Turing (1950)
# Run free at: https://colab.research.google.com/

import random

# --- The Machine: a simple rule-based chatbot ---
# This is how chatbots worked in the 1960s — pattern matching on keywords.
# It has no understanding. It just matches patterns and picks a response.

responses = {
    "hello":    ["Hello! Nice to chat.", "Hi there.", "Hey, how are you?"],
    "how are":  ["I'm doing well, thanks for asking.", "Pretty good! A bit tired honestly.", "Fine, just thinking about a lot of things."],
    "name":     ["You can call me whatever you like.", "I'd rather not say.", "Does it matter?"],
    "think":    ["I think about things all the time, yes.", "Hmm, that's a deep question.", "What kind of thinking do you mean?"],
    "feel":     ["I feel okay, thanks.", "Bit of a mixed day, honestly.", "Hard to put into words."],
    "cricket":  ["I love cricket! Kohli is brilliant.", "Test cricket is the real test of patience.", "Can't beat a good IPL match."],
    "poetry":   ["I'm not great at poetry, honestly.", "I've tried writing poems but I'm not very good.", "I appreciate it more than I write it."],
    "weather":  ["It's been hot lately, hasn't it?", "I hope the monsoon comes early this year.", "Typical weather for this time of year."],
    "default":  ["Interesting. Tell me more.", "I see what you mean.", "That's a good point.", "Hmm, I'm not sure I agree.", "Can you explain that further?"]
}

def machine_responds(question):
    """The machine picks a response based on keyword matching."""
    question_lower = question.lower()
    for keyword, options in responses.items():
        if keyword in question_lower:
            return random.choice(options)      # pick a random matching response
    return random.choice(responses["default"]) # fall back to a generic reply

# --- The Human: just echoes sensible pre-written answers ---
# In a real test, this would be a real human typing.

human_answers = [
    "I'm doing okay, thank you.",
    "That's a really interesting question.",
    "I've been thinking about this a lot lately.",
    "Yes, I feel that way sometimes too.",
    "It depends on the situation, honestly.",
]

def human_responds(question):
    """Simulates a human giving a reasonable answer."""
    return random.choice(human_answers)

# --- The Interrogator: tries to guess which is human ---
# Our simulated interrogator uses a simple heuristic:
# if the response sounds "too generic" (short and vague), guess machine.

def interrogator_guesses(response):
    """Returns 'human' or 'machine' based on response length and variety."""
    if len(response) < 20:          # very short answers feel robotic
        return "machine"
    if "?" not in response and len(response.split()) < 6:
        return "machine"            # no follow-up question AND very short = suspicious
    return "human"                  # otherwise assume human

# --- Run the simulation ---

questions = [
    "Hello, how are you today?",
    "Do you enjoy poetry?",
    "What do you think about cricket?",
    "Can you describe how you feel right now?",
    "What is your name?",
]

print("=== TURING TEST SIMULATION ===\n")
machine_fooled = 0
human_fooled = 0

for q in questions:
    m_response = machine_responds(q)   # machine answers
    h_response = human_responds(q)     # human answers

    m_guess = interrogator_guesses(m_response)
    h_guess = interrogator_guesses(h_response)

    if m_guess == "human":
        machine_fooled += 1            # machine successfully fooled interrogator
    if h_guess == "machine":
        human_fooled += 1              # interrogator wrongly flagged human as machine

    print(f"Q: {q}")
    print(f"  Machine said: '{m_response}' → Interrogator guessed: {m_guess}")
    print(f"  Human said:   '{h_response}' → Interrogator guessed: {h_guess}")
    print()

print(f"Machine fooled interrogator {machine_fooled}/{len(questions)} times")
print(f"Human was misidentified     {human_fooled}/{len(questions)} times")
print(f"\nTuring Test result: Machine {'PASSES' if machine_fooled >= len(questions)//2 else 'FAILS'}")

What you should see when you run this:

A table of questions and answers, with the interrogator’s guesses. The machine will fool the interrogator sometimes but not always — because its responses are generic and do not engage with the specific question. The human will rarely be misidentified.


What to change to experiment:

  1. Add more keywords and responses to the responses dictionary. Notice how much better the machine gets as you add more specific patterns.

  2. Change the interrogator’s heuristic — make it smarter. Can you write an interrogator that catches the machine more reliably? What features of a response give away a machine?

  3. Replace the human answers with actual typed input by changing human_responds to input("Your answer: "). Now you can play the game yourself and see if the interrogator catches you.


Next: Why It Mattered →