File size: 18,818 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "88f13606",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import os\n",
"import re\n",
"\n",
"import string\n",
"\n",
"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*\""
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "9bff3e8d",
"metadata": {},
"outputs": [],
"source": [
"mlma_dataset = \"../../data/hate_speech_mlma_fr_dataset.csv\"\n",
"output_dir = \"../../data/processed/\"\n",
"\n",
"mlma_df = pd.read_csv(mlma_dataset, index_col=[0])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "909d714e",
"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>sentiment</th>\n",
" <th>directness</th>\n",
" <th>annotator_sentiment</th>\n",
" <th>target</th>\n",
" <th>group</th>\n",
" </tr>\n",
" <tr>\n",
" <th>HITId</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>rt personnage ainsi q tte la clique gauchiste...</td>\n",
" <td>abusive</td>\n",
" <td>direct</td>\n",
" <td>shock</td>\n",
" <td>origin</td>\n",
" <td>other</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>bah oui t'as raison l'autre c'est un mongol.</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>shock</td>\n",
" <td>other</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>moi j'ai personne à qui m'adresser pour réclam...</td>\n",
" <td>hateful</td>\n",
" <td>direct</td>\n",
" <td>anger_confusion_sadness_fear_disgust</td>\n",
" <td>other</td>\n",
" <td>women</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>en algerie les migrants sont expulsés dans le ...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>indifference</td>\n",
" <td>other</td>\n",
" <td>immigrants</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>mytho t'as juste voulu te faire ken par les re...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>indifference_disgust</td>\n",
" <td>other</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4009</th>\n",
" <td>vos twt rappel l'époque ou les journalistes tr...</td>\n",
" <td>offensive</td>\n",
" <td>direct</td>\n",
" <td>shock</td>\n",
" <td>other</td>\n",
" <td>jews</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4010</th>\n",
" <td>tres bien. le cinema francais c...est souvent ...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>sadness</td>\n",
" <td>other</td>\n",
" <td>left_wing_people</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4011</th>\n",
" <td>j’ai que des rebeus en demande d’amis aussi</td>\n",
" <td>disrespectful_normal</td>\n",
" <td>direct</td>\n",
" <td>indifference</td>\n",
" <td>origin</td>\n",
" <td>arabs</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4012</th>\n",
" <td>sale juif de merde</td>\n",
" <td>offensive</td>\n",
" <td>direct</td>\n",
" <td>fear_shock_indifference_sadness_disgust</td>\n",
" <td>religion</td>\n",
" <td>jews</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4014</th>\n",
" <td>mdr c’est un mongol lui en mode lui demander d...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>indifference</td>\n",
" <td>disability</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>3193 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" tweet \\\n",
"HITId \n",
"1 rt personnage ainsi q tte la clique gauchiste... \n",
"2 bah oui t'as raison l'autre c'est un mongol. \n",
"3 moi j'ai personne à qui m'adresser pour réclam... \n",
"6 en algerie les migrants sont expulsés dans le ... \n",
"9 mytho t'as juste voulu te faire ken par les re... \n",
"... ... \n",
"4009 vos twt rappel l'époque ou les journalistes tr... \n",
"4010 tres bien. le cinema francais c...est souvent ... \n",
"4011 j’ai que des rebeus en demande d’amis aussi \n",
"4012 sale juif de merde \n",
"4014 mdr c’est un mongol lui en mode lui demander d... \n",
"\n",
" sentiment directness \\\n",
"HITId \n",
"1 abusive direct \n",
"2 offensive indirect \n",
"3 hateful direct \n",
"6 offensive indirect \n",
"9 offensive indirect \n",
"... ... ... \n",
"4009 offensive direct \n",
"4010 offensive indirect \n",
"4011 disrespectful_normal direct \n",
"4012 offensive direct \n",
"4014 offensive indirect \n",
"\n",
" annotator_sentiment target group \n",
"HITId \n",
"1 shock origin other \n",
"2 shock other individual \n",
"3 anger_confusion_sadness_fear_disgust other women \n",
"6 indifference other immigrants \n",
"9 indifference_disgust other individual \n",
"... ... ... ... \n",
"4009 shock other jews \n",
"4010 sadness other left_wing_people \n",
"4011 indifference origin arabs \n",
"4012 fear_shock_indifference_sadness_disgust religion jews \n",
"4014 indifference disability individual \n",
"\n",
"[3193 rows x 6 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mlma_df.tweet = mlma_df.tweet.apply(lambda x: re.sub(user_naming_pattern, \"\", x).strip())\n",
"\n",
"mlma_abuse_df = mlma_df \\\n",
" .loc[mlma_df.sentiment != \"normal\"]\n",
"\n",
"mlma_no_abuse_df = mlma_df \\\n",
" .loc[mlma_df.sentiment == \"normal\"]\n",
"\n",
"mlma_abuse_df"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "e227676c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_17576/1118330829.py:12: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" pruned_mlma_abuse.tweet = pruned_mlma_abuse.tweet.apply(clean_text)\n"
]
},
{
"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>sentiment</th>\n",
" <th>directness</th>\n",
" <th>annotator_sentiment</th>\n",
" <th>target</th>\n",
" <th>group</th>\n",
" </tr>\n",
" <tr>\n",
" <th>HITId</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>personnage ainsi q tte la clique gauchiste de...</td>\n",
" <td>abusive</td>\n",
" <td>direct</td>\n",
" <td>shock</td>\n",
" <td>origin</td>\n",
" <td>other</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>bah oui t'as raison l'autre c'est un mongol.</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>shock</td>\n",
" <td>other</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>en algerie les migrants sont expulsés dans le ...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>indifference</td>\n",
" <td>other</td>\n",
" <td>immigrants</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>mytho t'as juste voulu te faire ken par les re...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>indifference_disgust</td>\n",
" <td>other</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>c’est un giga attardé mdrrr ils va vraiment se...</td>\n",
" <td>offensive</td>\n",
" <td>direct</td>\n",
" <td>indifference</td>\n",
" <td>origin</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4008</th>\n",
" <td>pk tu mparles ching chong fils de pute</td>\n",
" <td>offensive</td>\n",
" <td>direct</td>\n",
" <td>indifference</td>\n",
" <td>origin</td>\n",
" <td>asians</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4009</th>\n",
" <td>vos twt rappel l'époque ou les journalistes tr...</td>\n",
" <td>offensive</td>\n",
" <td>direct</td>\n",
" <td>shock</td>\n",
" <td>other</td>\n",
" <td>jews</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4010</th>\n",
" <td>tres bien. le cinema francais c...est souvent ...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>sadness</td>\n",
" <td>other</td>\n",
" <td>left_wing_people</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4012</th>\n",
" <td>sale juif de merde</td>\n",
" <td>offensive</td>\n",
" <td>direct</td>\n",
" <td>fear_shock_indifference_sadness_disgust</td>\n",
" <td>religion</td>\n",
" <td>jews</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4014</th>\n",
" <td>mdr c’est un mongol lui en mode lui demander d...</td>\n",
" <td>offensive</td>\n",
" <td>indirect</td>\n",
" <td>indifference</td>\n",
" <td>disability</td>\n",
" <td>individual</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1930 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" tweet sentiment \\\n",
"HITId \n",
"1 personnage ainsi q tte la clique gauchiste de... abusive \n",
"2 bah oui t'as raison l'autre c'est un mongol. offensive \n",
"6 en algerie les migrants sont expulsés dans le ... offensive \n",
"9 mytho t'as juste voulu te faire ken par les re... offensive \n",
"10 c’est un giga attardé mdrrr ils va vraiment se... offensive \n",
"... ... ... \n",
"4008 pk tu mparles ching chong fils de pute offensive \n",
"4009 vos twt rappel l'époque ou les journalistes tr... offensive \n",
"4010 tres bien. le cinema francais c...est souvent ... offensive \n",
"4012 sale juif de merde offensive \n",
"4014 mdr c’est un mongol lui en mode lui demander d... offensive \n",
"\n",
" directness annotator_sentiment target \\\n",
"HITId \n",
"1 direct shock origin \n",
"2 indirect shock other \n",
"6 indirect indifference other \n",
"9 indirect indifference_disgust other \n",
"10 direct indifference origin \n",
"... ... ... ... \n",
"4008 direct indifference origin \n",
"4009 direct shock other \n",
"4010 indirect sadness other \n",
"4012 direct fear_shock_indifference_sadness_disgust religion \n",
"4014 indirect indifference disability \n",
"\n",
" group \n",
"HITId \n",
"1 other \n",
"2 individual \n",
"6 immigrants \n",
"9 individual \n",
"10 individual \n",
"... ... \n",
"4008 asians \n",
"4009 jews \n",
"4010 left_wing_people \n",
"4012 jews \n",
"4014 individual \n",
"\n",
"[1930 rows x 6 columns]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def clean_text(text):\n",
" \n",
" out = text.replace(\"rt \", \"\")\n",
" out = out.replace(\"\\xa0\", \"\")\n",
" for w in out.split():\n",
" if \"…\" in w:\n",
" out = out.replace(w, \"\")\n",
" return out\n",
"\n",
"target_categories = [\"abusive\", \"offensive\"]\n",
"pruned_mlma_abuse = mlma_abuse_df.loc[mlma_abuse_df.sentiment.isin(target_categories)]\n",
"pruned_mlma_abuse.tweet = pruned_mlma_abuse.tweet.apply(clean_text)\n",
"pruned_mlma_abuse"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "db79bea5",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"pruned_mlma_abuse.to_csv(os.path.join(output_dir, \"mlma_positive.csv\"))\n",
"mlma_no_abuse_df.to_csv(os.path.join(output_dir, \"mlma_negative.csv\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|