{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from fastcache import clru_cache\n",
    "from functools import lru_cache\n",
    "from nltk.corpus import brown\n",
    "from nltk.stem import WordNetLemmatizer\n",
    "import dautil as dl\n",
    "import numpy as np\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def lemmatize(word, lemmatizer):\n",
    "    return lemmatizer.lemmatize(word.lower())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def measure(impl, words, lemmatizer):\n",
    "    cache = dl.perf.LRUCache(impl, lemmatize)\n",
    "    times = []\n",
    "    hm = []\n",
    "\n",
    "    for i in range(5, 12):\n",
    "        cache.maxsize = 2 ** i\n",
    "        cache.cache()\n",
    "        with dl.perf.StopWatch() as sw:\n",
    "            _ = [cache.cached(w, lemmatizer) for w in words]\n",
    "\n",
    "        hm.append(cache.hits_miss())\n",
    "        times.append(sw.elapsed)\n",
    "        cache.clear()\n",
    "        \n",
    "    return (times, hm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "words = [w for w in brown.words()]\n",
    "lemmatizer = WordNetLemmatizer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('caching_lru')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with dl.perf.StopWatch() as sw:\n",
    "    _ = [lemmatizer.lemmatize(w.lower()) for w in words]\n",
    "    \n",
    "plain = sw.elapsed\n",
    "\n",
    "times, hm = measure(clru_cache, words, lemmatizer)\n",
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "sp.ax.plot(2 ** np.arange(5, 12), times)\n",
    "sp.ax.axhline(plain, lw=2, label='Uncached')\n",
    "sp.label()\n",
    "\n",
    "sp.next_ax().plot(2 ** np.arange(5, 12), hm)\n",
    "sp.label()\n",
    "\n",
    "times, hm = measure(lru_cache, words, lemmatizer)\n",
    "sp.next_ax().plot(2 ** np.arange(5, 12), times)\n",
    "sp.ax.axhline(plain, lw=2, label='Uncached')\n",
    "sp.label()\n",
    "\n",
    "sp.next_ax().plot(2 ** np.arange(5, 12), hm)\n",
    "sp.label()\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
