Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created July 31, 2019 22:23
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 aparrish/f9eedbc9bd41473141407c60ad2ccd95 to your computer and use it in GitHub Desktop.
Save aparrish/f9eedbc9bd41473141407c60ad2ccd95 to your computer and use it in GitHub Desktop.
json and tracery
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loading JSON to use with tracery\n",
"\n",
"\"JavaScript Object Notation\"\n",
"\n",
"[Corpora project](https://github.com/dariusk/corpora)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"data = json.load(open(\"academic_subjects.json\"))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"subjects = data['subjects']"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"data = json.load(open(\"firstNames.json\"))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"first = data['firstNames']"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"data = json.load(open(\"lastNames.json\"))\n",
"last = data['lastNames']"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"colleges = open(\"liberal-arts-colleges.txt\").read().split(\"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import tracery\n",
"from tracery.modifiers import base_english"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"My name is Eric Nguyen. I'm a full professor of Speech & Rhetorical Studies at Columbia College Chicago\""
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rules = {\n",
" \"origin\": \"My name is #first# #last#. I'm #proftype.a# professor of #subject# at #college#\",\n",
" \"first\": first,\n",
" \"last\": last,\n",
" \"subject\": subjects,\n",
" \"college\": colleges,\n",
" \"proftype\": [\"adjunct\", \"visiting\", \"assistant\", \"associate\", \"full\", \"mega\"]\n",
"}\n",
"grammar = tracery.Grammar(rules)\n",
"grammar.add_modifiers(base_english)\n",
"grammar.flatten(\"#origin#\")"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"cheeses = [\n",
" {\"name\": \"Cheddar\", \"tastiness\": 4},\n",
" {\"name\": \"Swiss\", \"tastiness\": 6},\n",
" {\"name\": \"Brie\", \"tastiness\": 6},\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cheddar\n",
"Swiss\n",
"Brie\n"
]
}
],
"source": [
"for item in cheeses:\n",
" print(item[\"name\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## tarot card generator"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"tarot_data = json.load(open(\"tarot_interpretations.json\"))"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Fool\n",
"The Magician\n",
"The Papess/High Priestess\n",
"The Empress\n",
"The Emperor\n",
"The Pope/Hierophant\n",
"The Lovers\n",
"The Chariot\n",
"Strength\n",
"The Hermit\n",
"The Wheel\n",
"Justice\n",
"The Hanged Man\n",
"Death\n",
"Temperance\n",
"The Devil\n",
"The Tower\n",
"The Star\n",
"The Moon\n",
"The Sun\n",
"Judgement\n",
"The World\n",
"ace of wands\n",
"two of wands\n",
"three of wands\n",
"four of wands\n",
"five of wands\n",
"six of wands\n",
"seven of wands\n",
"eight of wands\n",
"nine of wands\n",
"ten of wands\n",
"page of wands\n",
"knight of wands\n",
"queen of wands\n",
"king of wands\n",
"ace of cups\n",
"two of cups\n",
"three of cups\n",
"four of cups\n",
"five of cups\n",
"six of cups\n",
"seven of cups\n",
"eight of cups\n",
"nine of cups\n",
"ten of cups\n",
"page of cups\n",
"knight of cups\n",
"queen of cups\n",
"king of cups\n",
"ace of swords\n",
"two of swords\n",
"three of swords\n",
"four of swords\n",
"five of swords\n",
"six of swords\n",
"seven of swords\n",
"eight of swords\n",
"nine of swords\n",
"ten of swords\n",
"page of swords\n",
"knight of swords\n",
"queen of swords\n",
"king of swords\n",
"ace of coins\n",
"two of coins\n",
"three of coins\n",
"four of coins\n",
"five of coins\n",
"six of coins\n",
"seven of coins\n",
"eight of coins\n",
"nine of coins\n",
"ten of coins\n",
"page of coins\n",
"knight of coins\n",
"queen of coins\n",
"king of coins\n"
]
}
],
"source": [
"for item in tarot_data['tarot_interpretations']:\n",
" print(item[\"name\"])"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"light = []\n",
"shadow = []\n",
"for item in tarot_data['tarot_interpretations']:\n",
" light.extend(item['meanings']['light'])\n",
" shadow.extend(item['meanings']['shadow'])"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"x = []"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"x.append(1)"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1]"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"x.append(2)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"y = [4, 5, 6]"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"x.extend(y)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 4, 5, 6]"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"484"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(light)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"435"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(shadow)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Continuing a fight against all odds'"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.choice(light)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Taking a fatalistic approach to life'"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.choice(shadow)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"two of cups\n",
"Light: Taking a leap of faith\n",
"Shadow: Never being satisfied, no matter how much you have\n",
"\n",
"pi of cups\n",
"Light: Getting all the facts\n",
"Shadow: Ignoring gut feelings\n",
"\n",
"e of spades\n",
"Light: Nursing a secret crush\n",
"Shadow: Wallowing in despair\n",
"\n",
"seventeen of cups\n",
"Light: Celebrating your body\n",
"Shadow: Looking down on people who seem less capable\n",
"\n",
"ace of diamonds\n",
"Light: Discovering your sexuality\n",
"Shadow: Spending all of your money on extravagant gifts and possessions\n",
"\n",
"pi of spades\n",
"Light: Rejecting anxiety\n",
"Shadow: Depression\n",
"\n",
"ace of cheese\n",
"Light: Pursuing a course of study\n",
"Shadow: Disrupting group efforts\n",
"\n",
"puppy of pentacles\n",
"Light: Recognizing your needs and taking action to fulfill them\n",
"Shadow: Applying ruthless or twisted logic\n",
"\n",
"one of coins\n",
"Light: Living life to its fullest\n",
"Shadow: Picking fights\n",
"\n",
"A.L.F. of spades\n",
"Light: Celebrating your own successes\n",
"Shadow: Taking advantage of others\n",
"\n"
]
}
],
"source": [
"rules = {\n",
" \"origin\": \"#rank# of #suit#\\nLight: #light#\\nShadow: #shadow#\",\n",
" \"light\": light,\n",
" \"shadow\": shadow,\n",
" \"rank\": [\"ace\", \"one\", \"two\", \"three\", \"pi\", \"e\", \"seventeen\", \"puppy\", \"A.L.F.\"],\n",
" \"suit\": [\"cups\", \"diamonds\", \"cheese\", \"pentacles\", \"coins\", \"A.L.F.\", \"spades\"]\n",
"}\n",
"grammar = tracery.Grammar(rules)\n",
"grammar.add_modifiers(base_english)\n",
"for i in range(10):\n",
" print(grammar.flatten(\"#origin#\"))\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:anaconda]",
"language": "python",
"name": "conda-env-anaconda-py"
},
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment