Message Boards Message Boards

4
|
9270 Views
|
0 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Talking to Wolfram

I ran in to some problems with my website recently so instead of posting a summary of this project, I've given a little more detail and flair.  Apologies if this bothers anyone.

Coding myself out of a job

Recently, a speech recognition software package, Jasper, was released for the Raspberry Pi, so I had to try it out. After a number of bumps and hiccups I was able to get it up and running on (one of) my Raspberry Pis. Naturally (?!?), the next step was to use Mathematica in order to teach Jasper to speak Chemistry to me, and now my Raspberry Pi is lecturing about the physical properties of elements on the periodic table.  SetupThe first thing I did was download the software. I did choose the quickstart option of downloading an image to burn to (in my case) a 4 GB SD card (class 4, which was a good choice for me only because it is the only card I have left). Getting things working the first time was a bit troublesome. Here are a couple pointers that made life a lot easier for me:
  • I used the old fashioned way of configuring wireless (namely, modifying /etc/network/interfaces as shown in the tutorial at Adafruit.)
  • Make sure you start here after getting the networking up and running. (The prefab image doesn't contain Jasper source code installed in your home directory.)
  • I bought a standalone USB mic after agonizing over a USB mic/headset - configuring sound to go where I wanted it to and when was just too much of a pain
  • Use `alsamixer` to make sure your USB mic gain is set properly.
  • Use the 3.5 mm audio jack as opposed to HDMI sound (assuming you have connected your RPi to a TV/monitor)
  • Don't let Jasper start on boot (comment out the @reboot line in your crontab file). This made debugging much easier for me.
Additionally, I ended up sitting at my desktop with a couple terminals open so I could code in relative comfort. This also allowed me to start/stop Jasper in one terminal window and code in another.

W - the wolfram element

My intention here is to use Mathematica to create some Chemistry content. I've used the curated data sets before to build interactive activities, and I thought I could use the same functions to generate Periodic table fun facts. Here's some code:
z =RandomInteger[{1,92}];

props ={"AtomicWeight","AtomicRadius","Density","MeltingPoint","BoilingPoint"};
fact =RandomChoice[props];
info ={ElementData[z,"Name"],Quiet@NumberForm[ElementData[z,fact],{4,1}],ElementData[z,fact,"Units"]};
str ="The"<>StringReplace[fact,x_?UpperCaseQ:>" "<> x]<>" of "<> info[[1]]<>" is "<>ToString[info[[2]]]<>
   " "<>StringReplace[ToString@info[[3]],x_?UpperCaseQ:>" "<> x];
Print[str]

This code grabs a random number from 1 to 92, chooses one of the properties listed in props, and then calls ElementData to find the information. Finally, I make a text string that contains the periodic table fun fact (including units!). The messy StringReplace commands are there to change terms that Mathematica uses (e.g. "AtomicRadius" and "WattsPerMeterKelvin") into terms normal humans use ("atomic radius" and "watts per meter kelvin").
I quickly ran in to a problem here. The Jasper API uses python to call speech recognition and parsing functions. I can put the above code into a text file and call it as a script using wolfram -script; however, every time I executed the script, wolfram would take a very long time to load. In my years as a Chemistry professor, I have learned that if you do not answer a student's question within 2-3 seconds of the question being asked, you will get slammed in your student evaluations. Waiting for wolfram to initiate every single time I spoke to Jasper just wasn't going to cut it.

A Mathematica pipe dream

With a little help from my friends at Mathematica.StackExchange, I decided to try creating a named pipe that would send commands to an always-on instance of wolfram. I could then use system commands in python to get at the periodic table facts that the Wolfram script was generating for me. To make a long and painful story short, I ran in to some problems and came up with the following working-but-unsatisfactory solution:
mkfifo mmpipe

tail -f mmpipe | wolfram -noprompt >/home/pi/jw.tmp &
echo 'N[Pi]'>mmpipe

The first command makes a named pipe and the second redirects anything sent to the pipe to a running wolfram kernel, which in turn sends its output to a temporary text file. The last command is just an example of how a Mathematica function (N) would be executed by redirecting to the named pipe. I really don't like the temp file redirect (and will hopefully find a solution soon) but it was the only way I could get at the wolfram kernel's output. The advantage here is that I have a running log of the periodic table facts that Jasper is going to tell me.

Telling Jasper what to do

Last, I need the python code to tell Japser:
  1. To look out for a keyword
  2. To do something once the keyword was used appropriately
Have a look at the instructions provided by Jasper's developers on how to write this script, where to put it and what other things need to be done. I decided that I want Jasper to respond to the keyword CHEMISTRY with the output of the wolfram-generated periodic table fun fact.    
 import re
 import subprocess
 import time
 WORDS =["CHEMISTRY"]
 def handle(text, mic, profile):
     out = subprocess.Popen('cat /home/pi/jasper/client/modules/chem.wl>/home/pi/mmpipe', shell=True,stdout=subprocess.PIPE)
     time.sleep(2)
     f = open("/home/pi/jw.tmp","rw+")
     lines = f.readlines()
    mic.say(lines[-1].strip())
    f.close()
def isValid(text):
    return bool(re.search(r'\bchemistry\b', text, re.IGNORECASE))

Now we should have everything in place for Jasper to teach us some chemistry.

Professor Jasper, at your service

Since I prevented Jasper from starting whenever my RPi boots, I need to run ~/jasper/boot/boot.sh and wait a minute or two. The added bonus here is that I get a lot of debugging information, which is useful when it seems as if I am saying things like "What is a Chemistry fact?" and Jasper tells me today's weather - he heard "WEATHER CHEMISTRY" and since the weather module was loaded before the periodic-table fun fact module, I got the weather. (I quickly changed the order.) Also, for reasons that aren't entirely clear, the wolfram pipe takes a while to get going, so I have a helper python script that calls the wolfram code a couple times, and I run this code prior to speaking with Japser. Here's a youtube video of Jasper doing his Chemistry thing. Now excuse me while I dust off my resume.
POSTED BY: BoB LeSuer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract