28
Sep 16

AIML + Python + Python Libraries = I can talk with my chatbot

It's been a while...

...and I've recently decided to continue to work with AIML.

Why? Python. My newest programming love. I shunned it at first. I mean... you have to maintain the same amount of spaces?? That seemed ridiculous when I first head about it so many years ago (I'm a very old-school programmer). But I've been forced to use it and now that I have... it's so easy, which translates to the amount of time needed to get something done. Time, these days, is my most precious resource.

Which is why when I had an idea for a chatbot that involved speaking and listening to it, I wondered how easy or difficult it would be to do in python. After a few minutes of Googling, I saw that the three components I needed to make this happen all existed: PyAIML, a speech recognition library, and a text-to-speech library.

How long did it take to put it all together so I could speak to Zoe?

Less than an hour. And it only took that long because I kept getting interrupted by  two impatient guys wondering where dinner was my loving family.

It was so easy, I almost didn't think it was worth blogging about. But I figured that some might need this guide, so here it is. How to talk to your chatbot. All you need is some AIML (version 1.0.1... the PyAIML I'm using doesn't (yet) support AIML 2.).

Install the following:

pip install aiml
pip install SpeechRecognition
pip install PyAudio
pip install pyttsx

A couple notes... first, if you need more help on the PyAIML, look to this very helpful post: http://www.devdungeon.com/content/ai-chat-bot-python-aiml

If you don't already have it, you might nee pywin32 from sourceforge:  https://sourceforge.net/projects/pywin32/

And then here's what you python file looks like with it all put together:

import aiml
import speech_recognition as sr
import pyttsx
import os

# Create the kernel and learn AIML files
kernel = aiml.Kernel()
if os.path.isfile("bot_brain.brn"):
     kernel.bootstrap(brainFile = "bot_brain.brn")
else:
     kernel.bootstrap(learnFiles = "zoe-startup.xml", commands = "load zoe")
     kernel.saveBrain("bot_brain.brn")

# Start the TTS engine
engine = pyttsx.init('sapi5')
voices = engine.getProperty('voices')

# obtain audio from the microphone
r = sr.Recognizer()

# Press CTRL-C to break this loop
while True:
     # obtain audio from microphone
     with sr.Microphone() as source:
         print("Say something!")
         audio = r.listen(source)
     try:
         myinput = r.recognize_google(audio)
     except sr.UnknownValueError:
         print("Google Speech Recognition could not understand audio")
     except sr.RequestError as e:
         print("Could not request results from Google Speech Recognition service; {0}".format(e))

        print "You said: ", myinput
     if myinput == "exit":
         exit()
     # Get Zoe's response
     zoes_response = kernel.respond(myinput)
     print "Zoe said: ", zoes_response
     engine.setProperty('voice',voices[1].id)
     # have Zoe say the response
     engine.say(zoes_response)
     engine.runAndWait()

 

Of course, you'll need your own startup.xml file and corresponding aiml files (refer back to that helpful post I mentioned on PyAIML). And I chose the female voice when I set voices[1].id. On my windows machine, pyttsx only has one male and one female voice to start with.

Happy chatting!

Xkcd captured Python perfectly:  https://xkcd.com/353/

20
Feb 14

"Budget By Paycheck"

I've been wrestling with how to tell the world about my budgeting concept for more than a year.
ByPCover
This budgeting concept I HAD to live with my first year or two out of college - because I was only making enough money to BARELY cover my bills. But now I use it because it helps keep me in control of every dollar I spend, so I can ensure that I'm spending below my means, so I can be saving as much as possible for my goals...

I call my system... wait for it... "Budget By Paycheck... So You Don't Have To Live Paycheck to Paycheck."

When I graduated from college and earning a real income for the first time in my life, I was faced with my first real set of grown up bills. I needed to live on a budget. But... every system I knew about was a monthly budgeting system and I was paid every other week. I couldn't always count on getting paid on the same day relative to the 1st of the month when my rent was due. Or the 20th when both my car payment and student loan payment were due. Sometimes my paycheck would line up nicely, but not all the time. When I missed a car payment one month because the 20th came on a Wednesday, and I didn't get paid and couldn't afford to pay that bill till the following Friday, I had to make a change.

Out of necessity, I took a little notebook and wrote down the dates of my next several upcoming paychecks. Then I "assigned" bills to them.

For example, if my next two paychecks were Fri, 2/14 and Fri 2/28, I would assign my car payment and loan payment to the 2/14 paycheck and March's rent to 2/28. There was also the phone bill and other things I assigned. I also automatically assumed I'd spend $100 per paycheck on food (maybe it wasn't $100... but it's close enough to what I remember from 17 years ago) which became my limit on food per two weeks. I did something similar for gas. I planned out 2-3 months at a time, making sure that all of my bills were accounted for.

Once everything was accounted for, I could see (what little) I had left over for savings, entertainment, etc. It was following this system pretty religiously that I managed to start to work myself out of debt (yeah, in addition to graduating college with student loans, I took on a car loan right away, and I had built up several thousand dollars in credit card debt while I was a student) and live a slightly more stress-free life.

Fast forward something like 15 years later... or maybe it was about 4 years ago? Yes, 4 years ago. I had stopped using my system a long time ago. It wasn't intentional. It simply didn't seem necessary anymore. My income was 4 times what it was when I was a college graduate. My student loans were paid off, I had no car loan, no credit card debt, and a respectable amount of savings.

Then I bounced a check. (Or overdrew my checking account via too many debit transactions... semantics)

I freaked out. How was this possible? I'd been fairly responsible with my finances for a long time. And my income was 4 times what it had been when I was a college graduate. My student loans were paid off, I had no car loan, no credit card debt, and a respectable amount of savings.

Did I mention the freak out?

Well, I went back to the Budget By Paycheck system. This time, I did it in Excel instead of by hand. I've been doing it ever since!

When I set up the spreadsheet, I went out two years. Every year, I go out another full year (except this last time when I went out multiple years). It enables me to plan out irregular expenses... like that yearly membership to something, or yearly taxes, etc, so I'm not surprised when the time comes. It enables me to look at where I am with respect to the current paycheck and see if I have "extra" money for savings or entertainment or home improvements or anything else that is not a necessity.

I've been trying to figure out a way to educate folks on how to set up their own spreadsheet if they're not as handy in Excel as I am. If you're interested, let me know... that might provide me with some motivation.

But you don't need a spreadsheet. You only need a pen(cil) and a piece of paper to start "Budgeting By Paycheck" and getting your finances and spending under control.

Here's an example of what the system looks like in a spreadsheet form:
SpreadsheetExample

Good luck, and comment if you have any questions!