用自然语言查询 Wolfram Knowledgebase
生成一个函数,根据歌手和专辑查找歌曲并生成含有作曲家信息的表格.
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}}}]}]]
部署使用内置诠释器识别 "MusicAct" 和 "MusicAlbum" 实体类型的简单语法.
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]=