{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 3.64 s, sys: 749 ms, total: 4.39 s\n",
      "Wall time: 4.64 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import malaya"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import pandas as pd\n",
    "df = pd.read_csv('tests/02032018.csv',sep=';')\n",
    "df = df.iloc[3:,1:]\n",
    "df.columns = ['text','label']\n",
    "corpus = df.text.tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate scatter plot for unsupervised clustering\n",
    "\n",
    "```python\n",
    "\n",
    "def cluster_scatter(\n",
    "    corpus,\n",
    "    vectorizer,\n",
    "    num_clusters = 5,\n",
    "    titles = None,\n",
    "    colors = None,\n",
    "    stemming = True,\n",
    "    stop_words = None,\n",
    "    cleaning = simple_textcleaning,\n",
    "    clustering = KMeans,\n",
    "    decomposition = MDS,\n",
    "    ngram = (1, 3),\n",
    "    figsize = (17, 9),\n",
    "    batch_size = 20,\n",
    "):\n",
    "    \"\"\"\n",
    "    plot scatter plot on similar text clusters.\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "\n",
    "    corpus: list\n",
    "    vectorizer: class\n",
    "    num_clusters: int, (default=5)\n",
    "        size of unsupervised clusters.\n",
    "    titles: list\n",
    "        list of titles, length must same with corpus.\n",
    "    colors: list\n",
    "        list of colors, length must same with num_clusters.\n",
    "    stemming: bool, (default=True)\n",
    "        If True, sastrawi_stemmer will apply.\n",
    "    stop_words: list, (default=None)\n",
    "        list of stop words to remove. If None, default is malaya.texts._text_functions.STOPWORDS\n",
    "    ngram: tuple, (default=(1,3))\n",
    "        n-grams size to train a corpus.\n",
    "    cleaning: function, (default=simple_textcleaning)\n",
    "        function to clean the corpus.\n",
    "    batch_size: int, (default=10)\n",
    "        size of strings for each vectorization and attention. Only useful if use transformer vectorizer.\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    dictionary: {\n",
    "        'X': X,\n",
    "        'Y': Y,\n",
    "        'labels': clusters,\n",
    "        'vector': transformed_text_clean,\n",
    "        'titles': titles,\n",
    "    }\n",
    "    \"\"\"\n",
    "    ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "embedding_lookup_factorized. factorized embedding parameterization is used.\n",
      "INFO:tensorflow:Restoring parameters from /Users/huseinzolkepli/Malaya/albert-model/base/albert-base/model.ckpt\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 200524 characters]\n",
      "text/plain": [
       "<Figure size 1224x648 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model = malaya.transformer.load(model = 'albert', size = 'base')\n",
    "result_scatter = malaya.cluster.cluster_scatter(corpus,vectorizer=model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate dendogram plot for unsupervised clustering\n",
    "\n",
    "```python\n",
    "def cluster_dendogram(\n",
    "    corpus,\n",
    "    vectorizer,\n",
    "    titles = None,\n",
    "    stemming = True,\n",
    "    stop_words = None,\n",
    "    cleaning = simple_textcleaning,\n",
    "    random_samples = 0.3,\n",
    "    ngram = (1, 3),\n",
    "    figsize = (17, 9),\n",
    "    batch_size = 20,\n",
    "    **kwargs\n",
    "):\n",
    "    \"\"\"\n",
    "    plot hierarchical dendogram with similar texts.\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "\n",
    "    corpus: list\n",
    "    vectorizer: class\n",
    "    num_clusters: int, (default=5)\n",
    "        size of unsupervised clusters.\n",
    "    titles: list\n",
    "        list of titles, length must same with corpus.\n",
    "    stemming: bool, (default=True)\n",
    "        If True, sastrawi_stemmer will apply.\n",
    "    stop_words: list, (default=None)\n",
    "        list of stop words to remove. If None, default is malaya.texts._text_functions.STOPWORDS\n",
    "    cleaning: function, (default=simple_textcleaning)\n",
    "        function to clean the corpus.\n",
    "    random_samples: float, (default=0.3)\n",
    "        random samples from the corpus, 0.3 means 30%.\n",
    "    ngram: tuple, (default=(1,3))\n",
    "        n-grams size to train a corpus.\n",
    "    batch_size: int, (default=20)\n",
    "        size of strings for each vectorization and attention. Only useful if use transformer vectorizer.\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    dictionary: {'linkage_matrix': linkage_matrix, 'titles': titles}\n",
    "    \"\"\"\n",
    "``` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 87628 characters]\n",
      "text/plain": [
       "<Figure size 1224x648 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "result_scatter = malaya.cluster.cluster_dendogram(corpus,vectorizer=model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate undirected graph for unsupervised clustering\n",
    "\n",
    "```python\n",
    "def cluster_graph(\n",
    "    corpus,\n",
    "    vectorizer,\n",
    "    threshold = 0.9,\n",
    "    num_clusters = 5,\n",
    "    titles = None,\n",
    "    colors = None,\n",
    "    stop_words = None,\n",
    "    stemming = True,\n",
    "    ngram = (1, 3),\n",
    "    cleaning = simple_textcleaning,\n",
    "    clustering = KMeans,\n",
    "    figsize = (17, 9),\n",
    "    with_labels = True,\n",
    "    batch_size = 20,\n",
    "    **kwargs\n",
    "):\n",
    "    \"\"\"\n",
    "    plot undirected graph with similar texts.\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "\n",
    "    corpus: list\n",
    "    vectorizer: class\n",
    "    threshold: float, (default=0.9)\n",
    "        0.9 means, 90% above absolute pearson correlation.\n",
    "    num_clusters: int, (default=5)\n",
    "        size of unsupervised clusters.\n",
    "    titles: list\n",
    "        list of titles, length must same with corpus.\n",
    "    stemming: bool, (default=True)\n",
    "        If True, sastrawi_stemmer will apply.\n",
    "    stop_words: list, (default=None)\n",
    "        list of stop words to remove. If None, default is malaya.texts._text_functions.STOPWORDS\n",
    "    cleaning: function, (default=simple_textcleaning)\n",
    "        function to clean the corpus.\n",
    "    ngram: tuple, (default=(1,3))\n",
    "        n-grams size to train a corpus.\n",
    "    batch_size: int, (default=20)\n",
    "        size of strings for each vectorization and attention. Only useful if use transformer vectorizer.\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    dictionary: {\n",
    "        'G': G,\n",
    "        'pos': pos,\n",
    "        'node_colors': node_colors,\n",
    "        'node_labels': node_labels,\n",
    "    }\n",
    "    \"\"\"\n",
    "```  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 756456 characters]\n",
      "text/plain": [
       "<Figure size 1224x648 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "tf_vectorizer = TfidfVectorizer(\n",
    "    ngram_range = (1, 3),\n",
    "    min_df = 2,\n",
    "    max_df = 0.95,\n",
    ")\n",
    "\n",
    "result_scatter = malaya.cluster.cluster_graph(corpus,vectorizer=tf_vectorizer,threshold=0.3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate undirected graph for Entities and topics relationship\n",
    "\n",
    "```python\n",
    "def cluster_entity_linking(\n",
    "    corpus,\n",
    "    vectorizer,\n",
    "    entity_model,\n",
    "    topic_modeling_model,\n",
    "    threshold = 0.3,\n",
    "    topic_decomposition = 2,\n",
    "    topic_length = 10,\n",
    "    fuzzy_ratio = 70,\n",
    "    accepted_entities = ['law', 'location', 'organization', 'person', 'event'],\n",
    "    cleaning = simple_textcleaning,\n",
    "    stemming = True,\n",
    "    colors = None,\n",
    "    stop_words = None,\n",
    "    max_df = 1.0,\n",
    "    min_df = 1,\n",
    "    ngram = (2, 3),\n",
    "    figsize = (17, 9),\n",
    "    batch_size = 20,\n",
    "    **kwargs\n",
    "):\n",
    "    \"\"\"\n",
    "    plot undirected graph for Entities and topics relationship.\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "    corpus: list or str\n",
    "    vectorizer: class\n",
    "    titles: list\n",
    "        list of titles, length must same with corpus.\n",
    "    colors: list\n",
    "        list of colors, length must same with num_clusters.\n",
    "    threshold: float, (default=0.3)\n",
    "        0.3 means, 30% above absolute pearson correlation.\n",
    "    topic_decomposition: int, (default=2)\n",
    "        size of decomposition.\n",
    "    topic_length: int, (default=10)\n",
    "        size of topic models.\n",
    "    fuzzy_ratio: int, (default=70)\n",
    "        size of ratio for fuzzywuzzy.\n",
    "    stemming: bool, (default=True)\n",
    "        If True, sastrawi_stemmer will apply.\n",
    "    max_df: float, (default=0.95)\n",
    "        maximum of a word selected based on document frequency.\n",
    "    min_df: int, (default=2)\n",
    "        minimum of a word selected on based on document frequency.\n",
    "    ngram: tuple, (default=(1,3))\n",
    "        n-grams size to train a corpus.\n",
    "    cleaning: function, (default=simple_textcleaning)\n",
    "        function to clean the corpus.\n",
    "    stop_words: list, (default=None)\n",
    "        list of stop words to remove. If None, default is malaya.texts._text_functions.STOPWORDS\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    dictionary: {\n",
    "        'G': G,\n",
    "        'pos': pos,\n",
    "        'node_colors': node_colors,\n",
    "        'node_labels': node_labels,\n",
    "    }\n",
    "    \"\"\"\n",
    "```   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /Users/huseinzolkepli/Documents/Malaya/malaya/_utils/_utils.py:69: The name tf.GraphDef is deprecated. Please use tf.compat.v1.GraphDef instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "entity_model = malaya.entity.transformer(model = 'albert', size = 'base')\n",
    "topic_model = malaya.topic_model.lda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 365940 characters]\n",
      "text/plain": [
       "<Figure size 1224x648 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "result_linking = malaya.cluster.cluster_entity_linking(corpus,\n",
    "                                                       tf_vectorizer,\n",
    "                                                       entity_model,\n",
    "                                                       topic_model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cluster same word structure based on POS and Entities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "bahdanau_entities = malaya.entity.deep_model('bahdanau')\n",
    "bahdanau_pos = malaya.pos.deep_model('bahdanau')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_entities = bahdanau_entities.predict(string)\n",
    "result_pos = bahdanau_pos.predict(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['terbitan',\n",
       " 'orang ramai',\n",
       " 'Anthony Loke',\n",
       " 'Jalan Raya',\n",
       " 'sambutan',\n",
       " 'Mahathir Mohamad',\n",
       " 'Kuala Lumpur Sempena',\n",
       " 'Mohamad',\n",
       " 'Jabatan Keselamatan Jalan',\n",
       " 'Sempena sambutan',\n",
       " 'sekiranya',\n",
       " 'kampung halaman masing-masing',\n",
       " 'Menteri Pengangkutan Anthony',\n",
       " 'pesanan',\n",
       " 'masing-masing video',\n",
       " 'masing-masing video terbitan',\n",
       " 'Menteri Tun Dr',\n",
       " 'Dr Mahathir menasihati',\n",
       " 'Menteri',\n",
       " 'mahu pulang kampung',\n",
       " 'menitipkan pesanan orang',\n",
       " 'Perdana Menteri Tun',\n",
       " 'video terbitan',\n",
       " 'Mahathir menasihati',\n",
       " 'pulang kampung halaman',\n",
       " 'Menteri Tun',\n",
       " 'Pengangkutan Anthony',\n",
       " 'mengantuk memandu',\n",
       " 'depan',\n",
       " 'Jkjr Dr Mahathir',\n",
       " 'Pengangkutan',\n",
       " 'Lumpur Sempena',\n",
       " 'Loke Siew',\n",
       " 'halaman masing-masing video',\n",
       " 'minggu',\n",
       " 'pulang kampung',\n",
       " 'Mahathir',\n",
       " 'Anthony Loke Siew',\n",
       " 'terbitan Jabatan',\n",
       " 'Keselamatan Jalan',\n",
       " 'terbitan Jabatan Keselamatan',\n",
       " 'Dr',\n",
       " 'sekiranya mengantuk memandu',\n",
       " 'Lumpur Sempena sambutan',\n",
       " 'sambutan Aidilfitri minggu',\n",
       " 'Jabatan',\n",
       " 'tidur sekiranya',\n",
       " 'Aidilfitri minggu depan',\n",
       " 'masing-masing',\n",
       " 'Fook',\n",
       " 'memandu',\n",
       " 'video terbitan Jabatan',\n",
       " 'Siew',\n",
       " 'Pengangkutan Anthony Loke',\n",
       " 'Kuala',\n",
       " 'pulang',\n",
       " 'Loke',\n",
       " 'minggu depan Perdana',\n",
       " 'Jalan',\n",
       " 'Sempena sambutan Aidilfitri',\n",
       " 'ramai',\n",
       " 'Keselamatan Jalan Raya',\n",
       " 'berhenti berehat',\n",
       " 'Aidilfitri minggu',\n",
       " 'Fook menitipkan',\n",
       " 'sekiranya mengantuk',\n",
       " 'ramai mahu pulang',\n",
       " 'Anthony',\n",
       " 'Jalan Raya Jkjr',\n",
       " 'menasihati berhenti berehat',\n",
       " 'Loke Siew Fook',\n",
       " 'kampung',\n",
       " 'berehat tidur',\n",
       " 'sambutan Aidilfitri',\n",
       " 'menasihati berhenti',\n",
       " 'mahu',\n",
       " 'orang',\n",
       " 'Raya Jkjr',\n",
       " 'orang ramai mahu',\n",
       " 'Mahathir menasihati berhenti',\n",
       " 'kampung halaman',\n",
       " 'Menteri Pengangkutan',\n",
       " 'Lumpur',\n",
       " 'minggu depan',\n",
       " 'depan Perdana Menteri',\n",
       " 'Perdana Menteri',\n",
       " 'Mohamad Menteri Pengangkutan',\n",
       " 'Raya',\n",
       " 'Tun Dr',\n",
       " 'berhenti',\n",
       " 'mahu pulang',\n",
       " 'pesanan orang ramai',\n",
       " 'Tun Dr Mahathir',\n",
       " 'Dr Mahathir',\n",
       " 'Sempena',\n",
       " 'Perdana',\n",
       " 'Fook menitipkan pesanan',\n",
       " 'Raya Jkjr Dr',\n",
       " 'Jkjr Dr',\n",
       " 'menasihati',\n",
       " 'depan Perdana',\n",
       " 'halaman masing-masing',\n",
       " 'menitipkan',\n",
       " 'Jabatan Keselamatan',\n",
       " 'Siew Fook',\n",
       " 'menitipkan pesanan',\n",
       " 'Siew Fook menitipkan',\n",
       " 'pesanan orang',\n",
       " 'tidur',\n",
       " 'Mohamad Menteri',\n",
       " 'tidur sekiranya mengantuk',\n",
       " 'Keselamatan',\n",
       " 'Tun',\n",
       " 'halaman',\n",
       " 'ramai mahu',\n",
       " 'Aidilfitri',\n",
       " 'berhenti berehat tidur',\n",
       " 'Dr Mahathir Mohamad',\n",
       " 'Jkjr',\n",
       " 'mengantuk',\n",
       " 'Mahathir Mohamad Menteri',\n",
       " 'Kuala Lumpur',\n",
       " 'berehat',\n",
       " 'video',\n",
       " 'berehat tidur sekiranya']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generated_grams = malaya.generator.pos_entities_ngram(\n",
    "    result_pos,\n",
    "    result_entities,\n",
    "    ngram = (1, 3),\n",
    "    accept_pos = ['NOUN', 'PROPN', 'VERB'],\n",
    "    accept_entities = ['law', 'location', 'organization', 'person', 'time'],\n",
    ")\n",
    "generated_grams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['halaman masing-masing video',\n",
       " 'Anthony Loke Siew',\n",
       " 'sekiranya mengantuk memandu',\n",
       " 'terbitan Jabatan Keselamatan',\n",
       " 'Kuala Lumpur Sempena',\n",
       " 'sambutan Aidilfitri minggu',\n",
       " 'Lumpur Sempena sambutan',\n",
       " 'Jabatan Keselamatan Jalan',\n",
       " 'orang ramai mahu',\n",
       " 'Aidilfitri minggu depan',\n",
       " 'Siew Fook menitipkan',\n",
       " 'video terbitan Jabatan',\n",
       " 'Mahathir menasihati berhenti',\n",
       " 'tidur sekiranya mengantuk',\n",
       " 'Pengangkutan Anthony Loke',\n",
       " 'kampung halaman masing-masing',\n",
       " 'Menteri Pengangkutan Anthony',\n",
       " 'minggu depan Perdana',\n",
       " 'Sempena sambutan Aidilfitri',\n",
       " 'depan Perdana Menteri',\n",
       " 'Keselamatan Jalan Raya',\n",
       " 'masing-masing video terbitan',\n",
       " 'Menteri Tun Dr',\n",
       " 'Dr Mahathir menasihati',\n",
       " 'ramai mahu pulang',\n",
       " 'mahu pulang kampung',\n",
       " 'berhenti berehat tidur',\n",
       " 'Jalan Raya Jkjr',\n",
       " 'menitipkan pesanan orang',\n",
       " 'menasihati berhenti berehat',\n",
       " 'Mohamad Menteri Pengangkutan',\n",
       " 'Loke Siew Fook',\n",
       " 'Perdana Menteri Tun',\n",
       " 'pesanan orang ramai',\n",
       " 'Tun Dr Mahathir',\n",
       " 'pulang kampung halaman',\n",
       " 'Jkjr Dr Mahathir',\n",
       " 'Mahathir Mohamad Menteri',\n",
       " 'Fook menitipkan pesanan',\n",
       " 'Raya Jkjr Dr',\n",
       " 'Dr Mahathir Mohamad',\n",
       " 'berehat tidur sekiranya']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.cluster.cluster_words(generated_grams)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cluster Part-Of-Speech"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ADJ': ['khas', 'pendek'],\n",
       " 'ADP': ['kepada', 'ke', 'Dalam'],\n",
       " 'ADV': ['sebentar'],\n",
       " 'ADX': [],\n",
       " 'AUX': [],\n",
       " 'CCONJ': ['depan', 'dan'],\n",
       " 'DET': ['itu'],\n",
       " 'NOUN': ['sambutan',\n",
       "  'pesanan',\n",
       "  'orang ramai',\n",
       "  'kampung halaman masing-masing',\n",
       "  'video',\n",
       "  'terbitan Jabatan Keselamatan',\n",
       "  'Jkjr',\n",
       "  'berehat',\n",
       "  'sekiranya'],\n",
       " 'NUM': [],\n",
       " 'PART': [],\n",
       " 'PRON': ['yang', 'mereka'],\n",
       " 'PROPN': ['Kuala Lumpur Sempena',\n",
       "  'Aidilfitri',\n",
       "  'Perdana Menteri Tun Dr Mahathir Mohamad',\n",
       "  'Menteri Pengangkutan Anthony Loke Siew Fook',\n",
       "  'Jalan Raya',\n",
       "  'Dr Mahathir'],\n",
       " 'SCONJ': ['supaya', 'ketika'],\n",
       " 'SYM': [],\n",
       " 'VERB': ['minggu',\n",
       "  'menitipkan',\n",
       "  'mahu pulang',\n",
       "  'menasihati',\n",
       "  'berhenti',\n",
       "  'tidur',\n",
       "  'mengantuk'],\n",
       " 'X': []}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.cluster.cluster_pos(result_pos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'OTHER': ['sempena',\n",
       "  'dan',\n",
       "  'menitipkan pesanan khas kepada orang ramai yang mahu pulang ke kampung halaman masing-masing dalam video pendek terbitan',\n",
       "  'itu'],\n",
       " 'law': [],\n",
       " 'location': ['kuala lumpur'],\n",
       " 'organization': ['menteri pengangkutan', 'jabatan keselamatan jalan raya'],\n",
       " 'person': ['perdana menteri tun dr mahathir mohamad',\n",
       "  'anthony loke siew fook',\n",
       "  'jkjr',\n",
       "  'dr mahathir'],\n",
       " 'quantity': [],\n",
       " 'time': ['minggu depan'],\n",
       " 'event': ['sambutan aidilfitri']}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.cluster.cluster_entities(result_entities)"
   ]
  }
 ],
 "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
}
