File size: 7,400 Bytes
813cd99 |
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "cee0a836",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"\n",
"import string\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "33a2e4e7",
"metadata": {},
"outputs": [],
"source": [
"ftr_dataset = \"/opt/tmp/src/FTR-dataset/FTR_new_labels.csv\"\n",
"output_dir = \"../../data/processed/\"\n",
"\n",
"ftr_df = pd.read_csv(ftr_dataset, index_col=[0], encoding = \"ISO-8859-1\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "85957e26",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>tweet</th>\n",
" <th>label</th>\n",
" </tr>\n",
" <tr>\n",
" <th>id</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Elle ne devait pas dégager la nazi ???</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>je dois faire un oral en anglais sur Hitler et...</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>A ça ð¤ de ressembler à la croix nazi mdrr</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>\"Par curiosité, jâai voulu écouter les dis...</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Mdr deux nazi n importe quoi ð¤¦ð¼ââï¸</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2851</th>\n",
" <td>Kevin câest un sal noir.</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2852</th>\n",
" <td>bougnoule bamboula sale arabe , elle est belle...</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2853</th>\n",
" <td>Tu viens de le traiter de sal noir ?</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2854</th>\n",
" <td>SAL NOIR</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2855</th>\n",
" <td>Sal Noir</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>2856 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" tweet label\n",
"id \n",
"0 Elle ne devait pas dégager la nazi ??? 1\n",
"1 je dois faire un oral en anglais sur Hitler et... 0\n",
"2 A ça ð¤ de ressembler à la croix nazi mdrr 0\n",
"3 \"Par curiosité, jâai voulu écouter les dis... 0\n",
"4 Mdr deux nazi n importe quoi ð¤¦ð¼ââï¸ 1\n",
"... ... ...\n",
"2851 Kevin câest un sal noir. 1\n",
"2852 bougnoule bamboula sale arabe , elle est belle... 1\n",
"2853 Tu viens de le traiter de sal noir ? 1\n",
"2854 SAL NOIR 1\n",
"2855 Sal Noir 1\n",
"\n",
"[2856 rows x 2 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chars = string.ascii_letters + string.punctuation + string.whitespace\n",
"chars = chars + \"éèê\"\n",
"\n",
"url_pattern = r\"(http|https)\\S*\"\n",
"user_naming_pattern = r\"@\\S*\"\n",
"\n",
"ftr_df.tweet = ftr_df.tweet \\\n",
" .apply(lambda x: re.sub(url_pattern, \"\", x)) \\\n",
" .apply(lambda x: re.sub(user_naming_pattern, \"\", x))\n",
"\n",
"ftr_df"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "6ac2e998",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"def clean_text(text):\n",
" \n",
" out = text.replace(\"é\", \"é\")\n",
" out = out.replace(\"ç\", \"ç\")\n",
" out = out.replace(\"Ã\\x87\", \"ç\")\n",
" out = out.replace(\"Ã\\xa0\", \"à\")\n",
" out = out.replace(\"î\", \"ê\")\n",
" out = out.replace(\"è\", \"è\")\n",
" out = out.replace(\"â\", \"'\")\n",
" out = out.replace('Ã\\x8a', \"ê\")\n",
" out = out.replace('Ã\\x89', \"é\")\n",
" out = out.replace(\"Å\\x93\", \"oe\")\n",
" out = out.replace(\"ô\", \"ô\")\n",
" \n",
" \n",
" out = out.replace(\"ê\", \"\")\n",
" out = out.replace(\"\\n\", \"\")\n",
" out = out.replace(\"\\\\'\", \"\")\n",
" \n",
" # Smileys\n",
" out = out.replace(\"â\\x9c\\x8b\", \"\")\n",
" out = out.replace(\"â\\x9c\\x8b\", \"\")\n",
" out = out.replace(\"\\x8f\", \"\")\n",
" out = out.replace(\"â\\x9c\\x8a\", \"\")\n",
" out = out.replace(\"â\\x80\\x9d\", \"\")\n",
" out = out.replace(\"«Â\\xa0\", \"\")\n",
" out = out.replace(\"\\x85\", \"\")\n",
" \n",
" # Smileys\n",
" out = re.sub(\"ð([^\\s]+)\", \"\", out)\n",
" out = re.sub(\"â([^\\s]+)\", \"\", out)\n",
" \n",
" return out\n",
"\n",
"ftr_df[\"tweet_clean\"] = ftr_df.tweet.apply(clean_text)\n",
"\n",
"ftr_df.loc[ftr_df.label == 1].to_csv(os.path.join(output_dir, \"ftr_dataset_positive.csv\"))\n",
"ftr_df.loc[ftr_df.label == 0].to_csv(os.path.join(output_dir, \"ftr_dataset_negative.csv\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sexism_detection",
"language": "python",
"name": "sexism_detection"
},
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|