Spaces:
Sleeping
Sleeping
File size: 6,940 Bytes
dfe58cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
{
"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
}
|