MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Hacking Wolfram|Alpha

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102108] Re: Hacking Wolfram|Alpha
  • From: "gigabitbucket at BrockEng.com" <gigabitbucket at BrockEng.com>
  • Date: Wed, 29 Jul 2009 05:56:30 -0400 (EDT)
  • References: <h494vp$q0g$1@smc.vnet.net>

On Jul 23, 3:54 am, Fred Klingener <gigabitbuc... at BrockEng.com> wrote:
> While waiting for the Wolfram|Alpha API, I've been nosing around what
> we have available, looking for ways that Mathematica can make use of W|
> A. Here's an exercise that I thought was interesting enough to share
> (and post to a place where I could find it again.)

...

I'm so ashamed to reply to my own post, and I'm so ashamed to report
how much headroom there was to streamline the method to extract
Mathematica-ready data from Wolfram|Alpha output.

Here's the density of gasoline in a Units`-ready form.

-----------------------------------------------------------------------
<<Units`
Clear[rho]
rho =

 First@Cases[

   Import[
    "http://www.wolframalpha.com/input/?i=gasoline+density+in+lb%\
2Fgal", "XMLObject"]

   , Rule["title", x_] :>
    ToExpression[StringReplace[x, z__ ~~ "lb/gal" ~~ __ -> z]] Pound/
       Gallon /; StringMatchQ[x, __ ~~ "lb/gal" ~~ __]

, Infinity]
----------------------------------------------------------

The method sends the query string to W|A and Imports the result as an
XMLObject. The Cases searches all levels (the Infinity argument) of
the XMLObject for Rules that replace the "title" attribute, where the
numerical results appear.

There doesn't seem to be any reason that W|A codes the numerical
answer in a rule like this, but there doesn't seem to be a reason not
to either. Whatever the design intent, the form puts simple numerical
data in an accessible place.

Then, there's a StringReplace to pick out the numerical part of the
string to the left of the "lb/gal" units and an attachment of the
Units` units.

It's still not quite right. The /;StringMatchQ seems extraneous, the
thrashing between expressions and Strings is messy, but I couldn't
figure how to make it work without all that.

API? We don' need no steenking API!

Cheers,
Fred Klingener


  • Prev by Date: Re: Re: The audience for Mathematica (Was: Re: Show doesn't work inside
  • Next by Date: Re: The audience for Mathematica (Was: Show doesn't work inside Do loop ?)
  • Previous by thread: Hacking Wolfram|Alpha
  • Next by thread: space for NonCommutativeMultiply