{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import csv\n",
    "import ast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_file_path = \"aug_rev.csv\"  # Path of the CSV\n",
    "json_file_path = \"aug_rev.json\"  # Path of the JSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                               Reviews  is_positive?\n",
      "0    Their team was incredibly knowledgeable about ...             1\n",
      "1    Their team was incredibly knowledgeable about ...             1\n",
      "2                             I stayed here overnight.             1\n",
      "3                             Its okay and affordable.             1\n",
      "4                         Best here is their Sinigang.             1\n",
      "..                                                 ...           ...\n",
      "275   I recommend this hotel to all people who want...             1\n",
      "276  iodine commend this hotel to all people World_...             1\n",
      "277  They have bars restaurant coffee shop spa cent...             1\n",
      "278  They have bars restaurant coffee shop spa etc ...             1\n",
      "279  They are the best agents to contact if you are...             1\n",
      "\n",
      "[280 rows x 2 columns]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Ayooo\\AppData\\Local\\Temp\\ipykernel_1904\\2765031854.py:14: FutureWarning: The default value of regex will change from True to False in a future version.\n",
      "  df['Reviews'] = df['Reviews'].str.replace(r\"[\\\"\\',\\[\\]]\", \"\")\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Read the CSV file into a pandas DataFrame\n",
    "df = pd.read_csv('aug_rev.csv')\n",
    "\n",
    "\n",
    "# Separate the reviews by splitting the string at the ', ' separator\n",
    "df['Reviews'] = df['Reviews'].apply(lambda x: x.split(\"', \"))\n",
    "\n",
    "# Explode the \"Reviews\" column to separate each review into its own row\n",
    "df = df.explode('Reviews')\n",
    "\n",
    "# Remove quotation marks and square brackets from the reviews\n",
    "df['Reviews'] = df['Reviews'].str.replace(r\"[\\\"\\',\\[\\]]\", \"\")\n",
    "\n",
    "# Reset the index\n",
    "df.reset_index(drop=True, inplace=True)\n",
    "\n",
    "# Print the updated DataFrame\n",
    "print(df)\n",
    "\n",
    "# Save the updated DataFrame as a new CSV file\n",
    "df.to_csv('aug_rev.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "rows = []\n",
    "\n",
    "# Read the CSV file and preprocess the data\n",
    "with open(csv_file_path, newline='') as csv_file:\n",
    "    reader = csv.DictReader(csv_file)\n",
    "    for row in reader:\n",
    "        row[\"is_positive?\"] = int(row[\"is_positive?\"])\n",
    "        rows.append(row)\n",
    "\n",
    "# Convert the preprocessed data to JSON\n",
    "with open(json_file_path, 'w') as json_file:\n",
    "    json.dump(rows, json_file, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nltk\n",
    "import random\n",
    "\n",
    "nltk.download('punkt')\n",
    "\n",
    "# Define augmentation parameters\n",
    "synonym_replacement_prob = 0.1\n",
    "random_insertion_prob = 0.1\n",
    "random_deletion_prob = 0.1\n",
    "\n",
    "# Initialize NLTK's WordNet for synonym replacement\n",
    "nltk.download('wordnet')\n",
    "from nltk.corpus import wordnet\n",
    "\n",
    "# Function to replace a word with a random synonym\n",
    "def replace_with_synonym(word):\n",
    "    synsets = wordnet.synsets(word)\n",
    "    if synsets:\n",
    "        synonyms = [syn.lemmas()[0].name() for syn in synsets]\n",
    "        return random.choice(synonyms)\n",
    "    else:\n",
    "        return word\n",
    "\n",
    "augmented_sentences = []\n",
    "augmented_labels = []\n",
    "\n",
    "for sentence, label in zip(sentences, labels):\n",
    "    augmented_sentences.append(sentence)\n",
    "    augmented_labels.append(label)\n",
    "    \n",
    "    # Perform synonym replacement\n",
    "    if random.random() < synonym_replacement_prob:\n",
    "        words = nltk.word_tokenize(sentence)\n",
    "        words = [replace_with_synonym(word) for word in words]\n",
    "        augmented_sentences.append(' '.join(words))\n",
    "        augmented_labels.append(label)\n",
    "    \n",
    "    # Perform random insertion\n",
    "    if random.random() < random_insertion_prob:\n",
    "        words = nltk.word_tokenize(sentence)\n",
    "        random_word = random.choice(words)\n",
    "        words.insert(random.randint(0, len(words) - 1), random_word)\n",
    "        augmented_sentences.append(' '.join(words))\n",
    "        augmented_labels.append(label)\n",
    "    \n",
    "    # Perform random deletion\n",
    "    if random.random() < random_deletion_prob:\n",
    "        words = nltk.word_tokenize(sentence)\n",
    "        if len(words) > 1:\n",
    "            random_word_idx = random.randint(0, len(words) - 1)\n",
    "            words.pop(random_word_idx)\n",
    "            augmented_sentences.append(' '.join(words))\n",
    "            augmented_labels.append(label)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the output file path\n",
    "output_file = \"augmented_dataset.csv\"\n",
    "\n",
    "# Combine augmented sentences and labels into a list of tuples\n",
    "augmented_data = list(zip(augmented_sentences, augmented_labels))\n",
    "\n",
    "# Write the augmented data to a CSV file\n",
    "with open(output_file, 'w', newline='') as csvfile:\n",
    "    writer = csv.writer(csvfile)\n",
    "    \n",
    "    # Write the header\n",
    "    writer.writerow(['Sentence', 'Label'])\n",
    "    \n",
    "    # Write each augmented sentence and label as a row in the CSV file\n",
    "    writer.writerows(augmented_data)"
   ]
  }
 ],
 "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.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}