Cosy Computing
 
 
08. Python.
 
 

This is where the separate things I have been learning start to work together. I am a bit edgy about relying on an internet connection, and on running the bot only through the Pandorabots website. In looking into the possibilities of offline AIML interpreters, I found several references to PyAIML, which is "implemented as a 100% pure standard Python package". Python is a language I've come across a lot while setting up the Pi - it seems a lot of people use Pi with Python. The Raspbian OS has Python support built in. It looks like it might be useful for my longer-term goals of interacting with other hardware and peripherals. So I might be able to combine getting Pinnochion#1 to run offline, with learning programming.

Chris Watkins recommended a website called Codecademy, so I've signed up. One of the options is to learn Python, so I have begun that course. It's a combination of surpringly easy, and surprisingly difficult. The programming itself makes sense, but I get held up a lot having to work out the maths needed. But that is satisfying too, in a different way.

 
codecademy
 

Normally I would work through things pretty methodically, but time is pressing on, so I'm going to try working with PyAIML at the same time. Having researched how to use it, I found that the combination of the PyAIML site and this brilliant blog made it pretty clear. But I had a huge knowledge gap - I realised I didn't know how to download the interpreter to the Pi and set it up in the first place, so I asked at Solsug and got shown two ways - first of all via Git, which I don't really understand yet so I'll come back to that as I'll be needing it again later. Secondly, it turns out you can download from Sourceforge the same way you would on a PC. To do that, I installed Google Chrome (sudo apt-get install chromium), which is a useful browser. Then simply followed the instructions on Sourceforge from PyAIML's page. Having done that and downloaded the standard AIML set to play with, we couldnt get it set up to work. But after the meeting, Alan Campion did some digging around and found the missing link. So the process is:

 

> Download PyAIML-0.8.6.zip from Sourceforge.

> Make a folder for the project (in my case 'pinnochion1').

> Unzip PyAIML-0.8.6.zip into that folder (keeping its folder called PyAIML-0.8.6).

> Add any AIML files to the project folder.

> Set up PyAIML - this is the bit Alan figured out. Inside the folder PyAIML-0.8.6 is a file called 'setup.py'. To make this work, use the terminal to get into that folder using the 'cd' command. 'cd' means 'change directory', so you type 'cd' then where you want to go (see screenshot below). NOTE: file names are case-sensitive! Once you are in the right folder, type 'python2 setup.py install' . This means you are asking Python2 (There are two versions of Python, Python2 and Python3 and things aren't always compatible with both. PyAIML uses Python2.) to install 'setup.py'. You might need to put sudo first. I can't remember if I did or not.

 
installing
 

So the interpreter is now installed. The next thing to do is to work up a Python program to run the bot. I went through several stages of this. First, I tried the example given on inny.org . This is for a bot with audio speech, which I don't need for this, but Alan tried it on his machine and it worked, so it's a good way to test things. Alan sent me a copy of this file, and the test AIML, along with instructions on how to run it. Basically, you need to have the program saved as a .py file in the project folder, along with the AIML. Initially I used inny.org's test aiml, as that was what Alan used to get it working. To run it, make sure you are in the same folder, and type 'python2 chat.py'. As I didn't have any of the sound set up, I got a lot of error messages, but the actual chat mechanism worked, so I knew I had what I needed.

So the next thing was to make as program that did what I needed. I hadn't learned enough yet to do this from scratch, and I didn't want to blindly copy someone ele's work. So I began by looking up what all the individual elements of inny.org's program meant. This way I had a basic idea of what was going on, and what I would need to run Pinnochion#1.

The first step was to take out everything to do with the sound, because it isnt needed for what I am doing, and to replace the test AIML with Pinnochion#1's aiml. I tested a couple of files first.

 
first go
 
This worked! Now I had a few of Pinnochion's responses, rather than someone else's.
 
thatworks
 

Now to begin customising the program to do what I want.

> First I added the rest of Pinnochion1's AIML

> Then I followed inny.org's instructions for adding an option to quit the program.

> There was also a recommendation there for how to add a slight delay to the answer to make it look like it was thinking. My knowledge wasnt up to following that, but after a fair amount of googling, I worked it out, and added a 1 second delay, which just makes it feel a bit nicer to interact with.

> The next thing I wanted was to be able to save the conversations generated. I figured there must be a way to send things to a text file, so again with the googling to find out if this was a thing people do, and if so how to do it. This took a lot of looking on a lot of sites. I didn't just want to grab someone's code, I wanted to understand what was going on. I found this at dreamincode particularly helpful. Once I had the basics, I did a bit more poking around and a lot of trial and error to get the resulting text file to appear as a readable list of conversations. This took a lot of just trying things, and my code is probably not the best way of doing things, but it works.

> So now I have a basic but solid way to run Pinnochion#1 offline, and have learned a lot about Python and about using my Pi along the way.

 
my  code
 
pin happy
 
pins log
 

This gives me a very good basis for actually making the whole thing run in Python as a longer term project. While researching these specific things, I also found out a bit about how to make a GUI for it, and how to send the text to printers, which is something I would love to do. I'd really like people to be able to be able to take a printout of their conversation away with them. I can't afford the printer at the moment, but that is a good step for the future.