Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Last active April 26, 2020 00:45
Show Gist options
  • Save davidrichards/17e1912264e0a988871c653eeb0c637c to your computer and use it in GitHub Desktop.
Save davidrichards/17e1912264e0a988871c653eeb0c637c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Found Poetry\n",
"\n",
"> TBD\n",
"\n",
"There are NLP-based poetry-writing models, and I may get there someday. Right now, I'm using [simpler methods](https://www.thoughtco.com/found-poetry-4157546) to think about how generated text might be evaluated and rule-bound."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#hide\n",
"from nbdev.showdoc import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# export\n",
"from labs.imports import *\n",
"import textwrap\n",
"import wikipedia\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"text = \"\"\"\n",
"The reason why the lab work yesterday was powerful was it showed me a repeatable pattern that should grow to fit any kind of problem I’m working on with Branch. What I did was I wrote some interface code for a basic recommendation. This included the utilities to describe types expansively as well as dictionaries as an OpenStruct. It also included the ServiceObject as it now is implemented. I had written some reactive code using rx, but I decided that error control and a little flow control with if statements was enough for now.\n",
"\n",
"What these utilities did, in the experiments section of my code, was allow me to see the flow. I created a funny-to-me scheduler that schedules everything for the next March 31st. I’m tipping my hat to Parks and Recreation. What I do later is check rule adherence—not really, but I do the step. Finally, I evaluate my own performance. This composite response is quite instrutive. It tells me how I did, what I did, whether I think I did a good job, and specifically how the known rules do or don’t influence my recommendations.\n",
"\n",
"When I moved this code into the notebooks and generated library code and documentation from that, I realized how easy it will be to use this versioned software to solve many problems at once. I can have a service that is incomplete, but useful. I can replace a service with a better one. I can mix different kinds of services as well—machine learning, human delegated, database or knowledgebase lookups, or some sort of hybrid of this type of work.\n",
"\n",
"The container is large enough to do the job and small enough to be adjusted and understood.\n",
"\n",
"So, with these advantages, I began to see the services I’m working on, the effort it will take to get to a good demonstration of the Branch system, and the relative complexity of building my first 10 services compared to my first 100 services. These are roughly equivalent levels of effort. Roughly. Meaning, there is a lot of repeated work that creates efficiencies and balance in my efforts.\n",
"\n",
"Also, all of this work becomes more interesting because it’s creating efficiencies and balance with humanity. Give me a chance to balance the books, put an appropriate level of effort behind something, handle my obligations, consider the implications of policies and changes, or deal with the immense complexity in my life. This opportunity to engage effectively in modern life is the reason Branch is critical.\n",
"\n",
"When I look at who I am, the kind of person I am, what interests me in the middle of the night, what pushes me outside of my comfort zone or breaks down my capacity to engage—all of these things involve a harmony of sorts. Let me use the anxiety that I’ve developed to be humane to others. I can empathize with someone stressed with a sitation too big or too complex for them to do well. Let me address the system results. Let me address life, because that’s how I act. This is the kind of solution I can spend decades developing.\n",
"\n",
"What I’m saying is this is rewarding work, and it fels like a solution, even in these early versions of the software.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def wrap_text(text, width=80):\n",
" text = text.strip()\n",
" text = ' '.join(text.split())\n",
" \n",
" return textwrap.wrap(text, width, break_long_words=False)\n",
"\n",
"def odds(lines):\n",
" return lines[::2]\n",
"\n",
"def evens(lines):\n",
" return lines[1::2]\n",
"\n",
"def halves(lines):\n",
" length = len(lines)\n",
" middle_index = length//2\n",
" first_half = lines[:middle_index]\n",
" second_half = lines[middle_index:]\n",
" return (first_half, second_half)\n",
"\n",
"def sides(lines):\n",
" return (odds(lines), evens(lines))\n",
"\n",
"def quarters(lines):\n",
" top, bottom = halves(lines)\n",
" q1, q2 = sides(top)\n",
" q3, q4 = sides(bottom)\n",
" return (q1, q2, q3, q4)\n",
"\n",
"def quarter_text(text, width=80):\n",
" width = width//2\n",
" lines = wrap_text(text, width=width)\n",
" groups = quarters(lines)\n",
" return [' '.join(group) for group in groups]\n",
"\n",
"def draw_from(words, n=6):\n",
" if callable(n): n = n()\n",
" if isinstance(words, str): words = words.split()\n",
" return random.sample(words, n)\n",
"\n",
"def quatrain(text, width=80, n=6):\n",
" \"\"\"Not rhyming, this is more Dadaist than poetic.\"\"\"\n",
" groups = quarter_text(text, width=width)\n",
" return '\\n'.join([' '.join(draw_from(group, n=n)) for group in groups])\n",
"\n",
"def print_quatrain(text, maximum=6, minimum=4):\n",
" length_fn = partial(random.randint, minimum, maximum)\n",
" print(quatrain(text, n=length_fn))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I work What grow decided can\n",
"a powerful problems included a working\n",
"the look services to balance me\n",
"advantages, I even can reason because\n"
]
}
],
"source": [
"print_quatrain(text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" As I work, what grows decided\n",
" a powerful problem included\n",
" the services give me balance\n",
" advantages: I can reason."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" quite everything my software\n",
" but my rules one.\n",
" outside address is more the\n",
" the of consider of and"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Making Sense\n",
"\n",
"So that's kind of interesting. I cleaned up a few of the stanzas above:\n",
"\n",
" And that grows as\n",
" the scheduler rules fit the code,\n",
" each to its own level found--roughly\n",
" and handle someone first in harmony\n",
"\n",
" Many Mes: the code from March\n",
" all of one, serve as one\n",
" me and my work\n",
" one and the same, efficient\n",
"\n",
" It is that March\n",
" of the notebooks tipping\n",
" it feels as life outside\n",
" person small with empathy\n",
" \n",
"This morning I wrote:\n",
"\n",
" It's time I learn Swedish, not because I'm smarter or have a copious amount of free time, or any excess mental capacity at the moment, but because learning a language invites me to be gentle and patient and OK to be a child amid others' vast knowledge.\n",
"\n",
" Awe is underrated.\n",
"\n",
"All is similar: finding a way to be stunned by existence, redeem the world in words (paraphrasing [Zeeshan](https://twitter.com/ZeeshanJaanam/status/1247607335520030720))."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def print_hop(hint, stanza):\n",
" print(f\"{hint}\\n\\n{stanza}\\n\\n\")\n",
" \n",
"def hop(hint, n=6, items=[]):\n",
" if callable(n): n = n()\n",
" if n <= 0:\n",
" print()\n",
" for title, stanza in items:\n",
" print_hop(title, stanza)\n",
" return items\n",
" try:\n",
" page = wikipedia.page(hint)\n",
" q = quatrain(page.summary)\n",
" items.append((hint, q))\n",
" hint = random.sample(page.links, 1)[0]\n",
" print('.', end='')\n",
" except:\n",
" n = 0\n",
" return hop(hint, n=n-1, items=items)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"..\n",
"Ozarks\n",
"\n",
"Boston Ozark half and broad Ozarks:\n",
"area Mountains a of northern Mountains\n",
"in Salem, the popularizer between border.\n",
"Appalachians Louis named Missouri, Fayetteville. and\n",
"\n",
"\n",
"Douglas County, Missouri\n",
"\n",
"Missouri. As in state of the\n",
"U.S. portion population the The the\n",
"Senator county and presidential 1857, Democratic\n",
"A. and organized officially Stephen on\n",
"\n",
"\n"
]
},
{
"data": {
"text/plain": [
"[('Ozarks',\n",
" 'Boston Ozark half and broad Ozarks:\\narea Mountains a of northern Mountains\\nin Salem, the popularizer between border.\\nAppalachians Louis named Missouri, Fayetteville. and'),\n",
" ('Douglas County, Missouri',\n",
" 'Missouri. As in state of the\\nU.S. portion population the The the\\nSenator county and presidential 1857, Democratic\\nA. and organized officially Stephen on')]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hop('Ozarks')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"List of North American countries by GDP (nominal) per capita\n",
"\n",
"of the population. its services a\n",
"the goods (See greatly considered the\n",
"some of also of that in\n",
"in narrowly IMF in data here,\n",
"\n",
"\n",
"List of countries by GDP (nominal)\n",
"\n",
"per to one young for statistical\n",
"though in countries. high on take\n",
"2004, territories) to the a of\n",
"sources. as It IMF output. entities\n",
"\n",
"\n",
"Newly industrialized country\n",
"\n",
"scientists classification several of category to\n",
"socioeconomic (NIC) countries a by around\n",
"urbanization, industrialization, developing developing other such\n",
"social economic the are is and\n",
"\n",
"\n",
"South America\n",
"\n",
"is the the 16.5% land Ocean,\n",
"miles), America, its that an be\n",
"is and until period). Americans slaves\n",
"beginning and to reached the the\n",
"\n",
"\n",
"Per capita GDP\n",
"\n",
"little countries, the though living the\n",
"income figures countries. parity the into\n",
"of per which of dependent and\n",
"value appear subject are States than\n",
"\n",
"\n",
"Korea\n",
"\n",
"the century collapsed Korea and Baekje,\n",
"Around other Sea Following the state.\n",
"Both of the This in resulted\n",
"into the Korea the and to\n"
]
}
],
"source": [
"print(\"\"\"\n",
"List of North American countries by GDP (nominal) per capita\n",
"\n",
"of the population. its services a\n",
"the goods (See greatly considered the\n",
"some of also of that in\n",
"in narrowly IMF in data here,\n",
"\n",
"\n",
"List of countries by GDP (nominal)\n",
"\n",
"per to one young for statistical\n",
"though in countries. high on take\n",
"2004, territories) to the a of\n",
"sources. as It IMF output. entities\n",
"\n",
"\n",
"Newly industrialized country\n",
"\n",
"scientists classification several of category to\n",
"socioeconomic (NIC) countries a by around\n",
"urbanization, industrialization, developing developing other such\n",
"social economic the are is and\n",
"\n",
"\n",
"South America\n",
"\n",
"is the the 16.5% land Ocean,\n",
"miles), America, its that an be\n",
"is and until period). Americans slaves\n",
"beginning and to reached the the\n",
"\n",
"\n",
"Per capita GDP\n",
"\n",
"little countries, the though living the\n",
"income figures countries. parity the into\n",
"of per which of dependent and\n",
"value appear subject are States than\n",
"\n",
"\n",
"Korea\n",
"\n",
"the century collapsed Korea and Baekje,\n",
"Around other Sea Following the state.\n",
"Both of the This in resulted\n",
"into the Korea the and to\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"The century collapsed Baekje to Korea\n",
"around the sea and following the state\n",
"both this and its result\n",
"folded into Korea\n",
"\n",
"Little countries, though living\n",
"figures parity by income\n",
"dependent\n",
"value appears to the States\n",
"\n",
"Over ocean and through time\n",
"American slaves reached land\n",
"\n",
"Scientists newly industrialized the country\n",
"classification by socioeconomic status\n",
"urbanization, industrialization, developing developing\n",
"over such, here we are\n",
"\n",
"The young are statistical promises\n",
"sources of output\n",
"goods and services\n",
"narrowly seen through the IMF\n",
"\n"
]
}
],
"source": [
"print(\"\"\"\n",
"The century collapsed Baekje to Korea\n",
"around the sea and following the state\n",
"both this and its result\n",
"folded into Korea\n",
"\n",
"Little countries, though living\n",
"figures parity by income\n",
"dependent\n",
"value appears to the States\n",
"\n",
"Over ocean and through time\n",
"American slaves reached land\n",
"\n",
"Scientists newly industrialized the country\n",
"classification by socioeconomic status\n",
"urbanization, industrialization, developing developing\n",
"over such, here we are\n",
"\n",
"The young are statistical promises\n",
"sources of output\n",
"goods and services\n",
"narrowly seen through the IMF\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
import os,pickle,random,re,shutil,sys,traceback,uuid,zlib
from collections.abc import Iterable
from configobj import ConfigObj
from contextlib import contextmanager
import datetime as dt
from datetime import datetime
from functools import partial, reduce
from pathlib import Path
import pathlib
from time import process_time
import warnings
import numpy as np
from fastcore.utils import L
import seaborn as sns
import matplotlib.pyplot as plt
import inflection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment