File size: 26,155 Bytes
2d13434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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>full_title</th>\n",
       "      <th>authors</th>\n",
       "      <th>categories</th>\n",
       "      <th>description</th>\n",
       "      <th>full_desc</th>\n",
       "      <th>published_year</th>\n",
       "      <th>num_pages</th>\n",
       "      <th>average_rating</th>\n",
       "      <th>ratings_count</th>\n",
       "      <th>thumbnail</th>\n",
       "      <th>final_categories</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9780002005883</td>\n",
       "      <td>Gilead</td>\n",
       "      <td>Marilynne Robinson</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>A NOVEL THAT READERS and critics have been eag...</td>\n",
       "      <td>9780002005883 A NOVEL THAT READERS and critics...</td>\n",
       "      <td>2004.0</td>\n",
       "      <td>247.0</td>\n",
       "      <td>3.85</td>\n",
       "      <td>361.0</td>\n",
       "      <td>http://books.google.com/books/content?id=KQZCP...</td>\n",
       "      <td>Fiction</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9780002261982</td>\n",
       "      <td>Spider's Web: A Novel</td>\n",
       "      <td>Charles Osborne;Agatha Christie</td>\n",
       "      <td>Detective and mystery stories</td>\n",
       "      <td>A new 'Christie for Christmas' -- a full-lengt...</td>\n",
       "      <td>9780002261982 A new 'Christie for Christmas' -...</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>3.83</td>\n",
       "      <td>5164.0</td>\n",
       "      <td>http://books.google.com/books/content?id=gA5GP...</td>\n",
       "      <td>Fiction</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9780006178736</td>\n",
       "      <td>Rage of angels</td>\n",
       "      <td>Sidney Sheldon</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>A memorable, mesmerizing heroine Jennifer -- b...</td>\n",
       "      <td>9780006178736 A memorable, mesmerizing heroine...</td>\n",
       "      <td>1993.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>3.93</td>\n",
       "      <td>29532.0</td>\n",
       "      <td>http://books.google.com/books/content?id=FKo2T...</td>\n",
       "      <td>Fiction</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9780006280897</td>\n",
       "      <td>The Four Loves</td>\n",
       "      <td>Clive Staples Lewis</td>\n",
       "      <td>Christian life</td>\n",
       "      <td>Lewis' work on the nature of love divides love...</td>\n",
       "      <td>9780006280897 Lewis' work on the nature of lov...</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>170.0</td>\n",
       "      <td>4.15</td>\n",
       "      <td>33684.0</td>\n",
       "      <td>http://books.google.com/books/content?id=XhQ5X...</td>\n",
       "      <td>Nonfiction</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9780006280934</td>\n",
       "      <td>The Problem of Pain</td>\n",
       "      <td>Clive Staples Lewis</td>\n",
       "      <td>Christian life</td>\n",
       "      <td>\"In The Problem of Pain, C.S. Lewis, one of th...</td>\n",
       "      <td>9780006280934 \"In The Problem of Pain, C.S. Le...</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>4.09</td>\n",
       "      <td>37569.0</td>\n",
       "      <td>http://books.google.com/books/content?id=Kk-uV...</td>\n",
       "      <td>Nonfiction</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          isbn13             full_title                          authors  \\\n",
       "0  9780002005883                 Gilead               Marilynne Robinson   \n",
       "1  9780002261982  Spider's Web: A Novel  Charles Osborne;Agatha Christie   \n",
       "2  9780006178736         Rage of angels                   Sidney Sheldon   \n",
       "3  9780006280897         The Four Loves              Clive Staples Lewis   \n",
       "4  9780006280934    The Problem of Pain              Clive Staples Lewis   \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",
       "                                         description  \\\n",
       "0  A NOVEL THAT READERS and critics have been eag...   \n",
       "1  A new 'Christie for Christmas' -- a full-lengt...   \n",
       "2  A memorable, mesmerizing heroine Jennifer -- b...   \n",
       "3  Lewis' work on the nature of love divides love...   \n",
       "4  \"In The Problem of Pain, C.S. Lewis, one of th...   \n",
       "\n",
       "                                           full_desc  published_year  \\\n",
       "0  9780002005883 A NOVEL THAT READERS and critics...          2004.0   \n",
       "1  9780002261982 A new 'Christie for Christmas' -...          2000.0   \n",
       "2  9780006178736 A memorable, mesmerizing heroine...          1993.0   \n",
       "3  9780006280897 Lewis' work on the nature of lov...          2002.0   \n",
       "4  9780006280934 \"In The Problem of Pain, C.S. Le...          2002.0   \n",
       "\n",
       "   num_pages  average_rating  ratings_count  \\\n",
       "0      247.0            3.85          361.0   \n",
       "1      241.0            3.83         5164.0   \n",
       "2      512.0            3.93        29532.0   \n",
       "3      170.0            4.15        33684.0   \n",
       "4      176.0            4.09        37569.0   \n",
       "\n",
       "                                           thumbnail final_categories  \n",
       "0  http://books.google.com/books/content?id=KQZCP...          Fiction  \n",
       "1  http://books.google.com/books/content?id=gA5GP...          Fiction  \n",
       "2  http://books.google.com/books/content?id=FKo2T...          Fiction  \n",
       "3  http://books.google.com/books/content?id=XhQ5X...       Nonfiction  \n",
       "4  http://books.google.com/books/content?id=Kk-uV...       Nonfiction  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "books = pd.read_csv('data/books_with_categories.csv')\n",
    "books.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cuda:0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline('text-classification', model='j-hartmann/emotion-english-distilroberta-base',\n",
    "                      top_k=None,\n",
    "                      device=0 if torch.cuda.is_available() else -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives\n",
      "{'label': 'surprise', 'score': 0.7296027541160583}\n",
      "--------------------------------\n",
      "\n",
      "John Ames is a preacher, the son of a preacher and the grandson (both maternal and paternal) of preachers\n",
      "{'label': 'neutral', 'score': 0.4662497639656067}\n",
      "--------------------------------\n",
      "\n",
      "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\n",
      "{'label': 'neutral', 'score': 0.6978469491004944}\n",
      "--------------------------------\n",
      "\n",
      "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\n",
      "{'label': 'fear', 'score': 0.9839729070663452}\n",
      "--------------------------------\n",
      "\n",
      "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\n",
      "{'label': 'sadness', 'score': 0.9560651183128357}\n",
      "--------------------------------\n",
      "\n",
      "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\n",
      "{'label': 'joy', 'score': 0.9490270018577576}\n",
      "--------------------------------\n",
      "\n",
      "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\n",
      "{'label': 'joy', 'score': 0.6701961159706116}\n",
      "--------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "sentences  = [s.strip() for s in books['description'][0].split('.') if s.strip()]\n",
    "emotions   = classifier(sentences)\n",
    "\n",
    "for sentence, emotion in zip(sentences, emotions):\n",
    "    print(sentence)\n",
    "    print(emotion[0])\n",
    "    print('--------------------------------\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "emotion_labels = sorted(['anger', 'disgust', 'fear', 'joy', 'neutral', 'sadness', 'surprise'])\n",
    "\n",
    "def calculate_book_emotion_scores(emotions):\n",
    "    sentence_emotion_scores = {label: [] for label in emotion_labels}\n",
    "    \n",
    "    for emotion in emotions:\n",
    "        emotion = sorted(emotion, key=lambda x: x['label'])\n",
    "        \n",
    "        for idx, label in enumerate(emotion_labels):\n",
    "            sentence_emotion_scores[label].append(emotion[idx]['score'])\n",
    "            \n",
    "    return {label: np.max(scores) for label, scores in sentence_emotion_scores.items()} # Note: Try to use both max and mean later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5197/5197 [02:51<00:00, 30.27it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "isbns          = []\n",
    "emotion_scores = {label: [] for label in emotion_labels}\n",
    "\n",
    "for i in tqdm(range(len(books))):\n",
    "    isbns.append(books['isbn13'][i])\n",
    "    sentences = books['description'][i].split('.')\n",
    "    emotions  = classifier(sentences)\n",
    "    max_emotion_scores = calculate_book_emotion_scores(emotions)\n",
    "    for label in emotion_labels:\n",
    "        emotion_scores[label].append(max_emotion_scores[label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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>anger</th>\n",
       "      <th>disgust</th>\n",
       "      <th>fear</th>\n",
       "      <th>joy</th>\n",
       "      <th>neutral</th>\n",
       "      <th>sadness</th>\n",
       "      <th>surprise</th>\n",
       "      <th>isbn13</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.064134</td>\n",
       "      <td>0.273591</td>\n",
       "      <td>0.928168</td>\n",
       "      <td>0.932797</td>\n",
       "      <td>0.646217</td>\n",
       "      <td>0.967158</td>\n",
       "      <td>0.729603</td>\n",
       "      <td>9780002005883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.612619</td>\n",
       "      <td>0.348284</td>\n",
       "      <td>0.942528</td>\n",
       "      <td>0.704421</td>\n",
       "      <td>0.887940</td>\n",
       "      <td>0.111690</td>\n",
       "      <td>0.252545</td>\n",
       "      <td>9780002261982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.064134</td>\n",
       "      <td>0.104007</td>\n",
       "      <td>0.972321</td>\n",
       "      <td>0.767237</td>\n",
       "      <td>0.549477</td>\n",
       "      <td>0.111690</td>\n",
       "      <td>0.078766</td>\n",
       "      <td>9780006178736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.351483</td>\n",
       "      <td>0.150722</td>\n",
       "      <td>0.360707</td>\n",
       "      <td>0.251881</td>\n",
       "      <td>0.732686</td>\n",
       "      <td>0.111690</td>\n",
       "      <td>0.078766</td>\n",
       "      <td>9780006280897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.081412</td>\n",
       "      <td>0.184495</td>\n",
       "      <td>0.095043</td>\n",
       "      <td>0.040564</td>\n",
       "      <td>0.884390</td>\n",
       "      <td>0.475881</td>\n",
       "      <td>0.078766</td>\n",
       "      <td>9780006280934</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      anger   disgust      fear       joy   neutral   sadness  surprise  \\\n",
       "0  0.064134  0.273591  0.928168  0.932797  0.646217  0.967158  0.729603   \n",
       "1  0.612619  0.348284  0.942528  0.704421  0.887940  0.111690  0.252545   \n",
       "2  0.064134  0.104007  0.972321  0.767237  0.549477  0.111690  0.078766   \n",
       "3  0.351483  0.150722  0.360707  0.251881  0.732686  0.111690  0.078766   \n",
       "4  0.081412  0.184495  0.095043  0.040564  0.884390  0.475881  0.078766   \n",
       "\n",
       "          isbn13  \n",
       "0  9780002005883  \n",
       "1  9780002261982  \n",
       "2  9780006178736  \n",
       "3  9780006280897  \n",
       "4  9780006280934  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emotions_df = pd.DataFrame(emotion_scores)\n",
    "emotions_df['isbn13'] = isbns\n",
    "emotions_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>full_title</th>\n",
       "      <th>authors</th>\n",
       "      <th>categories</th>\n",
       "      <th>description</th>\n",
       "      <th>full_desc</th>\n",
       "      <th>published_year</th>\n",
       "      <th>num_pages</th>\n",
       "      <th>average_rating</th>\n",
       "      <th>ratings_count</th>\n",
       "      <th>thumbnail</th>\n",
       "      <th>final_categories</th>\n",
       "      <th>anger</th>\n",
       "      <th>disgust</th>\n",
       "      <th>fear</th>\n",
       "      <th>joy</th>\n",
       "      <th>neutral</th>\n",
       "      <th>sadness</th>\n",
       "      <th>surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9780002005883</td>\n",
       "      <td>Gilead</td>\n",
       "      <td>Marilynne Robinson</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>A NOVEL THAT READERS and critics have been eag...</td>\n",
       "      <td>9780002005883 A NOVEL THAT READERS and critics...</td>\n",
       "      <td>2004.0</td>\n",
       "      <td>247.0</td>\n",
       "      <td>3.85</td>\n",
       "      <td>361.0</td>\n",
       "      <td>http://books.google.com/books/content?id=KQZCP...</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>0.064134</td>\n",
       "      <td>0.273591</td>\n",
       "      <td>0.928168</td>\n",
       "      <td>0.932797</td>\n",
       "      <td>0.646217</td>\n",
       "      <td>0.967158</td>\n",
       "      <td>0.729603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9780002261982</td>\n",
       "      <td>Spider's Web: A Novel</td>\n",
       "      <td>Charles Osborne;Agatha Christie</td>\n",
       "      <td>Detective and mystery stories</td>\n",
       "      <td>A new 'Christie for Christmas' -- a full-lengt...</td>\n",
       "      <td>9780002261982 A new 'Christie for Christmas' -...</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>3.83</td>\n",
       "      <td>5164.0</td>\n",
       "      <td>http://books.google.com/books/content?id=gA5GP...</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>0.612619</td>\n",
       "      <td>0.348284</td>\n",
       "      <td>0.942528</td>\n",
       "      <td>0.704421</td>\n",
       "      <td>0.887940</td>\n",
       "      <td>0.111690</td>\n",
       "      <td>0.252545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9780006178736</td>\n",
       "      <td>Rage of angels</td>\n",
       "      <td>Sidney Sheldon</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>A memorable, mesmerizing heroine Jennifer -- b...</td>\n",
       "      <td>9780006178736 A memorable, mesmerizing heroine...</td>\n",
       "      <td>1993.0</td>\n",
       "      <td>512.0</td>\n",
       "      <td>3.93</td>\n",
       "      <td>29532.0</td>\n",
       "      <td>http://books.google.com/books/content?id=FKo2T...</td>\n",
       "      <td>Fiction</td>\n",
       "      <td>0.064134</td>\n",
       "      <td>0.104007</td>\n",
       "      <td>0.972321</td>\n",
       "      <td>0.767237</td>\n",
       "      <td>0.549477</td>\n",
       "      <td>0.111690</td>\n",
       "      <td>0.078766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9780006280897</td>\n",
       "      <td>The Four Loves</td>\n",
       "      <td>Clive Staples Lewis</td>\n",
       "      <td>Christian life</td>\n",
       "      <td>Lewis' work on the nature of love divides love...</td>\n",
       "      <td>9780006280897 Lewis' work on the nature of lov...</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>170.0</td>\n",
       "      <td>4.15</td>\n",
       "      <td>33684.0</td>\n",
       "      <td>http://books.google.com/books/content?id=XhQ5X...</td>\n",
       "      <td>Nonfiction</td>\n",
       "      <td>0.351483</td>\n",
       "      <td>0.150722</td>\n",
       "      <td>0.360707</td>\n",
       "      <td>0.251881</td>\n",
       "      <td>0.732686</td>\n",
       "      <td>0.111690</td>\n",
       "      <td>0.078766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9780006280934</td>\n",
       "      <td>The Problem of Pain</td>\n",
       "      <td>Clive Staples Lewis</td>\n",
       "      <td>Christian life</td>\n",
       "      <td>\"In The Problem of Pain, C.S. Lewis, one of th...</td>\n",
       "      <td>9780006280934 \"In The Problem of Pain, C.S. Le...</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>4.09</td>\n",
       "      <td>37569.0</td>\n",
       "      <td>http://books.google.com/books/content?id=Kk-uV...</td>\n",
       "      <td>Nonfiction</td>\n",
       "      <td>0.081412</td>\n",
       "      <td>0.184495</td>\n",
       "      <td>0.095043</td>\n",
       "      <td>0.040564</td>\n",
       "      <td>0.884390</td>\n",
       "      <td>0.475881</td>\n",
       "      <td>0.078766</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          isbn13             full_title                          authors  \\\n",
       "0  9780002005883                 Gilead               Marilynne Robinson   \n",
       "1  9780002261982  Spider's Web: A Novel  Charles Osborne;Agatha Christie   \n",
       "2  9780006178736         Rage of angels                   Sidney Sheldon   \n",
       "3  9780006280897         The Four Loves              Clive Staples Lewis   \n",
       "4  9780006280934    The Problem of Pain              Clive Staples Lewis   \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",
       "                                         description  \\\n",
       "0  A NOVEL THAT READERS and critics have been eag...   \n",
       "1  A new 'Christie for Christmas' -- a full-lengt...   \n",
       "2  A memorable, mesmerizing heroine Jennifer -- b...   \n",
       "3  Lewis' work on the nature of love divides love...   \n",
       "4  \"In The Problem of Pain, C.S. Lewis, one of th...   \n",
       "\n",
       "                                           full_desc  published_year  \\\n",
       "0  9780002005883 A NOVEL THAT READERS and critics...          2004.0   \n",
       "1  9780002261982 A new 'Christie for Christmas' -...          2000.0   \n",
       "2  9780006178736 A memorable, mesmerizing heroine...          1993.0   \n",
       "3  9780006280897 Lewis' work on the nature of lov...          2002.0   \n",
       "4  9780006280934 \"In The Problem of Pain, C.S. Le...          2002.0   \n",
       "\n",
       "   num_pages  average_rating  ratings_count  \\\n",
       "0      247.0            3.85          361.0   \n",
       "1      241.0            3.83         5164.0   \n",
       "2      512.0            3.93        29532.0   \n",
       "3      170.0            4.15        33684.0   \n",
       "4      176.0            4.09        37569.0   \n",
       "\n",
       "                                           thumbnail final_categories  \\\n",
       "0  http://books.google.com/books/content?id=KQZCP...          Fiction   \n",
       "1  http://books.google.com/books/content?id=gA5GP...          Fiction   \n",
       "2  http://books.google.com/books/content?id=FKo2T...          Fiction   \n",
       "3  http://books.google.com/books/content?id=XhQ5X...       Nonfiction   \n",
       "4  http://books.google.com/books/content?id=Kk-uV...       Nonfiction   \n",
       "\n",
       "      anger   disgust      fear       joy   neutral   sadness  surprise  \n",
       "0  0.064134  0.273591  0.928168  0.932797  0.646217  0.967158  0.729603  \n",
       "1  0.612619  0.348284  0.942528  0.704421  0.887940  0.111690  0.252545  \n",
       "2  0.064134  0.104007  0.972321  0.767237  0.549477  0.111690  0.078766  \n",
       "3  0.351483  0.150722  0.360707  0.251881  0.732686  0.111690  0.078766  \n",
       "4  0.081412  0.184495  0.095043  0.040564  0.884390  0.475881  0.078766  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books_with_emotions = pd.merge(books, emotions_df, on='isbn13')\n",
    "\n",
    "books_with_emotions.to_csv('data/books_with_emotions.csv', index=False)\n",
    "books_with_emotions.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "book_rcm",
   "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.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}