Data Science Career Path; Unit 26; Natural Language Processing; Alien Bot Project

Hi guys,


I’m currently working on the abovementioned project. Pasted below is my code.

I’ve tested it repeatedly and it works in all respects except one: the make_exit function does not execute if the bot gets to ask more than one question.

That is, if it asks me a first question (such as, “Does Earth have a leader?”) and I call the make_exit function, the code runs as it is supposed to. The conversation ends.

However, if I answer the first question and the bot proceeds to ask a second question, the make_exit function does not execute in response to the the second or any additional questions.

Consequently, the code does not enable the user to exit the conversation with the bot beyond the first question.

Any guidance you can provide will be really appreciated!

import re
import random

negative_responses = ("no", "nope", "nah", "naw", "not a chance", "sorry")

exit_commands = ("quit", "pause", "exit", "goodbye", "bye", "later", "stop")

random_questions = (
    "Why are you here? ", 
    "Are there many humans like you? ", 
    "What do you consume for sustenance? ", 
    "Is there intelligent life on this planet? ", 
    "Does Earth have a leader? ",
    "What planets have you visited? ",
    "What technology do you have on this planet? "
    "How long do humans take to metabolise?"
    "What is the method of food distribution"

name = ""

alienbabble = (
    # Your planet...
  {r'.*\s*your planet':
    ("My planet is a utopia of diverse organisms and species. ",
    "I am from Opidipus, the capital of the Wayward Galaxies. ")
  # why do you...?
     ("What makes you think I {0}?",
      "Do I {0}?",
      "Do you think I should {0}?")
  # why... ?
     ("I come in peace. ",
      "I am here to collect data on your planet and its inhabitants. ",
      "I heard the coffee is good. ")
  # what... ?
     ("Why do you ask? ",
      "What do you think? ")
  # it is...
     ("You seem very certain. Why is that? ",
      "Do you have any evidence? ")
  # I think...
     ("But you're not sure {0}? ",
      "Why do you think {0}? ")

     ("Please tell me more. ",
      "Tell me more! ",
      "Why do you say that? ",
      "I see. Can you elaborate? ",
      "Interesting. Can you tell me more? ",
      "I see. How do you think? ",
      "Why? ",
      "How do you think I feel when you say that? ")

def greet():
  name = input("Hello, what's your name?")
  will_help = input("Hi {}, I'm Derek. I'm not from this planet. Will you help me learn about your planet? ".format(name))
  for item in negative_responses:
    if item in will_help:
      print("Ok, we'll experiment on you first")
      return False
  return True

def make_exit(reply):
  for command in exit_commands:
    if command in reply:
      print("Hasta luego!")
      return True 
  return False 

def alienbot():
  if greet():
    reply = input(random.choice(random_questions)).lower()
    if make_exit(reply) == False:
      reply = converse(reply)
def converse(reply):
  for pair in alienbabble:
    for regex_pattern, alien_answers in pair.items():
      found_match = re.match(regex_pattern, reply)
      if found_match:
        alien_answer = random.choice(alien_answers)
        reply = input(alien_answer).lower()
        formatted_alien_answer = alien_answer.format(*[reflect(matching_group) for matching_group in found_match.groups()])
        reply = input(formatted_alien_answer).lower()
        return converse(reply)
reflections = {
    "i'm": "you are",
    "you're": "i'm",
    "was": "were",
    "i": "you",
    "are": "am",
    "am": "are",
    "i'd": "you would",
    "i've": "you have",
    "i'll": "you will",
    "my": "your",
    "you've": "I have",
    "you'll": "I will",
    "your": "my",
    "yours": "mine",
    "you": "I",
    "me": "you"
def reflect(response):
  words = response.split()
  for index, word in enumerate(words):
    if word in reflections:
      words[index] = reflections[word]
  return ' '.join(words)