{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f738c6f3-948e-4ba0-a69b-97fd0ce22e84", "metadata": { "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from kaggle.api.kaggle_api_extended import KaggleApi\n", "from sklearn.model_selection import train_test_split\n", "from glob import glob\n", "\n", "from preprocessing import preprocess_text" ] }, { "cell_type": "markdown", "id": "29a79e0d-9b44-40b0-81ab-7cc7eca2e77b", "metadata": { "tags": [] }, "source": [ "# Introduction:\n", "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", "\n", "Process Overview:\n", "- **Dataset Acquisition:** Download the Disaster Tweet Dataset and Fake/Real News Dataset from Kaggle using the Kaggle API.\n", "- **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", "- **Train/Test Split:** Split the combined preprocessed dataset into train and test sets using an 80/20 ratio.\n", "- **Save Preprocessed Data:** Save the preprocessed train and test datasets as separate CSV files." ] }, { "cell_type": "markdown", "id": "f758cf39-ccf4-477d-808b-ced13d80645d", "metadata": { "tags": [] }, "source": [ "# Disaster tweets" ] }, { "cell_type": "code", "execution_count": 2, "id": "ff78420b-e905-40ca-8453-432a7c193c18", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Instantiate the Kaggle API object\n", "api = KaggleApi()\n", "\n", "# Set the Kaggle API credentials\n", "api.authenticate()" ] }, { "cell_type": "code", "execution_count": 3, "id": "bdeec404-b214-4d2b-8f17-189cf149062b", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Set the dataset to download\n", "disaster_dataset_slug = 'vstepanenko/disaster-tweets'\n", "disaster_output_path = 'Data/disaster-tweets'\n", "\n", "# Download the dataset files\n", "api.dataset_download_files(disaster_dataset_slug, path=disaster_output_path, unzip=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "e3e01eb8-1182-4fd3-8252-2698ebc63e9f", "metadata": { "tags": [] }, "outputs": [], "source": [ "disaster_path = list(glob(disaster_output_path + '*/*'))[0]" ] }, { "cell_type": "code", "execution_count": 5, "id": "07641f87-7898-482c-9d14-5466ae2b396c", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Data/disaster-tweets/tweets.csv'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "disaster_path" ] }, { "cell_type": "code", "execution_count": 6, "id": "51f8a0a0-e85c-4bff-9881-c75dd6e14c9a", "metadata": { "tags": [] }, "outputs": [], "source": [ "disaster = pd.read_csv(disaster_path)\n", "disaster = disaster.drop(['id', 'keyword', 'location'], axis=1)" ] }, { "cell_type": "code", "execution_count": 7, "id": "ec69fa87-4ac7-469b-9de7-99d034127b31", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Invert the values in the 'Target' column\n", "disaster['target'] = disaster['target'].map({1: 0, 0: 1})\n", "disaster = disaster.rename(columns={'target': 'label'})" ] }, { "cell_type": "code", "execution_count": 8, "id": "7dc0ddbb-be2b-4faa-a639-396bdda82f67", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0Communal violence in Bhainsa, Telangana. \"Ston...0
1Telangana: Section 144 has been imposed in Bha...0
2Arsonist sets cars ablaze at dealership https:...0
3Arsonist sets cars ablaze at dealership https:...0
4\"Lord Jesus, your love brings freedom and pard...1
\n", "
" ], "text/plain": [ " text label\n", "0 Communal violence in Bhainsa, Telangana. \"Ston... 0\n", "1 Telangana: Section 144 has been imposed in Bha... 0\n", "2 Arsonist sets cars ablaze at dealership https:... 0\n", "3 Arsonist sets cars ablaze at dealership https:... 0\n", "4 \"Lord Jesus, your love brings freedom and pard... 1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "disaster.head() # real: 0 | fake: 1" ] }, { "cell_type": "markdown", "id": "83e8b2ca-783c-40ef-8240-868165c1a4b1", "metadata": { "tags": [] }, "source": [ "# Real/Fake News" ] }, { "cell_type": "code", "execution_count": 9, "id": "06fdce6a-4a29-47f8-813d-2fe74f883db9", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Set the dataset to download\n", "news_dataset_slug = 'clmentbisaillon/fake-and-real-news-dataset'\n", "news_output_path = 'Data/fake-and-real-news-dataset'\n", "\n", "# Download the dataset files\n", "api.dataset_download_files(news_dataset_slug, path=news_output_path, unzip=True)" ] }, { "cell_type": "code", "execution_count": 10, "id": "b2cf8b40-0c0b-4fdf-9fc0-5a3ed159a043", "metadata": { "tags": [] }, "outputs": [], "source": [ "news_path = list(glob(news_output_path + '*/*'))" ] }, { "cell_type": "code", "execution_count": 11, "id": "cce93c7f-c4fe-4123-bef5-742c9ab7c5dd", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Data/fake-and-real-news-dataset/Fake.csv',\n", " 'Data/fake-and-real-news-dataset/True.csv']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "news_path" ] }, { "cell_type": "code", "execution_count": 12, "id": "0f8ed26f-4c9a-4881-a23d-4d0024496999", "metadata": { "tags": [] }, "outputs": [], "source": [ "real_path = news_path[1]\n", "fake_path = news_path[0]" ] }, { "cell_type": "code", "execution_count": 13, "id": "2f990dc8-c5da-453b-82d1-36fa6d6c9d47", "metadata": { "tags": [] }, "outputs": [], "source": [ "real_news = pd.read_csv(real_path)\n", "fake_news = pd.read_csv(fake_path)" ] }, { "cell_type": "code", "execution_count": 14, "id": "cd84c34e-a991-45ae-865d-74b7e870e203", "metadata": { "tags": [] }, "outputs": [], "source": [ "real_news = real_news.drop(['title', 'subject', 'date'], axis=1)\n", "fake_news = fake_news.drop(['title', 'subject', 'date'], axis=1)" ] }, { "cell_type": "code", "execution_count": 15, "id": "ef055dba-c390-41ea-861d-c1cf9fc57211", "metadata": { "tags": [] }, "outputs": [], "source": [ "real_news['label'] = 0\n", "fake_news['label'] = 1" ] }, { "cell_type": "code", "execution_count": 16, "id": "909dcc92-1d5e-4794-ae70-de8803117cfb", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0WASHINGTON (Reuters) - The head of a conservat...0
1WASHINGTON (Reuters) - Transgender people will...0
2WASHINGTON (Reuters) - The special counsel inv...0
3WASHINGTON (Reuters) - Trump campaign adviser ...0
4SEATTLE/WASHINGTON (Reuters) - President Donal...0
\n", "
" ], "text/plain": [ " text label\n", "0 WASHINGTON (Reuters) - The head of a conservat... 0\n", "1 WASHINGTON (Reuters) - Transgender people will... 0\n", "2 WASHINGTON (Reuters) - The special counsel inv... 0\n", "3 WASHINGTON (Reuters) - Trump campaign adviser ... 0\n", "4 SEATTLE/WASHINGTON (Reuters) - President Donal... 0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "real_news.head()" ] }, { "cell_type": "code", "execution_count": 17, "id": "3ed9a420-5bc9-432e-b8b6-5401cb3c83ab", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0Donald Trump just couldn t wish all Americans ...1
1House Intelligence Committee Chairman Devin Nu...1
2On Friday, it was revealed that former Milwauk...1
3On Christmas day, Donald Trump announced that ...1
4Pope Francis used his annual Christmas Day mes...1
\n", "
" ], "text/plain": [ " text label\n", "0 Donald Trump just couldn t wish all Americans ... 1\n", "1 House Intelligence Committee Chairman Devin Nu... 1\n", "2 On Friday, it was revealed that former Milwauk... 1\n", "3 On Christmas day, Donald Trump announced that ... 1\n", "4 Pope Francis used his annual Christmas Day mes... 1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fake_news.head()" ] }, { "cell_type": "markdown", "id": "27f26a75-1f18-4762-971f-d17a20a9b587", "metadata": { "tags": [] }, "source": [ "# Concatenate the datasets" ] }, { "cell_type": "code", "execution_count": 18, "id": "c9ffb4f8-04a4-404a-8412-54e5f1a237f8", "metadata": { "tags": [] }, "outputs": [], "source": [ "data = pd.concat([disaster, real_news, fake_news]).reset_index().drop(columns = 'index')" ] }, { "cell_type": "code", "execution_count": 19, "id": "1a86a46d-25d0-4a25-914d-722754b91aa6", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0Communal violence in Bhainsa, Telangana. \"Ston...0
1Telangana: Section 144 has been imposed in Bha...0
2Arsonist sets cars ablaze at dealership https:...0
3Arsonist sets cars ablaze at dealership https:...0
4\"Lord Jesus, your love brings freedom and pard...1
\n", "
" ], "text/plain": [ " text label\n", "0 Communal violence in Bhainsa, Telangana. \"Ston... 0\n", "1 Telangana: Section 144 has been imposed in Bha... 0\n", "2 Arsonist sets cars ablaze at dealership https:... 0\n", "3 Arsonist sets cars ablaze at dealership https:... 0\n", "4 \"Lord Jesus, your love brings freedom and pard... 1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head()" ] }, { "cell_type": "markdown", "id": "c9b88873-63fe-43e6-91fd-7ad49c52b07a", "metadata": {}, "source": [ "# Preprocess the text" ] }, { "cell_type": "code", "execution_count": 20, "id": "b74564a5-1c89-4d85-b096-4a57c195234c", "metadata": { "tags": [] }, "outputs": [], "source": [ "data = preprocess_text(data)" ] }, { "cell_type": "code", "execution_count": 21, "id": "8c5ec549-1c07-4c2b-9357-69ea89b280cd", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0communal violence bhainsa telangana stones pel...0
1telangana section 144 imposed bhainsa january ...0
2arsonist sets cars ablaze dealership0
3arsonist sets cars ablaze dealership0
4lord jesus love brings freedom pardon fill hol...1
\n", "
" ], "text/plain": [ " text label\n", "0 communal violence bhainsa telangana stones pel... 0\n", "1 telangana section 144 imposed bhainsa january ... 0\n", "2 arsonist sets cars ablaze dealership 0\n", "3 arsonist sets cars ablaze dealership 0\n", "4 lord jesus love brings freedom pardon fill hol... 1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head()" ] }, { "cell_type": "code", "execution_count": 22, "id": "b2328328-e513-4bb8-9172-0d109d20797b", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Percentage of REAL data: 42.61%\n", "Percentage of FAKE data: 57.39%\n" ] } ], "source": [ "print(f'Percentage of REAL data: {round((len(data) - data[\"label\"].sum()) / len(data) * 100, 2)}%')\n", "print(f'Percentage of FAKE data: {round(data[\"label\"].sum() / len(data) * 100, 2)}%')" ] }, { "cell_type": "markdown", "id": "41564eef-4b4b-48a1-9cdb-cf615c753c83", "metadata": { "tags": [] }, "source": [ "# Train/Test split and save" ] }, { "cell_type": "code", "execution_count": 23, "id": "108aeb30-f294-4650-8ac7-afe96b97788b", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Shuffle the DataFrame\n", "shuffled_data = data.sample(frac=1, random_state=42) # Set random_state for reproducibility\n", "\n", "# Split the shuffled DataFrame into train and test sets\n", "train_data, test_data = train_test_split(shuffled_data, test_size=0.2, random_state=42) # Adjust test_size as needed\n", "\n", "# Reset the index column\n", "train_data = train_data.reset_index().drop(['index'], axis=1)\n", "test_data = test_data.reset_index().drop(['index'], axis=1)" ] }, { "cell_type": "code", "execution_count": 24, "id": "0e95eb9a-8e96-4871-8caf-50ee8c8b2ab8", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0name michael brown robbed local convenience st...1
1washington reuters japanese prime minister shi...0
2chicago reuters us house republican tax bill r...0
3reuters fbi interviewed michael flynn initial ...0
4harare reuters ousted zimbabwe finance ministe...0
\n", "
" ], "text/plain": [ " text label\n", "0 name michael brown robbed local convenience st... 1\n", "1 washington reuters japanese prime minister shi... 0\n", "2 chicago reuters us house republican tax bill r... 0\n", "3 reuters fbi interviewed michael flynn initial ... 0\n", "4 harare reuters ousted zimbabwe finance ministe... 0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_data.head()" ] }, { "cell_type": "code", "execution_count": 25, "id": "8b4318d6-b209-4587-b1f8-d469e29f83c6", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textlabel
0kraig moss die hard donald trump supporter fol...1
1white house lawyers last month learned former ...1
2awesome many levels hard know beginafghanistan...1
3please note overwhelming information regarding...1
4kabul reuters us ambassador afghanistan said m...0
\n", "
" ], "text/plain": [ " text label\n", "0 kraig moss die hard donald trump supporter fol... 1\n", "1 white house lawyers last month learned former ... 1\n", "2 awesome many levels hard know beginafghanistan... 1\n", "3 please note overwhelming information regarding... 1\n", "4 kabul reuters us ambassador afghanistan said m... 0" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_data.head()" ] }, { "cell_type": "code", "execution_count": 26, "id": "9ca92861-7526-4bb0-ad1b-50b3104acc02", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Save the train and test sets as separate CSV files\n", "train_data.to_csv('Data/train_dataset.csv', index=False)\n", "test_data.to_csv('Data/test_dataset.csv', index=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "439efe82-51e4-49f4-908f-6f1c9bc3d3d1", "metadata": {}, "outputs": [], "source": [] } ], "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }