{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.28 s, sys: 839 ms, total: 5.12 s\n",
      "Wall time: 4.27 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import malaya"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Describe supported POS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ADJ - Adjective, kata sifat\n",
      "ADP - Adposition\n",
      "ADV - Adverb, kata keterangan\n",
      "ADX - Auxiliary verb, kata kerja tambahan\n",
      "CCONJ - Coordinating conjuction, kata hubung\n",
      "DET - Determiner, kata penentu\n",
      "NOUN - Noun, kata nama\n",
      "NUM - Number, nombor\n",
      "PART - Particle\n",
      "PRON - Pronoun, kata ganti\n",
      "PROPN - Proper noun, kata ganti nama khas\n",
      "SCONJ - Subordinating conjunction\n",
      "SYM - Symbol\n",
      "VERB - Verb, kata kerja\n",
      "X - Other\n"
     ]
    }
   ],
   "source": [
    "malaya.pos.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List available Transformer POS models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bert': ['426.4 MB', 'accuracy: 0.952'],\n",
       " 'tiny-bert': ['57.7 MB', 'accuracy: 0.953'],\n",
       " 'albert': ['48.7 MB', 'accuracy: 0.951'],\n",
       " 'tiny-albert': ['22.4 MB', 'accuracy: 0.933'],\n",
       " 'xlnet': ['446.6 MB', 'accuracy: 0.954'],\n",
       " 'alxlnet': ['46.8 MB', 'accuracy: 0.951']}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.pos.available_transformer_model()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure you can check accuracy chart from here first before select a model, https://malaya.readthedocs.io/en/latest/Accuracy.html#pos-recognition\n",
    "\n",
    "**You might want to use Tiny-Albert, a very small size, 22.4MB, but the accuracy is still on the top notch.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = 'KUALA LUMPUR: Sempena sambutan Aidilfitri minggu depan, Perdana Menteri Tun Dr Mahathir Mohamad dan Menteri Pengangkutan Anthony Loke Siew Fook menitipkan pesanan khas kepada orang ramai yang mahu pulang ke kampung halaman masing-masing. Dalam video pendek terbitan Jabatan Keselamatan Jalan Raya (JKJR) itu, Dr Mahathir menasihati mereka supaya berhenti berehat dan tidur sebentar  sekiranya mengantuk ketika memandu.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load ALBERT model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:loading sentence piece model\n"
     ]
    }
   ],
   "source": [
    "model = malaya.pos.transformer(model = 'albert')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Kuala', 'PROPN'),\n",
       " ('Lumpur:', 'PROPN'),\n",
       " ('Sempena', 'ADP'),\n",
       " ('sambutan', 'NOUN'),\n",
       " ('Aidilfitri', 'NOUN'),\n",
       " ('minggu', 'NOUN'),\n",
       " ('depan,', 'ADJ'),\n",
       " ('Perdana', 'PROPN'),\n",
       " ('Menteri', 'PROPN'),\n",
       " ('Tun', 'PROPN'),\n",
       " ('Dr', 'PROPN'),\n",
       " ('Mahathir', 'PROPN'),\n",
       " ('Mohamad', 'PROPN'),\n",
       " ('dan', 'CCONJ'),\n",
       " ('Menteri', 'PROPN'),\n",
       " ('Pengangkutan', 'PROPN'),\n",
       " ('Anthony', 'PROPN'),\n",
       " ('Loke', 'PROPN'),\n",
       " ('Siew', 'PROPN'),\n",
       " ('Fook', 'PROPN'),\n",
       " ('menitipkan', 'VERB'),\n",
       " ('pesanan', 'NOUN'),\n",
       " ('khas', 'ADJ'),\n",
       " ('kepada', 'ADP'),\n",
       " ('orang', 'NOUN'),\n",
       " ('ramai', 'ADJ'),\n",
       " ('yang', 'PRON'),\n",
       " ('mahu', 'ADV'),\n",
       " ('pulang', 'VERB'),\n",
       " ('ke', 'ADP'),\n",
       " ('kampung', 'NOUN'),\n",
       " ('halaman', 'NOUN'),\n",
       " ('masing-masing.', 'DET'),\n",
       " ('Dalam', 'ADP'),\n",
       " ('video', 'NOUN'),\n",
       " ('pendek', 'ADJ'),\n",
       " ('terbitan', 'NOUN'),\n",
       " ('Jabatan', 'PROPN'),\n",
       " ('Keselamatan', 'PROPN'),\n",
       " ('Jalan', 'PROPN'),\n",
       " ('Raya', 'PROPN'),\n",
       " ('(JKJR)', 'PUNCT'),\n",
       " ('itu,', 'DET'),\n",
       " ('Dr', 'PROPN'),\n",
       " ('Mahathir', 'PROPN'),\n",
       " ('menasihati', 'VERB'),\n",
       " ('mereka', 'PRON'),\n",
       " ('supaya', 'SCONJ'),\n",
       " ('berhenti', 'VERB'),\n",
       " ('berehat', 'VERB'),\n",
       " ('dan', 'CCONJ'),\n",
       " ('tidur', 'VERB'),\n",
       " ('sebentar', 'NOUN'),\n",
       " ('sekiranya', 'SCONJ'),\n",
       " ('mengantuk', 'ADJ'),\n",
       " ('ketika', 'SCONJ'),\n",
       " ('memandu.', 'VERB')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words': ['Kuala',\n",
       "  'Lumpur:',\n",
       "  'Sempena',\n",
       "  'sambutan',\n",
       "  'Aidilfitri',\n",
       "  'minggu',\n",
       "  'depan,',\n",
       "  'Perdana',\n",
       "  'Menteri',\n",
       "  'Tun',\n",
       "  'Dr',\n",
       "  'Mahathir',\n",
       "  'Mohamad',\n",
       "  'dan',\n",
       "  'Menteri',\n",
       "  'Pengangkutan',\n",
       "  'Anthony',\n",
       "  'Loke',\n",
       "  'Siew',\n",
       "  'Fook',\n",
       "  'menitipkan',\n",
       "  'pesanan',\n",
       "  'khas',\n",
       "  'kepada',\n",
       "  'orang',\n",
       "  'ramai',\n",
       "  'yang',\n",
       "  'mahu',\n",
       "  'pulang',\n",
       "  'ke',\n",
       "  'kampung',\n",
       "  'halaman',\n",
       "  'masing-masing.',\n",
       "  'Dalam',\n",
       "  'video',\n",
       "  'pendek',\n",
       "  'terbitan',\n",
       "  'Jabatan',\n",
       "  'Keselamatan',\n",
       "  'Jalan',\n",
       "  'Raya',\n",
       "  '(JKJR)',\n",
       "  'itu,',\n",
       "  'Dr',\n",
       "  'Mahathir',\n",
       "  'menasihati',\n",
       "  'mereka',\n",
       "  'supaya',\n",
       "  'berhenti',\n",
       "  'berehat',\n",
       "  'dan',\n",
       "  'tidur',\n",
       "  'sebentar',\n",
       "  'sekiranya',\n",
       "  'mengantuk',\n",
       "  'ketika',\n",
       "  'memandu.'],\n",
       " 'tags': [{'text': 'Kuala Lumpur:',\n",
       "   'type': 'PROPN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 0,\n",
       "   'endOffset': 1},\n",
       "  {'text': 'Sempena',\n",
       "   'type': 'ADP',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 2,\n",
       "   'endOffset': 2},\n",
       "  {'text': 'sambutan Aidilfitri minggu',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 3,\n",
       "   'endOffset': 5},\n",
       "  {'text': 'depan,',\n",
       "   'type': 'ADJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 6,\n",
       "   'endOffset': 6},\n",
       "  {'text': 'Perdana Menteri Tun Dr Mahathir Mohamad',\n",
       "   'type': 'PROPN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 7,\n",
       "   'endOffset': 12},\n",
       "  {'text': 'dan',\n",
       "   'type': 'CCONJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 13,\n",
       "   'endOffset': 13},\n",
       "  {'text': 'Menteri Pengangkutan Anthony Loke Siew Fook',\n",
       "   'type': 'PROPN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 14,\n",
       "   'endOffset': 19},\n",
       "  {'text': 'menitipkan',\n",
       "   'type': 'VERB',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 20,\n",
       "   'endOffset': 20},\n",
       "  {'text': 'pesanan',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 21,\n",
       "   'endOffset': 21},\n",
       "  {'text': 'khas',\n",
       "   'type': 'ADJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 22,\n",
       "   'endOffset': 22},\n",
       "  {'text': 'kepada',\n",
       "   'type': 'ADP',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 23,\n",
       "   'endOffset': 23},\n",
       "  {'text': 'orang',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 24,\n",
       "   'endOffset': 24},\n",
       "  {'text': 'ramai',\n",
       "   'type': 'ADJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 25,\n",
       "   'endOffset': 25},\n",
       "  {'text': 'yang',\n",
       "   'type': 'PRON',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 26,\n",
       "   'endOffset': 26},\n",
       "  {'text': 'mahu',\n",
       "   'type': 'ADV',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 27,\n",
       "   'endOffset': 27},\n",
       "  {'text': 'pulang',\n",
       "   'type': 'VERB',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 28,\n",
       "   'endOffset': 28},\n",
       "  {'text': 'ke',\n",
       "   'type': 'ADP',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 29,\n",
       "   'endOffset': 29},\n",
       "  {'text': 'kampung halaman',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 30,\n",
       "   'endOffset': 31},\n",
       "  {'text': 'masing-masing.',\n",
       "   'type': 'DET',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 32,\n",
       "   'endOffset': 32},\n",
       "  {'text': 'Dalam',\n",
       "   'type': 'ADP',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 33,\n",
       "   'endOffset': 33},\n",
       "  {'text': 'video',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 34,\n",
       "   'endOffset': 34},\n",
       "  {'text': 'pendek',\n",
       "   'type': 'ADJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 35,\n",
       "   'endOffset': 35},\n",
       "  {'text': 'terbitan',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 36,\n",
       "   'endOffset': 36},\n",
       "  {'text': 'Jabatan Keselamatan Jalan Raya',\n",
       "   'type': 'PROPN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 37,\n",
       "   'endOffset': 40},\n",
       "  {'text': '(JKJR)',\n",
       "   'type': 'PUNCT',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 41,\n",
       "   'endOffset': 41},\n",
       "  {'text': 'itu,',\n",
       "   'type': 'DET',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 42,\n",
       "   'endOffset': 42},\n",
       "  {'text': 'Dr Mahathir',\n",
       "   'type': 'PROPN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 43,\n",
       "   'endOffset': 44},\n",
       "  {'text': 'menasihati',\n",
       "   'type': 'VERB',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 45,\n",
       "   'endOffset': 45},\n",
       "  {'text': 'mereka',\n",
       "   'type': 'PRON',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 46,\n",
       "   'endOffset': 46},\n",
       "  {'text': 'supaya',\n",
       "   'type': 'SCONJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 47,\n",
       "   'endOffset': 47},\n",
       "  {'text': 'berhenti berehat',\n",
       "   'type': 'VERB',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 48,\n",
       "   'endOffset': 49},\n",
       "  {'text': 'dan',\n",
       "   'type': 'CCONJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 50,\n",
       "   'endOffset': 50},\n",
       "  {'text': 'tidur',\n",
       "   'type': 'VERB',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 51,\n",
       "   'endOffset': 51},\n",
       "  {'text': 'sebentar',\n",
       "   'type': 'NOUN',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 52,\n",
       "   'endOffset': 52},\n",
       "  {'text': 'sekiranya',\n",
       "   'type': 'SCONJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 53,\n",
       "   'endOffset': 53},\n",
       "  {'text': 'mengantuk',\n",
       "   'type': 'ADJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 54,\n",
       "   'endOffset': 54},\n",
       "  {'text': 'ketika',\n",
       "   'type': 'SCONJ',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 55,\n",
       "   'endOffset': 55}]}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.analyze(string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Voting stack model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alxlnet = malaya.pos.transformer(model = 'alxlnet')\n",
    "malaya.stack.voting_stack([model, alxlnet, alxlnet], string)"
   ]
  }
 ],
 "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
