File size: 48,455 Bytes
391de07 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2b931208",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.document_loaders import TextLoader\n",
"from langchain_text_splitters import CharacterTextSplitter\n",
"from langchain_chroma import Chroma"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1adaad97",
"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>isbn13</th>\n",
" <th>isbn10</th>\n",
" <th>title</th>\n",
" <th>authors</th>\n",
" <th>categories</th>\n",
" <th>thumbnail</th>\n",
" <th>description</th>\n",
" <th>published_year</th>\n",
" <th>average_rating</th>\n",
" <th>num_pages</th>\n",
" <th>ratings_count</th>\n",
" <th>agg_of_book</th>\n",
" <th>title_and_subtitle</th>\n",
" <th>tagged_description</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>9780002005883</td>\n",
" <td>0002005883</td>\n",
" <td>Gilead</td>\n",
" <td>Marilynne Robinson</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=KQZCP...</td>\n",
" <td>A NOVEL THAT READERS and critics have been eag...</td>\n",
" <td>2004.0</td>\n",
" <td>3.85</td>\n",
" <td>247.0</td>\n",
" <td>361.0</td>\n",
" <td>21.0</td>\n",
" <td>Gilead</td>\n",
" <td>9780002005883 A NOVEL THAT READERS and critics...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>9780002261982</td>\n",
" <td>0002261987</td>\n",
" <td>Spider's Web</td>\n",
" <td>Charles Osborne;Agatha Christie</td>\n",
" <td>Detective and mystery stories</td>\n",
" <td>http://books.google.com/books/content?id=gA5GP...</td>\n",
" <td>A new 'Christie for Christmas' -- a full-lengt...</td>\n",
" <td>2000.0</td>\n",
" <td>3.83</td>\n",
" <td>241.0</td>\n",
" <td>5164.0</td>\n",
" <td>25.0</td>\n",
" <td>Spider's Web: A Novel</td>\n",
" <td>9780002261982 A new 'Christie for Christmas' -...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>9780006178736</td>\n",
" <td>0006178731</td>\n",
" <td>Rage of angels</td>\n",
" <td>Sidney Sheldon</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=FKo2T...</td>\n",
" <td>A memorable, mesmerizing heroine Jennifer -- b...</td>\n",
" <td>1993.0</td>\n",
" <td>3.93</td>\n",
" <td>512.0</td>\n",
" <td>29532.0</td>\n",
" <td>32.0</td>\n",
" <td>Rage of angels</td>\n",
" <td>9780006178736 A memorable, mesmerizing heroine...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>9780006280897</td>\n",
" <td>0006280897</td>\n",
" <td>The Four Loves</td>\n",
" <td>Clive Staples Lewis</td>\n",
" <td>Christian life</td>\n",
" <td>http://books.google.com/books/content?id=XhQ5X...</td>\n",
" <td>Lewis' work on the nature of love divides love...</td>\n",
" <td>2002.0</td>\n",
" <td>4.15</td>\n",
" <td>170.0</td>\n",
" <td>33684.0</td>\n",
" <td>23.0</td>\n",
" <td>The Four Loves</td>\n",
" <td>9780006280897 Lewis' work on the nature of lov...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>9780006280934</td>\n",
" <td>0006280935</td>\n",
" <td>The Problem of Pain</td>\n",
" <td>Clive Staples Lewis</td>\n",
" <td>Christian life</td>\n",
" <td>http://books.google.com/books/content?id=Kk-uV...</td>\n",
" <td>\"In The Problem of Pain, C.S. Lewis, one of th...</td>\n",
" <td>2002.0</td>\n",
" <td>4.09</td>\n",
" <td>176.0</td>\n",
" <td>37569.0</td>\n",
" <td>23.0</td>\n",
" <td>The Problem of Pain</td>\n",
" <td>9780006280934 \"In The Problem of Pain, C.S. Le...</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",
" </tr>\n",
" <tr>\n",
" <th>5192</th>\n",
" <td>9788172235222</td>\n",
" <td>8172235224</td>\n",
" <td>Mistaken Identity</td>\n",
" <td>Nayantara Sahgal</td>\n",
" <td>Indic fiction (English)</td>\n",
" <td>http://books.google.com/books/content?id=q-tKP...</td>\n",
" <td>On A Train Journey Home To North India After L...</td>\n",
" <td>2003.0</td>\n",
" <td>2.93</td>\n",
" <td>324.0</td>\n",
" <td>0.0</td>\n",
" <td>22.0</td>\n",
" <td>Mistaken Identity</td>\n",
" <td>9788172235222 On A Train Journey Home To North...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5193</th>\n",
" <td>9788173031014</td>\n",
" <td>8173031010</td>\n",
" <td>Journey to the East</td>\n",
" <td>Hermann Hesse</td>\n",
" <td>Adventure stories</td>\n",
" <td>http://books.google.com/books/content?id=rq6JP...</td>\n",
" <td>This book tells the tale of a man who goes on ...</td>\n",
" <td>2002.0</td>\n",
" <td>3.70</td>\n",
" <td>175.0</td>\n",
" <td>24.0</td>\n",
" <td>23.0</td>\n",
" <td>Journey to the East</td>\n",
" <td>9788173031014 This book tells the tale of a ma...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5194</th>\n",
" <td>9788179921623</td>\n",
" <td>817992162X</td>\n",
" <td>The Monk Who Sold His Ferrari: A Fable About F...</td>\n",
" <td>Robin Sharma</td>\n",
" <td>Health & Fitness</td>\n",
" <td>http://books.google.com/books/content?id=c_7mf...</td>\n",
" <td>Wisdom to Create a Life of Passion, Purpose, a...</td>\n",
" <td>2003.0</td>\n",
" <td>3.82</td>\n",
" <td>198.0</td>\n",
" <td>1568.0</td>\n",
" <td>22.0</td>\n",
" <td>The Monk Who Sold His Ferrari: A Fable About F...</td>\n",
" <td>9788179921623 Wisdom to Create a Life of Passi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5195</th>\n",
" <td>9788185300535</td>\n",
" <td>8185300534</td>\n",
" <td>I Am that</td>\n",
" <td>Sri Nisargadatta Maharaj;Sudhakar S. Dikshit</td>\n",
" <td>Philosophy</td>\n",
" <td>http://books.google.com/books/content?id=Fv_JP...</td>\n",
" <td>This collection of the timeless teachings of o...</td>\n",
" <td>1999.0</td>\n",
" <td>4.51</td>\n",
" <td>531.0</td>\n",
" <td>104.0</td>\n",
" <td>26.0</td>\n",
" <td>I Am that: Talks with Sri Nisargadatta Maharaj</td>\n",
" <td>9788185300535 This collection of the timeless ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5196</th>\n",
" <td>9789027712059</td>\n",
" <td>9027712050</td>\n",
" <td>The Berlin Phenomenology</td>\n",
" <td>Georg Wilhelm Friedrich Hegel</td>\n",
" <td>History</td>\n",
" <td>http://books.google.com/books/content?id=Vy7Sk...</td>\n",
" <td>Since the three volume edition ofHegel's Philo...</td>\n",
" <td>1981.0</td>\n",
" <td>0.00</td>\n",
" <td>210.0</td>\n",
" <td>0.0</td>\n",
" <td>44.0</td>\n",
" <td>The Berlin Phenomenology</td>\n",
" <td>9789027712059 Since the three volume edition o...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5197 rows × 14 columns</p>\n",
"</div>"
],
"text/plain": [
" isbn13 isbn10 \\\n",
"0 9780002005883 0002005883 \n",
"1 9780002261982 0002261987 \n",
"2 9780006178736 0006178731 \n",
"3 9780006280897 0006280897 \n",
"4 9780006280934 0006280935 \n",
"... ... ... \n",
"5192 9788172235222 8172235224 \n",
"5193 9788173031014 8173031010 \n",
"5194 9788179921623 817992162X \n",
"5195 9788185300535 8185300534 \n",
"5196 9789027712059 9027712050 \n",
"\n",
" title \\\n",
"0 Gilead \n",
"1 Spider's Web \n",
"2 Rage of angels \n",
"3 The Four Loves \n",
"4 The Problem of Pain \n",
"... ... \n",
"5192 Mistaken Identity \n",
"5193 Journey to the East \n",
"5194 The Monk Who Sold His Ferrari: A Fable About F... \n",
"5195 I Am that \n",
"5196 The Berlin Phenomenology \n",
"\n",
" authors \\\n",
"0 Marilynne Robinson \n",
"1 Charles Osborne;Agatha Christie \n",
"2 Sidney Sheldon \n",
"3 Clive Staples Lewis \n",
"4 Clive Staples Lewis \n",
"... ... \n",
"5192 Nayantara Sahgal \n",
"5193 Hermann Hesse \n",
"5194 Robin Sharma \n",
"5195 Sri Nisargadatta Maharaj;Sudhakar S. Dikshit \n",
"5196 Georg Wilhelm Friedrich Hegel \n",
"\n",
" categories \\\n",
"0 Fiction \n",
"1 Detective and mystery stories \n",
"2 Fiction \n",
"3 Christian life \n",
"4 Christian life \n",
"... ... \n",
"5192 Indic fiction (English) \n",
"5193 Adventure stories \n",
"5194 Health & Fitness \n",
"5195 Philosophy \n",
"5196 History \n",
"\n",
" thumbnail \\\n",
"0 http://books.google.com/books/content?id=KQZCP... \n",
"1 http://books.google.com/books/content?id=gA5GP... \n",
"2 http://books.google.com/books/content?id=FKo2T... \n",
"3 http://books.google.com/books/content?id=XhQ5X... \n",
"4 http://books.google.com/books/content?id=Kk-uV... \n",
"... ... \n",
"5192 http://books.google.com/books/content?id=q-tKP... \n",
"5193 http://books.google.com/books/content?id=rq6JP... \n",
"5194 http://books.google.com/books/content?id=c_7mf... \n",
"5195 http://books.google.com/books/content?id=Fv_JP... \n",
"5196 http://books.google.com/books/content?id=Vy7Sk... \n",
"\n",
" description published_year \\\n",
"0 A NOVEL THAT READERS and critics have been eag... 2004.0 \n",
"1 A new 'Christie for Christmas' -- a full-lengt... 2000.0 \n",
"2 A memorable, mesmerizing heroine Jennifer -- b... 1993.0 \n",
"3 Lewis' work on the nature of love divides love... 2002.0 \n",
"4 \"In The Problem of Pain, C.S. Lewis, one of th... 2002.0 \n",
"... ... ... \n",
"5192 On A Train Journey Home To North India After L... 2003.0 \n",
"5193 This book tells the tale of a man who goes on ... 2002.0 \n",
"5194 Wisdom to Create a Life of Passion, Purpose, a... 2003.0 \n",
"5195 This collection of the timeless teachings of o... 1999.0 \n",
"5196 Since the three volume edition ofHegel's Philo... 1981.0 \n",
"\n",
" average_rating num_pages ratings_count agg_of_book \\\n",
"0 3.85 247.0 361.0 21.0 \n",
"1 3.83 241.0 5164.0 25.0 \n",
"2 3.93 512.0 29532.0 32.0 \n",
"3 4.15 170.0 33684.0 23.0 \n",
"4 4.09 176.0 37569.0 23.0 \n",
"... ... ... ... ... \n",
"5192 2.93 324.0 0.0 22.0 \n",
"5193 3.70 175.0 24.0 23.0 \n",
"5194 3.82 198.0 1568.0 22.0 \n",
"5195 4.51 531.0 104.0 26.0 \n",
"5196 0.00 210.0 0.0 44.0 \n",
"\n",
" title_and_subtitle \\\n",
"0 Gilead \n",
"1 Spider's Web: A Novel \n",
"2 Rage of angels \n",
"3 The Four Loves \n",
"4 The Problem of Pain \n",
"... ... \n",
"5192 Mistaken Identity \n",
"5193 Journey to the East \n",
"5194 The Monk Who Sold His Ferrari: A Fable About F... \n",
"5195 I Am that: Talks with Sri Nisargadatta Maharaj \n",
"5196 The Berlin Phenomenology \n",
"\n",
" tagged_description \n",
"0 9780002005883 A NOVEL THAT READERS and critics... \n",
"1 9780002261982 A new 'Christie for Christmas' -... \n",
"2 9780006178736 A memorable, mesmerizing heroine... \n",
"3 9780006280897 Lewis' work on the nature of lov... \n",
"4 9780006280934 \"In The Problem of Pain, C.S. Le... \n",
"... ... \n",
"5192 9788172235222 On A Train Journey Home To North... \n",
"5193 9788173031014 This book tells the tale of a ma... \n",
"5194 9788179921623 Wisdom to Create a Life of Passi... \n",
"5195 9788185300535 This collection of the timeless ... \n",
"5196 9789027712059 Since the three volume edition o... \n",
"\n",
"[5197 rows x 14 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"books = pd.read_csv(\"books_cleaned.csv\")\n",
"books"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4144c4c6",
"metadata": {},
"outputs": [],
"source": [
"books[\"tagged_description\"].to_csv(\"tagged_description.txt\",\n",
" sep = \"\\n\",\n",
" index = False,\n",
" header = False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80bd0392",
"metadata": {},
"outputs": [],
"source": [
"raw_documents = TextLoader(\"tagged_description.txt\", encoding='utf-8').load()\n",
"text_splitter = CharacterTextSplitter(chunk_size=0, chunk_overlap=0, separator=\"\\n\")\n",
"documents = text_splitter.split_documents(raw_documents)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ddbb69ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Document(metadata={'source': 'tagged_description.txt'}, page_content='9780002005883 A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives. John Ames is a preacher, the son of a preacher and the grandson (both maternal and paternal) of preachers. It’s 1956 in Gilead, Iowa, towards the end of the Reverend Ames’s life, and he is absorbed in recording his family’s story, a legacy for the young son he will never see grow up. Haunted by his grandfather’s presence, John tells of the rift between his grandfather and his father: the elder, an angry visionary who fought for the abolitionist cause, and his son, an ardent pacifist. He is troubled, too, by his prodigal namesake, Jack (John Ames) Boughton, his best friend’s lost son who returns to Gilead searching for forgiveness and redemption. Told in John Ames’s joyous, rambling voice that finds beauty, humour and truth in the smallest of life’s details, Gilead is a song of celebration and acceptance of the best and the worst the world has to offer. At its heart is a tale of the sacred bonds between fathers and sons, pitch-perfect in style and story, set to dazzle critics and readers alike.')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documents[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bc1e564",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\0105725U\\Documents\\Personal\\Book-recommender\\books_env\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"c:\\Users\\0105725U\\Documents\\Personal\\Book-recommender\\books_env\\Lib\\site-packages\\huggingface_hub\\file_download.py:143: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\\Users\\0105725U\\.cache\\huggingface\\hub\\models--sentence-transformers--paraphrase-MiniLM-L3-v2. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.\n",
"To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development\n",
" warnings.warn(message)\n",
"Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n"
]
}
],
"source": [
"from langchain_community.embeddings import HuggingFaceEmbeddings\n",
"from langchain.vectorstores import Chroma\n",
"\n",
"embedding_model = HuggingFaceEmbeddings(model_name=\"sentence-transformers/paraphrase-MiniLM-L3-v2\")\n",
"\n",
"db_books = Chroma.from_documents(\n",
" documents,\n",
" embedding=embedding_model\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8ed611b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Document(metadata={'source': 'tagged_description.txt'}, page_content=\"9780941807555 THE LITTLE BIG BOOK FOR GOD'S CHILDREN is a wonderful resource for parents looking to introduce their children to the wisdom and beauty of Christian faith. Illustrated with early twentieth-century art from artists such as Jessie Wilcox Smith, Ellen H. Clapsaddle, and Maud and Miska Petersham, this Little Big Book presents selected bible stories, parables, and verses; poems and prayers; excerpts and fables; songs and hymns; and activities - all collected in one delightful volume. A celebration of God's enduring message of love, GOD'S CHILDREN is sure to provide an endless wellspring of inspiration and hope.\"),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='9788122200850 This book is the story of a young girl obsessed by a childhood prophecy of disaster. The author builds up an atmosphere of tension and oppression, in the middle of an Indian summer.'),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='9780374422080 This Newbery Honor Book tells the story of 11 -year-old Primrose, who lives in a small fishing village in British Columbia. She recounts her experiences and all she learns about human nature and the unpredictability of life after her parents are lost at sea.'),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content=\"9780802431486 Drawn from years of counseling and the author's own parenting experiences, this valuable resource, filled with Scripture passages and accompanying practical applications, provides principles for raising and nurturing children in these uncertain and rapidly changing times. Original.\"),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='9780374299194 The author describes growing up in a family of all boys in Webster Groves, Missouri, reflecting on such topics as the dynamics of a Christian youth fellowship, his role as the school prankster, his marriage, and the life lessons he has learned from birds.'),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='9780671631987 With more than half a million copies in print, Teach Your Child to Read in 100 Easy Lessons is the definitive guide to giving your child the reading skills needed now for a better chance at tomorrow, while bringing you and your child closer together. Is your child halfway through first grade and still unable to read? Is your preschooler bored with coloring and ready for reading? Do you want to help your child read, but are afraid you’ll do something wrong? Teach Your Child to Read in 100 Easy Lessons is a complete, step-by-step program that shows patents simply and clearly how to teach their children to read. Twenty minutes a day is all you need, and within 100 teaching days your child will be reading on a solid second-grade reading level. It’s a sensible, easy-to-follow, and enjoyable way to help your child gain the essential skills of reading. Everything you need is here—no paste, no scissors, no flash cards, no complicated directions—just you and your child learning together. One hundred lessons, fully illustrated and color-coded for clarity, give your child the basic and more advanced skills needed to become a good reader.'),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='9780143037392 Explains the importance of reading aloud to children, offers guidance on how to set up a read-aloud atmosphere in the home or classroom, and recommends titles to select.'),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content=\"9780064406925 The Little House books tell the story of a little pioneer girl and her family as they traveled by covered wagon across the Midwest. Laura Ingalls Wilder's classic books, illustrated with Garth Williams' timeless artwork, have been cherished by millions of readers ever since they were first published over sixty years ago. This My First Little House Book introduces Almanzo Wilder, the young boy from Farmer Boy who would one day marry Laura Ingalls. In Winter on the Farm, Almanzo goes through his afternoon barn chores, and then sits down to eat a hearty farm supper with his family. In this first book about Almanzo, young readers are able to share in the warmth and joy of another Little House family and celebrate new Little House adventures. Winter on the Farm is the first in an ongoing series about Laura’s beloved farmer boy.\"),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='\"9780064462341 The pioneer spirit lives on... Readers around the world know and love Laura, the little girl born in the Big Woods of Wisconsin and raised in covered wagons and on wide open prairies. Now Little House fans can learn more about \"\"Half-pint\"\" in this, the first picture book biography book of Laura Ingalls Wilder. With a simple, glowing text by noted historian and Little House scholar William Anderson, and glorious paintings by Dan Andreasen, Pioneer Girl is a very special portrait of a writer whose classic books and poineer adventures have made her one of the most popular literary figures in America. This picture-book biography of Laura Ingalls Wilder tells the remarkable story of the pioneer girl who would one day immortalize her adventures in the beloved Little House books. Written in simple, glowing text by noted Little House scholar William Anderson, and illustrated with glorious paintings by artist Dan Andreasen, this wonderful first biography captures the very essence of the little girl called ‘Half-pint,\\' whose classic books and pioneer adventures have made her one of the most popular literary figures in America. This picture-book biography of Laura Ingalls Wilder tells the remarkable story of the pioneer girl who would one day immortalize her adventures in the beloved Little House books. Written in simple, glowing text by noted Little House scholar William Anderson, and illustrated with glorious paintings by artist Dan Andreasen, this wonderful first biography captures the very essence of the little girl called ‘Half-pint,’ whose classic books and pioneer adventures have made her one of the most popular literary figures in America.\"'),\n",
" Document(metadata={'source': 'tagged_description.txt'}, page_content='9780743243780 The author describes his coming of age as a teacher, storyteller, and writer, a personal journey during which he spent fifteen years finding his voice in the classroom, and came to terms with the undervalued importance of teaching.')]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"A book to teach children about nature\"\n",
"docs = db_books.similarity_search(query, k = 10)\n",
"docs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47e4c48c",
"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>isbn13</th>\n",
" <th>isbn10</th>\n",
" <th>title</th>\n",
" <th>authors</th>\n",
" <th>categories</th>\n",
" <th>thumbnail</th>\n",
" <th>description</th>\n",
" <th>published_year</th>\n",
" <th>average_rating</th>\n",
" <th>num_pages</th>\n",
" <th>ratings_count</th>\n",
" <th>agg_of_book</th>\n",
" <th>title_and_subtitle</th>\n",
" <th>tagged_description</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>4264</th>\n",
" <td>9780941807555</td>\n",
" <td>094180755X</td>\n",
" <td>The Little Big Book for God's Children</td>\n",
" <td>Lena Tabori;Alice Wong</td>\n",
" <td>Religion</td>\n",
" <td>http://books.google.com/books/content?id=s2PfT...</td>\n",
" <td>THE LITTLE BIG BOOK FOR GOD'S CHILDREN is a wo...</td>\n",
" <td>2001.0</td>\n",
" <td>4.88</td>\n",
" <td>352.0</td>\n",
" <td>8.0</td>\n",
" <td>24.0</td>\n",
" <td>The Little Big Book for God's Children</td>\n",
" <td>9780941807555 THE LITTLE BIG BOOK FOR GOD'S CH...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" isbn13 isbn10 title \\\n",
"4264 9780941807555 094180755X The Little Big Book for God's Children \n",
"\n",
" authors categories \\\n",
"4264 Lena Tabori;Alice Wong Religion \n",
"\n",
" thumbnail \\\n",
"4264 http://books.google.com/books/content?id=s2PfT... \n",
"\n",
" description published_year \\\n",
"4264 THE LITTLE BIG BOOK FOR GOD'S CHILDREN is a wo... 2001.0 \n",
"\n",
" average_rating num_pages ratings_count agg_of_book \\\n",
"4264 4.88 352.0 8.0 24.0 \n",
"\n",
" title_and_subtitle \\\n",
"4264 The Little Big Book for God's Children \n",
"\n",
" tagged_description \n",
"4264 9780941807555 THE LITTLE BIG BOOK FOR GOD'S CH... "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"books[books[\"isbn13\"] == int(docs[0].page_content.split()[0].strip())]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "43ed7a8c",
"metadata": {},
"outputs": [],
"source": [
"def retrieve_semantic_recommendation(\n",
" query:str,\n",
" top_k: int = 10,\n",
") -> pd.DataFrame :\n",
" recs = db_books.similarity_search(query, k = 50)\n",
"\n",
" books_list = []\n",
"\n",
" for i in range (0, len(recs)):\n",
" books_list += [int(recs[i].page_content.strip('\"\"').split()[0])]\n",
"\n",
" return books[books[\"isbn13\"].isin(books_list)].head(top_k)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "89dfb386",
"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>isbn13</th>\n",
" <th>isbn10</th>\n",
" <th>title</th>\n",
" <th>authors</th>\n",
" <th>categories</th>\n",
" <th>thumbnail</th>\n",
" <th>description</th>\n",
" <th>published_year</th>\n",
" <th>average_rating</th>\n",
" <th>num_pages</th>\n",
" <th>ratings_count</th>\n",
" <th>agg_of_book</th>\n",
" <th>title_and_subtitle</th>\n",
" <th>tagged_description</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>9780007105045</td>\n",
" <td>0007105045</td>\n",
" <td>Tree and Leaf</td>\n",
" <td>John Ronald Reuel Tolkien</td>\n",
" <td>Literary Collections</td>\n",
" <td>http://books.google.com/books/content?id=aPb_A...</td>\n",
" <td>\"The two works 'On fairy-stories' and 'Leaf by...</td>\n",
" <td>2001.0</td>\n",
" <td>4.09</td>\n",
" <td>176.0</td>\n",
" <td>2245.0</td>\n",
" <td>24.0</td>\n",
" <td>Tree and Leaf: The Homecoming of Beorhtnoth : ...</td>\n",
" <td>9780007105045 \"The two works 'On fairy-stories...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>267</th>\n",
" <td>9780060882600</td>\n",
" <td>0060882603</td>\n",
" <td>The Annotated Charlotte's Web</td>\n",
" <td>E. B. White</td>\n",
" <td>Juvenile Nonfiction</td>\n",
" <td>http://books.google.com/books/content?id=vaYYH...</td>\n",
" <td>Charlotte's Web, one of America's best-loved c...</td>\n",
" <td>2006.0</td>\n",
" <td>4.16</td>\n",
" <td>320.0</td>\n",
" <td>41.0</td>\n",
" <td>19.0</td>\n",
" <td>The Annotated Charlotte's Web</td>\n",
" <td>9780060882600 Charlotte's Web, one of America'...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>324</th>\n",
" <td>9780060959036</td>\n",
" <td>0060959037</td>\n",
" <td>Prodigal Summer</td>\n",
" <td>Barbara Kingsolver</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=06IwG...</td>\n",
" <td>Barbara Kingsolver's fifth novel is a hymn to ...</td>\n",
" <td>2001.0</td>\n",
" <td>4.00</td>\n",
" <td>444.0</td>\n",
" <td>85440.0</td>\n",
" <td>24.0</td>\n",
" <td>Prodigal Summer: A Novel</td>\n",
" <td>9780060959036 Barbara Kingsolver's fifth novel...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>397</th>\n",
" <td>9780062512796</td>\n",
" <td>006251279X</td>\n",
" <td>The Pilgrimage</td>\n",
" <td>Paulo Coelho;Alan R. Clarke</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=atdJc...</td>\n",
" <td>Previously published as The Diary of a Magus, ...</td>\n",
" <td>1995.0</td>\n",
" <td>3.65</td>\n",
" <td>272.0</td>\n",
" <td>878.0</td>\n",
" <td>30.0</td>\n",
" <td>The Pilgrimage</td>\n",
" <td>9780062512796 Previously published as The Diar...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>416</th>\n",
" <td>9780064406925</td>\n",
" <td>006440692X</td>\n",
" <td>Winter on the Farm</td>\n",
" <td>Laura Ingalls Wilder</td>\n",
" <td>Juvenile Fiction</td>\n",
" <td>http://books.google.com/books/content?id=IvlKH...</td>\n",
" <td>The Little House books tell the story of a lit...</td>\n",
" <td>1997.0</td>\n",
" <td>4.13</td>\n",
" <td>32.0</td>\n",
" <td>400.0</td>\n",
" <td>28.0</td>\n",
" <td>Winter on the Farm</td>\n",
" <td>9780064406925 The Little House books tell the ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>429</th>\n",
" <td>9780064434980</td>\n",
" <td>0064434982</td>\n",
" <td>The Deer in the Wood</td>\n",
" <td>Laura Ingalls Wilder</td>\n",
" <td>Juvenile Fiction</td>\n",
" <td>http://books.google.com/books/content?id=V7YDW...</td>\n",
" <td>Even the youngest child can enjoy a special ad...</td>\n",
" <td>1999.0</td>\n",
" <td>4.17</td>\n",
" <td>32.0</td>\n",
" <td>302.0</td>\n",
" <td>26.0</td>\n",
" <td>The Deer in the Wood</td>\n",
" <td>9780064434980 Even the youngest child can enjo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>434</th>\n",
" <td>9780064462044</td>\n",
" <td>0064462048</td>\n",
" <td>My Little House Crafts Book</td>\n",
" <td>Carolyn Strom Collins</td>\n",
" <td>Juvenile Nonfiction</td>\n",
" <td>http://books.google.com/books/content?id=lTzrs...</td>\n",
" <td>Make the same pioneer crafts that Laura did! I...</td>\n",
" <td>1998.0</td>\n",
" <td>4.05</td>\n",
" <td>64.0</td>\n",
" <td>56.0</td>\n",
" <td>27.0</td>\n",
" <td>My Little House Crafts Book: 18 Projects from ...</td>\n",
" <td>9780064462044 Make the same pioneer crafts tha...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>435</th>\n",
" <td>9780064462341</td>\n",
" <td>006446234X</td>\n",
" <td>Pioneer Girl</td>\n",
" <td>William Anderson</td>\n",
" <td>Juvenile Nonfiction</td>\n",
" <td>http://books.google.com/books/content?id=Sj4UD...</td>\n",
" <td>The pioneer spirit lives on... Readers around ...</td>\n",
" <td>2000.0</td>\n",
" <td>4.15</td>\n",
" <td>32.0</td>\n",
" <td>414.0</td>\n",
" <td>25.0</td>\n",
" <td>Pioneer Girl: The Story of Laura Ingalls Wilder</td>\n",
" <td>9780064462341 The pioneer spirit lives on... R...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>440</th>\n",
" <td>9780066238500</td>\n",
" <td>0066238501</td>\n",
" <td>The Chronicles of Narnia (adult)</td>\n",
" <td>C. S. Lewis</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=3VGkK...</td>\n",
" <td>Journeys to the end of the world, fantastic cr...</td>\n",
" <td>2001.0</td>\n",
" <td>4.26</td>\n",
" <td>767.0</td>\n",
" <td>425445.0</td>\n",
" <td>24.0</td>\n",
" <td>The Chronicles of Narnia (adult)</td>\n",
" <td>9780066238500 Journeys to the end of the world...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>442</th>\n",
" <td>9780067575208</td>\n",
" <td>006757520X</td>\n",
" <td>The Sense of Wonder</td>\n",
" <td>Rachel Carson</td>\n",
" <td>Nature</td>\n",
" <td>http://books.google.com/books/content?id=Zee5S...</td>\n",
" <td>First published more than three decades ago, t...</td>\n",
" <td>1998.0</td>\n",
" <td>4.39</td>\n",
" <td>112.0</td>\n",
" <td>1160.0</td>\n",
" <td>27.0</td>\n",
" <td>The Sense of Wonder</td>\n",
" <td>9780067575208 First published more than three ...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" isbn13 isbn10 title \\\n",
"31 9780007105045 0007105045 Tree and Leaf \n",
"267 9780060882600 0060882603 The Annotated Charlotte's Web \n",
"324 9780060959036 0060959037 Prodigal Summer \n",
"397 9780062512796 006251279X The Pilgrimage \n",
"416 9780064406925 006440692X Winter on the Farm \n",
"429 9780064434980 0064434982 The Deer in the Wood \n",
"434 9780064462044 0064462048 My Little House Crafts Book \n",
"435 9780064462341 006446234X Pioneer Girl \n",
"440 9780066238500 0066238501 The Chronicles of Narnia (adult) \n",
"442 9780067575208 006757520X The Sense of Wonder \n",
"\n",
" authors categories \\\n",
"31 John Ronald Reuel Tolkien Literary Collections \n",
"267 E. B. White Juvenile Nonfiction \n",
"324 Barbara Kingsolver Fiction \n",
"397 Paulo Coelho;Alan R. Clarke Fiction \n",
"416 Laura Ingalls Wilder Juvenile Fiction \n",
"429 Laura Ingalls Wilder Juvenile Fiction \n",
"434 Carolyn Strom Collins Juvenile Nonfiction \n",
"435 William Anderson Juvenile Nonfiction \n",
"440 C. S. Lewis Fiction \n",
"442 Rachel Carson Nature \n",
"\n",
" thumbnail \\\n",
"31 http://books.google.com/books/content?id=aPb_A... \n",
"267 http://books.google.com/books/content?id=vaYYH... \n",
"324 http://books.google.com/books/content?id=06IwG... \n",
"397 http://books.google.com/books/content?id=atdJc... \n",
"416 http://books.google.com/books/content?id=IvlKH... \n",
"429 http://books.google.com/books/content?id=V7YDW... \n",
"434 http://books.google.com/books/content?id=lTzrs... \n",
"435 http://books.google.com/books/content?id=Sj4UD... \n",
"440 http://books.google.com/books/content?id=3VGkK... \n",
"442 http://books.google.com/books/content?id=Zee5S... \n",
"\n",
" description published_year \\\n",
"31 \"The two works 'On fairy-stories' and 'Leaf by... 2001.0 \n",
"267 Charlotte's Web, one of America's best-loved c... 2006.0 \n",
"324 Barbara Kingsolver's fifth novel is a hymn to ... 2001.0 \n",
"397 Previously published as The Diary of a Magus, ... 1995.0 \n",
"416 The Little House books tell the story of a lit... 1997.0 \n",
"429 Even the youngest child can enjoy a special ad... 1999.0 \n",
"434 Make the same pioneer crafts that Laura did! I... 1998.0 \n",
"435 The pioneer spirit lives on... Readers around ... 2000.0 \n",
"440 Journeys to the end of the world, fantastic cr... 2001.0 \n",
"442 First published more than three decades ago, t... 1998.0 \n",
"\n",
" average_rating num_pages ratings_count agg_of_book \\\n",
"31 4.09 176.0 2245.0 24.0 \n",
"267 4.16 320.0 41.0 19.0 \n",
"324 4.00 444.0 85440.0 24.0 \n",
"397 3.65 272.0 878.0 30.0 \n",
"416 4.13 32.0 400.0 28.0 \n",
"429 4.17 32.0 302.0 26.0 \n",
"434 4.05 64.0 56.0 27.0 \n",
"435 4.15 32.0 414.0 25.0 \n",
"440 4.26 767.0 425445.0 24.0 \n",
"442 4.39 112.0 1160.0 27.0 \n",
"\n",
" title_and_subtitle \\\n",
"31 Tree and Leaf: The Homecoming of Beorhtnoth : ... \n",
"267 The Annotated Charlotte's Web \n",
"324 Prodigal Summer: A Novel \n",
"397 The Pilgrimage \n",
"416 Winter on the Farm \n",
"429 The Deer in the Wood \n",
"434 My Little House Crafts Book: 18 Projects from ... \n",
"435 Pioneer Girl: The Story of Laura Ingalls Wilder \n",
"440 The Chronicles of Narnia (adult) \n",
"442 The Sense of Wonder \n",
"\n",
" tagged_description \n",
"31 9780007105045 \"The two works 'On fairy-stories... \n",
"267 9780060882600 Charlotte's Web, one of America'... \n",
"324 9780060959036 Barbara Kingsolver's fifth novel... \n",
"397 9780062512796 Previously published as The Diar... \n",
"416 9780064406925 The Little House books tell the ... \n",
"429 9780064434980 Even the youngest child can enjo... \n",
"434 9780064462044 Make the same pioneer crafts tha... \n",
"435 9780064462341 The pioneer spirit lives on... R... \n",
"440 9780066238500 Journeys to the end of the world... \n",
"442 9780067575208 First published more than three ... "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retrieve_semantic_recommendation(\"A book to teach children about nature\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f655cdbb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "books_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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|