File size: 3,334 Bytes
e909e6d | 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 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b814cbc1",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import re "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "43df7374",
"metadata": {},
"outputs": [],
"source": [
"DATA_TRAIN_DIR = \"../ML_Approaching/Preprocessing/train_augmentated.csv\"\n",
"DATA_VAL_DIR = \"../ML_Approaching/Preprocessing/val_data.csv\"\n",
"DATA_TEST_DIR = \"../ML_Approaching/Preprocessing/test_data.csv\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5c1bde4e",
"metadata": {},
"outputs": [],
"source": [
"df_train = pd.read_csv(DATA_TRAIN_DIR)\n",
"df_val = pd.read_csv(DATA_VAL_DIR)\n",
"df_test = pd.read_csv(DATA_TEST_DIR)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9e4eb75a",
"metadata": {},
"outputs": [],
"source": [
"def clean_text(text):\n",
" # 1. Chuyển đổi về string và xử lý khoảng trắng cơ bản\n",
" text = str(text)\n",
" \n",
" # Thay thế new line bằng khoảng trắng (comment thường có nhiều enter)\n",
" text = text.replace('\\n', ' ')\n",
" text = text.replace('\\t', ' ')\n",
" \n",
" # 2. Xử lý IP Address (thường xuất hiện trong wiki comments)\n",
" # Thay thế các IP v4 bằng token <IP> hoặc bỏ đi\n",
" text = re.sub(r'\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}', ' ', text)\n",
" \n",
" # 3. Xử lý Links/URLs\n",
" text = re.sub(r'http\\S+', ' ', text)\n",
" \n",
" # 4. Xử lý Username (Wiki usernames thường có dạng \"User:...\")\n",
" text = re.sub(r\"User:.*\", ' ', text)\n",
" text = re.sub(r'@[A-Za-z0-9]+', ' ', text)\n",
" \n",
" # 5. Xóa các khoảng trắng thừa (double spaces)\n",
" text = \" \".join(text.split())\n",
" \n",
" return text"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "67e47f8f",
"metadata": {},
"outputs": [],
"source": [
"df_train[\"clean_text\"] = df_train[\"comment_text\"].astype(str).apply(clean_text)\n",
"df_val[\"clean_text\"] = df_val[\"comment_text\"].astype(str).apply(clean_text)\n",
"df_test[\"clean_text\"] = df_test[\"comment_text\"].astype(str).apply(clean_text)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1979efcc",
"metadata": {},
"outputs": [],
"source": [
"df_train.drop(columns=['comment_text','Unnamed: 0','Unnamed: 0.1']).to_csv(\"./train_data_preprocessing.csv\")\n",
"df_test.drop(columns=['comment_text','Unnamed: 0']).to_csv(\"./val_data_preprocessing.csv\")\n",
"df_val.drop(columns=['comment_text','Unnamed: 0']).to_csv(\"./test_data_preprocessing.csv\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "hf_env",
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|