{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://raw.githubusercontent.com/huseinzol05/Malaya-Dataset/master/stemmer/stem.zip\n",
    "# !wget https://malaya-dataset.s3-ap-southeast-1.amazonaws.com/wiki-stem.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !unzip stem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['spider\\tspider',\n",
       " 'transeksual\\ttranseksual',\n",
       " 'khusus\\tkhusus',\n",
       " 'kriminologi\\tkriminologi',\n",
       " 'negeripingat\\tnegeripingat']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('stemmer-data-v5.txt') as fopen:\n",
    "    stemming = fopen.read().split('\\n')\n",
    "stemming[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "41528"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left, right = [], []\n",
    "\n",
    "for s in stemming:\n",
    "    l, r = s.split('\\t')\n",
    "    left.append(l)\n",
    "    right.append(r)\n",
    "    \n",
    "len(left)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open('news-stem.json') as fopen:\n",
    "    news = json.load(fopen)\n",
    "    \n",
    "for s in news:\n",
    "    left.append(s[0])\n",
    "    right.append(s[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('wiki-stem.json') as fopen:\n",
    "    wiki = json.load(fopen)\n",
    "    \n",
    "for s in wiki:\n",
    "    left.append(s[0])\n",
    "    right.append(s[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1832338, 1832338)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(left), len(right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import youtokentome as yttm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('text.txt', 'w') as fopen:\n",
    "    fopen.write('\\n'.join(left + right))\n",
    "    \n",
    "stemmer_bpe = yttm.BPE.train(data='text.txt', vocab_size=500, model='stemmer.model',\n",
    "              pad_id=0, unk_id=2, bos_id=3, eos_id=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/data_generators/translate.py#L300\n",
    "\n",
    "left = stemmer_bpe.encode(left, output_type=yttm.OutputType.ID)\n",
    "left = [i + [1] for i in left]\n",
    "right = stemmer_bpe.encode(right, output_type=yttm.OutputType.ID)\n",
    "right = [i + [1] for i in right]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_X, test_X, train_Y, test_Y = train_test_split(left, right, test_size = 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open('train-test-bpe.json', 'w') as fopen:\n",
    "    json.dump({'train_X': train_X, 'train_Y': train_Y,\n",
    "              'test_X': test_X,\n",
    "              'test_Y': test_Y}, fopen)"
   ]
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
