Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created July 8, 2021 17:56
Show Gist options
  • Save aparrish/c74dd125dfd2f3130af98cde1cdc2f06 to your computer and use it in GitHub Desktop.
Save aparrish/c74dd125dfd2f3130af98cde1cdc2f06 to your computer and use it in GitHub Desktop.
Some recipes 4 u
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Allison's recipe generator!\n",
"\n",
"By [Allison Parrish](http://www.decontextualize.com/)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import spacy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"nlp = spacy.load('en_core_web_md')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# replace \"84-0.txt\" with the name of your own text file\n",
"text = open(\"./pg14293.txt\").read()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"doc = nlp(text)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def flatten_subtree(st):\n",
" return ''.join([w.text_with_ws for w in list(st)]).strip()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import tracery\n",
"from tracery.modifiers import base_english"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"imperatives = [word.lemma_ for word in doc if word.pos_ == 'VERB'\n",
" and 'dobj' in [item.dep_ for item in word.children]]\n",
"objects = [flatten_subtree(word.subtree) for word in doc if word.dep_ == 'dobj']\n",
"objects = [item.replace(\"\\n\", \" \").replace(\"_\", \" \") for item in objects if len(item) > 5 and len(item) < 25]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Stir Livlander'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rules = {\n",
" \"origin\": \"#imperative.capitalize# #object#\",\n",
" \"imperative\": imperatives,\n",
" \"object\": objects\n",
"}\n",
"grammar = tracery.Grammar(rules)\n",
"grammar.add_modifiers(base_english)\n",
"grammar.flatten(\"#origin#\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"grammar = tracery.Grammar(rules)\n",
"grammar.add_modifiers(base_english)\n",
"\n",
"recipe_steps = []\n",
"for i in range(10000):\n",
" step = grammar.flatten(\"#origin#\")\n",
" recipe_steps.append(step)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Bubble full marks',\n",
" 'Trademark a curator who could',\n",
" 'Melt a Greek hat',\n",
" 'Pass the work',\n",
" 'Ask the dish',\n",
" 'Bles Chapter 3',\n",
" 'Add corks about 3/4 of',\n",
" 'Call U.S.A. ',\n",
" 'Stir Durmar Turkey ',\n",
" 'See drain off']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.sample(recipe_steps, 10)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ROQUEFORT\n",
"\n",
"1. Attack the cases.\n",
"2. Give great scope.\n",
"3. Mean cheese.\n",
"4. Export ornamental cheeses.\n",
"5. Adapt about 3 doz.\n",
"6. Invent Herrgårdsost.\n",
"7. Soak Durmar Turkey .\n",
"8. Need over 1/2 teacup of ale.\n",
"9. Use new meaning.\n",
"10. Take the taste buds.\n",
"\n",
"\n",
" SPROUTS\n",
"\n",
"1. Use milk and buttermilk.\n",
"2. Take Semisoft.\n",
"3. Roll donations.\n",
"4. See a buttered dish.\n",
"5. Bake the filling.\n",
"6. Restore a smooth batter.\n",
"7. Grate seasonings.\n",
"8. Impart two or three months.\n",
"9. Make a half-pound weight.\n",
"10. Bring this strong mixture.\n",
"\n",
"\n",
"MIXTURES\n",
"\n",
"1. Bake cheese.\n",
"2. See the best in America.\n",
"3. Provide sandwich buns.\n",
"4. Scrape the following poem.\n",
"5. Spread the kirsch.\n",
"6. See the job.\n",
"7. Add red spots.\n",
"8. Skim Chapter 3.\n",
"9. Weigh chives.\n",
"10. Dice Alderney.\n",
"\n",
"\n",
"THE STIFF WHITES\n",
"\n",
"1. Apply a fang.\n",
"2. Send Castillon.\n",
"3. Have this cheese.\n",
"4. Leave very high.\n",
"5. Beg Project.\n",
"6. Put cheese for a sidekick.\n",
"7. Contain hot milk.\n",
"8. Produce Pithiviers au Foin .\n",
"9. Fit the toasted cheese.\n",
"10. See the work.\n",
"\n",
"\n",
"JURA HEIGHTS\n",
"\n",
"1. Give the cheese.\n",
"2. Bring the cheese.\n",
"3. Remember thanks on bended knees.\n",
"4. Pick the works.\n",
"5. Finish Nantais.\n",
"6. Check one kind of cheese.\n",
"7. Put 3 tablespoons flour.\n",
"8. Give a young Stilton.\n",
"9. Keep Chapter 3.\n",
"10. Remove the number.\n",
"\n",
"\n",
"QUEIJO DA\n",
"\n",
"1. Mix Bleu d'Auvergne.\n",
"2. See the weight.\n",
"3. Check batter.\n",
"4. Call the waitress who.\n",
"5. Bake the corn.\n",
"6. Pay one-third of a pound.\n",
"7. Have as many different shapes.\n",
"8. String this strong mixture.\n",
"9. Thrill Swiss cheese.\n",
"10. Make one and a half.\n",
"\n",
"\n",
"A PAR\n",
"\n",
"1. Bake Gruyère.\n",
"2. Have a quarter of a pound.\n",
"3. Work the taste buds.\n",
"4. Accompany and quality of flavor.\n",
"5. Scrape a sandwich of these.\n",
"6. _ a moderate oven.\n",
"7. Peril a meal.\n",
"8. Have one way only.\n",
"9. Deprecate all crusty skin.\n",
"10. Add the egg whites.\n",
"\n",
"\n",
"PEOPLE\n",
"\n",
"1. See Caille.\n",
"2. Spread Parmesan.\n",
"3. Serve chili pepper.\n",
"4. See any upper crust.\n",
"5. Snap a scar.\n",
"6. Cuddle twenty-five pounds.\n",
"7. Get THE COUNTRY.\n",
"8. Haunt his piece.\n",
"9. Invite \"Defects,\" such as, but.\n",
"10. Mention Camembert and Vendôme.\n",
"\n",
"\n",
"CELERY SEEDS\n",
"\n",
"1. Form Barberey and Ervy.\n",
"2. Weigh their students.\n",
"3. Make the word.\n",
"4. Remove Swiss cheese.\n",
"5. See cloves.\n",
"6. Put their prime.\n",
"7. See only 1-1/2 cups.\n",
"8. Use three or four years.\n",
"9. Furnish l'Anglaise.\n",
"10. Bake an excellent reputation.\n",
"\n",
"\n",
"THE SUMMER\n",
"\n",
"1. Keep Cheddar Rabbit.\n",
"2. Grate one cup.\n",
"3. Toast kümmel.\n",
"4. Make Petìts Fromages.\n",
"5. Add buttery base.\n",
"6. Have Dutch .\n",
"7. Teach Olmützer Quargel.\n",
"8. Stir your glass.\n",
"9. Try its highest praise.\n",
"10. Resemble your own Liptauer.\n",
"\n",
"\n",
"THE EGG\n",
"\n",
"1. Play Chapter.\n",
"2. Produce the Stewed Rabbit.\n",
"3. Smack white wine.\n",
"4. Make fine 4 eggs.\n",
"5. Own gingerbread.\n",
"6. Put young Cheddar.\n",
"7. Head mellow Cheddar 1.\n",
"8. Use Cappelletti,.\n",
"9. Shred a firm hold.\n",
"10. Fold a little more or.\n",
"\n",
"\n",
"_\n",
"\n",
"1. Lend the mixture.\n",
"2. Make the ponderous beam.\n",
"3. Make milk with salty flavor.\n",
"4. Break two to three months.\n",
"5. Add tomato soup.\n",
"6. Add Chives.\n",
"7. Protect his piece.\n",
"8. Dunk a year.\n",
"9. Stir his piece.\n",
"10. Show the buttered toast.\n",
"\n",
"\n"
]
}
],
"source": [
"for j in range(12):\n",
"\n",
" recipe_name = random.choice(list(doc.noun_chunks))\n",
" steps = random.sample(recipe_steps, 10)\n",
"\n",
" print(recipe_name.text.upper().replace(\"\\n\", \" \"))\n",
" print()\n",
" for i, text in enumerate(steps):\n",
" print(f\"{i+1}. {text}.\")\n",
" print()\n",
" print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment