Created
March 7, 2018 04:57
-
-
Save canadaduane/cff4e71aea8df1b5f414ed9006fe72d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "f4b3c9d1b79e4232912299c790e8e59a", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/html": [ | |
"<p>Failed to display Jupyter Widget of type <code>QgridWidget</code>.</p>\n", | |
"<p>\n", | |
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", | |
" that the widgets JavaScript is still loading. If this message persists, it\n", | |
" likely means that the widgets JavaScript library is either not installed or\n", | |
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n", | |
" Widgets Documentation</a> for setup instructions.\n", | |
"</p>\n", | |
"<p>\n", | |
" If you're reading this message in another frontend (for example, a static\n", | |
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n", | |
" it may mean that your frontend doesn't currently support widgets.\n", | |
"</p>\n" | |
], | |
"text/plain": [ | |
"QgridWidget(grid_options={'fullWidthRows': True, 'syncColumnCellResize': True, 'forceFitColumns': True, 'defaultColumnWidth': 150, 'rowHeight': 28, 'enableColumnReorder': False, 'enableTextSelectionOnCells': True, 'editable': True, 'autoEdit': False, 'explicitInitialization': True, 'maxVisibleRows': 15, 'minVisibleRows': 8, 'sortable': True, 'filterable': True, 'highlightSelectedCell': False, 'highlightSelectedRow': True}, precision=5)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"import os, json\n", | |
"import pandas\n", | |
"from IPython.display import HTML, display\n", | |
"\n", | |
"data = os.popen(\"\"\"ruby -rbomdb -e 'puts JSON.pretty_generate(BomDB::Query.new(exclude: \"Bible-OT\").books.to_a)'\"\"\").read()\n", | |
"books = json.loads(data)\n", | |
"\n", | |
"# Use Mosiah Priority\n", | |
"books = books[7:] + books[0:7]\n", | |
"\n", | |
"word_count = [[len(text.split(\"\\\\s+\")[0])] for book, text in books]\n", | |
"word_count_df = pandas.DataFrame(word_count, index=[book for book, text in books], columns=[\"Word Count\"])\n", | |
"\n", | |
"import qgrid\n", | |
"qgrid.show_grid(word_count_df)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "c6f65f41299b49d9af7abe5f27d73974", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/html": [ | |
"<p>Failed to display Jupyter Widget of type <code>QgridWidget</code>.</p>\n", | |
"<p>\n", | |
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", | |
" that the widgets JavaScript is still loading. If this message persists, it\n", | |
" likely means that the widgets JavaScript library is either not installed or\n", | |
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n", | |
" Widgets Documentation</a> for setup instructions.\n", | |
"</p>\n", | |
"<p>\n", | |
" If you're reading this message in another frontend (for example, a static\n", | |
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n", | |
" it may mean that your frontend doesn't currently support widgets.\n", | |
"</p>\n" | |
], | |
"text/plain": [ | |
"QgridWidget(grid_options={'fullWidthRows': True, 'syncColumnCellResize': True, 'forceFitColumns': True, 'defaultColumnWidth': 150, 'rowHeight': 28, 'enableColumnReorder': False, 'enableTextSelectionOnCells': True, 'editable': True, 'autoEdit': False, 'explicitInitialization': True, 'maxVisibleRows': 15, 'minVisibleRows': 8, 'sortable': True, 'filterable': True, 'highlightSelectedCell': False, 'highlightSelectedRow': True}, precision=5)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"import re\n", | |
"from odictliteral import odict\n", | |
"\n", | |
"search = odict[\n", | |
" \"Wherefore\": r'wherefore',\n", | |
" \"Therefore\": r'therefore',\n", | |
" \"House of Israel\": r'house of israel',\n", | |
" \"Did [Verb]\": r'did (go|eat|march|join|come)',\n", | |
" \"Caused\": r'caused',\n", | |
" \"The Face [of the Land]\": r'the face',\n", | |
"]\n", | |
"\n", | |
"# Construct a list of the book and all search word counts in that book,\n", | |
"# e.g. ['1 Nephi', 0, 0, 20, 13, 14]\n", | |
"row = lambda book, text: [len(re.findall(value, text, re.I)) / word_count_df.loc[book]['Word Count'] for value in search.values()]\n", | |
"\n", | |
"special_words_count = [row(book, text) for book, text in books]\n", | |
"\n", | |
"df = pandas.DataFrame(\n", | |
" special_words_count,\n", | |
" index=[book for book, text in books],\n", | |
" columns=list(search.keys()))\n", | |
"\n", | |
"qgrid.show_grid(df)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~canadaduane/2.embed\" height=\"525px\" width=\"100%\"></iframe>" | |
], | |
"text/plain": [ | |
"<plotly.tools.PlotlyDisplay object>" | |
] | |
}, | |
"execution_count": 75, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import plotly.plotly as py\n", | |
"import plotly.graph_objs as go\n", | |
"\n", | |
"makeBar = lambda label: go.Bar(x=df.index, y=df[label], name=label)\n", | |
"traces = [makeBar(label) for label in search.keys()]\n", | |
"\n", | |
"layout = go.Layout(\n", | |
" barmode='group',\n", | |
" title='Style Shift of Certain Phrases in the Book of Mormon',\n", | |
")\n", | |
"\n", | |
"fig = go.Figure(data=traces, layout=layout)\n", | |
"py.iplot(fig, filename='grouped-bar')" | |
] | |
} | |
], | |
"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.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment