Some code that generates random sentences and simulates a very inattentive therapist
# The random sentences example from class
import random
def sentence():
nouns = ["Colin", "Cedric", "Bobby", "Brittany", "Pegah", "Jon", "Swati"]
verbs = ["runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
manner = ["very badly", "up and down", "over a log", "very loudly", "like a Ninja", "in a peculiar manner"]
circumstance = ["while debugging","instead of grading", "while typing on the CoWeb.", "during recitation"]
adverbial = manner + circumstance
print random.choice(nouns), random.choice(verbs), random.choice(adverbial )
# A miniaturized version of Eliza, the very inattentive therapist.
# Ask your doctor if Eliza is right for you.
def eliza():
input = ""
while input <> "stop": # This is a new type of loop. Unlike the for loop, we don't know when to stop until it is time to do so.
printNow(response(input)) # Have to use printNow() or the prompts don't get printed until after(!) the therapy session.
input = requestString("") # This is a useful JES function to get text input from a user.
printNow(input)
print "Thank you. Your 50 minutes are up. Don't forget your next session."
import random
def response(input):
# Starting input
if input == "":
return "Hello, what can I help you with today?"
# Look for psychologically loaded words and respond with stock replies.
if input.find("mother") <> -1:
return "Are you attached to your mother?"
if input.find("sex") <> -1:
return "No sex, please. I'm British."
if input.find("try") <> -1:
return "Do or do not. There is no try."
# If none of those worked...
# Look for a juicy content word (assume short words are function words)
allWords = input.split()
contentWords = []
for word in allWords:
if len(word) > 3:
contentWords.append(word)
word = random.choice(contentWords)
# Respond with a stock sentence about the word
return randomSentence(word)
def randomSentence(word):
preSentences = ["Tell me more about your ", "Do you have a hang-up about your "]
postSentences = [" seems important to you.", " can be a problem"]
if random.choice([0,1]) == 0:
return random.choice(preSentences) + word
else:
return word + random.choice(postSentences)
Link to this Page