Arshdeep004 commited on
Commit
5dedacf
·
verified ·
1 Parent(s): b117fa9

Upload 9 files

Browse files
EDA.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
Vector__Search.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
books.csv ADDED
The diff for this file is too large to render. See raw diff
 
books_cleaned.csv ADDED
The diff for this file is too large to render. See raw diff
 
books_with_categories.csv ADDED
The diff for this file is too large to render. See raw diff
 
books_with_emotions.csv ADDED
The diff for this file is too large to render. See raw diff
 
not_found.jpg ADDED
sentiment_analysis.ipynb ADDED
@@ -0,0 +1,881 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "56f827ee-ab3d-4aab-9796-aff00fa63924",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import pandas as pd\n",
11
+ "\n",
12
+ "books = pd.read_csv(\"books_with_categories.csv\")"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 3,
18
+ "id": "8da98828-1909-429b-b51a-8dea366d73ad",
19
+ "metadata": {},
20
+ "outputs": [
21
+ {
22
+ "name": "stderr",
23
+ "output_type": "stream",
24
+ "text": [
25
+ "Device set to use cpu\n"
26
+ ]
27
+ },
28
+ {
29
+ "name": "stdout",
30
+ "output_type": "stream",
31
+ "text": [
32
+ "[[{'label': 'joy', 'score': 0.9771687984466553}, {'label': 'surprise', 'score': 0.008528684265911579}, {'label': 'neutral', 'score': 0.005764583125710487}, {'label': 'anger', 'score': 0.004419783595949411}, {'label': 'sadness', 'score': 0.002092392183840275}, {'label': 'disgust', 'score': 0.0016119909705594182}, {'label': 'fear', 'score': 0.00041385277290828526}]]\n"
33
+ ]
34
+ }
35
+ ],
36
+ "source": [
37
+ "from transformers import pipeline\n",
38
+ "\n",
39
+ "classifier = pipeline(\n",
40
+ " \"text-classification\",\n",
41
+ " model=\"j-hartmann/emotion-english-distilroberta-base\",\n",
42
+ " top_k=None,\n",
43
+ " device=-1 # Use CPU\n",
44
+ ")\n",
45
+ "\n",
46
+ "print(classifier(\"I love this!\"))\n"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": 4,
52
+ "id": "ea965320-c400-4f37-bff5-ba5ee6cb7bee",
53
+ "metadata": {},
54
+ "outputs": [
55
+ {
56
+ "data": {
57
+ "text/plain": [
58
+ "'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.'"
59
+ ]
60
+ },
61
+ "execution_count": 4,
62
+ "metadata": {},
63
+ "output_type": "execute_result"
64
+ }
65
+ ],
66
+ "source": [
67
+ "books[\"description\"][0]"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": 5,
73
+ "id": "5f62a1cd-3dbb-48f4-ad88-acb89b7a47a4",
74
+ "metadata": {},
75
+ "outputs": [
76
+ {
77
+ "data": {
78
+ "text/plain": [
79
+ "[[{'label': 'fear', 'score': 0.6548405885696411},\n",
80
+ " {'label': 'neutral', 'score': 0.16985228657722473},\n",
81
+ " {'label': 'sadness', 'score': 0.11640921980142593},\n",
82
+ " {'label': 'surprise', 'score': 0.02070065587759018},\n",
83
+ " {'label': 'disgust', 'score': 0.019100677222013474},\n",
84
+ " {'label': 'joy', 'score': 0.01516144908964634},\n",
85
+ " {'label': 'anger', 'score': 0.003935146611183882}]]"
86
+ ]
87
+ },
88
+ "execution_count": 5,
89
+ "metadata": {},
90
+ "output_type": "execute_result"
91
+ }
92
+ ],
93
+ "source": [
94
+ "classifier(books[\"description\"][0])\n"
95
+ ]
96
+ },
97
+ {
98
+ "cell_type": "code",
99
+ "execution_count": 6,
100
+ "id": "918ee472-8899-465a-8ff0-9eeef07c07f5",
101
+ "metadata": {},
102
+ "outputs": [
103
+ {
104
+ "data": {
105
+ "text/plain": [
106
+ "[[{'label': 'surprise', 'score': 0.7296020984649658},\n",
107
+ " {'label': 'neutral', 'score': 0.14038600027561188},\n",
108
+ " {'label': 'fear', 'score': 0.06816228479146957},\n",
109
+ " {'label': 'joy', 'score': 0.04794260859489441},\n",
110
+ " {'label': 'anger', 'score': 0.009156366810202599},\n",
111
+ " {'label': 'disgust', 'score': 0.0026284765917807817},\n",
112
+ " {'label': 'sadness', 'score': 0.002122163539752364}],\n",
113
+ " [{'label': 'neutral', 'score': 0.44937002658843994},\n",
114
+ " {'label': 'disgust', 'score': 0.27359163761138916},\n",
115
+ " {'label': 'joy', 'score': 0.10908330976963043},\n",
116
+ " {'label': 'sadness', 'score': 0.09362746775150299},\n",
117
+ " {'label': 'anger', 'score': 0.04047830402851105},\n",
118
+ " {'label': 'surprise', 'score': 0.026970159262418747},\n",
119
+ " {'label': 'fear', 'score': 0.006879047024995089}],\n",
120
+ " [{'label': 'neutral', 'score': 0.6462159752845764},\n",
121
+ " {'label': 'sadness', 'score': 0.24273329973220825},\n",
122
+ " {'label': 'disgust', 'score': 0.04342271760106087},\n",
123
+ " {'label': 'surprise', 'score': 0.028300564736127853},\n",
124
+ " {'label': 'joy', 'score': 0.014211472123861313},\n",
125
+ " {'label': 'fear', 'score': 0.0140841044485569},\n",
126
+ " {'label': 'anger', 'score': 0.011031901463866234}],\n",
127
+ " [{'label': 'fear', 'score': 0.9281681180000305},\n",
128
+ " {'label': 'anger', 'score': 0.032191041857004166},\n",
129
+ " {'label': 'neutral', 'score': 0.01280867587774992},\n",
130
+ " {'label': 'sadness', 'score': 0.008756861090660095},\n",
131
+ " {'label': 'surprise', 'score': 0.008597906678915024},\n",
132
+ " {'label': 'disgust', 'score': 0.008431818336248398},\n",
133
+ " {'label': 'joy', 'score': 0.0010455837473273277}],\n",
134
+ " [{'label': 'sadness', 'score': 0.9671575427055359},\n",
135
+ " {'label': 'neutral', 'score': 0.015104127116501331},\n",
136
+ " {'label': 'disgust', 'score': 0.006480589509010315},\n",
137
+ " {'label': 'fear', 'score': 0.005393980070948601},\n",
138
+ " {'label': 'surprise', 'score': 0.002286937553435564},\n",
139
+ " {'label': 'anger', 'score': 0.0018428878393024206},\n",
140
+ " {'label': 'joy', 'score': 0.0017338756006211042}],\n",
141
+ " [{'label': 'joy', 'score': 0.932798445224762},\n",
142
+ " {'label': 'disgust', 'score': 0.0377168171107769},\n",
143
+ " {'label': 'neutral', 'score': 0.015891803428530693},\n",
144
+ " {'label': 'sadness', 'score': 0.006444480270147324},\n",
145
+ " {'label': 'anger', 'score': 0.005024945829063654},\n",
146
+ " {'label': 'surprise', 'score': 0.0015812049387022853},\n",
147
+ " {'label': 'fear', 'score': 0.0005423048860393465}],\n",
148
+ " [{'label': 'joy', 'score': 0.6528708338737488},\n",
149
+ " {'label': 'neutral', 'score': 0.25427478551864624},\n",
150
+ " {'label': 'surprise', 'score': 0.06808284670114517},\n",
151
+ " {'label': 'sadness', 'score': 0.009908987209200859},\n",
152
+ " {'label': 'disgust', 'score': 0.0065122200176119804},\n",
153
+ " {'label': 'anger', 'score': 0.004821304697543383},\n",
154
+ " {'label': 'fear', 'score': 0.0035290175583213568}],\n",
155
+ " [{'label': 'neutral', 'score': 0.5494767427444458},\n",
156
+ " {'label': 'sadness', 'score': 0.11169017851352692},\n",
157
+ " {'label': 'disgust', 'score': 0.10400667786598206},\n",
158
+ " {'label': 'surprise', 'score': 0.07876550406217575},\n",
159
+ " {'label': 'anger', 'score': 0.0641336441040039},\n",
160
+ " {'label': 'fear', 'score': 0.05136283114552498},\n",
161
+ " {'label': 'joy', 'score': 0.04056443274021149}]]"
162
+ ]
163
+ },
164
+ "execution_count": 6,
165
+ "metadata": {},
166
+ "output_type": "execute_result"
167
+ }
168
+ ],
169
+ "source": [
170
+ "classifier(books[\"description\"][0].split(\".\"))"
171
+ ]
172
+ },
173
+ {
174
+ "cell_type": "code",
175
+ "execution_count": 7,
176
+ "id": "f02c4264-7e91-4138-9b77-f4618b506ef3",
177
+ "metadata": {},
178
+ "outputs": [],
179
+ "source": [
180
+ "sentences = books[\"description\"][0].split(\".\")\n",
181
+ "predictions = classifier(sentences)"
182
+ ]
183
+ },
184
+ {
185
+ "cell_type": "code",
186
+ "execution_count": 8,
187
+ "id": "3e3f350f-f448-4408-90ac-842255ad7c1e",
188
+ "metadata": {},
189
+ "outputs": [
190
+ {
191
+ "data": {
192
+ "text/plain": [
193
+ "'A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives'"
194
+ ]
195
+ },
196
+ "execution_count": 8,
197
+ "metadata": {},
198
+ "output_type": "execute_result"
199
+ }
200
+ ],
201
+ "source": [
202
+ "sentences[0]"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "code",
207
+ "execution_count": 9,
208
+ "id": "61d037e6-f1ca-4330-bf2a-f42063974858",
209
+ "metadata": {},
210
+ "outputs": [
211
+ {
212
+ "data": {
213
+ "text/plain": [
214
+ "[{'label': 'surprise', 'score': 0.7296020984649658},\n",
215
+ " {'label': 'neutral', 'score': 0.14038600027561188},\n",
216
+ " {'label': 'fear', 'score': 0.06816228479146957},\n",
217
+ " {'label': 'joy', 'score': 0.04794260859489441},\n",
218
+ " {'label': 'anger', 'score': 0.009156366810202599},\n",
219
+ " {'label': 'disgust', 'score': 0.0026284765917807817},\n",
220
+ " {'label': 'sadness', 'score': 0.002122163539752364}]"
221
+ ]
222
+ },
223
+ "execution_count": 9,
224
+ "metadata": {},
225
+ "output_type": "execute_result"
226
+ }
227
+ ],
228
+ "source": [
229
+ "predictions[0]"
230
+ ]
231
+ },
232
+ {
233
+ "cell_type": "code",
234
+ "execution_count": 10,
235
+ "id": "7287bf76-1f12-4792-8138-a74b4b876e00",
236
+ "metadata": {},
237
+ "outputs": [
238
+ {
239
+ "data": {
240
+ "text/plain": [
241
+ "[[{'label': 'surprise', 'score': 0.7296020984649658},\n",
242
+ " {'label': 'neutral', 'score': 0.14038600027561188},\n",
243
+ " {'label': 'fear', 'score': 0.06816228479146957},\n",
244
+ " {'label': 'joy', 'score': 0.04794260859489441},\n",
245
+ " {'label': 'anger', 'score': 0.009156366810202599},\n",
246
+ " {'label': 'disgust', 'score': 0.0026284765917807817},\n",
247
+ " {'label': 'sadness', 'score': 0.002122163539752364}],\n",
248
+ " [{'label': 'neutral', 'score': 0.44937002658843994},\n",
249
+ " {'label': 'disgust', 'score': 0.27359163761138916},\n",
250
+ " {'label': 'joy', 'score': 0.10908330976963043},\n",
251
+ " {'label': 'sadness', 'score': 0.09362746775150299},\n",
252
+ " {'label': 'anger', 'score': 0.04047830402851105},\n",
253
+ " {'label': 'surprise', 'score': 0.026970159262418747},\n",
254
+ " {'label': 'fear', 'score': 0.006879047024995089}],\n",
255
+ " [{'label': 'neutral', 'score': 0.6462159752845764},\n",
256
+ " {'label': 'sadness', 'score': 0.24273329973220825},\n",
257
+ " {'label': 'disgust', 'score': 0.04342271760106087},\n",
258
+ " {'label': 'surprise', 'score': 0.028300564736127853},\n",
259
+ " {'label': 'joy', 'score': 0.014211472123861313},\n",
260
+ " {'label': 'fear', 'score': 0.0140841044485569},\n",
261
+ " {'label': 'anger', 'score': 0.011031901463866234}],\n",
262
+ " [{'label': 'fear', 'score': 0.9281681180000305},\n",
263
+ " {'label': 'anger', 'score': 0.032191041857004166},\n",
264
+ " {'label': 'neutral', 'score': 0.01280867587774992},\n",
265
+ " {'label': 'sadness', 'score': 0.008756861090660095},\n",
266
+ " {'label': 'surprise', 'score': 0.008597906678915024},\n",
267
+ " {'label': 'disgust', 'score': 0.008431818336248398},\n",
268
+ " {'label': 'joy', 'score': 0.0010455837473273277}],\n",
269
+ " [{'label': 'sadness', 'score': 0.9671575427055359},\n",
270
+ " {'label': 'neutral', 'score': 0.015104127116501331},\n",
271
+ " {'label': 'disgust', 'score': 0.006480589509010315},\n",
272
+ " {'label': 'fear', 'score': 0.005393980070948601},\n",
273
+ " {'label': 'surprise', 'score': 0.002286937553435564},\n",
274
+ " {'label': 'anger', 'score': 0.0018428878393024206},\n",
275
+ " {'label': 'joy', 'score': 0.0017338756006211042}],\n",
276
+ " [{'label': 'joy', 'score': 0.932798445224762},\n",
277
+ " {'label': 'disgust', 'score': 0.0377168171107769},\n",
278
+ " {'label': 'neutral', 'score': 0.015891803428530693},\n",
279
+ " {'label': 'sadness', 'score': 0.006444480270147324},\n",
280
+ " {'label': 'anger', 'score': 0.005024945829063654},\n",
281
+ " {'label': 'surprise', 'score': 0.0015812049387022853},\n",
282
+ " {'label': 'fear', 'score': 0.0005423048860393465}],\n",
283
+ " [{'label': 'joy', 'score': 0.6528708338737488},\n",
284
+ " {'label': 'neutral', 'score': 0.25427478551864624},\n",
285
+ " {'label': 'surprise', 'score': 0.06808284670114517},\n",
286
+ " {'label': 'sadness', 'score': 0.009908987209200859},\n",
287
+ " {'label': 'disgust', 'score': 0.0065122200176119804},\n",
288
+ " {'label': 'anger', 'score': 0.004821304697543383},\n",
289
+ " {'label': 'fear', 'score': 0.0035290175583213568}],\n",
290
+ " [{'label': 'neutral', 'score': 0.5494767427444458},\n",
291
+ " {'label': 'sadness', 'score': 0.11169017851352692},\n",
292
+ " {'label': 'disgust', 'score': 0.10400667786598206},\n",
293
+ " {'label': 'surprise', 'score': 0.07876550406217575},\n",
294
+ " {'label': 'anger', 'score': 0.0641336441040039},\n",
295
+ " {'label': 'fear', 'score': 0.05136283114552498},\n",
296
+ " {'label': 'joy', 'score': 0.04056443274021149}]]"
297
+ ]
298
+ },
299
+ "execution_count": 10,
300
+ "metadata": {},
301
+ "output_type": "execute_result"
302
+ }
303
+ ],
304
+ "source": [
305
+ "predictions"
306
+ ]
307
+ },
308
+ {
309
+ "cell_type": "code",
310
+ "execution_count": 11,
311
+ "id": "5413ca38-27a3-40a8-b17e-1b68baaa4235",
312
+ "metadata": {},
313
+ "outputs": [
314
+ {
315
+ "data": {
316
+ "text/plain": [
317
+ "[{'label': 'anger', 'score': 0.009156366810202599},\n",
318
+ " {'label': 'disgust', 'score': 0.0026284765917807817},\n",
319
+ " {'label': 'fear', 'score': 0.06816228479146957},\n",
320
+ " {'label': 'joy', 'score': 0.04794260859489441},\n",
321
+ " {'label': 'neutral', 'score': 0.14038600027561188},\n",
322
+ " {'label': 'sadness', 'score': 0.002122163539752364},\n",
323
+ " {'label': 'surprise', 'score': 0.7296020984649658}]"
324
+ ]
325
+ },
326
+ "execution_count": 11,
327
+ "metadata": {},
328
+ "output_type": "execute_result"
329
+ }
330
+ ],
331
+ "source": [
332
+ "sorted(predictions[0], key=lambda x: x[\"label\"])"
333
+ ]
334
+ },
335
+ {
336
+ "cell_type": "code",
337
+ "execution_count": 12,
338
+ "id": "803691f6-a635-44f4-af31-fd7293f7ada4",
339
+ "metadata": {},
340
+ "outputs": [],
341
+ "source": [
342
+ "import numpy as np\n",
343
+ "\n",
344
+ "emotion_labels = [\"anger\", \"disgust\", \"fear\", \"joy\", \"sadness\", \"surprise\", \"neutral\"]\n",
345
+ "isbn = []\n",
346
+ "emotion_scores = {label: [] for label in emotion_labels}\n",
347
+ "\n",
348
+ "def calculate_max_emotion_scores(predictions):\n",
349
+ " per_emotion_scores = {label: [] for label in emotion_labels}\n",
350
+ " for prediction in predictions:\n",
351
+ " sorted_predictions = sorted(prediction, key=lambda x: x[\"label\"])\n",
352
+ " for index, label in enumerate(emotion_labels):\n",
353
+ " per_emotion_scores[label].append(sorted_predictions[index][\"score\"])\n",
354
+ " return {label: np.max(scores) for label, scores in per_emotion_scores.items()}\n"
355
+ ]
356
+ },
357
+ {
358
+ "cell_type": "code",
359
+ "execution_count": 13,
360
+ "id": "7f5de092-255b-476b-b51a-6e9961a28f1f",
361
+ "metadata": {},
362
+ "outputs": [],
363
+ "source": [
364
+ "for i in range(10):\n",
365
+ " isbn.append(books[\"isbn13\"][i])\n",
366
+ " sentences = books[\"description\"][i].split(\".\")\n",
367
+ " predictions = classifier(sentences)\n",
368
+ " max_scores = calculate_max_emotion_scores(predictions)\n",
369
+ " for label in emotion_labels:\n",
370
+ " emotion_scores[label].append(max_scores[label])"
371
+ ]
372
+ },
373
+ {
374
+ "cell_type": "code",
375
+ "execution_count": 14,
376
+ "id": "f2390bbc-c4a1-4a1c-91ac-88b48b3f2afb",
377
+ "metadata": {},
378
+ "outputs": [
379
+ {
380
+ "data": {
381
+ "text/plain": [
382
+ "{'anger': [np.float64(0.0641336441040039),\n",
383
+ " np.float64(0.6126197576522827),\n",
384
+ " np.float64(0.0641336441040039),\n",
385
+ " np.float64(0.35148438811302185),\n",
386
+ " np.float64(0.08141235262155533),\n",
387
+ " np.float64(0.2322252243757248),\n",
388
+ " np.float64(0.5381842255592346),\n",
389
+ " np.float64(0.0641336441040039),\n",
390
+ " np.float64(0.3006700277328491),\n",
391
+ " np.float64(0.0641336441040039)],\n",
392
+ " 'disgust': [np.float64(0.27359163761138916),\n",
393
+ " np.float64(0.3482847511768341),\n",
394
+ " np.float64(0.10400667786598206),\n",
395
+ " np.float64(0.1507224589586258),\n",
396
+ " np.float64(0.18449543416500092),\n",
397
+ " np.float64(0.7271744608879089),\n",
398
+ " np.float64(0.155854731798172),\n",
399
+ " np.float64(0.10400667786598206),\n",
400
+ " np.float64(0.2794816195964813),\n",
401
+ " np.float64(0.17792661488056183)],\n",
402
+ " 'fear': [np.float64(0.9281681180000305),\n",
403
+ " np.float64(0.9425276517868042),\n",
404
+ " np.float64(0.9723208546638489),\n",
405
+ " np.float64(0.3607059419155121),\n",
406
+ " np.float64(0.09504333138465881),\n",
407
+ " np.float64(0.05136283114552498),\n",
408
+ " np.float64(0.7474274635314941),\n",
409
+ " np.float64(0.4044976532459259),\n",
410
+ " np.float64(0.9155241250991821),\n",
411
+ " np.float64(0.05136283114552498)],\n",
412
+ " 'joy': [np.float64(0.932798445224762),\n",
413
+ " np.float64(0.7044219970703125),\n",
414
+ " np.float64(0.7672380805015564),\n",
415
+ " np.float64(0.2518811821937561),\n",
416
+ " np.float64(0.04056443274021149),\n",
417
+ " np.float64(0.043375786393880844),\n",
418
+ " np.float64(0.872565746307373),\n",
419
+ " np.float64(0.04056443274021149),\n",
420
+ " np.float64(0.04056443274021149),\n",
421
+ " np.float64(0.04056443274021149)],\n",
422
+ " 'sadness': [np.float64(0.6462159752845764),\n",
423
+ " np.float64(0.8879395127296448),\n",
424
+ " np.float64(0.5494767427444458),\n",
425
+ " np.float64(0.7326844930648804),\n",
426
+ " np.float64(0.8843896985054016),\n",
427
+ " np.float64(0.621392011642456),\n",
428
+ " np.float64(0.7121941447257996),\n",
429
+ " np.float64(0.5494767427444458),\n",
430
+ " np.float64(0.840289294719696),\n",
431
+ " np.float64(0.8603722453117371)],\n",
432
+ " 'surprise': [np.float64(0.9671575427055359),\n",
433
+ " np.float64(0.11169017851352692),\n",
434
+ " np.float64(0.11169017851352692),\n",
435
+ " np.float64(0.11169017851352692),\n",
436
+ " np.float64(0.47588053345680237),\n",
437
+ " np.float64(0.11169017851352692),\n",
438
+ " np.float64(0.40800127387046814),\n",
439
+ " np.float64(0.8202826380729675),\n",
440
+ " np.float64(0.35446077585220337),\n",
441
+ " np.float64(0.11169017851352692)],\n",
442
+ " 'neutral': [np.float64(0.7296020984649658),\n",
443
+ " np.float64(0.252546101808548),\n",
444
+ " np.float64(0.07876550406217575),\n",
445
+ " np.float64(0.07876550406217575),\n",
446
+ " np.float64(0.07876550406217575),\n",
447
+ " np.float64(0.27190330624580383),\n",
448
+ " np.float64(0.07876550406217575),\n",
449
+ " np.float64(0.2344866842031479),\n",
450
+ " np.float64(0.13561423122882843),\n",
451
+ " np.float64(0.07876550406217575)]}"
452
+ ]
453
+ },
454
+ "execution_count": 14,
455
+ "metadata": {},
456
+ "output_type": "execute_result"
457
+ }
458
+ ],
459
+ "source": [
460
+ "emotion_scores"
461
+ ]
462
+ },
463
+ {
464
+ "cell_type": "code",
465
+ "execution_count": null,
466
+ "id": "61c40789-c367-4a0f-8701-6c49a1688048",
467
+ "metadata": {},
468
+ "outputs": [
469
+ {
470
+ "name": "stderr",
471
+ "output_type": "stream",
472
+ "text": [
473
+ " 1%|▍ | 27/5197 [00:24<1:01:20, 1.40it/s]"
474
+ ]
475
+ }
476
+ ],
477
+ "source": [
478
+ "from tqdm import tqdm\n",
479
+ "\n",
480
+ "emotion_labels = [\"anger\", \"disgust\", \"fear\", \"joy\", \"sadness\", \"surprise\", \"neutral\"]\n",
481
+ "isbn = []\n",
482
+ "emotion_scores = {label: [] for label in emotion_labels}\n",
483
+ "\n",
484
+ "for i in tqdm(range(len(books))):\n",
485
+ " isbn.append(books[\"isbn13\"][i])\n",
486
+ " sentences = books[\"description\"][i].split(\".\")\n",
487
+ " predictions = classifier(sentences)\n",
488
+ " max_scores = calculate_max_emotion_scores(predictions)\n",
489
+ " for label in emotion_labels:\n",
490
+ " emotion_scores[label].append(max_scores[label])"
491
+ ]
492
+ },
493
+ {
494
+ "cell_type": "code",
495
+ "execution_count": null,
496
+ "id": "36ba8f19-3bdc-4d03-afa4-9db9c93bd488",
497
+ "metadata": {},
498
+ "outputs": [],
499
+ "source": [
500
+ "emotions_df = pd.DataFrame(emotion_scores)\n",
501
+ "emotions_df[\"isbn13\"] = isbn"
502
+ ]
503
+ },
504
+ {
505
+ "cell_type": "code",
506
+ "execution_count": null,
507
+ "id": "8f415e4c-9869-4fd5-b74e-3426d3ac5cb6",
508
+ "metadata": {},
509
+ "outputs": [],
510
+ "source": [
511
+ "emotions_df"
512
+ ]
513
+ },
514
+ {
515
+ "cell_type": "code",
516
+ "execution_count": null,
517
+ "id": "b3335a2a-13af-43f8-a6e4-0d39cc90bcce",
518
+ "metadata": {},
519
+ "outputs": [],
520
+ "source": [
521
+ "# Drop old emotion columns from books\n",
522
+ "emotion_cols = [\"anger\", \"disgust\", \"fear\", \"joy\", \"sadness\", \"surprise\", \"neutral\"]\n",
523
+ "books = books.drop(columns=emotion_cols, errors='ignore') # errors='ignore' skips if columns not found\n",
524
+ "\n",
525
+ "# Now do the merge\n",
526
+ "books = pd.merge(books, emotions_df, on=\"isbn13\")\n"
527
+ ]
528
+ },
529
+ {
530
+ "cell_type": "code",
531
+ "execution_count": null,
532
+ "id": "8e3267bc-920e-4880-91d2-80eeaee583a9",
533
+ "metadata": {},
534
+ "outputs": [],
535
+ "source": [
536
+ "books.to_csv(\"books_with_emotions.csv\", index = False)"
537
+ ]
538
+ },
539
+ {
540
+ "cell_type": "code",
541
+ "execution_count": null,
542
+ "id": "53a10c8d-7a25-48ac-92a6-61e089488e0e",
543
+ "metadata": {},
544
+ "outputs": [],
545
+ "source": []
546
+ },
547
+ {
548
+ "cell_type": "code",
549
+ "execution_count": null,
550
+ "id": "5259ca8e-f373-40ee-985f-239725a7cc12",
551
+ "metadata": {},
552
+ "outputs": [],
553
+ "source": []
554
+ },
555
+ {
556
+ "cell_type": "code",
557
+ "execution_count": null,
558
+ "id": "6d147cdc-2f26-465b-8d79-1c38470bbc71",
559
+ "metadata": {},
560
+ "outputs": [],
561
+ "source": []
562
+ },
563
+ {
564
+ "cell_type": "code",
565
+ "execution_count": null,
566
+ "id": "ce4a6203-d988-473e-9545-d97a5db5527c",
567
+ "metadata": {},
568
+ "outputs": [],
569
+ "source": []
570
+ },
571
+ {
572
+ "cell_type": "code",
573
+ "execution_count": null,
574
+ "id": "8b9d9ec7-db3a-49b2-8916-157fc5c4d2af",
575
+ "metadata": {},
576
+ "outputs": [],
577
+ "source": []
578
+ },
579
+ {
580
+ "cell_type": "code",
581
+ "execution_count": null,
582
+ "id": "a36c7be4-c24d-4075-9d2e-3aa0b571c0b5",
583
+ "metadata": {},
584
+ "outputs": [],
585
+ "source": []
586
+ },
587
+ {
588
+ "cell_type": "code",
589
+ "execution_count": null,
590
+ "id": "87f1cd5b-b853-4822-be4d-4eace4316a02",
591
+ "metadata": {},
592
+ "outputs": [],
593
+ "source": []
594
+ },
595
+ {
596
+ "cell_type": "code",
597
+ "execution_count": null,
598
+ "id": "8b4e7587-2988-40d7-8642-344b20ffe895",
599
+ "metadata": {},
600
+ "outputs": [],
601
+ "source": []
602
+ },
603
+ {
604
+ "cell_type": "code",
605
+ "execution_count": null,
606
+ "id": "acb61813-beae-4dd4-b62c-3d303868891d",
607
+ "metadata": {},
608
+ "outputs": [],
609
+ "source": []
610
+ },
611
+ {
612
+ "cell_type": "code",
613
+ "execution_count": null,
614
+ "id": "54984cfc-50cf-4893-930f-be9fcab8cd03",
615
+ "metadata": {},
616
+ "outputs": [],
617
+ "source": []
618
+ },
619
+ {
620
+ "cell_type": "code",
621
+ "execution_count": null,
622
+ "id": "63f38034-c61d-4c1c-8146-4a2fbb2e76b2",
623
+ "metadata": {},
624
+ "outputs": [],
625
+ "source": []
626
+ },
627
+ {
628
+ "cell_type": "code",
629
+ "execution_count": null,
630
+ "id": "6ffb41bd-24bb-4fb6-a5f3-902d62110f71",
631
+ "metadata": {},
632
+ "outputs": [],
633
+ "source": []
634
+ },
635
+ {
636
+ "cell_type": "code",
637
+ "execution_count": null,
638
+ "id": "d6b7f6c5-124c-4f36-8971-95900fa6edce",
639
+ "metadata": {},
640
+ "outputs": [],
641
+ "source": []
642
+ },
643
+ {
644
+ "cell_type": "code",
645
+ "execution_count": null,
646
+ "id": "25d57054-8971-4baa-b8dd-23b403e3165f",
647
+ "metadata": {},
648
+ "outputs": [],
649
+ "source": []
650
+ },
651
+ {
652
+ "cell_type": "code",
653
+ "execution_count": null,
654
+ "id": "8427fc4a-54bb-4c2e-bcae-b243ae46d24f",
655
+ "metadata": {},
656
+ "outputs": [],
657
+ "source": []
658
+ },
659
+ {
660
+ "cell_type": "code",
661
+ "execution_count": null,
662
+ "id": "5f88e0cb-7e77-4295-959b-4ad239d3e63e",
663
+ "metadata": {},
664
+ "outputs": [],
665
+ "source": []
666
+ },
667
+ {
668
+ "cell_type": "code",
669
+ "execution_count": null,
670
+ "id": "5b9d2c10-1b4d-4675-a6e3-fccc3b1ff9db",
671
+ "metadata": {},
672
+ "outputs": [],
673
+ "source": []
674
+ },
675
+ {
676
+ "cell_type": "code",
677
+ "execution_count": null,
678
+ "id": "5662902f-cb8a-4fb0-aad2-c50cddba8084",
679
+ "metadata": {},
680
+ "outputs": [],
681
+ "source": []
682
+ },
683
+ {
684
+ "cell_type": "code",
685
+ "execution_count": null,
686
+ "id": "c917f161-529f-488d-9829-9a7257c6821e",
687
+ "metadata": {},
688
+ "outputs": [],
689
+ "source": []
690
+ },
691
+ {
692
+ "cell_type": "code",
693
+ "execution_count": null,
694
+ "id": "d9a0a784-fa39-4412-951f-76b66d4e05f5",
695
+ "metadata": {},
696
+ "outputs": [],
697
+ "source": []
698
+ },
699
+ {
700
+ "cell_type": "code",
701
+ "execution_count": null,
702
+ "id": "2949e3ea-2943-434f-95a9-22438cb21567",
703
+ "metadata": {},
704
+ "outputs": [],
705
+ "source": []
706
+ },
707
+ {
708
+ "cell_type": "code",
709
+ "execution_count": null,
710
+ "id": "7312f2a9-810e-4f48-a198-2256f656f2f0",
711
+ "metadata": {},
712
+ "outputs": [],
713
+ "source": []
714
+ },
715
+ {
716
+ "cell_type": "code",
717
+ "execution_count": null,
718
+ "id": "07662c44-73b2-47c0-9e54-9064511d44f9",
719
+ "metadata": {},
720
+ "outputs": [],
721
+ "source": []
722
+ },
723
+ {
724
+ "cell_type": "code",
725
+ "execution_count": null,
726
+ "id": "45fd0c42-75f2-47da-8b4b-66f7c211298a",
727
+ "metadata": {},
728
+ "outputs": [],
729
+ "source": []
730
+ },
731
+ {
732
+ "cell_type": "code",
733
+ "execution_count": null,
734
+ "id": "99580564-5d8e-4b5a-8dea-49fdf0414876",
735
+ "metadata": {},
736
+ "outputs": [],
737
+ "source": []
738
+ },
739
+ {
740
+ "cell_type": "code",
741
+ "execution_count": null,
742
+ "id": "a594e162-c2e1-4202-a590-e96e63ba984f",
743
+ "metadata": {},
744
+ "outputs": [],
745
+ "source": []
746
+ },
747
+ {
748
+ "cell_type": "code",
749
+ "execution_count": null,
750
+ "id": "678eb881-c80c-47fe-bcc2-48ce8fc19b34",
751
+ "metadata": {},
752
+ "outputs": [],
753
+ "source": []
754
+ },
755
+ {
756
+ "cell_type": "code",
757
+ "execution_count": null,
758
+ "id": "f40866c7-3882-4867-a1db-92e5c27e4d3e",
759
+ "metadata": {},
760
+ "outputs": [],
761
+ "source": []
762
+ },
763
+ {
764
+ "cell_type": "code",
765
+ "execution_count": null,
766
+ "id": "a8c1d89e-53b6-4b74-ab65-0235cf0c7abd",
767
+ "metadata": {},
768
+ "outputs": [],
769
+ "source": []
770
+ },
771
+ {
772
+ "cell_type": "code",
773
+ "execution_count": null,
774
+ "id": "15ad11f8-d5fa-47c6-8fc6-d3c60badb3a0",
775
+ "metadata": {},
776
+ "outputs": [],
777
+ "source": []
778
+ },
779
+ {
780
+ "cell_type": "code",
781
+ "execution_count": null,
782
+ "id": "5ca0bc6e-18cd-4f74-ba65-959fc58f5a4c",
783
+ "metadata": {},
784
+ "outputs": [],
785
+ "source": []
786
+ },
787
+ {
788
+ "cell_type": "code",
789
+ "execution_count": null,
790
+ "id": "df8895f9-9e8f-4f80-91bd-1f48ba9f7716",
791
+ "metadata": {},
792
+ "outputs": [],
793
+ "source": []
794
+ },
795
+ {
796
+ "cell_type": "code",
797
+ "execution_count": null,
798
+ "id": "e4161ddf-71fe-4323-b750-099d42b52174",
799
+ "metadata": {},
800
+ "outputs": [],
801
+ "source": []
802
+ },
803
+ {
804
+ "cell_type": "code",
805
+ "execution_count": null,
806
+ "id": "aac7d70a-c92a-47d9-83ed-7bf0d2e170f6",
807
+ "metadata": {},
808
+ "outputs": [],
809
+ "source": []
810
+ },
811
+ {
812
+ "cell_type": "code",
813
+ "execution_count": null,
814
+ "id": "9a438b5b-c5b7-4abe-80f6-d90217e22649",
815
+ "metadata": {},
816
+ "outputs": [],
817
+ "source": []
818
+ },
819
+ {
820
+ "cell_type": "code",
821
+ "execution_count": null,
822
+ "id": "ecca9640-fda8-4d3a-906e-952b400d0900",
823
+ "metadata": {},
824
+ "outputs": [],
825
+ "source": []
826
+ },
827
+ {
828
+ "cell_type": "code",
829
+ "execution_count": null,
830
+ "id": "68cb657a-d404-431f-a446-390a6e2106a7",
831
+ "metadata": {},
832
+ "outputs": [],
833
+ "source": []
834
+ },
835
+ {
836
+ "cell_type": "code",
837
+ "execution_count": null,
838
+ "id": "6e53f5fc-683e-4957-9fc1-dd5560c32136",
839
+ "metadata": {},
840
+ "outputs": [],
841
+ "source": []
842
+ },
843
+ {
844
+ "cell_type": "code",
845
+ "execution_count": null,
846
+ "id": "61e5cf29-fe64-4ee7-b955-bed4ad3c0e98",
847
+ "metadata": {},
848
+ "outputs": [],
849
+ "source": []
850
+ },
851
+ {
852
+ "cell_type": "code",
853
+ "execution_count": null,
854
+ "id": "8a377345-9277-4fe1-a036-fdf0c397b257",
855
+ "metadata": {},
856
+ "outputs": [],
857
+ "source": []
858
+ }
859
+ ],
860
+ "metadata": {
861
+ "kernelspec": {
862
+ "display_name": "Python 3 (ipykernel)",
863
+ "language": "python",
864
+ "name": "python3"
865
+ },
866
+ "language_info": {
867
+ "codemirror_mode": {
868
+ "name": "ipython",
869
+ "version": 3
870
+ },
871
+ "file_extension": ".py",
872
+ "mimetype": "text/x-python",
873
+ "name": "python",
874
+ "nbconvert_exporter": "python",
875
+ "pygments_lexer": "ipython3",
876
+ "version": "3.11.9"
877
+ }
878
+ },
879
+ "nbformat": 4,
880
+ "nbformat_minor": 5
881
+ }
text_classification.ipynb ADDED
The diff for this file is too large to render. See raw diff