, , , , , , , , , , , , ,

In my last post I created a virtual girlfriend for Arkwood. He simply spoke into the microphone attached to the Raspberry Pi and Google converted his speech into text. The Python program then fetched his sweetheart’s response from a file, before using Google to convert her retorts to an audio output. Fantastic. The Pi has provided my buddy with a bidirectional love affair, without fear of repulsion.

But, as always with Arkwood, he bites the hand that feeds. ‘Can I have a virtual mother too!’ he cried, giving me some sob story of being abandoned by his parents in a supermarket car park. The big softie I am, I conceded. ‘Very well.’

Okay. So first off we need to create a base class for the virtual people in my Belgian friend’s life:

import urllib
from urllib2 import urlopen
import json

class Person(object):

    # set person up
    def __init__(self, is_active, hello, goodbye):
        self.is_active = is_active
        self.hello = hello
        self.goodbye = goodbye

    # check for hellos and goodbyes 
    def is_hello_goodbye(self, my_chat):
        if (my_chat == self.hello):
            self.is_active = True
            return True
        elif (my_chat == self.goodbye):
            self.is_active = False
            return True
        return False

Cool. Now every person we create will know how to handle Hello and Goodbye requests, which determines whether they are active to the program or not.

Now let’s create mother:

class Mother(Person):

    # ask mother something
    def speak_to(self, my_chat):
            # encode the question
            question = urllib.quote_plus(my_chat)
            # build url
            url = "http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=[get your own key]&output=json&query=" + question

            # get answers
            answers = json.load(urlopen(url))

            # find her response to my pleading
            answer = answers["all"]["questions"][0].get('ChosenAnswer')
            if (answer is not None):
                return answer
            # otherwise just return her stock phrase 
            return "Mummy is busy right now"
            print ("Error getting words of wisdom from mother")

Mother inherits from Person, and when spoken to uses Yahoo! to obtain a suitable response. You’ll need to get your own api key from Yahoo Developer Network to utilise their Yahoo! Answers service, fetching back some JSON to interrogate.

Of course, we still have Arkwood’s girlfriend at hand, also inheriting from Person:

class Girlfriend(Person):
    # ask girlfriend something
    def speak_to(self, my_chat):
            # get textbook
            book = {}
            with open("textbook.conf") as textbook:
                for line in textbook:
                    me, her = line.partition("=")[::2]
                    book[me.strip()] = her.strip()
            # find her response to my smooth talkin'
            if (my_chat in book):
                return book[my_chat]
            # otherwise just return her stock phrase
            return "What you say baby"
            print ("Error trying to make out with girlfriend")

As before, his special lady uses a simple key/value file to select a suitable response to his probing question.

Putting this all together, we now have bidirectional conversations between Arkwood and his mother and his girlfriend at the same time. A recipe for disaster if you ask me, but this is what the little fellow wants.

from person import *
from raswik import Raswik
from speech import Speech
from time import sleep

#set up acquaintances
mother = Mother(True, "mummy", "night night")
girlfriend = Girlfriend(False, "baby doll", "so long")

# loop until pi crashes
while True:

    # if button pressed...
        # let's capture Arkwood's speech
        my_chat = Speech().speech_to_text('/home/pi/PiAUISuite/VoiceCommand/speech-recog.sh')

        # handle hellos and goodbyes
        if (mother.is_hello_goodbye(my_chat)): continue
        if (girlfriend.is_hello_goodbye(my_chat)): continue
        # Arkwood conducts his social intercourse
            mother_chat = mother.speak_to(my_chat)

        if (girlfriend.is_active):
            girlfriend_chat = girlfriend.speak_to(my_chat)

We set up his mother and girlfriend with suitable Hello and Goodbye phrases, in case we need to bring them into the conversation or dismiss them (plus we also state whether they are active to begin with). After that, it is business as usual, looping through the code and, if our wireless RasWIK button is pressed, we capture Arkwood’s speech through the microphone. Once we have his wicked desires in a string, we can check whether he is asking his mother or girlfriend to take a hike, before getting their responses and turning them into audio output.

It all works rather well, as the screenshot indicates:


First, he asks his mother for cuddles, to which she consults Yahoo! (as all mothers do) in order to provide her son with an explanation of what cuddling involves. At no point does Arkwood actually receive a cuddle.

Next, he invites his girlfriend into the conversation with the signature ‘baby doll’ so that she is at his beck and call. ‘I love you,’ he exclaims, to which his mother goes off on a religious rant while his girlfriend asks for some money.

At this point Arkwood makes a mistake. Instead of dismissing his mother with the signature ‘night night’ he says instead ’99’. His mother, confused, makes reference to ’99 Red Balloons’, a hit pop record from eighties German band Nena. His girlfriend simply says, ‘What you say baby’.

My buddy gets it right on second time of asking and his mother vacates the room, leaving the sordid pervert alone with his mistress. Nerves get the better of him and the microphone records nothing, prompting his girlfriend’s stock response. Then he blurts out ‘marry me’ and she demands that the house be part of a prenuptial.

‘There you go,’ I said to him, rather chuffed with myself. ‘You now have a mother, so no more tears.’

Arkwood thought for a moment, then replied, ‘Can I have a virtual God too?’

I was filled with rage. ‘How the fuck do you expect me to code up the universe and all within it, by teatime?’ He cried. And so I begun on my third virtual person.