Skip to content

Instantly share code, notes, and snippets.

@wupeixian
Last active February 2, 2023 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wupeixian/5446a4a8311022d76ba4539eeee1dfd8 to your computer and use it in GitHub Desktop.
Save wupeixian/5446a4a8311022d76ba4539eeee1dfd8 to your computer and use it in GitHub Desktop.
Assignment 1 for Reading and Writing Electronic Texts
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Implementations of early and well-known poetry generators\n",
"\n",
"By [Allison Parrish](http://www.decontextualize.com/)\n",
"\n",
"This notebook has some Python implementations of a number of early and well-known poetry generators, including Knowles and Tenney's *A House of Dust*, Strachey's love letter generator and Nick Montfort's *Taroko Gorge*.\n",
"Hiiiii"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## To Make a Dadaist Poem\n",
"\n",
"Original written by [Tristan Tzara](http://www.391.org/manifestos/1920-dada-manifesto-feeble-love-bitter-love-tristan-tzara.html#.WnPkJYJOndd) in 1920."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The informal more Dada corresponded in that was the against\n",
"bourgeois War the to believed war, society Dadaists\n",
"international and cause the the America. correspond\n",
"conformity the World participants many — intellectual and of\n",
"was of protest nationalist against with in movement, — I.\n",
"participants, the to were root North Europe colonialist\n",
"beginnings Dada and and outbreak broadly and the in\n",
"interests, which an art a many For war. cultural movement of\n"
]
}
],
"source": [
"import random\n",
"import textwrap\n",
"\n",
"newspaper = \"\"\"\n",
"Dada was an informal international movement, with \n",
"participants in Europe and North America. The \n",
"beginnings of Dada correspond to the outbreak of \n",
"World War I. For many participants, the movement \n",
"was a protest against the bourgeois nationalist \n",
"and colonialist interests, which many Dadaists \n",
"believed were the root cause of the war, and \n",
"against the cultural and intellectual \n",
"conformity — in art and more broadly in \n",
"society — that corresponded to the war.\"\"\"\n",
"\n",
"words = newspaper.split()\n",
"random.shuffle(words)\n",
"\n",
"print(textwrap.fill(\" \".join(words), 60))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A House of Dust\n",
"\n",
"Original written in Fortran in 1967 by Alison Knowles and James Tenney. [ELMCIP entry](https://elmcip.net/creative-work/house-dust). [More information](http://blog.calarts.edu/2009/09/10/alison-knowles-james-tenney-and-the-house-of-dust-at-calarts/). [Watch Alison Knowles read from this piece](https://www.youtube.com/watch?v=-68Z708lFsY)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"materials = [\n",
" 'brick',\n",
" 'broken dishes',\n",
" 'discarded clothing',\n",
" 'dust',\n",
" 'glass',\n",
" 'leaves',\n",
" 'mud',\n",
" 'paper',\n",
" 'plastic',\n",
" 'roots',\n",
" 'sand',\n",
" 'steel',\n",
" 'stone',\n",
" 'straw',\n",
" 'tin',\n",
" 'weeds',\n",
" 'wood'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"locations = [\n",
" 'among high mountains',\n",
" 'among other houses',\n",
" 'among small hills',\n",
" 'by a river',\n",
" 'by an abandoned lake',\n",
" 'by the sea',\n",
" 'in a cold, windy climate',\n",
" 'in a deserted airport',\n",
" 'in a deserted church',\n",
" 'in a deserted factory',\n",
" 'in a green, mossy terrain',\n",
" 'in a hot climate',\n",
" 'in a metropolis',\n",
" 'in a place with both heavy rain and bright sun',\n",
" 'in an overpopulated area',\n",
" 'in dense woods',\n",
" 'in heavy jungle undergrowth',\n",
" 'in japan',\n",
" 'in michigan',\n",
" 'in southern france',\n",
" 'inside a mountain',\n",
" 'on an island',\n",
" 'on the sea',\n",
" 'underwater'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"lights = [\n",
" 'all available lighting',\n",
" 'candles',\n",
" 'electricity',\n",
" 'natural light'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"inhabitants = [\n",
" 'all races of men represented wearing predominantly red clothing',\n",
" 'children and old people',\n",
" 'collectors of all types',\n",
" 'fishermen and families',\n",
" 'french and german speaking people',\n",
" 'friends',\n",
" 'friends and enemies',\n",
" 'horses and birds',\n",
" 'little boys',\n",
" 'lovers',\n",
" 'people from many walks of life',\n",
" 'people speaking many languages wearing little or no clothing',\n",
" 'people who eat a great deal',\n",
" 'people who enjoy eating together',\n",
" 'people who love to read',\n",
" 'people who sleep almost all the time',\n",
" 'people who sleep very little',\n",
" 'various birds and fish',\n",
" 'vegetarians',\n",
" 'very tall people'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"A house of glass\n",
" in a deserted church\n",
" using all available lighting\n",
" inhabited by friends and enemies\n",
"\n",
"A house of weeds\n",
" by an abandoned lake\n",
" using natural light\n",
" inhabited by all races of men represented wearing predominantly red clothing\n",
"\n",
"A house of wood\n",
" on an island\n",
" using all available lighting\n",
" inhabited by vegetarians\n",
"\n",
"A house of paper\n",
" in heavy jungle undergrowth\n",
" using natural light\n",
" inhabited by people who love to read\n",
"\n",
"A house of dust\n",
" in a metropolis\n",
" using candles\n",
" inhabited by very tall people\n",
"\n",
"A house of broken dishes\n",
" in dense woods\n",
" using all available lighting\n",
" inhabited by people who sleep very little\n",
"\n",
"A house of brick\n",
" in japan\n",
" using candles\n",
" inhabited by people speaking many languages wearing little or no clothing\n"
]
}
],
"source": [
"stanza_count = 7\n",
"for i in range(stanza_count):\n",
" print()\n",
" print(\"A house of \" + random.choice(materials))\n",
" print(\" \" + random.choice(locations))\n",
" print(\" using \" + random.choice(lights))\n",
" print(\" inhabited by \" + random.choice(inhabitants))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Love Letter Generator\n",
"\n",
"Original by Christopher Strachey, written for the Manchester Mark I in 1952. [Read more here](https://grandtextauto.soe.ucsc.edu/2005/08/01/christopher-strachey-first-digital-artist/).\n",
"\n",
"Vocabulary based on [this implementation](https://github.com/gingerbeardman/loveletter/blob/master/index.php)."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"sal_adjs = [\n",
" \"Beloved\",\n",
" \"Darling\",\n",
" \"Dear\",\n",
" \"Dearest\",\n",
" \"Fanciful\",\n",
" \"Honey\"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"sal_nouns = [\n",
" \"Chickpea\",\n",
" \"Dear\",\n",
" \"Duck\",\n",
" \"Jewel\",\n",
" \"Love\",\n",
" \"Moppet\",\n",
" \"Sweetheart\"\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"adjs = [\n",
" 'affectionate',\n",
" 'amorous',\n",
" 'anxious',\n",
" 'avid',\n",
" 'beautiful',\n",
" 'breathless',\n",
" 'burning',\n",
" 'covetous',\n",
" 'craving',\n",
" 'curious',\n",
" 'eager',\n",
" 'fervent',\n",
" 'fondest',\n",
" 'loveable',\n",
" 'lovesick',\n",
" 'loving',\n",
" 'passionate',\n",
" 'precious',\n",
" 'seductive',\n",
" 'sweet',\n",
" 'sympathetic',\n",
" 'tender',\n",
" 'unsatisfied',\n",
" 'winning',\n",
" 'wistful'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"nouns = [\n",
" 'adoration',\n",
" 'affection',\n",
" 'ambition',\n",
" 'appetite',\n",
" 'ardour',\n",
" 'being',\n",
" 'burning',\n",
" 'charm',\n",
" 'craving',\n",
" 'desire',\n",
" 'devotion',\n",
" 'eagerness',\n",
" 'enchantment',\n",
" 'enthusiasm',\n",
" 'fancy',\n",
" 'fellow feeling',\n",
" 'fervour',\n",
" 'fondness',\n",
" 'heart',\n",
" 'hunger',\n",
" 'infatuation',\n",
" 'little liking',\n",
" 'longing',\n",
" 'love',\n",
" 'lust',\n",
" 'passion',\n",
" 'rapture',\n",
" 'sympathy',\n",
" 'thirst',\n",
" 'wish',\n",
" 'yearning'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"advs = [\n",
" 'affectionately',\n",
" 'ardently',\n",
" 'anxiously',\n",
" 'beautifully',\n",
" 'burningly',\n",
" 'covetously',\n",
" 'curiously',\n",
" 'eagerly',\n",
" 'fervently',\n",
" 'fondly',\n",
" 'impatiently',\n",
" 'keenly',\n",
" 'lovingly',\n",
" 'passionately',\n",
" 'seductively',\n",
" 'tenderly',\n",
" 'wistfully'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"verbs = [\n",
" 'adores',\n",
" 'attracts',\n",
" 'clings to',\n",
" 'holds dear',\n",
" 'hopes for',\n",
" 'hungers for',\n",
" 'likes',\n",
" 'longs for',\n",
" 'loves',\n",
" 'lusts after',\n",
" 'pants for',\n",
" 'pines for',\n",
" 'sighs for',\n",
" 'tempts',\n",
" 'thirsts for',\n",
" 'treasures',\n",
" 'yearns for',\n",
" 'woos'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dearest Love,\n",
"\n",
"You are my lovesick fellow feeling. My ardour tempts your\n",
"sympathetic affection. My lust fervently hungers for your\n",
"unsatisfied desire. My fondness anxiously lusts after your\n",
"fellow feeling. You are my precious yearning.\n",
"\n",
"Yours beautifully,\n",
"M.U.C.\n"
]
}
],
"source": [
"# textwrap library used to \"wrap\" the text at a particular length\n",
"import textwrap\n",
"\n",
"# output begins with salutation\n",
"output = random.choice(sal_adjs) + \" \" + random.choice(sal_nouns) + \",\\n\"\n",
"output += \"\\n\"\n",
"\n",
"# inside this loop, build the phrases. strachey implemented \"short\" phrases\n",
"# and \"long\" phrases; two or more \"short\" phrases in a row have special\n",
"# formatting rules, so we need to know what the last phrase kind was in\n",
"# order to generate the output.\n",
"history = []\n",
"body = \"\"\n",
"for i in range(5):\n",
" kind = random.choice([\"short\", \"long\"])\n",
" if kind == \"long\":\n",
" # adjectives and adverbs will be present only 50% of the time\n",
" line = \" \".join([\n",
" \"My\",\n",
" random.choice([random.choice(adjs), \"\"]),\n",
" random.choice(nouns),\n",
" random.choice([random.choice(advs), \"\"]),\n",
" random.choice(verbs),\n",
" \"your\",\n",
" random.choice([random.choice(adjs), \"\"]),\n",
" random.choice(nouns)])\n",
" body += line\n",
" else:\n",
" adj_noun = random.choice(adjs) + \" \" + random.choice(nouns)\n",
" # if the last phrase was \"short,\" use truncated form\n",
" if len(history) > 0 and history[-1] == \"short\":\n",
" body += \": my \" + adj_noun\n",
" else:\n",
" body += \"You are my \" + adj_noun\n",
" body += \". \"\n",
" history.append(kind)\n",
"# clean up output\n",
"body = body.replace(\" \", \" \")\n",
"body = body.replace(\". :\", \":\")\n",
"# put everything together\n",
"output += textwrap.fill(body, 60)\n",
"output += \"\\n\\nYours \" + random.choice(advs) + \",\\n\"\n",
"output += \"M.U.C.\"\n",
"print(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Taroko Gorge\n",
"\n",
"[Original](http://nickm.com/taroko_gorge/) by [Nick Montfort](http://nickm.com/). [ELMCIP entry here](https://elmcip.net/creative-work/taroko-gorge)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"above = ['brow', 'mist', 'shape', 'layer', 'the crag', 'stone', 'forest', 'height']\n",
"below = ['flow', 'basin', 'shape', 'vein', 'rippling', 'stone', 'cove', 'rock']\n",
"transitive = ['command', 'pace', 'roam', 'trail', 'frame', 'sweep', 'exercise', 'range']\n",
"imperative = ['track', 'shade', 'translate', 'stamp', 'progress through', 'direct', 'run', 'enter']\n",
"intransitive = ['linger', 'dwell', 'rest', 'relax', 'hold', 'dream', 'hum']\n",
"texture = ['rough', 'fine']\n",
"adjectives = ['encompassing', 'sinuous', 'straight', 'objective', 'arched', 'cool', 'clear', 'dim', 'driven']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def path():\n",
" plural = random.sample([\"s\", \"\"], k=2)\n",
" words = random.choice(above)\n",
" if words == \"forest\" and random.randrange(4) == 0:\n",
" words = \"monkeys\" + \" \" + random.choice(transitive)\n",
" else:\n",
" words += plural[0] + \" \" + random.choice(transitive) + plural[1]\n",
" words += \" the \" + random.choice(below) + random.choice([\"s\", \"\"]) + \".\"\n",
" return words.capitalize()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def cave():\n",
" adjs = adjectives[:] + random.sample(texture, 1)\n",
" return \" \" + random.choice(imperative) + \" \" + \\\n",
" \" \".join(random.sample(adjs, random.randrange(1, 4))) + \" —\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def site():\n",
" if random.randrange(2) == 0:\n",
" words = random.choice(above)\n",
" else:\n",
" words = random.choice(below)\n",
" words += \"s \" + random.choice(intransitive) + \".\"\n",
" return words.capitalize()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stone knocks the stones.\n",
"Stones dream.\n",
"The crags tap the cove.\n",
"\n",
" track arched —\n",
"\n",
"The crag roams the flows.\n",
"Stones rest.\n",
"Shapes rest.\n",
"Mist holds the flows.\n",
"\n",
" enter objective arched sinuous —\n",
"\n",
"Stones knock the cove.\n",
"Heights cross the stones.\n",
"\n",
" direct fine —\n",
"\n",
"Mist sweeps the basins.\n",
"Layers knock the veins.\n",
"\n",
" direct arched straight —\n",
"\n",
"Shapes grip the flow.\n",
"Shape holds the rock.\n",
"\n",
" run straight arched encompassing —\n",
"\n",
"Height grips the shapes.\n",
"Heights hum.\n",
"Shapes whisper.\n",
"Brow crosss the vein.\n",
"\n",
" track clear fine —\n",
"\n",
"Forests grip the basin.\n",
"Shapes scream.\n",
"Shapes hum.\n",
"Stone taps the flows.\n",
"\n",
" track encompassing sinuous —\n",
"\n",
"Layers kiss the stone.\n",
"Height holds the shapes.\n",
"\n",
" translate objective —\n",
"\n",
"Shape knocks the rock.\n",
"Coves shout.\n",
"Basins shout.\n",
"The crags cross the flows.\n",
"\n",
" direct sinuous —\n",
"\n",
"Shape holds the stones.\n",
"Rocks hum.\n",
"Mist holds the cove.\n",
"\n",
" shade driven straight dim —\n",
"\n"
]
}
],
"source": [
"stanza_count = 10\n",
"for repeat in range(stanza_count):\n",
" line_count = random.randrange(3, 6)\n",
" for i in range(line_count):\n",
" if i == 0:\n",
" print(path())\n",
" elif i == line_count - 2:\n",
" print(path())\n",
" elif i == line_count - 1:\n",
" print()\n",
" print(cave())\n",
" print()\n",
" else:\n",
" print(site())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assignment1"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mouths kiss the staircases.\n",
"Empty dome rests\n",
"Solitude melts.\n",
"\n",
"Limbs tap the walls.\n",
"Empty wall screams\n",
"Solitude evaporates.\n",
"\n",
"Noses kiss the roof.\n",
"Void roof shivers\n",
"Despair evaporates.\n",
"\n"
]
}
],
"source": [
"body = [\"mouth\", \"eye\", \"ear\", \"nose\",\"limb\", \"finger\", \"toe\", \"palm\" ]\n",
"transitive = ['tap','kiss','hold','grip','brush','cross','roam','knock','sweep','blow']\n",
"space = ['staircase','hallway','arch','dome','pillar','roof','wall','floor','door','window']\n",
"emotion = ['melancholy','despair','fear','agony','weariness','regret','solitude']\n",
"cycle_intransitive = ['condense','melt','evaporate','precipitate']\n",
"intransitive = ['hum','whisper','weep','shiver','rest','dream','giggle','shout','scream']\n",
"adjective = ['flat','straight','boundless','endless','sinuous','spiral','void','empty']\n",
"\n",
"def movement():\n",
" words = random.choice(body) + \"s \"\n",
" words += random.choice(transitive)+\" the \" + random.choice(space) + random.choice([\"s\", \"\"]) + \".\"\n",
"\n",
" return words.capitalize()\n",
"\n",
"def architecture():\n",
" words = random.choice(adjective)+\" \"+random.choice(space)+\" \"\n",
" words += random.choice(intransitive)+\"s\"\n",
" return words.capitalize()\n",
"\n",
"def cycle():\n",
" words = random.choice(emotion)+\" \"+random.choice(cycle_intransitive)+'s.'\n",
" return words.capitalize()\n",
" \n",
"\n",
"stanza_count = 3\n",
"line_count = 3\n",
"for repeat in range(stanza_count):\n",
" for i in range(line_count):\n",
" if i == 0:\n",
" print(movement())\n",
" elif i==1:\n",
" print(architecture())\n",
" else:\n",
" print(cycle())\n",
" print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment