Android and Linux

Monday, October 18, 2010

Speech recognition with Tasker

The Android Scripting Environment, now called SL4A, is now working with Tasker and on the Tasker Google group, a user named baudi posted an example of using a Python script for calling the Google voice search API.

His example uses the camera button to execute the script. Since the Nexus One doesn't have a camera button, I just set up the task and used it with a widget instead and combined both tasks into one. I left the Python alone (probably a good idea since I don't know Python) except to rename it as and rename the output file to /sdcard/.voice because I hate typing long filenames.

Here's a rundown of my Task:

1- Write File .voice (this is used to zero out the file)
2- Run Script
3- Read Paragraph file: .voice to %VOICE
4- Goto Action 3 if %VOICE matches EOF
5 ... whatever you want

When you run this, it executes the python script which pops up the Google voice search API which translates your speech to text then puts that text in a file which Tasker reads into a variable.

Ho hum, what good is this?

Well for starters, you can carry out tasks if that text matches a predefined string. For example, using my last two posts about sharing the clipboard contents between a Linux computer and the phone, I can speak the words "copy" and "paste." Once Tasker recognizes them, it carries out the tasks to copy the clipboard from the computer to the phone, or send the phone's clipboard to the computer's. This can be done simply by adding this as step 5, where XXX is the task to send or pull the clipboard contents and YYY is the word to trigger it:

5- Perform Action XXX if %VOICE matches YYY

Another example, I took this simple script and changed it quite a bit to get the weather current conditions and forecast and use Tasker to download it every hour and put it in a text file called "/sdcard/.metar" (the actual script also grabs METAR weather data from another site). The forecast looks like this:

Saturday...Partly sunny. Highs in the lower 70s.

Saturday Night...Mostly cloudy. Lows in the upper 40s.

Sunday...Partly sunny. Highs in the mid 70s.

Sunday Night...Mostly cloudy. Lows in the lower 50s.


Now, make this script called "wxday"
#! /system/bin/sh
grep -i $(awkc '{print $2}' /sdcard/.voice) /sdcard/.metar > /sdcard/.wxday
In the voice task above, step 5 can be:
5- Perform Action WXDAY if %VOICE matches forecast**

Here is the WXDAY task:

1- Locale Execute Plugin: execute @! wxday
2- Read Paragraph file: .wxday to var %WXDAY
3- Goto 2 if %WXDAY matches EOF
4- Say %WXDAY

Now, I can tap the voice widget, say "forecast Thrusday," it will see that I said "forecast" and call the WXDAY action. That action extracts Thursday from my weather file and speaks the forecast for Thursday.

Is Android/Tasker cool or what?