marmiskarian commited on
Commit
f23e828
·
1 Parent(s): d789dae

Add data preprocessing notebook and custom text preprocessing function

Browse files
Files changed (2) hide show
  1. data_processing.ipynb +964 -0
  2. preprocessing.py +34 -0
data_processing.ipynb ADDED
@@ -0,0 +1,964 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "f738c6f3-948e-4ba0-a69b-97fd0ce22e84",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "import pandas as pd\n",
13
+ "import sys\n",
14
+ "\n",
15
+ "from kaggle.api.kaggle_api_extended import KaggleApi\n",
16
+ "from sklearn.model_selection import train_test_split\n",
17
+ "from glob import glob\n",
18
+ "\n",
19
+ "from preprocessing import preprocess_text"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "markdown",
24
+ "id": "29a79e0d-9b44-40b0-81ab-7cc7eca2e77b",
25
+ "metadata": {
26
+ "tags": []
27
+ },
28
+ "source": [
29
+ "# Introduction:\n",
30
+ "This notebook aims to preprocess two datasets, the Disaster Tweet Dataset and the Fake/Real News Dataset, obtained from Kaggle using the Kaggle API. The goal is to bring both datasets into a consistent format with two columns: 'text' and 'label' **(0 for real, 1 for fake)**. The data will be split into train and test sets (80/20 ratio) and saved as CSV files.\n",
31
+ "\n",
32
+ "Process Overview:\n",
33
+ "- **Dataset Acquisition:** Download the Disaster Tweet Dataset and Fake/Real News Dataset from Kaggle using the Kaggle API.\n",
34
+ "- **Dataset Preprocessing:** Ensure a consistent format by keeping only the 'text' column, assigning labels (0 for real, 1 for fake), and merging the datasets. The text column in both datasets will undergo preprocessing using a custom function called `preprocess_text`. This function applies various cleaning operations to the text, including URL and user mention removal, non-alphanumeric character removal, hashtag removal, punctuation removal, lowercase conversion, stop word removal and keeping only texts containing at least 3 words.\n",
35
+ "- **Train/Test Split:** Split the combined preprocessed dataset into train and test sets using an 80/20 ratio.\n",
36
+ "- **Save Preprocessed Data:** Save the preprocessed train and test datasets as separate CSV files."
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "markdown",
41
+ "id": "f758cf39-ccf4-477d-808b-ced13d80645d",
42
+ "metadata": {
43
+ "tags": []
44
+ },
45
+ "source": [
46
+ "# Disaster tweets"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": 2,
52
+ "id": "ff78420b-e905-40ca-8453-432a7c193c18",
53
+ "metadata": {
54
+ "tags": []
55
+ },
56
+ "outputs": [],
57
+ "source": [
58
+ "# Instantiate the Kaggle API object\n",
59
+ "api = KaggleApi()\n",
60
+ "\n",
61
+ "# Set the Kaggle API credentials\n",
62
+ "api.authenticate()"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "execution_count": 3,
68
+ "id": "bdeec404-b214-4d2b-8f17-189cf149062b",
69
+ "metadata": {
70
+ "tags": []
71
+ },
72
+ "outputs": [],
73
+ "source": [
74
+ "# Set the dataset to download\n",
75
+ "disaster_dataset_slug = 'vstepanenko/disaster-tweets'\n",
76
+ "disaster_output_path = 'Data/disaster-tweets'\n",
77
+ "\n",
78
+ "# Download the dataset files\n",
79
+ "api.dataset_download_files(disaster_dataset_slug, path=disaster_output_path, unzip=True)"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 4,
85
+ "id": "e3e01eb8-1182-4fd3-8252-2698ebc63e9f",
86
+ "metadata": {
87
+ "tags": []
88
+ },
89
+ "outputs": [],
90
+ "source": [
91
+ "disaster_path = list(glob(disaster_output_path + '*/*'))[0]"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "code",
96
+ "execution_count": 5,
97
+ "id": "07641f87-7898-482c-9d14-5466ae2b396c",
98
+ "metadata": {
99
+ "tags": []
100
+ },
101
+ "outputs": [
102
+ {
103
+ "data": {
104
+ "text/plain": [
105
+ "'Data/disaster-tweets/tweets.csv'"
106
+ ]
107
+ },
108
+ "execution_count": 5,
109
+ "metadata": {},
110
+ "output_type": "execute_result"
111
+ }
112
+ ],
113
+ "source": [
114
+ "disaster_path"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": 6,
120
+ "id": "51f8a0a0-e85c-4bff-9881-c75dd6e14c9a",
121
+ "metadata": {
122
+ "tags": []
123
+ },
124
+ "outputs": [],
125
+ "source": [
126
+ "disaster = pd.read_csv(disaster_path)\n",
127
+ "disaster = disaster.drop(['id', 'keyword', 'location'], axis=1)"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 7,
133
+ "id": "ec69fa87-4ac7-469b-9de7-99d034127b31",
134
+ "metadata": {
135
+ "tags": []
136
+ },
137
+ "outputs": [],
138
+ "source": [
139
+ "# Invert the values in the 'Target' column\n",
140
+ "disaster['target'] = disaster['target'].map({1: 0, 0: 1})\n",
141
+ "disaster = disaster.rename(columns={'target': 'label'})"
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": 8,
147
+ "id": "7dc0ddbb-be2b-4faa-a639-396bdda82f67",
148
+ "metadata": {
149
+ "tags": []
150
+ },
151
+ "outputs": [
152
+ {
153
+ "data": {
154
+ "text/html": [
155
+ "<div>\n",
156
+ "<style scoped>\n",
157
+ " .dataframe tbody tr th:only-of-type {\n",
158
+ " vertical-align: middle;\n",
159
+ " }\n",
160
+ "\n",
161
+ " .dataframe tbody tr th {\n",
162
+ " vertical-align: top;\n",
163
+ " }\n",
164
+ "\n",
165
+ " .dataframe thead th {\n",
166
+ " text-align: right;\n",
167
+ " }\n",
168
+ "</style>\n",
169
+ "<table border=\"1\" class=\"dataframe\">\n",
170
+ " <thead>\n",
171
+ " <tr style=\"text-align: right;\">\n",
172
+ " <th></th>\n",
173
+ " <th>text</th>\n",
174
+ " <th>label</th>\n",
175
+ " </tr>\n",
176
+ " </thead>\n",
177
+ " <tbody>\n",
178
+ " <tr>\n",
179
+ " <th>0</th>\n",
180
+ " <td>Communal violence in Bhainsa, Telangana. \"Ston...</td>\n",
181
+ " <td>0</td>\n",
182
+ " </tr>\n",
183
+ " <tr>\n",
184
+ " <th>1</th>\n",
185
+ " <td>Telangana: Section 144 has been imposed in Bha...</td>\n",
186
+ " <td>0</td>\n",
187
+ " </tr>\n",
188
+ " <tr>\n",
189
+ " <th>2</th>\n",
190
+ " <td>Arsonist sets cars ablaze at dealership https:...</td>\n",
191
+ " <td>0</td>\n",
192
+ " </tr>\n",
193
+ " <tr>\n",
194
+ " <th>3</th>\n",
195
+ " <td>Arsonist sets cars ablaze at dealership https:...</td>\n",
196
+ " <td>0</td>\n",
197
+ " </tr>\n",
198
+ " <tr>\n",
199
+ " <th>4</th>\n",
200
+ " <td>\"Lord Jesus, your love brings freedom and pard...</td>\n",
201
+ " <td>1</td>\n",
202
+ " </tr>\n",
203
+ " </tbody>\n",
204
+ "</table>\n",
205
+ "</div>"
206
+ ],
207
+ "text/plain": [
208
+ " text label\n",
209
+ "0 Communal violence in Bhainsa, Telangana. \"Ston... 0\n",
210
+ "1 Telangana: Section 144 has been imposed in Bha... 0\n",
211
+ "2 Arsonist sets cars ablaze at dealership https:... 0\n",
212
+ "3 Arsonist sets cars ablaze at dealership https:... 0\n",
213
+ "4 \"Lord Jesus, your love brings freedom and pard... 1"
214
+ ]
215
+ },
216
+ "execution_count": 8,
217
+ "metadata": {},
218
+ "output_type": "execute_result"
219
+ }
220
+ ],
221
+ "source": [
222
+ "disaster.head() # real: 0 | fake: 1"
223
+ ]
224
+ },
225
+ {
226
+ "cell_type": "markdown",
227
+ "id": "83e8b2ca-783c-40ef-8240-868165c1a4b1",
228
+ "metadata": {
229
+ "tags": []
230
+ },
231
+ "source": [
232
+ "# Real/Fake News"
233
+ ]
234
+ },
235
+ {
236
+ "cell_type": "code",
237
+ "execution_count": 9,
238
+ "id": "06fdce6a-4a29-47f8-813d-2fe74f883db9",
239
+ "metadata": {
240
+ "tags": []
241
+ },
242
+ "outputs": [],
243
+ "source": [
244
+ "# Set the dataset to download\n",
245
+ "news_dataset_slug = 'clmentbisaillon/fake-and-real-news-dataset'\n",
246
+ "news_output_path = 'Data/fake-and-real-news-dataset'\n",
247
+ "\n",
248
+ "# Download the dataset files\n",
249
+ "api.dataset_download_files(news_dataset_slug, path=news_output_path, unzip=True)"
250
+ ]
251
+ },
252
+ {
253
+ "cell_type": "code",
254
+ "execution_count": 10,
255
+ "id": "b2cf8b40-0c0b-4fdf-9fc0-5a3ed159a043",
256
+ "metadata": {
257
+ "tags": []
258
+ },
259
+ "outputs": [],
260
+ "source": [
261
+ "news_path = list(glob(news_output_path + '*/*'))"
262
+ ]
263
+ },
264
+ {
265
+ "cell_type": "code",
266
+ "execution_count": 11,
267
+ "id": "cce93c7f-c4fe-4123-bef5-742c9ab7c5dd",
268
+ "metadata": {
269
+ "tags": []
270
+ },
271
+ "outputs": [
272
+ {
273
+ "data": {
274
+ "text/plain": [
275
+ "['Data/fake-and-real-news-dataset/Fake.csv',\n",
276
+ " 'Data/fake-and-real-news-dataset/True.csv']"
277
+ ]
278
+ },
279
+ "execution_count": 11,
280
+ "metadata": {},
281
+ "output_type": "execute_result"
282
+ }
283
+ ],
284
+ "source": [
285
+ "news_path"
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "code",
290
+ "execution_count": 12,
291
+ "id": "0f8ed26f-4c9a-4881-a23d-4d0024496999",
292
+ "metadata": {
293
+ "tags": []
294
+ },
295
+ "outputs": [],
296
+ "source": [
297
+ "real_path = news_path[1]\n",
298
+ "fake_path = news_path[0]"
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "execution_count": 13,
304
+ "id": "2f990dc8-c5da-453b-82d1-36fa6d6c9d47",
305
+ "metadata": {
306
+ "tags": []
307
+ },
308
+ "outputs": [],
309
+ "source": [
310
+ "real_news = pd.read_csv(real_path)\n",
311
+ "fake_news = pd.read_csv(fake_path)"
312
+ ]
313
+ },
314
+ {
315
+ "cell_type": "code",
316
+ "execution_count": 14,
317
+ "id": "cd84c34e-a991-45ae-865d-74b7e870e203",
318
+ "metadata": {
319
+ "tags": []
320
+ },
321
+ "outputs": [],
322
+ "source": [
323
+ "real_news = real_news.drop(['title', 'subject', 'date'], axis=1)\n",
324
+ "fake_news = fake_news.drop(['title', 'subject', 'date'], axis=1)"
325
+ ]
326
+ },
327
+ {
328
+ "cell_type": "code",
329
+ "execution_count": 15,
330
+ "id": "ef055dba-c390-41ea-861d-c1cf9fc57211",
331
+ "metadata": {
332
+ "tags": []
333
+ },
334
+ "outputs": [],
335
+ "source": [
336
+ "real_news['label'] = 0\n",
337
+ "fake_news['label'] = 1"
338
+ ]
339
+ },
340
+ {
341
+ "cell_type": "code",
342
+ "execution_count": 16,
343
+ "id": "909dcc92-1d5e-4794-ae70-de8803117cfb",
344
+ "metadata": {
345
+ "tags": []
346
+ },
347
+ "outputs": [
348
+ {
349
+ "data": {
350
+ "text/html": [
351
+ "<div>\n",
352
+ "<style scoped>\n",
353
+ " .dataframe tbody tr th:only-of-type {\n",
354
+ " vertical-align: middle;\n",
355
+ " }\n",
356
+ "\n",
357
+ " .dataframe tbody tr th {\n",
358
+ " vertical-align: top;\n",
359
+ " }\n",
360
+ "\n",
361
+ " .dataframe thead th {\n",
362
+ " text-align: right;\n",
363
+ " }\n",
364
+ "</style>\n",
365
+ "<table border=\"1\" class=\"dataframe\">\n",
366
+ " <thead>\n",
367
+ " <tr style=\"text-align: right;\">\n",
368
+ " <th></th>\n",
369
+ " <th>text</th>\n",
370
+ " <th>label</th>\n",
371
+ " </tr>\n",
372
+ " </thead>\n",
373
+ " <tbody>\n",
374
+ " <tr>\n",
375
+ " <th>0</th>\n",
376
+ " <td>WASHINGTON (Reuters) - The head of a conservat...</td>\n",
377
+ " <td>0</td>\n",
378
+ " </tr>\n",
379
+ " <tr>\n",
380
+ " <th>1</th>\n",
381
+ " <td>WASHINGTON (Reuters) - Transgender people will...</td>\n",
382
+ " <td>0</td>\n",
383
+ " </tr>\n",
384
+ " <tr>\n",
385
+ " <th>2</th>\n",
386
+ " <td>WASHINGTON (Reuters) - The special counsel inv...</td>\n",
387
+ " <td>0</td>\n",
388
+ " </tr>\n",
389
+ " <tr>\n",
390
+ " <th>3</th>\n",
391
+ " <td>WASHINGTON (Reuters) - Trump campaign adviser ...</td>\n",
392
+ " <td>0</td>\n",
393
+ " </tr>\n",
394
+ " <tr>\n",
395
+ " <th>4</th>\n",
396
+ " <td>SEATTLE/WASHINGTON (Reuters) - President Donal...</td>\n",
397
+ " <td>0</td>\n",
398
+ " </tr>\n",
399
+ " </tbody>\n",
400
+ "</table>\n",
401
+ "</div>"
402
+ ],
403
+ "text/plain": [
404
+ " text label\n",
405
+ "0 WASHINGTON (Reuters) - The head of a conservat... 0\n",
406
+ "1 WASHINGTON (Reuters) - Transgender people will... 0\n",
407
+ "2 WASHINGTON (Reuters) - The special counsel inv... 0\n",
408
+ "3 WASHINGTON (Reuters) - Trump campaign adviser ... 0\n",
409
+ "4 SEATTLE/WASHINGTON (Reuters) - President Donal... 0"
410
+ ]
411
+ },
412
+ "execution_count": 16,
413
+ "metadata": {},
414
+ "output_type": "execute_result"
415
+ }
416
+ ],
417
+ "source": [
418
+ "real_news.head()"
419
+ ]
420
+ },
421
+ {
422
+ "cell_type": "code",
423
+ "execution_count": 17,
424
+ "id": "3ed9a420-5bc9-432e-b8b6-5401cb3c83ab",
425
+ "metadata": {
426
+ "tags": []
427
+ },
428
+ "outputs": [
429
+ {
430
+ "data": {
431
+ "text/html": [
432
+ "<div>\n",
433
+ "<style scoped>\n",
434
+ " .dataframe tbody tr th:only-of-type {\n",
435
+ " vertical-align: middle;\n",
436
+ " }\n",
437
+ "\n",
438
+ " .dataframe tbody tr th {\n",
439
+ " vertical-align: top;\n",
440
+ " }\n",
441
+ "\n",
442
+ " .dataframe thead th {\n",
443
+ " text-align: right;\n",
444
+ " }\n",
445
+ "</style>\n",
446
+ "<table border=\"1\" class=\"dataframe\">\n",
447
+ " <thead>\n",
448
+ " <tr style=\"text-align: right;\">\n",
449
+ " <th></th>\n",
450
+ " <th>text</th>\n",
451
+ " <th>label</th>\n",
452
+ " </tr>\n",
453
+ " </thead>\n",
454
+ " <tbody>\n",
455
+ " <tr>\n",
456
+ " <th>0</th>\n",
457
+ " <td>Donald Trump just couldn t wish all Americans ...</td>\n",
458
+ " <td>1</td>\n",
459
+ " </tr>\n",
460
+ " <tr>\n",
461
+ " <th>1</th>\n",
462
+ " <td>House Intelligence Committee Chairman Devin Nu...</td>\n",
463
+ " <td>1</td>\n",
464
+ " </tr>\n",
465
+ " <tr>\n",
466
+ " <th>2</th>\n",
467
+ " <td>On Friday, it was revealed that former Milwauk...</td>\n",
468
+ " <td>1</td>\n",
469
+ " </tr>\n",
470
+ " <tr>\n",
471
+ " <th>3</th>\n",
472
+ " <td>On Christmas day, Donald Trump announced that ...</td>\n",
473
+ " <td>1</td>\n",
474
+ " </tr>\n",
475
+ " <tr>\n",
476
+ " <th>4</th>\n",
477
+ " <td>Pope Francis used his annual Christmas Day mes...</td>\n",
478
+ " <td>1</td>\n",
479
+ " </tr>\n",
480
+ " </tbody>\n",
481
+ "</table>\n",
482
+ "</div>"
483
+ ],
484
+ "text/plain": [
485
+ " text label\n",
486
+ "0 Donald Trump just couldn t wish all Americans ... 1\n",
487
+ "1 House Intelligence Committee Chairman Devin Nu... 1\n",
488
+ "2 On Friday, it was revealed that former Milwauk... 1\n",
489
+ "3 On Christmas day, Donald Trump announced that ... 1\n",
490
+ "4 Pope Francis used his annual Christmas Day mes... 1"
491
+ ]
492
+ },
493
+ "execution_count": 17,
494
+ "metadata": {},
495
+ "output_type": "execute_result"
496
+ }
497
+ ],
498
+ "source": [
499
+ "fake_news.head()"
500
+ ]
501
+ },
502
+ {
503
+ "cell_type": "markdown",
504
+ "id": "27f26a75-1f18-4762-971f-d17a20a9b587",
505
+ "metadata": {
506
+ "tags": []
507
+ },
508
+ "source": [
509
+ "# Concatenate the datasets"
510
+ ]
511
+ },
512
+ {
513
+ "cell_type": "code",
514
+ "execution_count": 18,
515
+ "id": "c9ffb4f8-04a4-404a-8412-54e5f1a237f8",
516
+ "metadata": {
517
+ "tags": []
518
+ },
519
+ "outputs": [],
520
+ "source": [
521
+ "data = pd.concat([disaster, real_news, fake_news]).reset_index().drop(columns = 'index')"
522
+ ]
523
+ },
524
+ {
525
+ "cell_type": "code",
526
+ "execution_count": 19,
527
+ "id": "1a86a46d-25d0-4a25-914d-722754b91aa6",
528
+ "metadata": {
529
+ "tags": []
530
+ },
531
+ "outputs": [
532
+ {
533
+ "data": {
534
+ "text/html": [
535
+ "<div>\n",
536
+ "<style scoped>\n",
537
+ " .dataframe tbody tr th:only-of-type {\n",
538
+ " vertical-align: middle;\n",
539
+ " }\n",
540
+ "\n",
541
+ " .dataframe tbody tr th {\n",
542
+ " vertical-align: top;\n",
543
+ " }\n",
544
+ "\n",
545
+ " .dataframe thead th {\n",
546
+ " text-align: right;\n",
547
+ " }\n",
548
+ "</style>\n",
549
+ "<table border=\"1\" class=\"dataframe\">\n",
550
+ " <thead>\n",
551
+ " <tr style=\"text-align: right;\">\n",
552
+ " <th></th>\n",
553
+ " <th>text</th>\n",
554
+ " <th>label</th>\n",
555
+ " </tr>\n",
556
+ " </thead>\n",
557
+ " <tbody>\n",
558
+ " <tr>\n",
559
+ " <th>0</th>\n",
560
+ " <td>Communal violence in Bhainsa, Telangana. \"Ston...</td>\n",
561
+ " <td>0</td>\n",
562
+ " </tr>\n",
563
+ " <tr>\n",
564
+ " <th>1</th>\n",
565
+ " <td>Telangana: Section 144 has been imposed in Bha...</td>\n",
566
+ " <td>0</td>\n",
567
+ " </tr>\n",
568
+ " <tr>\n",
569
+ " <th>2</th>\n",
570
+ " <td>Arsonist sets cars ablaze at dealership https:...</td>\n",
571
+ " <td>0</td>\n",
572
+ " </tr>\n",
573
+ " <tr>\n",
574
+ " <th>3</th>\n",
575
+ " <td>Arsonist sets cars ablaze at dealership https:...</td>\n",
576
+ " <td>0</td>\n",
577
+ " </tr>\n",
578
+ " <tr>\n",
579
+ " <th>4</th>\n",
580
+ " <td>\"Lord Jesus, your love brings freedom and pard...</td>\n",
581
+ " <td>1</td>\n",
582
+ " </tr>\n",
583
+ " </tbody>\n",
584
+ "</table>\n",
585
+ "</div>"
586
+ ],
587
+ "text/plain": [
588
+ " text label\n",
589
+ "0 Communal violence in Bhainsa, Telangana. \"Ston... 0\n",
590
+ "1 Telangana: Section 144 has been imposed in Bha... 0\n",
591
+ "2 Arsonist sets cars ablaze at dealership https:... 0\n",
592
+ "3 Arsonist sets cars ablaze at dealership https:... 0\n",
593
+ "4 \"Lord Jesus, your love brings freedom and pard... 1"
594
+ ]
595
+ },
596
+ "execution_count": 19,
597
+ "metadata": {},
598
+ "output_type": "execute_result"
599
+ }
600
+ ],
601
+ "source": [
602
+ "data.head()"
603
+ ]
604
+ },
605
+ {
606
+ "cell_type": "markdown",
607
+ "id": "c9b88873-63fe-43e6-91fd-7ad49c52b07a",
608
+ "metadata": {},
609
+ "source": [
610
+ "# Preprocess the text"
611
+ ]
612
+ },
613
+ {
614
+ "cell_type": "code",
615
+ "execution_count": 20,
616
+ "id": "b74564a5-1c89-4d85-b096-4a57c195234c",
617
+ "metadata": {
618
+ "tags": []
619
+ },
620
+ "outputs": [],
621
+ "source": [
622
+ "data = preprocess_text(data)"
623
+ ]
624
+ },
625
+ {
626
+ "cell_type": "code",
627
+ "execution_count": 21,
628
+ "id": "8c5ec549-1c07-4c2b-9357-69ea89b280cd",
629
+ "metadata": {
630
+ "tags": []
631
+ },
632
+ "outputs": [
633
+ {
634
+ "data": {
635
+ "text/html": [
636
+ "<div>\n",
637
+ "<style scoped>\n",
638
+ " .dataframe tbody tr th:only-of-type {\n",
639
+ " vertical-align: middle;\n",
640
+ " }\n",
641
+ "\n",
642
+ " .dataframe tbody tr th {\n",
643
+ " vertical-align: top;\n",
644
+ " }\n",
645
+ "\n",
646
+ " .dataframe thead th {\n",
647
+ " text-align: right;\n",
648
+ " }\n",
649
+ "</style>\n",
650
+ "<table border=\"1\" class=\"dataframe\">\n",
651
+ " <thead>\n",
652
+ " <tr style=\"text-align: right;\">\n",
653
+ " <th></th>\n",
654
+ " <th>text</th>\n",
655
+ " <th>label</th>\n",
656
+ " </tr>\n",
657
+ " </thead>\n",
658
+ " <tbody>\n",
659
+ " <tr>\n",
660
+ " <th>0</th>\n",
661
+ " <td>communal violence bhainsa telangana stones pel...</td>\n",
662
+ " <td>0</td>\n",
663
+ " </tr>\n",
664
+ " <tr>\n",
665
+ " <th>1</th>\n",
666
+ " <td>telangana section 144 imposed bhainsa january ...</td>\n",
667
+ " <td>0</td>\n",
668
+ " </tr>\n",
669
+ " <tr>\n",
670
+ " <th>2</th>\n",
671
+ " <td>arsonist sets cars ablaze dealership</td>\n",
672
+ " <td>0</td>\n",
673
+ " </tr>\n",
674
+ " <tr>\n",
675
+ " <th>3</th>\n",
676
+ " <td>arsonist sets cars ablaze dealership</td>\n",
677
+ " <td>0</td>\n",
678
+ " </tr>\n",
679
+ " <tr>\n",
680
+ " <th>4</th>\n",
681
+ " <td>lord jesus love brings freedom pardon fill hol...</td>\n",
682
+ " <td>1</td>\n",
683
+ " </tr>\n",
684
+ " </tbody>\n",
685
+ "</table>\n",
686
+ "</div>"
687
+ ],
688
+ "text/plain": [
689
+ " text label\n",
690
+ "0 communal violence bhainsa telangana stones pel... 0\n",
691
+ "1 telangana section 144 imposed bhainsa january ... 0\n",
692
+ "2 arsonist sets cars ablaze dealership 0\n",
693
+ "3 arsonist sets cars ablaze dealership 0\n",
694
+ "4 lord jesus love brings freedom pardon fill hol... 1"
695
+ ]
696
+ },
697
+ "execution_count": 21,
698
+ "metadata": {},
699
+ "output_type": "execute_result"
700
+ }
701
+ ],
702
+ "source": [
703
+ "data.head()"
704
+ ]
705
+ },
706
+ {
707
+ "cell_type": "code",
708
+ "execution_count": 22,
709
+ "id": "b2328328-e513-4bb8-9172-0d109d20797b",
710
+ "metadata": {
711
+ "tags": []
712
+ },
713
+ "outputs": [
714
+ {
715
+ "name": "stdout",
716
+ "output_type": "stream",
717
+ "text": [
718
+ "Percentage of REAL data: 42.61%\n",
719
+ "Percentage of FAKE data: 57.39%\n"
720
+ ]
721
+ }
722
+ ],
723
+ "source": [
724
+ "print(f'Percentage of REAL data: {round((len(data) - data[\"label\"].sum()) / len(data) * 100, 2)}%')\n",
725
+ "print(f'Percentage of FAKE data: {round(data[\"label\"].sum() / len(data) * 100, 2)}%')"
726
+ ]
727
+ },
728
+ {
729
+ "cell_type": "markdown",
730
+ "id": "41564eef-4b4b-48a1-9cdb-cf615c753c83",
731
+ "metadata": {
732
+ "tags": []
733
+ },
734
+ "source": [
735
+ "# Train/Test split and save"
736
+ ]
737
+ },
738
+ {
739
+ "cell_type": "code",
740
+ "execution_count": 23,
741
+ "id": "108aeb30-f294-4650-8ac7-afe96b97788b",
742
+ "metadata": {
743
+ "tags": []
744
+ },
745
+ "outputs": [],
746
+ "source": [
747
+ "# Shuffle the DataFrame\n",
748
+ "shuffled_data = data.sample(frac=1, random_state=42) # Set random_state for reproducibility\n",
749
+ "\n",
750
+ "# Split the shuffled DataFrame into train and test sets\n",
751
+ "train_data, test_data = train_test_split(shuffled_data, test_size=0.2, random_state=42) # Adjust test_size as needed\n",
752
+ "\n",
753
+ "# Reset the index column\n",
754
+ "train_data = train_data.reset_index().drop(['index'], axis=1)\n",
755
+ "test_data = test_data.reset_index().drop(['index'], axis=1)"
756
+ ]
757
+ },
758
+ {
759
+ "cell_type": "code",
760
+ "execution_count": 24,
761
+ "id": "0e95eb9a-8e96-4871-8caf-50ee8c8b2ab8",
762
+ "metadata": {
763
+ "tags": []
764
+ },
765
+ "outputs": [
766
+ {
767
+ "data": {
768
+ "text/html": [
769
+ "<div>\n",
770
+ "<style scoped>\n",
771
+ " .dataframe tbody tr th:only-of-type {\n",
772
+ " vertical-align: middle;\n",
773
+ " }\n",
774
+ "\n",
775
+ " .dataframe tbody tr th {\n",
776
+ " vertical-align: top;\n",
777
+ " }\n",
778
+ "\n",
779
+ " .dataframe thead th {\n",
780
+ " text-align: right;\n",
781
+ " }\n",
782
+ "</style>\n",
783
+ "<table border=\"1\" class=\"dataframe\">\n",
784
+ " <thead>\n",
785
+ " <tr style=\"text-align: right;\">\n",
786
+ " <th></th>\n",
787
+ " <th>text</th>\n",
788
+ " <th>label</th>\n",
789
+ " </tr>\n",
790
+ " </thead>\n",
791
+ " <tbody>\n",
792
+ " <tr>\n",
793
+ " <th>0</th>\n",
794
+ " <td>name michael brown robbed local convenience st...</td>\n",
795
+ " <td>1</td>\n",
796
+ " </tr>\n",
797
+ " <tr>\n",
798
+ " <th>1</th>\n",
799
+ " <td>washington reuters japanese prime minister shi...</td>\n",
800
+ " <td>0</td>\n",
801
+ " </tr>\n",
802
+ " <tr>\n",
803
+ " <th>2</th>\n",
804
+ " <td>chicago reuters us house republican tax bill r...</td>\n",
805
+ " <td>0</td>\n",
806
+ " </tr>\n",
807
+ " <tr>\n",
808
+ " <th>3</th>\n",
809
+ " <td>reuters fbi interviewed michael flynn initial ...</td>\n",
810
+ " <td>0</td>\n",
811
+ " </tr>\n",
812
+ " <tr>\n",
813
+ " <th>4</th>\n",
814
+ " <td>harare reuters ousted zimbabwe finance ministe...</td>\n",
815
+ " <td>0</td>\n",
816
+ " </tr>\n",
817
+ " </tbody>\n",
818
+ "</table>\n",
819
+ "</div>"
820
+ ],
821
+ "text/plain": [
822
+ " text label\n",
823
+ "0 name michael brown robbed local convenience st... 1\n",
824
+ "1 washington reuters japanese prime minister shi... 0\n",
825
+ "2 chicago reuters us house republican tax bill r... 0\n",
826
+ "3 reuters fbi interviewed michael flynn initial ... 0\n",
827
+ "4 harare reuters ousted zimbabwe finance ministe... 0"
828
+ ]
829
+ },
830
+ "execution_count": 24,
831
+ "metadata": {},
832
+ "output_type": "execute_result"
833
+ }
834
+ ],
835
+ "source": [
836
+ "train_data.head()"
837
+ ]
838
+ },
839
+ {
840
+ "cell_type": "code",
841
+ "execution_count": 25,
842
+ "id": "8b4318d6-b209-4587-b1f8-d469e29f83c6",
843
+ "metadata": {
844
+ "tags": []
845
+ },
846
+ "outputs": [
847
+ {
848
+ "data": {
849
+ "text/html": [
850
+ "<div>\n",
851
+ "<style scoped>\n",
852
+ " .dataframe tbody tr th:only-of-type {\n",
853
+ " vertical-align: middle;\n",
854
+ " }\n",
855
+ "\n",
856
+ " .dataframe tbody tr th {\n",
857
+ " vertical-align: top;\n",
858
+ " }\n",
859
+ "\n",
860
+ " .dataframe thead th {\n",
861
+ " text-align: right;\n",
862
+ " }\n",
863
+ "</style>\n",
864
+ "<table border=\"1\" class=\"dataframe\">\n",
865
+ " <thead>\n",
866
+ " <tr style=\"text-align: right;\">\n",
867
+ " <th></th>\n",
868
+ " <th>text</th>\n",
869
+ " <th>label</th>\n",
870
+ " </tr>\n",
871
+ " </thead>\n",
872
+ " <tbody>\n",
873
+ " <tr>\n",
874
+ " <th>0</th>\n",
875
+ " <td>kraig moss die hard donald trump supporter fol...</td>\n",
876
+ " <td>1</td>\n",
877
+ " </tr>\n",
878
+ " <tr>\n",
879
+ " <th>1</th>\n",
880
+ " <td>white house lawyers last month learned former ...</td>\n",
881
+ " <td>1</td>\n",
882
+ " </tr>\n",
883
+ " <tr>\n",
884
+ " <th>2</th>\n",
885
+ " <td>awesome many levels hard know beginafghanistan...</td>\n",
886
+ " <td>1</td>\n",
887
+ " </tr>\n",
888
+ " <tr>\n",
889
+ " <th>3</th>\n",
890
+ " <td>please note overwhelming information regarding...</td>\n",
891
+ " <td>1</td>\n",
892
+ " </tr>\n",
893
+ " <tr>\n",
894
+ " <th>4</th>\n",
895
+ " <td>kabul reuters us ambassador afghanistan said m...</td>\n",
896
+ " <td>0</td>\n",
897
+ " </tr>\n",
898
+ " </tbody>\n",
899
+ "</table>\n",
900
+ "</div>"
901
+ ],
902
+ "text/plain": [
903
+ " text label\n",
904
+ "0 kraig moss die hard donald trump supporter fol... 1\n",
905
+ "1 white house lawyers last month learned former ... 1\n",
906
+ "2 awesome many levels hard know beginafghanistan... 1\n",
907
+ "3 please note overwhelming information regarding... 1\n",
908
+ "4 kabul reuters us ambassador afghanistan said m... 0"
909
+ ]
910
+ },
911
+ "execution_count": 25,
912
+ "metadata": {},
913
+ "output_type": "execute_result"
914
+ }
915
+ ],
916
+ "source": [
917
+ "test_data.head()"
918
+ ]
919
+ },
920
+ {
921
+ "cell_type": "code",
922
+ "execution_count": 26,
923
+ "id": "9ca92861-7526-4bb0-ad1b-50b3104acc02",
924
+ "metadata": {
925
+ "tags": []
926
+ },
927
+ "outputs": [],
928
+ "source": [
929
+ "# Save the train and test sets as separate CSV files\n",
930
+ "train_data.to_csv('Data/train_dataset.csv', index=False)\n",
931
+ "test_data.to_csv('Data/test_dataset.csv', index=False)"
932
+ ]
933
+ },
934
+ {
935
+ "cell_type": "code",
936
+ "execution_count": null,
937
+ "id": "439efe82-51e4-49f4-908f-6f1c9bc3d3d1",
938
+ "metadata": {},
939
+ "outputs": [],
940
+ "source": []
941
+ }
942
+ ],
943
+ "metadata": {
944
+ "kernelspec": {
945
+ "display_name": "Python 3 (ipykernel)",
946
+ "language": "python",
947
+ "name": "python3"
948
+ },
949
+ "language_info": {
950
+ "codemirror_mode": {
951
+ "name": "ipython",
952
+ "version": 3
953
+ },
954
+ "file_extension": ".py",
955
+ "mimetype": "text/x-python",
956
+ "name": "python",
957
+ "nbconvert_exporter": "python",
958
+ "pygments_lexer": "ipython3",
959
+ "version": "3.10.9"
960
+ }
961
+ },
962
+ "nbformat": 4,
963
+ "nbformat_minor": 5
964
+ }
preprocessing.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from nltk.corpus import stopwords
3
+
4
+ def preprocess_text(df):
5
+ """
6
+ Preprocesses the text column in a DataFrame by applying various cleaning operations.
7
+
8
+ Args:
9
+ df (pandas.DataFrame): The DataFrame containing the text column to be preprocessed.
10
+
11
+ Returns:
12
+ None. The text column in the provided DataFrame is modified in place.
13
+ """
14
+ # Remove URLs, user mentions, non-alphanumeric characters and hashtags from the tweets
15
+ df['text'] = df['text'].apply(lambda x: re.sub(r'http\S+', '', str(x))) # remove URLs
16
+ df['text'] = df['text'].apply(lambda x: re.sub(r'@\S+', '', str(x))) # remove user mentions
17
+ df['text'] = df['text'].apply(lambda x: re.sub(r'[^a-zA-Z0-9\s]', '', str(x))) # remove non-alphanumeric characters
18
+ df['text'] = df['text'].apply(lambda x: re.sub(r'#\S+', '', str(x))) # remove hashtags
19
+
20
+ # Remove punctuation and convert text to lowercase
21
+ df['text'] = df['text'].apply(lambda x: re.sub('[^\w\s]', '', x))
22
+ df['text'] = df['text'].apply(lambda x: x.lower())
23
+
24
+ # Remove stop word (such as "a", "an", "the", "is", "of", etc.)
25
+ stop_words = set(stopwords.words('english'))
26
+ df['text'] = df['text'].apply(lambda x: ' '.join([word for word in x.split() if word not in stop_words]))
27
+
28
+ # Remove any remaining white space
29
+ df['text'] = df['text'].apply(lambda x: x.strip())
30
+
31
+ # Remove observations with less than 3 words
32
+ df = df[df['text'].apply(lambda x: len(x.split()) >= 3)]
33
+
34
+ return df