Accessing Wolfram|Alpha from Mathematica

Lou D’Andria, Wolfram Research

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 widgets, toolbars, query boxes, and more

What is the Wolfram|Alpha API

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

url="http://api.wolframalpha.com/v1/query.jsp?appid=U2YPK6-9K5JY5YK6Y&input=2%2B2&async=false&format=cell,minput,msound,dataformats&reinterpret=true&interactive=true&mclient=1&msessinfo=%7B%22Allowed%22+-%3E+%22Unknown%22%7D&mlicense=1001-1010-LYGRLU&sig=e8dc2c55fda634fedc2b9b888a95a330";

Import[url,"XML"]

What unique features would you expect in a Mathematica client?

Native access to the displayed form of the results.

Native access to the data behind the displayed results.

A way to make a Wolfram|Alpha query and get a result fully integrated into your Mathematica session.

A way to have Wolfram|Alpha compute with elements of your Mathematica session.

A way to access just the parsing features of Wolfram|Alpha.

Free-form Input:

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

Goals

Instant access to all of Wolfram|Alpha

Returns a single result, in native Mathematica rendering if possible

Exploratory interface for using Wolfram|Alpha data directly

Session integration…

Session Integration

Button["cheat",NotebookOpen["/Users/lou/Desktop/TechConf2010/CheatPalette.nb"],Appearance -> None,BaseStyle -> {"ControlStyle",GrayLevel[0.8]}]

Image manipulation

CurrentImage[]

Text analysis

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

Mathematica Session Information

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

Full Wolfram|Alpha Query:

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

Goals

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

The function behind it all: WolframAlpha[]

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

“FullOutput”

WolframAlpha["2+2","FullOutput"] (* same as  *)

“Result”

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

“MathematicaForms”

WolframAlpha["sinx","MathematicaForms"]

“PodPlaintext”

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

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",{All}]

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

Example: primes over a million

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

WolframAlpha["primes over a million",{"Values"}]

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

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

Example: sounds

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

WolframAlpha["lgm 1",{All,"SoundData"}]

Example: graphics

ListAnimate[Table[WolframAlpha[StringJoin["Koch snowflake, ",ToString[i]," iterations"],{{"KochSnowflake",1},"Content"}],{i,1,5}]]

Example: time series

WolframAlpha["BP",{{"PriceHistory",1},"TimeSeriesData"},InputAssumptions->{"*C.BP-_*Financial-"}]

DateListPlot[%]

DateListPlot[Last[#]]& /@ WolframAlpha["BP",{All,"TimeSeriesData"},InputAssumptions->{"*C.BP-_*Financial-"}]

WolframAlpha[] Options

WolframAlpha[] has many options corresponding to API parameters, or other web site features, including:

InputAssumptions

PodStates

IncludePods/ExcludePods

PodWidth

IgnoreCase

Example

WolframAlpha["8/29",InputAssumptions->{"*C.8%2F29-_*DateObject-"},IncludePods->{"Input","DifferenceConversions","PhaseOfTheMoon"},PodStates->{"Phase of the Moon_Large image"},AppearanceElements->{"Pods"}]

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 the Wolfram|Alpha 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["two plus two","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.