Spaces:
Build error
Build error
File size: 38,716 Bytes
0dec8a3 |
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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "42801c6a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"workding dir: /Users/inflaton/code/engd/papers/maritime/global-incidents\n",
"loading env vars from: /Users/inflaton/code/engd/papers/maritime/global-incidents/.env\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"import os\n",
"import sys\n",
"from pathlib import Path\n",
"\n",
"workding_dir = str(Path.cwd().parent)\n",
"os.chdir(workding_dir)\n",
"sys.path.append(workding_dir)\n",
"print(\"workding dir:\", workding_dir)\n",
"\n",
"from dotenv import find_dotenv, load_dotenv\n",
"\n",
"found_dotenv = find_dotenv(\".env\")\n",
"\n",
"if len(found_dotenv) == 0:\n",
" found_dotenv = find_dotenv(\".env.example\")\n",
"print(f\"loading env vars from: {found_dotenv}\")\n",
"load_dotenv(found_dotenv, override=True)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ddf1e32e-7751-43db-9b5a-22cb08e35c6c",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import nltk\n",
"from nltk.corpus import PlaintextCorpusReader\n",
"from nltk.corpus import stopwords\n",
"from nltk.stem.porter import *\n",
"from nltk import pos_tag, word_tokenize\n",
"from nltk.stem import WordNetLemmatizer\n",
"from nltk.probability import FreqDist\n",
"from nltk.tokenize import sent_tokenize\n",
"from nltk.tokenize import word_tokenize\n",
"import contractions\n",
"\n",
"import gensim\n",
"from gensim import corpora\n",
"from gensim import similarities\n",
"from gensim import models\n",
"from gensim.models import CoherenceModel\n",
"\n",
"# from wordcloud import WordCloud, ImageColorGenerator\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"import pandas as pd\n",
"import re\n",
"import os\n",
"import glob\n",
"import json\n",
"\n",
"import pickle\n",
"from datetime import datetime\n",
"import datetime\n",
"from newspaper import Article\n",
"from gnews import GNews"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a9c6ed07-a7d0-4aaa-b571-038919c75e05",
"metadata": {},
"outputs": [],
"source": [
"# Function to get full news article by headline\n",
"def get_news_article(headline):\n",
" gnews = GNews()\n",
" articles = gnews.get_news(headline)\n",
"\n",
" if articles:\n",
" for article in articles:\n",
" # Assuming the headline contains the URL of the news article\n",
" try:\n",
" article = Article(article[\"url\"])\n",
" article.download()\n",
" article.parse()\n",
" return article.url, article.title, article.text\n",
" except Exception as e:\n",
" # link cannot be scraped\n",
" # print(\"get_news_article - exception:\", e)\n",
" return [\n",
" f\"cannot scrape the url: {e}\",\n",
" \"cannot scrape the title\",\n",
" \"cannot scrape the content\",\n",
" ]\n",
" # No articles found for the given title.\n",
" else:\n",
" return [\"no url found\", \"no title found\", \"no content found\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0acaf899-cd96-4424-b825-c71bf042355a",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Read the CSV file with news titles\n",
"csv_file_path = \"data/cleaned_data.csv\"\n",
"df = pd.read_csv(csv_file_path)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "465acb47-5f57-4d90-a272-7972d56e002b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5782, 17)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "801d892d-3b87-4ecf-805e-3441815d00f1",
"metadata": {},
"outputs": [],
"source": [
"# drop empty news\n",
"df.dropna(subset=[\"Headline\"], inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a65a8eaf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5780, 17)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1267bf68-4726-49bf-ae21-9fed09682945",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"70"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[[\"Year\", \"Headline\", \"Region\"]].duplicated().sum()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "51e0f672",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5710, 17)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_uni = df.drop_duplicates(subset=[\"Year\", \"Headline\", \"Region\"], keep=\"first\")\n",
"df_uni.shape"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f3c35a3c-d852-4285-86bf-3f7db7df78e7",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 13%|█▎ | 757/5710 [27:31<3:29:09, 2.53s/it] /Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/dateutil/parser/_parser.py:1207: UnknownTimezoneWarning: tzname PST identified but not understood. Pass `tzinfos` argument in order to correctly return a timezone-aware datetime. In a future version, this will raise an exception.\n",
" warnings.warn(\"tzname {tzname} identified but not understood. \"\n",
" 28%|██▊ | 1588/5710 [57:29<3:22:58, 2.95s/it] /Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/dateutil/parser/_parser.py:1207: UnknownTimezoneWarning: tzname PDT identified but not understood. Pass `tzinfos` argument in order to correctly return a timezone-aware datetime. In a future version, this will raise an exception.\n",
" warnings.warn(\"tzname {tzname} identified but not understood. \"\n",
" 68%|██████▊ | 3862/5710 [2:24:11<1:04:55, 2.11s/it] 06/29/2024 07:38:31 PM - newspaper ignoring lxml node error: Input object is not an XML element: HtmlProcessingInstruction\n",
" 70%|██████▉ | 3978/5710 [2:27:39<1:44:23, 3.62s/it]Building prefix dict from /Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/jieba/dict.txt ...\n",
"06/29/2024 07:41:56 PM - Building prefix dict from /Users/inflaton/anaconda3/envs/maritime/lib/python3.12/site-packages/jieba/dict.txt ...\n",
"Loading model from cache /var/folders/7x/56svhln929zdh2xhr3mwqg4r0000gn/T/jieba.cache\n",
"06/29/2024 07:41:56 PM - Loading model from cache /var/folders/7x/56svhln929zdh2xhr3mwqg4r0000gn/T/jieba.cache\n",
"Loading model cost 0.28935670852661133 seconds.\n",
"06/29/2024 07:41:56 PM - Loading model cost 0.28935670852661133 seconds.\n",
"Prefix dict has been built succesfully.\n",
"06/29/2024 07:41:56 PM - Prefix dict has been built succesfully.\n",
"100%|██████████| 5710/5710 [3:30:27<00:00, 2.21s/it] "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 16min 31s, sys: 3min 20s, total: 19min 51s\n",
"Wall time: 3h 30min 27s\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"<timed exec>:20: 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",
"<timed exec>:21: 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",
"<timed exec>:22: 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"
]
}
],
"source": [
"%%time\n",
"\n",
"from tqdm import tqdm\n",
"\n",
"# Iterate through each row and get the full news article\n",
"tes = df_uni\n",
"\n",
"urls = []\n",
"titles = []\n",
"contents = []\n",
"\n",
"for index in tqdm(range(len(tes))):\n",
" row = tes.iloc[index]\n",
" headline = row[\"Headline\"]\n",
"\n",
" results = get_news_article(headline)\n",
" urls.append(results[0])\n",
" titles.append(results[1])\n",
" contents.append(results[2])\n",
"\n",
"# Update the DataFrame with the fetched data\n",
"tes[\"url\"] = urls\n",
"tes[\"title\"] = titles\n",
"tes[\"content\"] = contents"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "66d98c3b-c1f2-42e6-ae5f-8192c5a91f44",
"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>id</th>\n",
" <th>Headline</th>\n",
" <th>Details</th>\n",
" <th>Severity</th>\n",
" <th>Category</th>\n",
" <th>Region</th>\n",
" <th>Datetime</th>\n",
" <th>Year</th>\n",
" <th>lat</th>\n",
" <th>lon</th>\n",
" <th>maritime_label</th>\n",
" <th>found_ports</th>\n",
" <th>contains_port_info</th>\n",
" <th>if_labeled</th>\n",
" <th>Month</th>\n",
" <th>Week</th>\n",
" <th>Headline_Details</th>\n",
" <th>url</th>\n",
" <th>title</th>\n",
" <th>content</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.0</td>\n",
" <td>Grasberg Mine- Grasberg mine workers extend st...</td>\n",
" <td>Media sources indicate that workers at the Gra...</td>\n",
" <td>Moderate</td>\n",
" <td>Mine Workers Strike</td>\n",
" <td>Indonesia</td>\n",
" <td>28/5/17 17:08</td>\n",
" <td>2017.0</td>\n",
" <td>-4.05608</td>\n",
" <td>137.11302</td>\n",
" <td>False</td>\n",
" <td>['freeport']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>5.0</td>\n",
" <td>21.0</td>\n",
" <td>Grasberg Mine- Grasberg mine workers extend st...</td>\n",
" <td>https://news.google.com/rss/articles/CBMiZ2h0d...</td>\n",
" <td>Freeport Indonesia mine workers extend strike ...</td>\n",
" <td>By Wilda Asmarini and Susan Taylor\\n\\nJAKARTA/...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2.0</td>\n",
" <td>Indonesia: Undersea internet cables damaged by...</td>\n",
" <td>News sources are stating that recent typhoons ...</td>\n",
" <td>Minor</td>\n",
" <td>Travel Warning</td>\n",
" <td>Indonesia</td>\n",
" <td>4/9/17 14:30</td>\n",
" <td>2017.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" <td>['hong kong']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>4.0</td>\n",
" <td>14.0</td>\n",
" <td>Indonesia: Undersea internet cables damaged by...</td>\n",
" <td>no url found</td>\n",
" <td>no title found</td>\n",
" <td>no content found</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3.0</td>\n",
" <td>Shanghai port congestion impacts terminals in ...</td>\n",
" <td>The persisting port congestion at Shanghai’s Y...</td>\n",
" <td>Minor</td>\n",
" <td>Port Congestion</td>\n",
" <td>China</td>\n",
" <td>27/4/17 9:16</td>\n",
" <td>2017.0</td>\n",
" <td>29.52000</td>\n",
" <td>121.33190</td>\n",
" <td>True</td>\n",
" <td>['ningbo', 'qingdao', 'shanghai']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>4.0</td>\n",
" <td>17.0</td>\n",
" <td>Shanghai port congestion impacts terminals in ...</td>\n",
" <td>https://news.google.com/rss/articles/CBMiZmh0d...</td>\n",
" <td>Bad weather causing delays, congestion at majo...</td>\n",
" <td>LA-LB ports investing $25 million in charging ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.0</td>\n",
" <td>UPDATE - Indonesia: Explosion at KP Terminal i...</td>\n",
" <td>Updated local media sources from Jakarta indic...</td>\n",
" <td>Extreme</td>\n",
" <td>Bombing, Police Operations</td>\n",
" <td>Indonesia</td>\n",
" <td>24/5/17 15:15</td>\n",
" <td>2017.0</td>\n",
" <td>-6.22465</td>\n",
" <td>106.86700</td>\n",
" <td>True</td>\n",
" <td>['jakarta']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>5.0</td>\n",
" <td>21.0</td>\n",
" <td>UPDATE - Indonesia: Explosion at KP Terminal i...</td>\n",
" <td>no url found</td>\n",
" <td>no title found</td>\n",
" <td>no content found</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>UPDATE - Indonesia: Police confirm two explosi...</td>\n",
" <td>According to local police in Jakarta, two expl...</td>\n",
" <td>Extreme</td>\n",
" <td>Bombing, Police Operations</td>\n",
" <td>Indonesia</td>\n",
" <td>24/5/17 16:20</td>\n",
" <td>2017.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>True</td>\n",
" <td>['jakarta']</td>\n",
" <td>1.0</td>\n",
" <td>True</td>\n",
" <td>5.0</td>\n",
" <td>21.0</td>\n",
" <td>UPDATE - Indonesia: Police confirm two explosi...</td>\n",
" <td>https://news.google.com/rss/articles/CBMiV2h0d...</td>\n",
" <td>Three dead confirmed in Jakarta double bombing</td>\n",
" <td>Two explosions strike bus station in Indonesia...</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",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5777</th>\n",
" <td>5776.0</td>\n",
" <td>Winter storm may bring inclement weather condi...</td>\n",
" <td>Intelligence received by Everstream Analytics ...</td>\n",
" <td>Moderate</td>\n",
" <td>Ice Storm</td>\n",
" <td>United States</td>\n",
" <td>16/12/20 2:47</td>\n",
" <td>2020.0</td>\n",
" <td>38.90072</td>\n",
" <td>-77.05440</td>\n",
" <td>False</td>\n",
" <td>['new york']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>12.0</td>\n",
" <td>51.0</td>\n",
" <td>Winter storm may bring inclement weather condi...</td>\n",
" <td>https://news.google.com/rss/articles/CBMiWWh0d...</td>\n",
" <td>Winter Storm Warnings posted for parts of Nort...</td>\n",
" <td>A coastal storm is set to deliver another slop...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5778</th>\n",
" <td>5777.0</td>\n",
" <td>Winter weather expected to continue to impact ...</td>\n",
" <td>Meteorological sources indicate that a series ...</td>\n",
" <td>Minor</td>\n",
" <td>Roadway Closure / Disruption, Ground Transport...</td>\n",
" <td>United States</td>\n",
" <td>31/12/20 18:15</td>\n",
" <td>2020.0</td>\n",
" <td>41.30357</td>\n",
" <td>-72.90561</td>\n",
" <td>False</td>\n",
" <td>['new york', 'virginia']</td>\n",
" <td>1.0</td>\n",
" <td>True</td>\n",
" <td>12.0</td>\n",
" <td>53.0</td>\n",
" <td>Winter weather expected to continue to impact ...</td>\n",
" <td>https://news.google.com/rss/articles/CBMiT2h0d...</td>\n",
" <td>Deadly nor'easter wreaks havoc in New England ...</td>\n",
" <td>BOSTON – A deadly late-season nor’easter that ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5779</th>\n",
" <td>5778.0</td>\n",
" <td>Workers of Svitzer Australia plan to strike on...</td>\n",
" <td>Industry sources report on December 7 that Svi...</td>\n",
" <td>Moderate</td>\n",
" <td>Industrial Action</td>\n",
" <td>Australia</td>\n",
" <td>7/12/20 6:16</td>\n",
" <td>2020.0</td>\n",
" <td>-38.35169</td>\n",
" <td>145.25050</td>\n",
" <td>False</td>\n",
" <td>['geelong', 'melbourne']</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>7.0</td>\n",
" <td>28.0</td>\n",
" <td>Workers of Svitzer Australia plan to strike on...</td>\n",
" <td>no url found</td>\n",
" <td>no title found</td>\n",
" <td>no content found</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5780</th>\n",
" <td>5779.0</td>\n",
" <td>Workers stage 24-hour stoppage at DP World Ter...</td>\n",
" <td>Industry sources indicate on December 14 that ...</td>\n",
" <td>Moderate</td>\n",
" <td>Port Strike</td>\n",
" <td>Australia</td>\n",
" <td>14/12/20 16:52</td>\n",
" <td>2020.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>True</td>\n",
" <td>['fremantle']</td>\n",
" <td>1.0</td>\n",
" <td>True</td>\n",
" <td>12.0</td>\n",
" <td>51.0</td>\n",
" <td>Workers stage 24-hour stoppage at DP World Ter...</td>\n",
" <td>https://news.google.com/rss/articles/CBMiOWh0d...</td>\n",
" <td>Union shuts down strikes as DP World threatens...</td>\n",
" <td>The Maritime Union of Australia (MUA) has curt...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5781</th>\n",
" <td>5780.0</td>\n",
" <td>Workers with Ertsoverslagbedrijf Europoort C.V...</td>\n",
" <td>On November 17, Dutch media sources reported t...</td>\n",
" <td>Minor</td>\n",
" <td>Port Strike</td>\n",
" <td>Netherlands</td>\n",
" <td>18/11/20 17:55</td>\n",
" <td>2020.0</td>\n",
" <td>51.96121</td>\n",
" <td>4.10550</td>\n",
" <td>False</td>\n",
" <td>['rotterdam']</td>\n",
" <td>1.0</td>\n",
" <td>True</td>\n",
" <td>11.0</td>\n",
" <td>47.0</td>\n",
" <td>Workers with Ertsoverslagbedrijf Europoort C.V...</td>\n",
" <td>no url found</td>\n",
" <td>no title found</td>\n",
" <td>no content found</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5710 rows × 20 columns</p>\n",
"</div>"
],
"text/plain": [
" id Headline \\\n",
"0 1.0 Grasberg Mine- Grasberg mine workers extend st... \n",
"1 2.0 Indonesia: Undersea internet cables damaged by... \n",
"2 3.0 Shanghai port congestion impacts terminals in ... \n",
"3 4.0 UPDATE - Indonesia: Explosion at KP Terminal i... \n",
"4 5.0 UPDATE - Indonesia: Police confirm two explosi... \n",
"... ... ... \n",
"5777 5776.0 Winter storm may bring inclement weather condi... \n",
"5778 5777.0 Winter weather expected to continue to impact ... \n",
"5779 5778.0 Workers of Svitzer Australia plan to strike on... \n",
"5780 5779.0 Workers stage 24-hour stoppage at DP World Ter... \n",
"5781 5780.0 Workers with Ertsoverslagbedrijf Europoort C.V... \n",
"\n",
" Details Severity \\\n",
"0 Media sources indicate that workers at the Gra... Moderate \n",
"1 News sources are stating that recent typhoons ... Minor \n",
"2 The persisting port congestion at Shanghai’s Y... Minor \n",
"3 Updated local media sources from Jakarta indic... Extreme \n",
"4 According to local police in Jakarta, two expl... Extreme \n",
"... ... ... \n",
"5777 Intelligence received by Everstream Analytics ... Moderate \n",
"5778 Meteorological sources indicate that a series ... Minor \n",
"5779 Industry sources report on December 7 that Svi... Moderate \n",
"5780 Industry sources indicate on December 14 that ... Moderate \n",
"5781 On November 17, Dutch media sources reported t... Minor \n",
"\n",
" Category Region \\\n",
"0 Mine Workers Strike Indonesia \n",
"1 Travel Warning Indonesia \n",
"2 Port Congestion China \n",
"3 Bombing, Police Operations Indonesia \n",
"4 Bombing, Police Operations Indonesia \n",
"... ... ... \n",
"5777 Ice Storm United States \n",
"5778 Roadway Closure / Disruption, Ground Transport... United States \n",
"5779 Industrial Action Australia \n",
"5780 Port Strike Australia \n",
"5781 Port Strike Netherlands \n",
"\n",
" Datetime Year lat lon maritime_label \\\n",
"0 28/5/17 17:08 2017.0 -4.05608 137.11302 False \n",
"1 4/9/17 14:30 2017.0 NaN NaN False \n",
"2 27/4/17 9:16 2017.0 29.52000 121.33190 True \n",
"3 24/5/17 15:15 2017.0 -6.22465 106.86700 True \n",
"4 24/5/17 16:20 2017.0 NaN NaN True \n",
"... ... ... ... ... ... \n",
"5777 16/12/20 2:47 2020.0 38.90072 -77.05440 False \n",
"5778 31/12/20 18:15 2020.0 41.30357 -72.90561 False \n",
"5779 7/12/20 6:16 2020.0 -38.35169 145.25050 False \n",
"5780 14/12/20 16:52 2020.0 NaN NaN True \n",
"5781 18/11/20 17:55 2020.0 51.96121 4.10550 False \n",
"\n",
" found_ports contains_port_info if_labeled Month \\\n",
"0 ['freeport'] 1.0 False 5.0 \n",
"1 ['hong kong'] 1.0 False 4.0 \n",
"2 ['ningbo', 'qingdao', 'shanghai'] 1.0 False 4.0 \n",
"3 ['jakarta'] 1.0 False 5.0 \n",
"4 ['jakarta'] 1.0 True 5.0 \n",
"... ... ... ... ... \n",
"5777 ['new york'] 1.0 False 12.0 \n",
"5778 ['new york', 'virginia'] 1.0 True 12.0 \n",
"5779 ['geelong', 'melbourne'] 1.0 False 7.0 \n",
"5780 ['fremantle'] 1.0 True 12.0 \n",
"5781 ['rotterdam'] 1.0 True 11.0 \n",
"\n",
" Week Headline_Details \\\n",
"0 21.0 Grasberg Mine- Grasberg mine workers extend st... \n",
"1 14.0 Indonesia: Undersea internet cables damaged by... \n",
"2 17.0 Shanghai port congestion impacts terminals in ... \n",
"3 21.0 UPDATE - Indonesia: Explosion at KP Terminal i... \n",
"4 21.0 UPDATE - Indonesia: Police confirm two explosi... \n",
"... ... ... \n",
"5777 51.0 Winter storm may bring inclement weather condi... \n",
"5778 53.0 Winter weather expected to continue to impact ... \n",
"5779 28.0 Workers of Svitzer Australia plan to strike on... \n",
"5780 51.0 Workers stage 24-hour stoppage at DP World Ter... \n",
"5781 47.0 Workers with Ertsoverslagbedrijf Europoort C.V... \n",
"\n",
" url \\\n",
"0 https://news.google.com/rss/articles/CBMiZ2h0d... \n",
"1 no url found \n",
"2 https://news.google.com/rss/articles/CBMiZmh0d... \n",
"3 no url found \n",
"4 https://news.google.com/rss/articles/CBMiV2h0d... \n",
"... ... \n",
"5777 https://news.google.com/rss/articles/CBMiWWh0d... \n",
"5778 https://news.google.com/rss/articles/CBMiT2h0d... \n",
"5779 no url found \n",
"5780 https://news.google.com/rss/articles/CBMiOWh0d... \n",
"5781 no url found \n",
"\n",
" title \\\n",
"0 Freeport Indonesia mine workers extend strike ... \n",
"1 no title found \n",
"2 Bad weather causing delays, congestion at majo... \n",
"3 no title found \n",
"4 Three dead confirmed in Jakarta double bombing \n",
"... ... \n",
"5777 Winter Storm Warnings posted for parts of Nort... \n",
"5778 Deadly nor'easter wreaks havoc in New England ... \n",
"5779 no title found \n",
"5780 Union shuts down strikes as DP World threatens... \n",
"5781 no title found \n",
"\n",
" content \n",
"0 By Wilda Asmarini and Susan Taylor\\n\\nJAKARTA/... \n",
"1 no content found \n",
"2 LA-LB ports investing $25 million in charging ... \n",
"3 no content found \n",
"4 Two explosions strike bus station in Indonesia... \n",
"... ... \n",
"5777 A coastal storm is set to deliver another slop... \n",
"5778 BOSTON – A deadly late-season nor’easter that ... \n",
"5779 no content found \n",
"5780 The Maritime Union of Australia (MUA) has curt... \n",
"5781 no content found \n",
"\n",
"[5710 rows x 20 columns]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tes"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d6fe260c-1bbb-4556-b9ac-2b9b62c5ba5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3218"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows = tes[\n",
" ~tes[\"content\"].isin([\"cannot scrape the content\", \"no content found\"])\n",
"].shape[0]\n",
"count_rows"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "6818fe7b-6e7d-4d30-8ee8-866b29fd40c8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2132"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows = tes[tes[\"content\"].isin([\"no content found\"])].shape[0]\n",
"count_rows"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "f59e2ce4-072a-43e2-a247-fd399c388c2d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"360"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows = tes[tes[\"content\"].isin([\"cannot scrape the content\"])].shape[0]\n",
"count_rows"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "8063b964",
"metadata": {},
"outputs": [],
"source": [
"scrapped_df1 = pd.read_parquet(\"data/scrapped_data1.parquet\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "78896dd3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5710, 20)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scrapped_df1.shape"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "316d6be5",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 5710/5710 [00:00<00:00, 35312.10it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 162 ms, sys: 3.81 ms, total: 166 ms\n",
"Wall time: 165 ms\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"<timed exec>:23: 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",
"<timed exec>:24: 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",
"<timed exec>:25: 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"
]
}
],
"source": [
"%%time\n",
"\n",
"from tqdm import tqdm\n",
"\n",
"urls = []\n",
"titles = []\n",
"contents = []\n",
"\n",
"not_found = [\"cannot scrape the content\", \"no content found\", None]\n",
"\n",
"for index in tqdm(range(len(tes))):\n",
" row1 = scrapped_df1.iloc[index]\n",
" row2 = tes.iloc[index]\n",
" \n",
" if row1[\"content\"] in not_found and not row2[\"content\"] in not_found and row2[\"title\"] is not None:\n",
" row = row2\n",
" else:\n",
" row = row1\n",
"\n",
" urls.append(row[\"url\"])\n",
" titles.append(row[\"title\"])\n",
" contents.append(row[\"content\"])\n",
"\n",
"# Update the DataFrame with the fetched data\n",
"tes[\"url\"] = urls\n",
"tes[\"title\"] = titles\n",
"tes[\"content\"] = contents"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "4926f410",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3694"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows = tes[~tes[\"content\"].isin(not_found)].shape[0]\n",
"count_rows"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "44bff271",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6469352014010508"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows / len(tes)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e573b48a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1945"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows = tes[tes[\"content\"].isin([\"no content found\"])].shape[0]\n",
"count_rows"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "d4a527b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"71"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_rows = tes[tes[\"content\"].isin([\"cannot scrape the content\"])].shape[0]\n",
"count_rows"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "158acaec",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/7x/56svhln929zdh2xhr3mwqg4r0000gn/T/ipykernel_64033/1183224327.py:1: 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",
" tes[\"id\"] = tes[\"id\"].astype(int)\n"
]
}
],
"source": [
"tes[\"id\"] = tes[\"id\"].astype(int)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "154aada9-8e4a-479a-8288-a63b72ae86ad",
"metadata": {},
"outputs": [],
"source": [
"tes.to_csv(\"data/scrapped_data2.csv\", index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|