Pizza maker: A Tutorial on Building Twitterbots

Preview:

Citation preview

CreateanaccountonTwitter,logintoTwitterwiththisaccount,thenvisitCheapBotsDoneQuick.comandSignin.

Openyourgrammarwitha{intheJSONwindow…

ThemainruleofyourTracerygrammariscalled“origin.”Thenameofeachruleisanon-terminalsymbolthatcanbeexpandedusingtheright-hand-side.Werefertonon-terminalsbyencasingthemin#…#ontheright-handsideofarule.

Next,writerulestoexpandthenon-terminals#meat#and#veggie#inourearlierruleformakingapizza

Addasmanyoptionsasyouwanttotheright-hand-sideofeachrule;allareequallylikely

Dittoforthenon-terminals#cheese#and#sauce#Addasmuchvarietyasyourimaginationwillallow…

WhenTraceryexpandsarule(togenerateapizza,say),itrandomlypicksfromtheright-hand-sideoptionsforeachnon-terminal.

Wenowwritearuleforthelastnon-terminal,#base#,andclosethenourgrammarwithafinal}

Everynon-terminalineveryrulenowhasitsownexplicitruledefinition,soourgrammarisreadytostartgenerating.

Thishandybuttononthebottomrightallowsyoutotestthegrammar,andtweettheresultsyoulike…

Usethisdrop-downmenutoselectthefrequencywithwhichyourbotwilltweet

theoutputsofitsTracerygrammar.

Finally,saveyourbotonCheapBotsDoneQuick.com

BotPizzaKitchen

Choosetomakeyourgrammarvisibletousersofthesite.

Now,whatifwewantourgrammartogiveanametoeachpizza?

Icanthinkofafew…

Wecanmakeanamebyjoiningtwonamelets

Butournamesbearnospecificrelationtotheirpizzas

Ifonlytheexpansionsof#Meat#,#Cheese#andtheotheringredientswereinfluencedbytheexpansionsfor#Prefix#and#Suffix#

ButTracerygrammarsareContext-Free:everynon-terminallike#Meat#isexpandedinanullcontext,

independentlyofallothers.NoContext,NoInfluence!

Here’swhatweaimfor:theCavewallprefixsuitsQuorn,whileCowboyisaptforRanchsauce.Thenameinfluencestheingredients.

Here’sanaïvesolution:writespecificpizzarulesthathardcoderelationsacrossnon-terminals.So#Prefix_A#generatesnamesthatgowith#Meat_A#,andsoon.

*&$^$%

Flippin’Nora!

Pre-NameIngredientsPrefix SuffixPost-NameIngredients

Sosplitthegeneratorintotwoself-containedcomponents

MeatPrefix SuffixVegetableonCheese&SauceonBase

Meat&VegetablePrefix SuffixonCheese&SauceonBase

Meat&VegetableonCheesePrefix SuffixonSauceonBase

Meat&VegetableonCheese&SaucePrefix SuffixonBase

We’llneedfourhigh-levelrulesforfourdifferentkindsofsplit.

SuffixVegetableonCheese&SauceonBase

SuffixVegetableonCheese&SauceonBase

SuffixVegetableonCheese&SauceonBase

SuffixVegetableonCheese&SauceonBase

Eachself-containedcomponenthasinternalagreementthatcanvaryinthenumberofdependencypairings

MeatPrefix

1Possibility 4AgreementPossibilities

Yeah!

Ournewtop-levelrulesarebelow,andproducenamedpizzasasabove.Intotal,thenewgrammarhas100rules.

YoucandownloadthegrammarandthisspreadsheetfromourGithubrepository

Allthedataforourgrammarisstoredinaspreadsheet

Whatagreatflippin’pantry!

CheapBotsDoneQuickallowsustomakeourbotsresponsivetotweetsthatexplicitly@mentionthem.Solet’smakeourbot

deliverpizzarecommendationsthatmatchspecifictastes.

Icouldmurderaseafoodpizza!

First,weneedtoaddanewcolumntoourDB…

Thetypeofapizzaisafunctionofthetypeofitsingredients.Soforeachqualificationoningredients,generatenewrulesforpizzasandtheirelements.

MakerSpecialized

NamerPizza-specific

CombinedGrammar

Thenamed-pizzamakerhas100rules.Thisjumpsto800ruleswhenweaddqualifications,andto1500ruleswhenweaddnegativequalificationsaswell.

Yourpizza,M’lord.

ButourTracerygrammarstillfitsintoCheapBotsDoneQuick.comsolet’spressonwiththedeliveryofspecialitypizzas.

ChoosetheReplyoptionforyourbot(stillinBeta)soyoucanconfigurehowitwillreplyto@mentionsfromothers.

{ “.”: “#pizza#” }

ResponsegrammarsuseasimplerJSONformat,withjustoneelementontheright-hand-side.

ButyouCANrefertonon-terminalsinthemaingrammarhere.

Theleft-hand-sideofaresponseruleisnotanon-terminal,butastringthatmustliterallymatchpartofan@mention

Eachqualificationofaningredientislistedontheleft.Thecorrespondingpizzanon-terminalison

theright.

Theseresponserulesareappliedinthegivenorder,soitisimportantthatthenegativequalifications(e.g.“nomeat”)arelistedbeforethepositives(e.g.“meat”).

Eachleft-hand-sidecanalsobearegularexpression.Use“.”tomatchanystring.Use[x|X]forachoicebetween“x”and“X”,asinthecase-insensitivetextsabove.

YourF**kingWine,Sir.

Let’sconsiderhowourpizzabotmightusefullyinteractwithothers.Howaboutanotherbotthatsuggestswinepairingsforournewlyinventedpizzas?

Weneedtowhipupacocktailofcomplementary

grammars.

Onewillmapfromfoodstowines,andanotherwilllistenforthetriggerstoexploitthismapping.

Tostart,weshouldcodifyourwine-pairingknowledgeinanexplicitform,asinthisveryspreadsheet…

Eachfood-feature-rule(non-terminalsoftheform“featurebev”)mapsontoitsownspecialist

listofrecommendations

Addaseparateruleforeachfeatureofthefoodonwhichawinepairingcanbepredicated…

Now,weneedameansofinvokingeachfood-feature-rule,soforeachfoodfeature,addastimulus:responsepairtotheresponsegrammar.

Nowweneedtogetthebicameralpartsofthegrammartotalktoeachother.

Wemustmakesurethateverypizzacreatedbyourbotmentionsthenameofarecommendationbot,suchasitself

Nowourrecommendercanbetriggeredbyanymentionofspecificingredients

(orawholeclassofingredients)inourpizzadescriptions…

…andrespondaccordingly,asbelow.

Butour

winegrammarofferspairingsonthebasisofjustasingleingredient!

Fancyadrinkwithyourpizza?

Flippin’heck,you’reright!

Solution:considerall

pairwisecombosofingredientsthatsuggestthesamewine!

Foreach

combo,addanexplicitrecomm-endationruletotheresponsegrammar.

Noticetheuseofthewildcardcharacters.+here.Thedot.matchesanycharacterwhilethe+meansoneormoreuses

Nowourresponse

grammarcanrecommendwineforapairingofingredients.