{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use Wikipedia to train dict\n",
    "\n",
    "Reference:\n",
    "- [以 gensim 訓練中文詞向量](http://zake7749.github.io/2016/08/28/word2vec-with-gensim/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 取得語料\n",
    "\n",
    "挑選的 `pages-articles.xml.bz2` 結尾的備份，例如 `zhwiki-20180101-pages-articles.xml.bz2` (1.4 GB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def download_file(url, path):\n",
    "    '''Download large file as stream\n",
    "    https://stackoverflow.com/a/16696317/3744499\n",
    "    '''\n",
    "    import requests\n",
    "    from tqdm import tqdm\n",
    "    \n",
    "    if path is None:\n",
    "        path = url.split('/')[-1]\n",
    "\n",
    "    r = requests.get(url, stream=True)  # NOTE the stream=True parameter\n",
    "    total_length = int(r.headers.get('content-length'))\n",
    "    \n",
    "    with open(path, 'wb') as f:\n",
    "        for chunk in tqdm(r.iter_content(chunk_size=1024), total=(total_length // 1024) + 1): \n",
    "            if chunk: # filter out keep-alive new chunks\n",
    "                f.write(chunk)\n",
    "                #f.flush() commented by recommendation from J.F.Sebastian\n",
    "    return path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1485026/1485026 [12:41<00:00, 1950.60it/s]\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'local_filename' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-8364d6d0214a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m download_file('https://dumps.wikimedia.org/zhwiki/20180101/zhwiki-20180101-pages-articles.xml.bz2',\n\u001b[0;32m----> 2\u001b[0;31m               path='/tmp/wiki-articles.xml.bz2')\n\u001b[0m",
      "\u001b[0;32m<ipython-input-1-53bf88aa155c>\u001b[0m in \u001b[0;36mdownload_file\u001b[0;34m(url, path)\u001b[0m\n\u001b[1;32m     17\u001b[0m                 \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchunk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m                 \u001b[0;31m#f.flush() commented by recommendation from J.F.Sebastian\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mlocal_filename\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'local_filename' is not defined"
     ]
    }
   ],
   "source": [
    "download_file('https://dumps.wikimedia.org/zhwiki/20180101/zhwiki-20180101-pages-articles.xml.bz2',\n",
    "              path='/tmp/wiki-articles.xml.bz2')"
   ]
  },
  {
   "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
