Using Real-World Data in Your Class

Carlo Barbieri
Wolfram Research
training@wolfram.com

Outline

Sciences: ChemicalData

Social sciences: WorldData

Planning a school trip abroad: WeatherData

English and other languages: DictionaryLookup and WordData

Conclusions

ChemicalData

Basic Data Exploration

ChemicalData contains a very large range of information on a large number of chemical compounds:

Length[ChemicalData[]]

Length[ChemicalData["Properties"]]

That means 100 properties of over 40,000 chemical compounds!

ChemicalData/@RandomChoice[ChemicalData[],5]

This much data can be accessed programmatically and used to do cool stuff with just a couple of lines of code.

What are the hydrocarbons?

hydrocarbons = ChemicalData["Hydrocarbons"]

What is the relationship between combustion heat and molecular weight?

ListPlot[Tooltip[{ChemicalData[#,"MolecularWeight"],ChemicalData[#,"CombustionHeat"]},#]&/@hydrocarbons]

ChemicalData["Ethylcyclopropane", "MoleculePlot"]

Advanced Uses

With a little more code, one can write some really helpful things such as this stoichiometry checker:

Clear[CheckStoichiometry]
CombineTotal[x_] := Sort[{#[[1,1]], Total[#[[All,2]]]}& /@ GatherBy[x, First]]
CheckStoichiometry[Plus[lhs__] == Plus[rhs__]] := Equal @@ (CombineTotal /@ ({lhs, rhs} /. {
	Times[n_Integer, a_String] :> (MapAt[n * #&,ChemicalData[a,"ElementTally"],{All,2}]),
	a_String :> ChemicalData[a,"ElementTally"],
	Plus :> Join
}))

CheckStoichiometry["Methane" +2"Oxygen" == "CarbonDioxide" +  "Water" ]

CountryData

CountryData contains huge amounts of information that can be useful for writing papers on geography or for teaching statistics:

Length[CountryData["Properties"]]

CountryData contains a very large number of properties. Some are common:

Labeled[CountryData[#, "Population"],#]&/@CountryData["G8"]

Some are arcane:

Labeled[First[CountryData[#, "ElectricalGridSocketImages"]],#]&/@CountryData["G8"]

It is very easy to use this data to get nice visualizations:

ColorByValue[list_] := Graphics[Function[{country, value},{EdgeForm[{Thin,Black}], ColorData["TemperatureMap"][value],CountryData[country,{"Polygon","Mollweide"}]}]@@@ Transpose[{list[[All,1]],Rescale[list[[All,2]]]}], ImageSize-> 800]

ColorByValue[{#,CountryData[#, "GovernmentDebt"]}&/@CountryData["G8"]]

ColorByValue[{#,CountryData[#, "GovernmentDebt"]/CountryData[#, "Population"]}&/@CountryData["G8"]]

WeatherData

Mathematica® knows about the weather all over the world, both daily and going back a few decades!

Does it rain more often in London or in Paris?

FractionOfRainyDays[city_] :=With[{data =WeatherData[CityData[city, "Coordinates"],"PrecipitationTypes",{{2000,1,1},DateList[],"Day"}]},
N[Count[data,{_,{___,"Rain",___}}]/Length[data]]]

Grid[{CityData[#,"Name"],Round[100 FractionOfRainyDays[#]]}& /@{{"London", "GreaterLondon", "UnitedKingdom"},{"Paris", "IleDeFrance", "France"},{"Rome", "Lazio", "Italy"},{"Boston", "Massachusetts", "UnitedStates"},{"BuenosAires", "BuenosAires", "Argentina"},{"SanFrancisco", "California", "UnitedStates"},{"Lima","Lima","Peru"},{"Beijing","Beijing","China"},{"Sydney","NewSouthWales","Australia"},{"Moscow","Moscow","Russia"}}]

Tapping Wolfram|Alpha® for more data:

DateListPlot@WolframAlpha["Weather in paris",{{"WeatherCharts:WeatherData",4},"ComputableData"},PodStates->{"WeatherCharts:WeatherData__Past 10 years","WeatherCharts:WeatherData__More","WeatherCharts:WeatherData__Precipitation amount_Show annual"}][[1,1]]

DictionaryLookup

Mathematica contains dictionaries in various different languages:

DictionaryLookup[All]

You can take a look at what is included:

numwords=Reverse@SortBy[{#,(DictionaryLookup[{#,"*"}]//Length)}&/@DictionaryLookup[All],Last]

ListLogPlot[Tooltip[#[[2]],Row[{#[[1]]," ",#[[2]]}]]&/@numwords,PlotRange->All]

DictionaryLookup can be used for searching the dictionaries in various ways:

anagrams[s_String] := With[{c = Sort[Characters[s]],l = StringLength[s]},DictionaryLookup[x__ /; StringLength[x] == l &&  Sort[Characters[x]] === c]]

anagrams["lemon"]

palindromes[n_]:=DictionaryLookup[x__/;(StringLength[x]≥n && x==StringReverse[x])]

palindromes[5]

In English, spellings are not a good clue to pronunciation, but in Italian, you can use Mathematica to write a rhyming dictionary:

DictionaryLookup[{"Italian", "*ello"}]

WordData

WordData contains much more detailed information on words:

WordData["Properties"]

Labeled[#,WordData[#,"PartsOfSpeech"]]&/@StringSplit["the boy and dog were fishing at the lake"]

A fun example would be to find all the words that have different spellings in British and American English:

brit=Select[WordData[],WordData[#,"AmericanSpelling"]≠WordData[#,"BritishSpelling"]&];

Length[brit]

brit[[1;;10]]

amer2brit=Rule@@@Union[{WordData[#,"AmericanSpelling"],WordData[#,"BritishSpelling"]}&/@brit];

str="When I parked my car at the freshly painted curb next to the parking meter I got paint on my tire and changed its color";

StringJoin[Riffle[Replace[StringSplit[str],amer2brit,{1}]," "]]

Conclusion

This slide show covers only a small subset of the data in Mathematica.

The documentation can guide you through all the included data.

Data and computation can be combined in new, interesting ways to write programs that know about the world.

Check the stoichiometric balance, write rhyming dictionaries…

Data and computation can be useful for languages and literature.

Other than what you have seen here, you can query Wolfram|Alpha for Shakespeare’s plays, such as Hamlet, to see what can be done.

If what you are looking for is not included in Mathematica, you can always tap Wolfram|Alpha.

Either use the WolframAlpha function or download data with Wolfram|Alpha Pro.