Archive

Posts Tagged ‘subprocess’

Talking Python

April 25, 2010 1 comment

The following was a small amusement that I put together for my 5 year old daughter. It must have made quite the impression on her as she has informed me that she wants to be a “horse programmer” when she grows up.

This program relies on another program called “espeak” which should be present on most Linux systems. This is a command line tool which takes  text and converts it to speech. If you are using a Mac you could adapt the program to use the command “say” which does the same thing (the command line arguments are bound to be different though). Windows doesn’t include such a tool, because Microsoft just doesn’t love you, however Google turned up this result when I searched for “Windows command line speech”.

Apart from that you will see a few Python constructs which may be interesting to Python newbies, such as calling another program from within your Python script, and generating random silly sentences.

Note that it also uses the new style of string formatting so you will need at least Python version 2.6.

#! /usr/bin/python

import subprocess
import random

def say(sentence):
    speed = "-s " + str(random.randrange(120, 200))
    pitch = "-p " + str(random.randrange(100))
    subprocess.call(["espeak", speed, pitch, sentence])

sentences = [
    "Hello {name}.",
    "{name}? What a {adjective} name!",
    "Congratulations, {name}! You have won a {noun}!",
    "Excuse me {name}. Have you seen my {adjective} {noun}?",
    "Hello {name}. Are you going to Wellington Zoo soon {name}?",
    "What is your name {name}? Oh, that's right, silly me. Your name is {name}!",
    "{name}! Your {adjective} {noun} is {verb}ing! {adverb} grab a {noun2}, {name}!",
    "What is your favourite {noun}, {name}? Is it the {adjective} one?",
    "Quickly quickly {name}! No and Yes!",
    "Where's your mummy {name}? Is she {verb}ing today?",
    "That's what I said {name}",
    "What are you doing {name}? Are you {adverb} {verb}ing? Do you think that's a good idea?",
    "{noun}s are fun. Don't you think so {name}?",
    "Red alert. We go beserk. No {name} moves, no {name} gets hurt.",
    "Please {name}. May we go to the {noun} shop?",
    "Happy birthday to you. Happy Birthday to you."
    " Happy birthday dear {name}. Happy birthday to you!",
    "This is a shout out to {name}. Keep it real homey.",
    "Last night I had the strangest dream. {name} was {adverb} riding on "
    "a {adjective} {noun}, being chased by a thousand {adjective2} {noun2}s. "
    "Luckily, the {noun} was just too {adjective} for the {noun2}",
    "{name} is a good reader!",
    "Time for bed {name}. Go on... off you go!",
    "Make me a {noun} {name}, as fast as you can! I would like a {adjective} one please!",
    "Who ate all of my cake? {name}, was it you?",
    "I'm lost {name}! Can you tell me where I am?",
    "Do you like the circus {name}?",
    "This is the {adjective}est {noun} that I have ever seen. It is even more {adjective} than "
    "that {noun2} that I saw yesterday. Although, to be honest, that {noun2} "
    "was probably more {adjective2} than {adjective}",
    "I just saw a {adjective} {noun} {adverb} {verb}ing a {adjective2} {noun2}.",
    "Never let a {noun} {verb} more than once a day, otherwise it may {adverb} turn "
    "into a {noun2}",

    ]

nouns = [
    "pony",
    "horse",
    "cheesecake",
    "muffin",
    "dragon",
    "car",
    "DVD",
    "Computer",
    "Rollercoaster",
    "Hamburger",
    "Chestnut Park",
    "Cat",
    "dog",
    "TV",
    "soccer ball",
    "sandwich",
    "X box",
    "house",
    "bus",
    "van",
    ]

adjectives = [
    "silly",
    "fast",
    "brown",
    "pink",
    "yellow",
    "blue",
    "purple",
    "golden",
    "funky",
    "red",
    "bouncy",
    "pretend",
    "angry",
    "madd",
    "sadd",
    "badd",
    "grumpy",
    "stupid",
    "awesome",
    "ridiculous",
    "dubious",
    "dodgy",
    "flightless",
    "sleepy",
    "antogonistic",
    ]

verbs = [
    "talk",
    "ignore",
    "runn",
    "grump",
    "riyd",
    "gallop",
    "program",
    "slide",
    "gasp",
    "shout",
    "jump",
    "read",
    "play"
    "dodge",
    "crawl",
    "whisper",
    "hiyd",
    "smell",
    "sink",
    ]

adverbs = [
    "quickly",
    "suddenly",
    "slowly",
    "agrily",
    "Greedily",
    "happily",
    "mostly",
    "rapidly",
    "forcefully",
    "woodenly",
    "passionately",
    ]

n = ""
while True:
    n = raw_input("What is your name? ")
    if not n: break
    #[say(word) for word in random.choice(sentences).format(name=n).split()]
    for i in range(3):
        say(random.choice(sentences).format(name=n,
                                            noun = random.choice(nouns),
                                            noun2 = random.choice(nouns),
                                            verb = random.choice(verbs),
                                            adverb = random.choice(adverbs),
                                            adjective = random.choice(adjectives),
                                            adjective2 = random.choice(adjectives),
                                            ))

 
Advertisements