Query the Wolfram Knowledgebase Using Natural Language
Create a function to look up songs by artist and album and generate a table with composer information.
In[1]:=
songLookup[act_, album_] :=
Module[{actEntity, albumEntity, songs, composers},
actEntity = Interpreter["MusicAct"][act];
albumEntity = Interpreter["MusicAlbum"][album];
songs =
EntityList[
EntityClass[
"MusicWork", {"AssociatedMusicActs" -> actEntity,
"AssociatedMusicAlbums" -> albumEntity}]];
composers = EntityValue[songs, "Composers"];
Column[{albumEntity["Image"],
Grid[Transpose[{songs, composers}],
Background -> {Automatic, {{LightOrange, White}}}]}]]
Deploy a simple grammar that utilizes built-in interpreters to recognize "MusicAct" and "MusicAlbum" entity types.
In[2]:=
songGrammar = CloudDeploy[
GrammarRules[
{AnyOrder[
OptionalElement[
FixedOrder[
"songs" | "titles" | "tracks" | "tunes" | "recordings",
OptionalElement["by" | "from"]]],
act : GrammarToken["MusicAct"],
FixedOrder[OptionalElement["from" | "on"],
album : GrammarToken["MusicAlbum"]]
] :> songLookup[act, album]}]]
In[4]:=
GrammarApply[songGrammar, "tracks by Michael Jackson on Thriller"]
Out[4]=
In[5]:=
GrammarApply[songGrammar, "Pink Floyd's Dark Side of the Moon"]
Out[5]=