Friday, June 19, 2009

How Amy Iris knows where Best Buy is

Amy Iris is an extensible bot that you can place on your website, like this:

Feel free to talk to her, by typing into the text box above. If you ask her questions, she'll answer you. The cool thing is that we have designed her for extensibility, so that the internet community can make her smarter. As an example, I built a small code snippet to interface with the Best Buy Remix API so that you can ask her questions about where Best Buy stores are. Here's a picture of a dialog that I had with Amy Iris earlier today:

As you can see, I have asked Amy Iris a couple of different ways to tell me where various Best Buy stores are. This example could be extended for all of the Best Buy API calls (such as product lookups).

Here's the code that's part of the Bot's logic. One little 14-line code snippet, submitted to Amy Iris' brain, and she now is that much smarter.

# example of Best Buy Store Locator
import amyiris, re
from google.appengine.api import urlfetch
if ("best buy" in str(textin)) and ((' in ' in str(textin)) or
('near' in str(textin))):
fields = ['name','distance','address']
url = ",50))?show="
url += ",".join(fields) + "&apiKey=amysremixkeygoeshere"
r = "The nearest Best Buy store to %s is the %s store, "+
"which is %s miles away, at %s."

vals = ['\d'*5,str(textin)).group(0),] #grab the zip code
page = urlfetch.fetch(url%vals[0]).content #look up results based on zipcode
for tag in fields: #parse the xml
tag+'>',page, re.DOTALL).group(1))
say(r%tuple(vals),confidence=21 )

A quick code review reveals the tricks (and limitations) of this conversational parser. I scan for the words "best buy", " in ", and "near", and rely on a 5-digit zip code in a regex search (that is, r'\d'*5). And if I find all these, then the snippet will retrieve the information from the Best Buy web site and present it to the user in conversation form.

Imagine - it's now available on the web, on twitter, on your cell phone. And this is just one small look-up. Imagine what happens as people begin contributing similar code snippets over the years! Amy Iris will be brilliant!

No comments: