{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.21 s, sys: 798 ms, total: 5.01 s\n",
      "Wall time: 4.31 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import malaya"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Describe supported entities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OTHER - Other\n",
      "law - law, regulation, related law documents, documents, etc\n",
      "location - location, place\n",
      "organization - organization, company, government, facilities, etc\n",
      "person - person, group of people, believes, unique arts (eg; food, drink), etc\n",
      "quantity - numbers, quantity\n",
      "time - date, day, time, etc\n",
      "event - unique event happened, etc\n"
     ]
    }
   ],
   "source": [
    "malaya.entity.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List available Transformer NER models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bert': ['426.4 MB', 'accuracy: 0.994'],\n",
       " 'tiny-bert': ['57.7 MB', 'accuracy: 0.986'],\n",
       " 'albert': ['48.6 MB', 'accuracy: 0.984'],\n",
       " 'tiny-albert': ['22.4 MB', 'accuracy: 0.971'],\n",
       " 'xlnet': ['446.6 MB', 'accuracy: 0.992'],\n",
       " 'alxlnet': ['46.8 MB', 'accuracy: 0.993']}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.entity.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#entities-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": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/__init__.py:54: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.\n",
      "\n",
      "WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/__init__.py:55: The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.\n",
      "\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.7/site-packages/albert/tokenization.py:240: The name tf.logging.info is deprecated. Please use tf.compat.v1.logging.info instead.\n",
      "\n",
      "INFO:tensorflow:loading sentence piece model\n",
      "WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/function/__init__.py:49: The name tf.InteractiveSession is deprecated. Please use tf.compat.v1.InteractiveSession instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "model = malaya.entity.transformer(model = 'albert')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Kuala', 'location'),\n",
       " ('Lumpur:', 'location'),\n",
       " ('Sempena', 'OTHER'),\n",
       " ('sambutan', 'OTHER'),\n",
       " ('Aidilfitri', 'event'),\n",
       " ('minggu', 'OTHER'),\n",
       " ('depan,', 'OTHER'),\n",
       " ('Perdana', 'person'),\n",
       " ('Menteri', 'person'),\n",
       " ('Tun', 'person'),\n",
       " ('Dr', 'person'),\n",
       " ('Mahathir', 'person'),\n",
       " ('Mohamad', 'person'),\n",
       " ('dan', 'OTHER'),\n",
       " ('Menteri', 'person'),\n",
       " ('Pengangkutan', 'person'),\n",
       " ('Anthony', 'person'),\n",
       " ('Loke', 'person'),\n",
       " ('Siew', 'person'),\n",
       " ('Fook', 'person'),\n",
       " ('menitipkan', 'OTHER'),\n",
       " ('pesanan', 'OTHER'),\n",
       " ('khas', 'OTHER'),\n",
       " ('kepada', 'OTHER'),\n",
       " ('orang', 'OTHER'),\n",
       " ('ramai', 'OTHER'),\n",
       " ('yang', 'OTHER'),\n",
       " ('mahu', 'OTHER'),\n",
       " ('pulang', 'OTHER'),\n",
       " ('ke', 'OTHER'),\n",
       " ('kampung', 'OTHER'),\n",
       " ('halaman', 'OTHER'),\n",
       " ('masing-masing.', 'OTHER'),\n",
       " ('Dalam', 'OTHER'),\n",
       " ('video', 'OTHER'),\n",
       " ('pendek', 'OTHER'),\n",
       " ('terbitan', 'OTHER'),\n",
       " ('Jabatan', 'organization'),\n",
       " ('Keselamatan', 'organization'),\n",
       " ('Jalan', 'organization'),\n",
       " ('Raya', 'organization'),\n",
       " ('(JKJR)', 'organization'),\n",
       " ('itu,', 'OTHER'),\n",
       " ('Dr', 'person'),\n",
       " ('Mahathir', 'person'),\n",
       " ('menasihati', 'OTHER'),\n",
       " ('mereka', 'OTHER'),\n",
       " ('supaya', 'OTHER'),\n",
       " ('berhenti', 'OTHER'),\n",
       " ('berehat', 'OTHER'),\n",
       " ('dan', 'OTHER'),\n",
       " ('tidur', 'OTHER'),\n",
       " ('sebentar', 'OTHER'),\n",
       " ('sekiranya', 'OTHER'),\n",
       " ('mengantuk', 'OTHER'),\n",
       " ('ketika', 'OTHER'),\n",
       " ('memandu.', 'OTHER')]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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': 'location',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 0,\n",
       "   'endOffset': 1},\n",
       "  {'text': 'Sempena sambutan',\n",
       "   'type': 'OTHER',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 2,\n",
       "   'endOffset': 3},\n",
       "  {'text': 'Aidilfitri',\n",
       "   'type': 'event',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 4,\n",
       "   'endOffset': 4},\n",
       "  {'text': 'minggu depan,',\n",
       "   'type': 'OTHER',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 5,\n",
       "   'endOffset': 6},\n",
       "  {'text': 'Perdana Menteri Tun Dr Mahathir Mohamad',\n",
       "   'type': 'person',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 7,\n",
       "   'endOffset': 12},\n",
       "  {'text': 'dan',\n",
       "   'type': 'OTHER',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 13,\n",
       "   'endOffset': 13},\n",
       "  {'text': 'Menteri Pengangkutan Anthony Loke Siew Fook',\n",
       "   'type': 'person',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 14,\n",
       "   'endOffset': 19},\n",
       "  {'text': 'menitipkan pesanan khas kepada orang ramai yang mahu pulang ke kampung halaman masing-masing. Dalam video pendek terbitan',\n",
       "   'type': 'OTHER',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 20,\n",
       "   'endOffset': 36},\n",
       "  {'text': 'Jabatan Keselamatan Jalan Raya (JKJR)',\n",
       "   'type': 'organization',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 37,\n",
       "   'endOffset': 41},\n",
       "  {'text': 'itu,',\n",
       "   'type': 'OTHER',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 42,\n",
       "   'endOffset': 42},\n",
       "  {'text': 'Dr Mahathir',\n",
       "   'type': 'person',\n",
       "   'score': 1.0,\n",
       "   'beginOffset': 43,\n",
       "   'endOffset': 44}]}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.analyze(string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load general Malaya entity model\n",
    "\n",
    "This model able to classify,\n",
    "\n",
    "1. date\n",
    "2. money\n",
    "3. temperature\n",
    "4. distance\n",
    "5. volume\n",
    "6. duration\n",
    "7. phone\n",
    "8. email\n",
    "9. url\n",
    "10. time\n",
    "11. datetime\n",
    "12. local and generic foods, can check available rules in malaya.texts._food\n",
    "13. local and generic drinks, can check available rules in malaya.texts._food\n",
    "\n",
    "We can insert BERT or any deep learning model by passing `malaya.entity.general_entity(model = model)`, as long the model has `predict` method and return `[(string, label), (string, label)]`. This is an optional."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "entity = malaya.entity.general_entity(model = model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'person': ['Husein'],\n",
       " 'OTHER': ['baca buku Perlembagaan yang berharga 3k ringgit dekat',\n",
       "  'minggu lepas,',\n",
       "  ', suhu 32 celcius, sambil makan ayam goreng dan milo o ais'],\n",
       " 'location': ['kfc sungai petani'],\n",
       " 'time': {'2 PM': datetime.datetime(2020, 4, 26, 14, 0),\n",
       "  '2 PM 2 oktober 2019': datetime.datetime(2019, 10, 2, 14, 0)},\n",
       " 'date': {'2 oktober 2019': datetime.datetime(2019, 10, 2, 0, 0),\n",
       "  'minggu lalu': datetime.datetime(2020, 4, 19, 23, 51, 23, 231714)},\n",
       " 'money': {'3k ringgit': 'RM3000.0'},\n",
       " 'temperature': ['32 celcius'],\n",
       " 'distance': [],\n",
       " 'volume': [],\n",
       " 'duration': [],\n",
       " 'phone': [],\n",
       " 'email': [],\n",
       " 'url': [],\n",
       " 'datetime': {'2 ptg 2 oktober 2019': datetime.datetime(2019, 10, 2, 14, 0)},\n",
       " 'food': ['ayam goreng'],\n",
       " 'drink': ['milo o ais'],\n",
       " 'weight': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entity.predict('Husein baca buku Perlembagaan yang berharga 3k ringgit dekat kfc sungai petani minggu lepas, 2 ptg 2 oktober 2019 , suhu 32 celcius, sambil makan ayam goreng dan milo o ais')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'OTHER': ['contact', 'at'],\n",
       " 'person': ['Husein', 'husein.zol05@gmail.com'],\n",
       " 'date': {},\n",
       " 'money': {},\n",
       " 'temperature': [],\n",
       " 'distance': [],\n",
       " 'volume': [],\n",
       " 'duration': [],\n",
       " 'phone': [],\n",
       " 'email': ['husein.zol05@gmail.com'],\n",
       " 'url': [],\n",
       " 'time': {},\n",
       " 'datetime': {},\n",
       " 'food': [],\n",
       " 'drink': [],\n",
       " 'weight': []}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entity.predict('contact Husein at husein.zol05@gmail.com')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'OTHER': ['tolong tempahkan meja makan makan', 'dan', 'esok dekat'],\n",
       " 'person': ['nasi dagang', 'jus apple, milo tarik', 'Restoran Sebulek'],\n",
       " 'date': {'esok': datetime.datetime(2020, 4, 27, 23, 51, 23, 646172)},\n",
       " 'money': {},\n",
       " 'temperature': [],\n",
       " 'distance': [],\n",
       " 'volume': [],\n",
       " 'duration': [],\n",
       " 'phone': [],\n",
       " 'email': [],\n",
       " 'url': [],\n",
       " 'time': {},\n",
       " 'datetime': {},\n",
       " 'food': ['nasi dagang'],\n",
       " 'drink': ['milo tarik', 'jus apple'],\n",
       " 'weight': []}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entity.predict('tolong tempahkan meja makan makan nasi dagang dan jus apple, milo tarik esok dekat Restoran Sebulek')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Voting stack model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('tolong', 'OTHER'),\n",
       " ('tempahkan', 'OTHER'),\n",
       " ('meja', 'OTHER'),\n",
       " ('makan', 'OTHER'),\n",
       " ('makan', 'OTHER'),\n",
       " ('nasi', 'OTHER'),\n",
       " ('dagang', 'person'),\n",
       " ('dan', 'OTHER'),\n",
       " ('jus', 'OTHER'),\n",
       " ('apple,', 'person'),\n",
       " ('milo', 'person'),\n",
       " ('tarik', 'OTHER'),\n",
       " ('esok', 'OTHER'),\n",
       " ('dekat', 'OTHER'),\n",
       " ('Restoran', 'organization'),\n",
       " ('Sebulek', 'organization')]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alxlnet = malaya.entity.transformer(model = 'alxlnet')\n",
    "malaya.stack.voting_stack([model, alxlnet, alxlnet],\n",
    "'tolong tempahkan meja makan makan nasi dagang dan jus apple, milo tarik esok dekat Restoran Sebulek')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
