Accessing Wolfram|Alpha from Mathematica

Lou D’Andria

Wolfram Research, Inc.

What if programming in Mathematica were as simple as entering a Wolfram|Alpha query? What if all the data contained in Wolfram|Alpha results were natively accessible from within Mathematica programs? In this talk we’ll take a look at features built in to Mathematica 8 which blur the line between writing programs and writing prose, including:

querying Wolfram|Alpha directly from inside Mathematica

interpreting free-form queries as precise Mathematica commands

accessing the data within Wolfram|Alpha results

customizing search results for the current Mathematica session

What is Wolfram|Alpha?

It’s a web site

http://www.wolframalpha.com

or is it?

http://products.wolframalpha.com/api/webserviceapi.html

Plus cusotm apps, widgets, toolbars, query boxes, Siri integration, and more

What is the Wolfram|Alpha API

Basically, it’s a web service that takes a URL and returns XML

url=WolframAlpha["2+2","URL"]

Import[url,"XML"]

Mathematica’s API use

There are four main ways to interact with Wolfram|Alpha from within Mathematica.

“Double-equal” or Full Wolfram|Alpha Query -- the full page of results

“Single-equal” or Free-form Input -- a single result in a natrive form

“Control-equal” or Inline Free-form Input -- direct parsing to the Mathematica language

WolframAlpha[] function -- All these and more

“Double-equal”: Full Query

Press the equal key twice at the beginning of an input cell.

Give access to the native Mathematica rendering of the results

Displays the results in a way that Wolfram|Alpha users will immediately recognize

Exploratory interface for learning WolframAlpha[] syntax

“Single-equal”: Free-form Input

Press the equal key once at the beginning of an input cell.

Returns a single result, in native Mathematica rendering if possible

Exploratory interface for using Wolfram|Alpha data directly

Session integration…

Session Integration

Image manipulation

ExampleData[{"TestImage","Mandrill"}]

Text analysis

txt=ExampleData[{"Text","AliceInWonderland"}];

Additional examples

There are some very nice examples of session integration in Vitaliy Kaurov’s video.

What exactly is shared?

Plot[Sin[x],{x,0,10}]

WolframAlpha["blur","SessionInfo"]

Plot[Sin[x],{x,0,10}]

WolframAlpha["blur","URL"]

To change: Mathematica Preferences > Internet Connectivity > Wolfram|Alpha settings

The function behind it all: WolframAlpha[]

WolframAlpha[“query”, “format”] gives the available data in the specified format.

WolframAlpha["Mathematica plotting functions","FullOutput"] (* same as  *)

WolframAlpha["Monty Hall problem","Result"] (* same as  *)

WolframAlpha["figure 8 knot","MathematicaParse"] (* same as LinguisticAssistant *)

But there are other formats too

WolframAlpha["sinx","MathematicaForms"]

WolframAlpha["primes over a million","PodPlaintext"]

WolframAlpha["mt kilimanjaro","XML"]

and many others

WolframAlpha[] help page

Getting data via WolframAlpha[]

WolframAlpha[“query”, {podids, dataformats}] returns one or more individual pieces of data

What pod ids and data formats are available?

WolframAlpha["2+2","PodIDs"]

WolframAlpha["2+2",{"NumberName"}]

WolframAlpha["2+2",{{"NumberName",1},"Plaintext"}]

WolframAlpha["2+2",{All}]

WolframAlpha["2+2",{All,All}]

Example: numbers

WolframAlpha["primes over a million",{All,"NumberData"}]

Example: sounds

WolframAlpha["Ebm7",{All,"SoundData"}]

Example: graphics

ListAnimate[Table[WolframAlpha["Hilbert curve, n=" <> ToString[i],{{"LSystemCurve",1},"Content"}],{i,1,6}]]

Example: time series

WolframAlpha["Apple",{{"PriceHistory",1},"TimeSeriesData"}]

DateListPlot[%]

DateListPlot /@ Last /@ WolframAlpha["Apple",{All,"TimeSeriesData"}]

More examples

Data Formats in WolframAlpha[] tutorial

Interesting Results: Interfaces

Wolfram|Alpha results can contain any native Mathematica expressions, including interfaces.

Remember

Wolfram|Alpha updates get released much more frequently than Mathematica.

Accessing Wolfram|Alpha’s Mathematica parser

In some cases, the Wolfram|Alpha compute engine is not even needed to translate a query string directly to a Mathematica expression. You can access this Wolfram|Alpha-to-Mathematica parser.

WolframAlpha["lat long for champaign il","MathematicaParse"]

There is also an inline input field for accessing this inside your program.

Plot[{,},{x,-3,3}]

Conclusion

With the advent of , , and other Wolfram|Alpha-based features in Mathematica 8, many programming and development tasks really are as easy as entering a Wolfram|Alpha query.