File size: 21,323 Bytes
53c5309 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "f75239c5-8dd9-4cd7-a3c9-4f1883b2050d",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd "
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "88e2b62c-2bca-4eac-b890-422b069372ae",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"step1.csv\", sep=';', encoding='utf-8-sig')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "8014eeac-f890-49f0-9453-d65458b363a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(8, 4)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "96a8a6ed-715c-432a-89d5-9f6d36dbdba0",
"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>url</th>\n",
" <th>title</th>\n",
" <th>text</th>\n",
" <th>label</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>https://www.sport-fm.gr/article/o-roueda-sti-S...</td>\n",
" <td>Ο «πρώην» της Ferrari πήγε στην Sauber</td>\n",
" <td>Τον πρώην επικεφαλής στρατηγικής και αθλητικό ...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>https://www.sport-fm.gr/article/sainth-ithela-...</td>\n",
" <td>Σάινθ: «Ήθελα μία νίκη προτού αποχωρήσω από τη...</td>\n",
" <td>Περιχαρής για τη νίκη του στο Grand Prix του Μ...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>https://www.sport-fm.gr/article/Formula-1-anem...</td>\n",
" <td>«Άνεμος» Σάινθ στο Μεξικό, κερδισμένος ο Νόρις...</td>\n",
" <td>Κυρίαρχος Σάινθ και κυρίαρχη Ferrari στο Μεξικ...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>https://www.sport-fm.gr/article/Moto-GP-nikise...</td>\n",
" <td>Moto GP: Νίκησε στην Ταϊλάνδη ο Μπανάια και πλ...</td>\n",
" <td>Σε φινάλε-θρίλερ πηγαίνει το Moto GP. Ο Πέκο Μ...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>https://www.sport-fm.gr/article/Formula-1-prwt...</td>\n",
" <td>Smooth Operator: Πρώτη φετινή pole position γι...</td>\n",
" <td>Την πρώτη του φετινή pole position και έκτη τη...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>https://www.sport-fm.gr/article/o-roueda-sti-S...</td>\n",
" <td>Ο «πρώην» της Ferrari πήγε στην Sauber</td>\n",
" <td>Τον πρώην επικεφαλής στρατηγικής και αθλητικό ...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>https://www.sport-fm.gr/article/Formula-1-epid...</td>\n",
" <td>Επίδειξη δύναμης της Ferrari στο Τέξας με θρια...</td>\n",
" <td>Κυριαρχία της Ferrari στο Τέξας! Σε έναν από τ...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>https://www.sport-fm.gr/article/nikitis-sto-ra...</td>\n",
" <td>Νικητής στο Ράλι Κεντρικής Ευρώπης ο Τάνακ, πα...</td>\n",
" <td>Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής...</td>\n",
" <td>auto</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" url \\\n",
"0 https://www.sport-fm.gr/article/o-roueda-sti-S... \n",
"1 https://www.sport-fm.gr/article/sainth-ithela-... \n",
"2 https://www.sport-fm.gr/article/Formula-1-anem... \n",
"3 https://www.sport-fm.gr/article/Moto-GP-nikise... \n",
"4 https://www.sport-fm.gr/article/Formula-1-prwt... \n",
"5 https://www.sport-fm.gr/article/o-roueda-sti-S... \n",
"6 https://www.sport-fm.gr/article/Formula-1-epid... \n",
"7 https://www.sport-fm.gr/article/nikitis-sto-ra... \n",
"\n",
" title \\\n",
"0 Ο «πρώην» της Ferrari πήγε στην Sauber \n",
"1 Σάινθ: «Ήθελα μία νίκη προτού αποχωρήσω από τη... \n",
"2 «Άνεμος» Σάινθ στο Μεξικό, κερδισμένος ο Νόρις... \n",
"3 Moto GP: Νίκησε στην Ταϊλάνδη ο Μπανάια και πλ... \n",
"4 Smooth Operator: Πρώτη φετινή pole position γι... \n",
"5 Ο «πρώην» της Ferrari πήγε στην Sauber \n",
"6 Επίδειξη δύναμης της Ferrari στο Τέξας με θρια... \n",
"7 Νικητής στο Ράλι Κεντρικής Ευρώπης ο Τάνακ, πα... \n",
"\n",
" text label \n",
"0 Τον πρώην επικεφαλής στρατηγικής και αθλητικό ... auto \n",
"1 Περιχαρής για τη νίκη του στο Grand Prix του Μ... auto \n",
"2 Κυρίαρχος Σάινθ και κυρίαρχη Ferrari στο Μεξικ... auto \n",
"3 Σε φινάλε-θρίλερ πηγαίνει το Moto GP. Ο Πέκο Μ... auto \n",
"4 Την πρώτη του φετινή pole position και έκτη τη... auto \n",
"5 Τον πρώην επικεφαλής στρατηγικής και αθλητικό ... auto \n",
"6 Κυριαρχία της Ferrari στο Τέξας! Σε έναν από τ... auto \n",
"7 Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής... auto "
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "7b78cdec-351d-497b-9431-6d02818e0157",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"def clean_text(text):\n",
" unwanted_patterns = [\n",
" r'Ακολουθήστε τον bwinΣΠΟΡ FM 94.6.*', # Removes text from \"Ακολουθήστε τον bwinΣΠΟΡ FM 94.6...\" onwards\n",
" r'(https?:\\/\\/|www\\.)\\S+', # Removes all links\n",
" r'#\\S+', # Removes hashtags\n",
" r'@\\S+', # Removes mentions\n",
" r'pic\\.twitter\\.com/\\S+', # Removes \"pic.twitter.com\" links\n",
" r'\\b(?:Από|Πηγή|Time|Ώρα|Date|Ημερομηνία):.*', # Removes time or source/author credit lines\n",
" r'Απαγόρευση της στάσης, στάθμευσης και κυκλοφορίας των οχημάτων, καθώς και της κυκλοφορίας πεζών \\(πλην των αγωνιστικών και των οχημάτων της διοργάνωσης\\):?',\n",
" r'\\\\n|\\\\r',\n",
" r'[\\n]', #carriage \\n\n",
" r'(?i)\\b(?:January|February|March|April|May|June|July|August|September|October|November|December)\\b \\d{2},? \\d{4}', #remenant dates of tweets\n",
" r'\\(?\\d{1,2}\\/\\d{1,2}\\/\\d{4}\\)?', #dates ths morfhs 22/04/2024\n",
" r'(?:[^\\u0370-\\u03FF\\s\\d]+(?:\\s+[^\\u0370-\\u03FF\\s\\d]+|\\s*[.?\\d]+){3,})', #removes sentences that have 3 or more english words\n",
" r'[\\U0001F600-\\U0001F64F\\U0001F300-\\U0001F5FF\\U0001F680-\\U0001F6FF\\U0001F700-\\U0001F77F\\U0001F900-\\U0001F9FF\\U0001FA70-\\U0001FAFF\\U00002702-\\U000027B0\\U000024C2-\\U0001F251\\u21D2\\u21D0-\\U0001F77F•‣◦▪▫●]'\n",
" #removes emojies and bulletpoints\n",
" ]\n",
" \n",
" for pattern in unwanted_patterns:\n",
" text = re.sub(pattern, '', text) # Apply each pattern to remove matching text\n",
" \n",
" # Remove extra whitespace that might be left behind\n",
" text = re.sub(r'\\s+', ' ', text).strip()\n",
" \n",
" return text"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "e1160e1f-edb7-44e1-866f-81ab608493c6",
"metadata": {},
"outputs": [],
"source": [
"df.loc[df['url'].str.contains('sport-fm.gr'), 'clean_text'] = df.loc[df['url'].str.contains(\"sport-fm.gr\"), 'text'].apply(clean_text)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "fa0f06a5-e939-48dd-8bb8-62cd5e7665be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής Ευρώπης, τον προτελευταίο γύρο του παγκόσμιου πρωταθλήματος WRC, και παραμένει στη διεκδίκηση του παγκόσμιου τίτλου απέναντι στον ομόσταβλό Τιέρι Νεβίλ, που τερμάτισε τρίτος αλλά χρειάζεται μόλις έξι βαθμούς για να είναι πρωταθλητής.Ο Εσθονός νικητής του αγώνα έφτασε τους 196 βαθμούς, ενώ 222 έχει ο Βέλγος που έχει τον πρώτο λόγο για τον τίτλο εν όψει του τελευταίου αγώνα στην Ιαπωνία από τις 21 έως τις 24 Νοεμβρίου.Ο οκτώ φορές Γάλλος παγκόσμιος πρωταθλητής Σεμπαστιάν Οζιέ (Toyota) αν και ήταν δεύτερος ως το πρωί της Κυριακής, υποχρεώθηκε να εγκαταλείψει στην προτελευταία ειδική μετά από έξοδο από το δρόμο. Έτσι τέθηκε οριστικά εκτός διεκδίκησης του τίτλου, καθώς υποχώρησε στην τέταρτη θέση, πίσω και από τον Βρετανό ομόσταυλό του στην Toyota Έλφιν Έβανς.'"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['clean_text'].iloc[7]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "a191b326-8d17-4137-858b-7e5f996da3ce",
"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>url</th>\n",
" <th>title</th>\n",
" <th>text</th>\n",
" <th>label</th>\n",
" <th>clean_text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>https://www.sport-fm.gr/article/o-roueda-sti-S...</td>\n",
" <td>Ο «πρώην» της Ferrari πήγε στην Sauber</td>\n",
" <td>Τον πρώην επικεφαλής στρατηγικής και αθλητικό ...</td>\n",
" <td>auto</td>\n",
" <td>Τον πρώην επικεφαλής στρατηγικής και αθλητικό ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>https://www.sport-fm.gr/article/sainth-ithela-...</td>\n",
" <td>Σάινθ: «Ήθελα μία νίκη προτού αποχωρήσω από τη...</td>\n",
" <td>Περιχαρής για τη νίκη του στο Grand Prix του Μ...</td>\n",
" <td>auto</td>\n",
" <td>Περιχαρής για τη νίκη του στο Grand Prix του Μ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>https://www.sport-fm.gr/article/Formula-1-anem...</td>\n",
" <td>«Άνεμος» Σάινθ στο Μεξικό, κερδισμένος ο Νόρις...</td>\n",
" <td>Κυρίαρχος Σάινθ και κυρίαρχη Ferrari στο Μεξικ...</td>\n",
" <td>auto</td>\n",
" <td>Κυρίαρχος Σάινθ και κυρίαρχη Ferrari στο Μεξικ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>https://www.sport-fm.gr/article/Moto-GP-nikise...</td>\n",
" <td>Moto GP: Νίκησε στην Ταϊλάνδη ο Μπανάια και πλ...</td>\n",
" <td>Σε φινάλε-θρίλερ πηγαίνει το Moto GP. Ο Πέκο Μ...</td>\n",
" <td>auto</td>\n",
" <td>Σε φινάλε-θρίλερ πηγαίνει το Moto GP. Ο Πέκο Μ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>https://www.sport-fm.gr/article/Formula-1-prwt...</td>\n",
" <td>Smooth Operator: Πρώτη φετινή pole position γι...</td>\n",
" <td>Την πρώτη του φετινή pole position και έκτη τη...</td>\n",
" <td>auto</td>\n",
" <td>Την πρώτη του φετινή pole position και έκτη τη...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>https://www.sport-fm.gr/article/o-roueda-sti-S...</td>\n",
" <td>Ο «πρώην» της Ferrari πήγε στην Sauber</td>\n",
" <td>Τον πρώην επικεφαλής στρατηγικής και αθλητικό ...</td>\n",
" <td>auto</td>\n",
" <td>Τον πρώην επικεφαλής στρατηγικής και αθλητικό ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>https://www.sport-fm.gr/article/Formula-1-epid...</td>\n",
" <td>Επίδειξη δύναμης της Ferrari στο Τέξας με θρια...</td>\n",
" <td>Κυριαρχία της Ferrari στο Τέξας! Σε έναν από τ...</td>\n",
" <td>auto</td>\n",
" <td>Κυριαρχία της Ferrari στο Τέξας! Σε έναν από τ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>https://www.sport-fm.gr/article/nikitis-sto-ra...</td>\n",
" <td>Νικητής στο Ράλι Κεντρικής Ευρώπης ο Τάνακ, πα...</td>\n",
" <td>Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής...</td>\n",
" <td>auto</td>\n",
" <td>Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" url \\\n",
"0 https://www.sport-fm.gr/article/o-roueda-sti-S... \n",
"1 https://www.sport-fm.gr/article/sainth-ithela-... \n",
"2 https://www.sport-fm.gr/article/Formula-1-anem... \n",
"3 https://www.sport-fm.gr/article/Moto-GP-nikise... \n",
"4 https://www.sport-fm.gr/article/Formula-1-prwt... \n",
"5 https://www.sport-fm.gr/article/o-roueda-sti-S... \n",
"6 https://www.sport-fm.gr/article/Formula-1-epid... \n",
"7 https://www.sport-fm.gr/article/nikitis-sto-ra... \n",
"\n",
" title \\\n",
"0 Ο «πρώην» της Ferrari πήγε στην Sauber \n",
"1 Σάινθ: «Ήθελα μία νίκη προτού αποχωρήσω από τη... \n",
"2 «Άνεμος» Σάινθ στο Μεξικό, κερδισμένος ο Νόρις... \n",
"3 Moto GP: Νίκησε στην Ταϊλάνδη ο Μπανάια και πλ... \n",
"4 Smooth Operator: Πρώτη φετινή pole position γι... \n",
"5 Ο «πρώην» της Ferrari πήγε στην Sauber \n",
"6 Επίδειξη δύναμης της Ferrari στο Τέξας με θρια... \n",
"7 Νικητής στο Ράλι Κεντρικής Ευρώπης ο Τάνακ, πα... \n",
"\n",
" text label \\\n",
"0 Τον πρώην επικεφαλής στρατηγικής και αθλητικό ... auto \n",
"1 Περιχαρής για τη νίκη του στο Grand Prix του Μ... auto \n",
"2 Κυρίαρχος Σάινθ και κυρίαρχη Ferrari στο Μεξικ... auto \n",
"3 Σε φινάλε-θρίλερ πηγαίνει το Moto GP. Ο Πέκο Μ... auto \n",
"4 Την πρώτη του φετινή pole position και έκτη τη... auto \n",
"5 Τον πρώην επικεφαλής στρατηγικής και αθλητικό ... auto \n",
"6 Κυριαρχία της Ferrari στο Τέξας! Σε έναν από τ... auto \n",
"7 Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής... auto \n",
"\n",
" clean_text \n",
"0 Τον πρώην επικεφαλής στρατηγικής και αθλητικό ... \n",
"1 Περιχαρής για τη νίκη του στο Grand Prix του Μ... \n",
"2 Κυρίαρχος Σάινθ και κυρίαρχη Ferrari στο Μεξικ... \n",
"3 Σε φινάλε-θρίλερ πηγαίνει το Moto GP. Ο Πέκο Μ... \n",
"4 Την πρώτη του φετινή pole position και έκτη τη... \n",
"5 Τον πρώην επικεφαλής στρατηγικής και αθλητικό ... \n",
"6 Κυριαρχία της Ferrari στο Τέξας! Σε έναν από τ... \n",
"7 Ο Οτ Τάνακ (Hyundai) κέρδισε το Ράλι Κεντρικής... "
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "3f3236cd-df32-4462-90a8-8f5e753c24d9",
"metadata": {},
"outputs": [],
"source": [
"df.to_csv(\"step2.csv\",sep=';',encoding='utf-8-sig',index=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5df29888-cdd6-467b-ab15-0582330f82e5",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|