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
adjustments:
<< 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