Spaces:
Running
Running
File size: 42,129 Bytes
a6576f0 |
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "9f7cc561-f375-4cd6-953f-65af221bc1ae",
"metadata": {},
"source": [
"# Keyword Extraction Analysis\n",
"Analyze buzzwords driving sentiment on any given day"
]
},
{
"cell_type": "code",
"execution_count": 461,
"id": "59e2493c-10e5-402f-9875-d07d989cd451",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T02:14:47.665748Z",
"iopub.status.busy": "2025-04-30T02:14:47.665748Z",
"iopub.status.idle": "2025-04-30T02:14:47.674765Z",
"shell.execute_reply": "2025-04-30T02:14:47.673749Z",
"shell.execute_reply.started": "2025-04-30T02:14:47.665748Z"
}
},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "d5b34940-8c46-421b-b00b-0badaca194fc",
"metadata": {},
"source": [
"### Download data from HF Hub"
]
},
{
"cell_type": "code",
"execution_count": 462,
"id": "03be6fde-e68b-4026-8f90-cd6f5d5f21db",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T02:14:49.167723Z",
"iopub.status.busy": "2025-04-30T02:14:49.167723Z",
"iopub.status.idle": "2025-04-30T02:14:50.725451Z",
"shell.execute_reply": "2025-04-30T02:14:50.725451Z",
"shell.execute_reply.started": "2025-04-30T02:14:49.167723Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total records across 16 days: 4520\n"
]
}
],
"source": [
"from huggingface_hub import HfApi\n",
"\n",
"api = HfApi()\n",
"all_files = api.list_repo_files(\"hblim/top_reddit_posts_daily\", repo_type=\"dataset\")\n",
"parquet_files = sorted([f for f in all_files if f.startswith('data_scored') and f.endswith(\".parquet\")])\n",
"\n",
"df = []\n",
"for shard in parquet_files:\n",
" local_path = api.hf_hub_download(repo_id=\"hblim/top_reddit_posts_daily\", filename=shard, repo_type=\"dataset\")\n",
" file_date = os.path.splitext(os.path.basename(local_path))[0]\n",
" df.append(pd.read_parquet(local_path).assign(filedate=file_date))\n",
"df = pd.concat(df, ignore_index=True)\n",
"print(f\"Total records across {df.filedate.nunique()} days: {len(df)}\")"
]
},
{
"cell_type": "code",
"execution_count": 464,
"id": "dbcaf06b-9c29-4913-b3a9-938017eb6ffd",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T02:14:51.655177Z",
"iopub.status.busy": "2025-04-30T02:14:51.654664Z",
"iopub.status.idle": "2025-04-30T02:14:51.669190Z",
"shell.execute_reply": "2025-04-30T02:14:51.669190Z",
"shell.execute_reply.started": "2025-04-30T02:14:51.655177Z"
}
},
"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>subreddit</th>\n",
" <th>created_at</th>\n",
" <th>retrieved_at</th>\n",
" <th>type</th>\n",
" <th>text</th>\n",
" <th>score</th>\n",
" <th>post_id</th>\n",
" <th>parent_id</th>\n",
" <th>sentiment</th>\n",
" <th>confidence</th>\n",
" <th>filedate</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>apple</td>\n",
" <td>2025-04-14 11:19:50-05:00</td>\n",
" <td>2025-04-14 23:44:27.136181-05:00</td>\n",
" <td>post</td>\n",
" <td>iPhone 16e Helps Apple Take Q1 2025 Top Spot in Global Smartphone Market\\n\\n</td>\n",
" <td>655</td>\n",
" <td>1jz2xrw</td>\n",
" <td>None</td>\n",
" <td>1</td>\n",
" <td>0.9971</td>\n",
" <td>2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>apple</td>\n",
" <td>2025-04-14 11:00:16-05:00</td>\n",
" <td>2025-04-14 23:44:27.136181-05:00</td>\n",
" <td>comment</td>\n",
" <td>I've closed all rings every day starting on June 19 2015. This won't be a problem as long as I don't get run over or die.</td>\n",
" <td>9</td>\n",
" <td>mn2wpoi</td>\n",
" <td>t3_1jyzp05</td>\n",
" <td>1</td>\n",
" <td>0.9965</td>\n",
" <td>2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>apple</td>\n",
" <td>2025-04-14 11:59:56-05:00</td>\n",
" <td>2025-04-14 23:44:27.136181-05:00</td>\n",
" <td>post</td>\n",
" <td>Smartphone tariffs are coming back in ‘a month or two,’ says Trump admin\\n\\n</td>\n",
" <td>194</td>\n",
" <td>1jz3wsi</td>\n",
" <td>None</td>\n",
" <td>0</td>\n",
" <td>0.9829</td>\n",
" <td>2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>apple</td>\n",
" <td>2025-04-14 11:59:56-05:00</td>\n",
" <td>2025-04-14 23:44:27.136181-05:00</td>\n",
" <td>comment</td>\n",
" <td>This topic has been automatically locked due to being controversial and/or political by nature. However, the submission itself will remain accessible as long as it is related to Apple.\\n\\n\\nThis decision was made by a bot based on specific keywords. If you feel that this was in error, please report it to the moderators so that it can be reviewed.\\n \\n\\n*I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/apple) if you have any questions or concerns.*</td>\n",
" <td>1</td>\n",
" <td>mn38mac</td>\n",
" <td>t3_1jz3wsi</td>\n",
" <td>0</td>\n",
" <td>0.9972</td>\n",
" <td>2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>apple</td>\n",
" <td>2025-04-14 18:04:42-05:00</td>\n",
" <td>2025-04-14 23:44:27.136181-05:00</td>\n",
" <td>post</td>\n",
" <td>Apple to Analyze User Data on Devices to Bolster AI Technology\\n\\n</td>\n",
" <td>69</td>\n",
" <td>1jzcpwz</td>\n",
" <td>None</td>\n",
" <td>1</td>\n",
" <td>0.9976</td>\n",
" <td>2025-04-14</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subreddit created_at retrieved_at \\\n",
"0 apple 2025-04-14 11:19:50-05:00 2025-04-14 23:44:27.136181-05:00 \n",
"1 apple 2025-04-14 11:00:16-05:00 2025-04-14 23:44:27.136181-05:00 \n",
"2 apple 2025-04-14 11:59:56-05:00 2025-04-14 23:44:27.136181-05:00 \n",
"3 apple 2025-04-14 11:59:56-05:00 2025-04-14 23:44:27.136181-05:00 \n",
"4 apple 2025-04-14 18:04:42-05:00 2025-04-14 23:44:27.136181-05:00 \n",
"\n",
" type \\\n",
"0 post \n",
"1 comment \n",
"2 post \n",
"3 comment \n",
"4 post \n",
"\n",
" text \\\n",
"0 iPhone 16e Helps Apple Take Q1 2025 Top Spot in Global Smartphone Market\\n\\n \n",
"1 I've closed all rings every day starting on June 19 2015. This won't be a problem as long as I don't get run over or die. \n",
"2 Smartphone tariffs are coming back in ‘a month or two,’ says Trump admin\\n\\n \n",
"3 This topic has been automatically locked due to being controversial and/or political by nature. However, the submission itself will remain accessible as long as it is related to Apple.\\n\\n\\nThis decision was made by a bot based on specific keywords. If you feel that this was in error, please report it to the moderators so that it can be reviewed.\\n \\n\\n*I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/apple) if you have any questions or concerns.* \n",
"4 Apple to Analyze User Data on Devices to Bolster AI Technology\\n\\n \n",
"\n",
" score post_id parent_id sentiment confidence filedate \n",
"0 655 1jz2xrw None 1 0.9971 2025-04-14 \n",
"1 9 mn2wpoi t3_1jyzp05 1 0.9965 2025-04-14 \n",
"2 194 1jz3wsi None 0 0.9829 2025-04-14 \n",
"3 1 mn38mac t3_1jz3wsi 0 0.9972 2025-04-14 \n",
"4 69 1jzcpwz None 1 0.9976 2025-04-14 "
]
},
"execution_count": 464,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"id": "958f8e47-37c9-4d53-9d20-41a29a0c2714",
"metadata": {},
"source": [
"### Look at specific subreddit, date"
]
},
{
"cell_type": "code",
"execution_count": 562,
"id": "9f20145f-1ae0-4fa5-ac99-d00d9909cd76",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T02:54:23.431576Z",
"iopub.status.busy": "2025-04-30T02:54:23.431576Z",
"iopub.status.idle": "2025-04-30T02:54:23.436791Z",
"shell.execute_reply": "2025-04-30T02:54:23.436791Z",
"shell.execute_reply.started": "2025-04-30T02:54:23.431576Z"
}
},
"outputs": [],
"source": [
"# 1. Filter your dataframe\n",
"date = '2025-04-14'\n",
"subreddit = 'apple'\n",
"day_sub = (df['filedate'] == date) & (df['subreddit'] == subreddit) "
]
},
{
"cell_type": "code",
"execution_count": 589,
"id": "2c286057-13db-49f4-a5d0-178a5d004b53",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T03:24:38.658484Z",
"iopub.status.busy": "2025-04-30T03:24:38.658484Z",
"iopub.status.idle": "2025-04-30T03:24:38.669335Z",
"shell.execute_reply": "2025-04-30T03:24:38.669335Z",
"shell.execute_reply.started": "2025-04-30T03:24:38.658484Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Daily aggregated sentiment stats\n",
"Community Weighted Sentiment = 0.3353147742165998\n",
"Average Sentiment = 0.43902439024390244\n"
]
}
],
"source": [
"dftest = df[day_sub]\n",
"print(\"Daily aggregated sentiment stats\")\n",
"print(\"Community Weighted Sentiment =\",((2 * dftest['sentiment'] - 1) * np.log1p(dftest['score'].clip(0,None))).mean())\n",
"print(\"Average Sentiment =\",dftest['sentiment'].mean())\n",
"# dftest.sort_values('score',ascending=False)\n",
"# dftest.groupby('parent_id').agg({'sentiment': ['mean','sum','count']})"
]
},
{
"cell_type": "markdown",
"id": "85d4797e-c9dd-45b0-b72f-cb8d279f5ebc",
"metadata": {},
"source": [
"### Use KeyBERT and sentiment transformers model to extract keywords"
]
},
{
"cell_type": "code",
"execution_count": 587,
"id": "8c0a4f6a-d34a-4397-add9-1d68e670eaf7",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T02:59:03.237586Z",
"iopub.status.busy": "2025-04-30T02:59:03.237586Z",
"iopub.status.idle": "2025-04-30T02:59:05.388763Z",
"shell.execute_reply": "2025-04-30T02:59:05.388763Z",
"shell.execute_reply.started": "2025-04-30T02:59:03.237586Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('smartphone market', 0.5024), ('command thanks universal', 0.0662), ('dimensions leak years', 0.0196), ('wwdc non paywall', -0.0052), ('animations new techniques', -0.0178)]\n"
]
}
],
"source": [
"from keybert import KeyBERT\n",
"from sentence_transformers import SentenceTransformer\n",
"import spacy\n",
"\n",
"raw_text = \" \".join(df.loc[day_sub, 'text'].astype(str))\n",
"\n",
"# 2. Load spaCy with parser enabled for noun_chunks\n",
"nlp = spacy.load(\"en_core_web_sm\") # keep the parser on\n",
"doc = nlp(raw_text.lower())\n",
"\n",
"# 3. Build candidate phrases\n",
"candidates = \" \".join(\n",
" [chunk.text for chunk in doc.noun_chunks]\n",
" + [ent.text for ent in doc.ents if ent.label_ in {\"PRODUCT\",\"EVENT\",}]\n",
")\n",
"\n",
"for exclude in ['google','pixel','android','apple','rationale','advice','blog','topic','locked','author','moderator','error','bot','comments','archive','support','discord']:\n",
" candidates = candidates.replace(exclude,' ')\n",
"\n",
"# 4. Keyword extraction with local embeddings\n",
"model = SentenceTransformer(\"all-MiniLM-L6-v2\")\n",
"kw_model = KeyBERT(model)\n",
"keywords = kw_model.extract_keywords(\n",
" candidates,\n",
" keyphrase_ngram_range=(1, 3),\n",
" stop_words=\"english\",\n",
" use_mmr=True,\n",
" diversity=0.9,\n",
" top_n=5\n",
")\n",
"\n",
"print(keywords)\n"
]
},
{
"cell_type": "markdown",
"id": "3ea178ad-676c-4362-b817-e66563dce6de",
"metadata": {},
"source": [
"### Ensure keywords actually match to posts or comments based on cosine similarity"
]
},
{
"cell_type": "code",
"execution_count": 591,
"id": "63e1b61f-cdcb-4c5a-9024-531e26eee495",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T03:27:16.957826Z",
"iopub.status.busy": "2025-04-30T03:27:16.957826Z",
"iopub.status.idle": "2025-04-30T03:27:17.668631Z",
"shell.execute_reply": "2025-04-30T03:27:17.667592Z",
"shell.execute_reply.started": "2025-04-30T03:27:16.957826Z"
}
},
"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>keyword</th>\n",
" <th>mean_sentiment</th>\n",
" <th>community_weighted_sentiment</th>\n",
" <th>n_posts</th>\n",
" <th>total_score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>smartphone market</td>\n",
" <td>-0.076923</td>\n",
" <td>0.841451</td>\n",
" <td>13</td>\n",
" <td>2798</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>dimensions leak years</td>\n",
" <td>-1.000000</td>\n",
" <td>-5.939423</td>\n",
" <td>2</td>\n",
" <td>804</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>animations new techniques</td>\n",
" <td>1.000000</td>\n",
" <td>2.944439</td>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>wwdc non paywall</td>\n",
" <td>-1.000000</td>\n",
" <td>-2.397895</td>\n",
" <td>1</td>\n",
" <td>10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" keyword mean_sentiment community_weighted_sentiment \\\n",
"0 smartphone market -0.076923 0.841451 \n",
"1 dimensions leak years -1.000000 -5.939423 \n",
"2 animations new techniques 1.000000 2.944439 \n",
"3 wwdc non paywall -1.000000 -2.397895 \n",
"\n",
" n_posts total_score \n",
"0 13 2798 \n",
"1 2 804 \n",
"2 1 18 \n",
"3 1 10 "
]
},
"execution_count": 591,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.metrics.pairwise import cosine_similarity\n",
"\n",
"# 1) Precompute embeddings for all texts in your day/subreddit slice\n",
"texts = df.loc[day_sub, 'text'].tolist()\n",
"text_embs = model.encode(texts, convert_to_tensor=False) # shape: (n_texts, 384)\n",
"\n",
"results = []\n",
"subsets = {}\n",
"# if you only want to test on a single kw, iterate keywords_test instead\n",
"for kw, _score in keywords: \n",
" # kw is now a string\n",
" kw_emb = model.encode(kw, convert_to_tensor=False) # shape: (384,)\n",
" kw_emb = kw_emb.reshape(1, -1) # shape: (1, 384)\n",
" \n",
" sims = cosine_similarity(text_embs, kw_emb).flatten() # OK: (n_texts,) vs (1,384)\n",
" \n",
" # rank or threshold as before\n",
" hits = df.loc[day_sub].iloc[sims.argsort()[::-1]]\n",
" mask = sims >= 0.3\n",
" \n",
" subset = df.loc[day_sub].iloc[mask]\n",
" if subset.empty:\n",
" continue\n",
" subsets[kw] = subset\n",
" \n",
" # compute sentiment stats on subset…\n",
" mean_sent = 2 * subset['sentiment'].mean() - 1\n",
" weighted = ((2 * subset['sentiment'] - 1) * np.log1p(subset['score'].clip(0,None))).mean()\n",
" total_score = subset['score'].sum()\n",
" results.append((kw, mean_sent, weighted, len(subset), total_score))\n",
"\n",
"summary = pd.DataFrame(results, columns=[\n",
" 'keyword', 'mean_sentiment', 'community_weighted_sentiment', 'n_posts' , 'total_score'\n",
"]).sort_values('total_score', ascending=False).reset_index(drop=True)\n",
"\n",
"summary"
]
},
{
"cell_type": "markdown",
"id": "fbcbd745-8bbf-49f3-9fac-a4c9a056de6f",
"metadata": {},
"source": [
"### Manually inspect posts and comments associated with the keyword"
]
},
{
"cell_type": "code",
"execution_count": 593,
"id": "1b2dadf2-c2b3-447a-bcdf-aec627635f49",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T03:27:33.238178Z",
"iopub.status.busy": "2025-04-30T03:27:33.237146Z",
"iopub.status.idle": "2025-04-30T03:27:33.245143Z",
"shell.execute_reply": "2025-04-30T03:27:33.245143Z",
"shell.execute_reply.started": "2025-04-30T03:27:33.238178Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"</style>\n",
"<table id=\"T_d6ecc\">\n",
" <caption>KEYWORD = smartphone market</caption>\n",
" <thead>\n",
" <tr>\n",
" <th class=\"blank level0\" > </th>\n",
" <th id=\"T_d6ecc_level0_col0\" class=\"col_heading level0 col0\" >subreddit</th>\n",
" <th id=\"T_d6ecc_level0_col1\" class=\"col_heading level0 col1\" >created_at</th>\n",
" <th id=\"T_d6ecc_level0_col2\" class=\"col_heading level0 col2\" >retrieved_at</th>\n",
" <th id=\"T_d6ecc_level0_col3\" class=\"col_heading level0 col3\" >type</th>\n",
" <th id=\"T_d6ecc_level0_col4\" class=\"col_heading level0 col4\" >text</th>\n",
" <th id=\"T_d6ecc_level0_col5\" class=\"col_heading level0 col5\" >score</th>\n",
" <th id=\"T_d6ecc_level0_col6\" class=\"col_heading level0 col6\" >post_id</th>\n",
" <th id=\"T_d6ecc_level0_col7\" class=\"col_heading level0 col7\" >parent_id</th>\n",
" <th id=\"T_d6ecc_level0_col8\" class=\"col_heading level0 col8\" >sentiment</th>\n",
" <th id=\"T_d6ecc_level0_col9\" class=\"col_heading level0 col9\" >confidence</th>\n",
" <th id=\"T_d6ecc_level0_col10\" class=\"col_heading level0 col10\" >filedate</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th id=\"T_d6ecc_level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
" <td id=\"T_d6ecc_row0_col0\" class=\"data row0 col0\" >apple</td>\n",
" <td id=\"T_d6ecc_row0_col1\" class=\"data row0 col1\" >2025-04-14 11:19:50-05:00</td>\n",
" <td id=\"T_d6ecc_row0_col2\" class=\"data row0 col2\" >2025-04-14 23:44:27.136181-05:00</td>\n",
" <td id=\"T_d6ecc_row0_col3\" class=\"data row0 col3\" >post</td>\n",
" <td id=\"T_d6ecc_row0_col4\" class=\"data row0 col4\" >iPhone 16e Helps Apple Take Q1 2025 Top Spot in Global Smartphone Market\n",
"\n",
"</td>\n",
" <td id=\"T_d6ecc_row0_col5\" class=\"data row0 col5\" >655</td>\n",
" <td id=\"T_d6ecc_row0_col6\" class=\"data row0 col6\" >1jz2xrw</td>\n",
" <td id=\"T_d6ecc_row0_col7\" class=\"data row0 col7\" >None</td>\n",
" <td id=\"T_d6ecc_row0_col8\" class=\"data row0 col8\" >1</td>\n",
" <td id=\"T_d6ecc_row0_col9\" class=\"data row0 col9\" >0.997100</td>\n",
" <td id=\"T_d6ecc_row0_col10\" class=\"data row0 col10\" >2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_d6ecc_level0_row1\" class=\"row_heading level0 row1\" >2</th>\n",
" <td id=\"T_d6ecc_row1_col0\" class=\"data row1 col0\" >apple</td>\n",
" <td id=\"T_d6ecc_row1_col1\" class=\"data row1 col1\" >2025-04-14 11:59:56-05:00</td>\n",
" <td id=\"T_d6ecc_row1_col2\" class=\"data row1 col2\" >2025-04-14 23:44:27.136181-05:00</td>\n",
" <td id=\"T_d6ecc_row1_col3\" class=\"data row1 col3\" >post</td>\n",
" <td id=\"T_d6ecc_row1_col4\" class=\"data row1 col4\" >Smartphone tariffs are coming back in ‘a month or two,’ says Trump admin\n",
"\n",
"</td>\n",
" <td id=\"T_d6ecc_row1_col5\" class=\"data row1 col5\" >194</td>\n",
" <td id=\"T_d6ecc_row1_col6\" class=\"data row1 col6\" >1jz3wsi</td>\n",
" <td id=\"T_d6ecc_row1_col7\" class=\"data row1 col7\" >None</td>\n",
" <td id=\"T_d6ecc_row1_col8\" class=\"data row1 col8\" >0</td>\n",
" <td id=\"T_d6ecc_row1_col9\" class=\"data row1 col9\" >0.982900</td>\n",
" <td id=\"T_d6ecc_row1_col10\" class=\"data row1 col10\" >2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_d6ecc_level0_row2\" class=\"row_heading level0 row2\" >3</th>\n",
" <td id=\"T_d6ecc_row2_col0\" class=\"data row2 col0\" >apple</td>\n",
" <td id=\"T_d6ecc_row2_col1\" class=\"data row2 col1\" >2025-04-14 11:59:56-05:00</td>\n",
" <td id=\"T_d6ecc_row2_col2\" class=\"data row2 col2\" >2025-04-14 23:44:27.136181-05:00</td>\n",
" <td id=\"T_d6ecc_row2_col3\" class=\"data row2 col3\" >comment</td>\n",
" <td id=\"T_d6ecc_row2_col4\" class=\"data row2 col4\" >This topic has been automatically locked due to being controversial and/or political by nature. However, the submission itself will remain accessible as long as it is related to Apple.\n",
"\n",
"\n",
"This decision was made by a bot based on specific keywords. If you feel that this was in error, please report it to the moderators so that it can be reviewed.\n",
" \n",
"\n",
"*I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/apple) if you have any questions or concerns.*</td>\n",
" <td id=\"T_d6ecc_row2_col5\" class=\"data row2 col5\" >1</td>\n",
" <td id=\"T_d6ecc_row2_col6\" class=\"data row2 col6\" >mn38mac</td>\n",
" <td id=\"T_d6ecc_row2_col7\" class=\"data row2 col7\" >t3_1jz3wsi</td>\n",
" <td id=\"T_d6ecc_row2_col8\" class=\"data row2 col8\" >0</td>\n",
" <td id=\"T_d6ecc_row2_col9\" class=\"data row2 col9\" >0.997200</td>\n",
" <td id=\"T_d6ecc_row2_col10\" class=\"data row2 col10\" >2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_d6ecc_level0_row3\" class=\"row_heading level0 row3\" >23</th>\n",
" <td id=\"T_d6ecc_row3_col0\" class=\"data row3 col0\" >apple</td>\n",
" <td id=\"T_d6ecc_row3_col1\" class=\"data row3 col1\" >2025-04-14 11:43:39-05:00</td>\n",
" <td id=\"T_d6ecc_row3_col2\" class=\"data row3 col2\" >2025-04-14 23:44:27.136181-05:00</td>\n",
" <td id=\"T_d6ecc_row3_col3\" class=\"data row3 col3\" >comment</td>\n",
" <td id=\"T_d6ecc_row3_col4\" class=\"data row3 col4\" >My boss purchased me a 16e to use for iOS development. It may not make much sense for end users but it is a nearly perfect corporate phone.</td>\n",
" <td id=\"T_d6ecc_row3_col5\" class=\"data row3 col5\" >309</td>\n",
" <td id=\"T_d6ecc_row3_col6\" class=\"data row3 col6\" >mn35a6r</td>\n",
" <td id=\"T_d6ecc_row3_col7\" class=\"data row3 col7\" >t3_1jz2xrw</td>\n",
" <td id=\"T_d6ecc_row3_col8\" class=\"data row3 col8\" >1</td>\n",
" <td id=\"T_d6ecc_row3_col9\" class=\"data row3 col9\" >0.998600</td>\n",
" <td id=\"T_d6ecc_row3_col10\" class=\"data row3 col10\" >2025-04-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_d6ecc_level0_row4\" class=\"row_heading level0 row4\" >24</th>\n",
" <td id=\"T_d6ecc_row4_col0\" class=\"data row4 col0\" >apple</td>\n",
" <td id=\"T_d6ecc_row4_col1\" class=\"data row4 col1\" >2025-04-14 11:24:30-05:00</td>\n",
" <td id=\"T_d6ecc_row4_col2\" class=\"data row4 col2\" >2025-04-14 23:44:27.136181-05:00</td>\n",
" <td id=\"T_d6ecc_row4_col3\" class=\"data row4 col3\" >comment</td>\n",
" <td id=\"T_d6ecc_row4_col4\" class=\"data row4 col4\" >Despite what the tech influencers might have said, Apple clearly knows what they’re doing.</td>\n",
" <td id=\"T_d6ecc_row4_col5\" class=\"data row4 col5\" >283</td>\n",
" <td id=\"T_d6ecc_row4_col6\" class=\"data row4 col6\" >mn31i1a</td>\n",
" <td id=\"T_d6ecc_row4_col7\" class=\"data row4 col7\" >t3_1jz2xrw</td>\n",
" <td id=\"T_d6ecc_row4_col8\" class=\"data row4 col8\" >1</td>\n",
" <td id=\"T_d6ecc_row4_col9\" class=\"data row4 col9\" >0.999600</td>\n",
" <td id=\"T_d6ecc_row4_col10\" class=\"data row4 col10\" >2025-04-14</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x26d025bdb80>"
]
},
"execution_count": 593,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"keyword_index = 0\n",
"subsets[summary.keyword[keyword_index]].head().style.set_caption(f\"KEYWORD = {summary.keyword[keyword_index]}\")"
]
},
{
"cell_type": "markdown",
"id": "1f8adbfe-3141-417f-bc13-922b7f1098a7",
"metadata": {},
"source": [
"### Helper tool: Retrieve post and comments by post_id"
]
},
{
"cell_type": "code",
"execution_count": 534,
"id": "ea794d80-bf98-47c7-877d-1b2e6a626b12",
"metadata": {
"execution": {
"iopub.execute_input": "2025-04-30T02:46:15.966447Z",
"iopub.status.busy": "2025-04-30T02:46:15.966447Z",
"iopub.status.idle": "2025-04-30T02:46:15.979590Z",
"shell.execute_reply": "2025-04-30T02:46:15.979590Z",
"shell.execute_reply.started": "2025-04-30T02:46:15.966447Z"
}
},
"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>subreddit</th>\n",
" <th>created_at</th>\n",
" <th>retrieved_at</th>\n",
" <th>type</th>\n",
" <th>text</th>\n",
" <th>score</th>\n",
" <th>post_id</th>\n",
" <th>parent_id</th>\n",
" <th>sentiment</th>\n",
" <th>confidence</th>\n",
" <th>filedate</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2748</th>\n",
" <td>Android</td>\n",
" <td>2025-04-23 08:15:55-05:00</td>\n",
" <td>2025-04-23 19:03:18.888116-05:00</td>\n",
" <td>post</td>\n",
" <td>The new feature that gives higher memory priority to background tabs containing user edits, such as fillable forms or drafts (reducing the chance of them being killed and thus not losing your progress) is now available in Chrome Canary for Android.\\n\\n</td>\n",
" <td>224</td>\n",
" <td>1k5ywd6</td>\n",
" <td>None</td>\n",
" <td>0</td>\n",
" <td>0.9717</td>\n",
" <td>2025-04-23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2749</th>\n",
" <td>Android</td>\n",
" <td>2025-04-23 08:43:37-05:00</td>\n",
" <td>2025-04-23 19:03:18.888116-05:00</td>\n",
" <td>comment</td>\n",
" <td>Android's task refreshing is so bad and random that I've adapted my whole workflow around it by simply never trusting it and constantly copying whatever I input. If I write something and need switch away to another app even for a second, I copy the text before I do it. \\n\\nAndroid still does this even if you have 16GB of RAM!</td>\n",
" <td>1</td>\n",
" <td>molv84l</td>\n",
" <td>t3_1k5ywd6</td>\n",
" <td>0</td>\n",
" <td>0.9996</td>\n",
" <td>2025-04-23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2750</th>\n",
" <td>Android</td>\n",
" <td>2025-04-23 08:19:42-05:00</td>\n",
" <td>2025-04-23 19:03:18.888116-05:00</td>\n",
" <td>comment</td>\n",
" <td>I love that \"it reduces the chance\" but it doesn't eliminate the chance something I am working on it is killed...</td>\n",
" <td>1</td>\n",
" <td>molr08u</td>\n",
" <td>t3_1k5ywd6</td>\n",
" <td>1</td>\n",
" <td>0.9835</td>\n",
" <td>2025-04-23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2751</th>\n",
" <td>Android</td>\n",
" <td>2025-04-23 08:17:05-05:00</td>\n",
" <td>2025-04-23 19:03:18.888116-05:00</td>\n",
" <td>comment</td>\n",
" <td>Context: [**Background tabs containing user edits, such as filled forms or drafts, will soon have a higher memory priority in Chrome for Android, this will reduce the likelihood of these tabs been killed prematurely.**](https://old.reddit.com/r/Android/comments/1j3ktpg/background_tabs_containing_user_edits_such_as/)\\n\\n.\\n\\nThe patch responsible for this change [**was merged yesterday.**](https://chromium-review.googlesource.com/c/chromium/src/+/6321765)</td>\n",
" <td>1</td>\n",
" <td>molqjut</td>\n",
" <td>t3_1k5ywd6</td>\n",
" <td>0</td>\n",
" <td>0.9996</td>\n",
" <td>2025-04-23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2752</th>\n",
" <td>Android</td>\n",
" <td>2025-04-23 12:13:43-05:00</td>\n",
" <td>2025-04-23 19:03:18.888116-05:00</td>\n",
" <td>comment</td>\n",
" <td>Would love it if Android would let me \"pin\" apps by default that I didn't want to come out of memory. Would be amazing for apps that are slow to re-open.</td>\n",
" <td>1</td>\n",
" <td>mon1t9v</td>\n",
" <td>t3_1k5ywd6</td>\n",
" <td>0</td>\n",
" <td>0.9928</td>\n",
" <td>2025-04-23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2753</th>\n",
" <td>Android</td>\n",
" <td>2025-04-23 11:59:23-05:00</td>\n",
" <td>2025-04-23 19:03:18.888116-05:00</td>\n",
" <td>comment</td>\n",
" <td>Solid upgrade for Android users.</td>\n",
" <td>1</td>\n",
" <td>momytb8</td>\n",
" <td>t3_1k5ywd6</td>\n",
" <td>1</td>\n",
" <td>0.9996</td>\n",
" <td>2025-04-23</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subreddit created_at retrieved_at \\\n",
"2748 Android 2025-04-23 08:15:55-05:00 2025-04-23 19:03:18.888116-05:00 \n",
"2749 Android 2025-04-23 08:43:37-05:00 2025-04-23 19:03:18.888116-05:00 \n",
"2750 Android 2025-04-23 08:19:42-05:00 2025-04-23 19:03:18.888116-05:00 \n",
"2751 Android 2025-04-23 08:17:05-05:00 2025-04-23 19:03:18.888116-05:00 \n",
"2752 Android 2025-04-23 12:13:43-05:00 2025-04-23 19:03:18.888116-05:00 \n",
"2753 Android 2025-04-23 11:59:23-05:00 2025-04-23 19:03:18.888116-05:00 \n",
"\n",
" type \\\n",
"2748 post \n",
"2749 comment \n",
"2750 comment \n",
"2751 comment \n",
"2752 comment \n",
"2753 comment \n",
"\n",
" text \\\n",
"2748 The new feature that gives higher memory priority to background tabs containing user edits, such as fillable forms or drafts (reducing the chance of them being killed and thus not losing your progress) is now available in Chrome Canary for Android.\\n\\n \n",
"2749 Android's task refreshing is so bad and random that I've adapted my whole workflow around it by simply never trusting it and constantly copying whatever I input. If I write something and need switch away to another app even for a second, I copy the text before I do it. \\n\\nAndroid still does this even if you have 16GB of RAM! \n",
"2750 I love that \"it reduces the chance\" but it doesn't eliminate the chance something I am working on it is killed... \n",
"2751 Context: [**Background tabs containing user edits, such as filled forms or drafts, will soon have a higher memory priority in Chrome for Android, this will reduce the likelihood of these tabs been killed prematurely.**](https://old.reddit.com/r/Android/comments/1j3ktpg/background_tabs_containing_user_edits_such_as/)\\n\\n.\\n\\nThe patch responsible for this change [**was merged yesterday.**](https://chromium-review.googlesource.com/c/chromium/src/+/6321765) \n",
"2752 Would love it if Android would let me \"pin\" apps by default that I didn't want to come out of memory. Would be amazing for apps that are slow to re-open. \n",
"2753 Solid upgrade for Android users. \n",
"\n",
" score post_id parent_id sentiment confidence filedate \n",
"2748 224 1k5ywd6 None 0 0.9717 2025-04-23 \n",
"2749 1 molv84l t3_1k5ywd6 0 0.9996 2025-04-23 \n",
"2750 1 molr08u t3_1k5ywd6 1 0.9835 2025-04-23 \n",
"2751 1 molqjut t3_1k5ywd6 0 0.9996 2025-04-23 \n",
"2752 1 mon1t9v t3_1k5ywd6 0 0.9928 2025-04-23 \n",
"2753 1 momytb8 t3_1k5ywd6 1 0.9996 2025-04-23 "
]
},
"execution_count": 534,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"postid = '1k5ywd6'\n",
"df[lambda x: ((x.post_id == postid) | (x.parent_id == f't3_{postid}'))]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:reddit_streamlit]",
"language": "python",
"name": "conda-env-reddit_streamlit-py"
},
"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
}
|