Re: Hacking Wolfram|Alpha

• To: mathgroup at smc.vnet.net
• Subject: [mg102249] Re: Hacking Wolfram|Alpha
• From: "gigabitbucket at BrockEng.com" <gigabitbucket at BrockEng.com>
• Date: Sun, 2 Aug 2009 06:02:39 -0400 (EDT)
• References: <h494vp\$q0g\$1@smc.vnet.net> <h4p6ai\$l6r\$1@smc.vnet.net>

```On Jul 29, 5:55 am, "gigabitbuc... at BrockEng.com"
<gigabitbuc... at BrockEng.com> wrote:
> 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.

But bat-winged gotchas lurk everywhere.

Looking for the energy stored in a 18v, 3.0 amp-hour lithium ion
battery, I fed

"3.0 amp hour 18 volt in Joule"

into W|A's query line and get back a page at

"http://www.wolframalpha.com/input/?i=3.0%20amp%20hour%2018%20volt%20in
%20Joule&t=ff3tb01"

, which reports the answer as 194 400 J. This matches the answer
returned by Units`.

So I fed the query line into the code from the second post above, with

<< Units`

Clear[W]
W =

First@Cases[

xml = Import[
"http://www.wolframalpha.com/input/?i=3%20Amp%20hour%2018%20volt%
\
20in%20Joule&t=ff3tb01", "XMLObject"]

, Rule["title", x_] :>
ToExpression[StringReplace[x, z__ ~~ "J" ~~ __ -> z]] Joule /;
StringMatchQ[x, __ ~~ "J" ~~ __], Infinity]

and get 77 200 Joule as a result.

Never mind how long it took me to figure out the obvious. The
ToExpression[] interprets the string result 194 400 as 194 x 400 and
evaluates it.

Slot[] Function[] Prefix[] Out[] Not Not (That's swearing in
Mathematica.)

I applied my customary Monte Carlo programming techniques and
discovered that ToExpression could be persuaded to interpret the
string the way I wanted it to iff I suggested TeXForm.

In[39]:= << Units`

In[80]:= Clear[W]
W =

First@Cases[

xml = Import[
"http://www.wolframalpha.com/input/?i=3%20Amp%20hour%2018%20volt%
\
20in%20Joule&t=ff3tb01", "XMLObject"]

, Rule["title", x_] :>
ToExpression[StringReplace[x, z__ ~~ "J" ~~ __ -> z],
TeXForm] Joule /; StringMatchQ[x, __ ~~ "J" ~~ __], Infinity]

Out[81]= 194400 Joule

Maybe W|A doesn't WANT this to be easy.

Cheers,
Fred Klingener

```

• Prev by Date: Re: Re: Creating a Random Function to Select an Irrational
• Next by Date: Re: Re: Creating a Random Function to Select an Irrational
• Previous by thread: Creating a Demo Project for Convenient Database Access
• Next by thread: Re: Hacking Wolfram|Alpha