{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "datasets = pd.read_csv('datasets/datasets-label.csv')\n",
    "svm = pd.read_csv('datasets/evaluation_results_SVM.csv')\n",
    "nb = pd.read_csv('datasets/evaluation_results_nb.csv')\n",
    "knn = pd.read_csv('datasets/evaluation_results_knn.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Distribusi Label')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22588 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#pie chart\n",
    "label_counts = datasets['label'].value_counts()\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.pie(label_counts, labels=label_counts.index, autopct='%1.1f%%', colors=['#66b3ff','#ff9999','#99ff99'])\n",
    "ax.set_title('Distribusi Label')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8369 entries, 0 to 8368\n",
      "Data columns (total 11 columns):\n",
      " #   Column              Non-Null Count  Dtype              \n",
      "---  ------              --------------  -----              \n",
      " 0   created_at          8369 non-null   datetime64[ns, UTC]\n",
      " 1   full_text           8369 non-null   object             \n",
      " 2   keyword             8369 non-null   object             \n",
      " 3   cleanning_text      8369 non-null   object             \n",
      " 4   case_folding        8369 non-null   object             \n",
      " 5   convert_slang_word  8369 non-null   object             \n",
      " 6   filtering           8369 non-null   object             \n",
      " 7   tokenizing          8369 non-null   object             \n",
      " 8   stemming            8369 non-null   object             \n",
      " 9   score               8369 non-null   int64              \n",
      " 10  label               8369 non-null   object             \n",
      "dtypes: datetime64[ns, UTC](1), int64(1), object(9)\n",
      "memory usage: 719.3+ KB\n"
     ]
    }
   ],
   "source": [
    "# convert created_at to datetime\n",
    "import datetime\n",
    "import pandas as pd\n",
    "\n",
    "datasets = pd.read_csv('datasets/datasets-keyword-label.csv') \n",
    "datasets['created_at'] = pd.to_datetime(datasets['created_at'], format=\"%a %b %d %H:%M:%S %z %Y\")\n",
    "\n",
    "datasets.info()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets.to_csv('datasets/datasets-keyword-label.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 created_at  \\\n",
      "0 2024-12-30 07:08:54+00:00   \n",
      "1 2024-12-30 02:09:58+00:00   \n",
      "2 2024-12-29 02:45:40+00:00   \n",
      "3 2024-12-28 11:10:45+00:00   \n",
      "4 2024-12-28 07:30:00+00:00   \n",
      "\n",
      "                                           tweet_url      username  \\\n",
      "0  https://x.com/JavanicaPost/status/187362730463...  JavanicaPost   \n",
      "1  https://x.com/digivestasi/status/1873552074291...   digivestasi   \n",
      "2  https://x.com/ilhamiasnawi/status/187319867055...  ilhamiasnawi   \n",
      "3  https://x.com/bankbsi_id/status/18729633946321...    bankbsi_id   \n",
      "4  https://x.com/CIMBNiaga/status/187290783871422...     CIMBNiaga   \n",
      "\n",
      "                                           full_text    label  \n",
      "0  mudah belanja akses produk pinjam tantang fina...   Netral  \n",
      "1  langkah sederhana investasi gen z capai stabil...  Positif  \n",
      "2     langkah mudah investasi gen z finansial stabil  Positif  \n",
      "3  asalamualaikum alaikum sahabat syariat tantang...  Positif  \n",
      "4  finansial sehat uang kelola tabung investasi b...  Positif  \n",
      "created_at    datetime64[ns, UTC]\n",
      "tweet_url                  object\n",
      "username                   object\n",
      "full_text                  object\n",
      "label                      object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# Output hasil konversi\n",
    "print(datasets.head())\n",
    "print(datasets.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  model                       Confusion Matrix  \\\n",
      "0   svm  [847  11  31   0 889   0   9   0 881]   \n",
      "1    nb  [668  43 178   0 889   0   2  35 853]   \n",
      "2   knn  [324 289 276   0 889   0   0   4 886]   \n",
      "\n",
      "                               Classification Report  \\\n",
      "0  {'negatif': {'precision': 0.9894859813084113, ...   \n",
      "1  {'negatif': {'precision': 0.9970149253731343, ...   \n",
      "2  {'negatif': {'precision': 1.0, 'recall': 0.364...   \n",
      "\n",
      "                             Cross-Validation Scores  Mean CV Score  \\\n",
      "0  [0.9456521739130435, 0.9647676161919041, 0.980...       0.969562   \n",
      "1  [0.8860569715142429, 0.8931784107946027, 0.902...       0.901411   \n",
      "2  [0.6930284857571214, 0.6941529235382309, 0.763...       0.753644   \n",
      "\n",
      "   Std Dev CV Score  \n",
      "0          0.014335  \n",
      "1          0.010737  \n",
      "2          0.065743  \n"
     ]
    }
   ],
   "source": [
    "# penggabungan data evaluation\n",
    "# Menambahkan kolom 'model' ke setiap DataFrame\n",
    "svm['model'] = 'svm'\n",
    "nb['model'] = 'nb'\n",
    "knn['model'] = 'knn'\n",
    "\n",
    "# Mengatur ulang kolom agar 'model' menjadi kolom pertama\n",
    "svm = svm[['model'] + [col for col in svm.columns if col != 'model']]\n",
    "nb = nb[['model'] + [col for col in nb.columns if col != 'model']]\n",
    "knn = knn[['model'] + [col for col in knn.columns if col != 'model']]\n",
    "\n",
    "# Menggabungkan semua DataFrame\n",
    "combined_df = pd.concat([svm, nb, knn], axis=0, ignore_index=True)\n",
    "\n",
    "# Menampilkan hasil\n",
    "print(combined_df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_df.to_csv('datasets/evaluation_results.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1541964 characters]",
      "text/plain": [
       "<Figure size 1000x3000 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# wordcloud\n",
    "from wordcloud import WordCloud\n",
    "\n",
    "# Membuat wordcloud untuk setiap kelas\n",
    "labels = datasets['label'].unique()\n",
    "\n",
    "fig, axes = plt.subplots(len(labels), 1, figsize=(10, 30))\n",
    "\n",
    "for i, label in enumerate(labels):\n",
    "    text = \" \".join(datasets[datasets['label'] == label]['full_text'])\n",
    "    wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)\n",
    "    \n",
    "    axes[i].imshow(wordcloud, interpolation='bilinear')\n",
    "    axes[i].set_title(f'Wordcloud for {label} class')\n",
    "    axes[i].axis('off')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5.2\n"
     ]
    }
   ],
   "source": [
    "import sklearn\n",
    "print(sklearn.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "label\n",
       "netral     8662\n",
       "negatif    1246\n",
       "positif     519\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('datasets/word_count_labeled.csv')\n",
    "\n",
    "df.value_counts('label')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# testing model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib\n",
    "\n",
    "# muat model\n",
    "model_svm = joblib.load('models/svm_model.pkl')\n",
    "model_nb = joblib.load('models/nb_model.pkl')\n",
    "model_knn = joblib.load('models/knn_model.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "# Load model yang sudah disimpan sebelumnya\n",
    "with open('models/svm_model.pkl', 'rb') as file:\n",
    "    model = pickle.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'tfidf_vectorizer.pkl'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfeature_extraction\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtext\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TfidfVectorizer\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# Load kembali TF-IDF vectorizer\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtfidf_vectorizer.pkl\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mrb\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[0;32m      5\u001b[0m     vectorizer \u001b[38;5;241m=\u001b[39m pickle\u001b[38;5;241m.\u001b[39mload(file)\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# Contoh data baru untuk prediksi\u001b[39;00m\n",
      "File \u001b[1;32mc:\\Users\\62851\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:284\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    277\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    278\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    279\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    280\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    281\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    282\u001b[0m     )\n\u001b[1;32m--> 284\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'tfidf_vectorizer.pkl'"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "\n",
    "# Load kembali TF-IDF vectorizer\n",
    "with open('tfidf_vectorizer.pkl', 'rb') as file:\n",
    "    vectorizer = pickle.load(file)\n",
    "\n",
    "# Contoh data baru untuk prediksi\n",
    "new_text = [\"Gaji rendah membuat saya stres\"]\n",
    "\n",
    "# Transformasi teks menjadi fitur numerik\n",
    "X_new = vectorizer.transform(new_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Lakukan prediksi\n",
    "predicted_class = model.predict(X_new)\n",
    "\n",
    "# Cetak hasil prediksi\n",
    "print(\"Sentimen Prediksi:\", predicted_class[0])"
   ]
  }
 ],
 "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
