-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic_modelling.html
6309 lines (6210 loc) · 874 KB
/
topic_modelling.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Olivier Caron">
<meta name="author" content="Christophe Benavent">
<meta name="dcterms.date" content="2023-10-03">
<title>Systematic literature review</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging-indent div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}</style>
<script src="topic_modelling_files/libs/clipboard/clipboard.min.js"></script>
<script src="topic_modelling_files/libs/quarto-html/quarto.js"></script>
<script src="topic_modelling_files/libs/quarto-html/popper.min.js"></script>
<script src="topic_modelling_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="topic_modelling_files/libs/quarto-html/anchor.min.js"></script>
<link href="topic_modelling_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="topic_modelling_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
<link href="topic_modelling_files/libs/quarto-html/quarto-syntax-highlighting-dark.css" rel="prefetch" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
<script src="topic_modelling_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="topic_modelling_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="topic_modelling_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
<link href="topic_modelling_files/libs/bootstrap/bootstrap-dark.min.css" rel="prefetch" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header id="title-block-header" class="quarto-title-block default toc-left page-columns page-full">
<div class="quarto-title-banner page-columns page-full">
<div class="quarto-title column-body">
<div class="quarto-title-block"><div><h1 class="title">Systematic literature review</h1><button type="button" class="btn code-tools-button dropdown-toggle" id="quarto-code-tools-menu" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi"></i> Code</button><ul class="dropdown-menu dropdown-menu-end" aria-labelelledby="quarto-code-tools-menu"><li><a id="quarto-show-all-code" class="dropdown-item" href="javascript:void(0)" role="button">Show All Code</a></li><li><a id="quarto-hide-all-code" class="dropdown-item" href="javascript:void(0)" role="button">Hide All Code</a></li><li><hr class="dropdown-divider"></li><li><a id="quarto-view-source" class="dropdown-item" href="javascript:void(0)" role="button">View Source</a></li></ul></div></div>
<p class="subtitle lead">Topic modelling</p>
</div>
</div>
<div class="quarto-title-meta-author">
<div class="quarto-title-meta-heading">Authors</div>
<div class="quarto-title-meta-heading">Affiliation</div>
<div class="quarto-title-meta-contents">
<p class="author">Olivier Caron </p>
</div>
<div class="quarto-title-meta-contents">
<p class="affiliation">
Paris Dauphine - PSL
</p>
</div>
<div class="quarto-title-meta-contents">
<p class="author">Christophe Benavent </p>
</div>
<div class="quarto-title-meta-contents">
<p class="affiliation">
Paris Dauphine - PSL
</p>
</div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">October 3, 2023</p>
</div>
</div>
</div>
</header><div id="quarto-content" class="page-columns page-rows-contents page-layout-article toc-left">
<div id="quarto-sidebar-toc-left" class="sidebar toc-left">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#libraries-r" id="toc-libraries-r" class="nav-link active" data-scroll-target="#libraries-r"><span class="header-section-number">1</span> Libraries R</a></li>
<li><a href="#loading-data-r" id="toc-loading-data-r" class="nav-link" data-scroll-target="#loading-data-r"><span class="header-section-number">2</span> Loading data R</a></li>
<li><a href="#a-glimpse-of-data" id="toc-a-glimpse-of-data" class="nav-link" data-scroll-target="#a-glimpse-of-data"><span class="header-section-number">3</span> A glimpse of data</a></li>
<li><a href="#python-libraries-and-loading-data" id="toc-python-libraries-and-loading-data" class="nav-link" data-scroll-target="#python-libraries-and-loading-data"><span class="header-section-number">4</span> Python libraries and loading data</a></li>
<li><a href="#cuda-status-and-device-info" id="toc-cuda-status-and-device-info" class="nav-link" data-scroll-target="#cuda-status-and-device-info"><span class="header-section-number">5</span> CUDA Status and Device Info</a></li>
<li><a href="#detect-interpretable-topics-with-bertopic" id="toc-detect-interpretable-topics-with-bertopic" class="nav-link" data-scroll-target="#detect-interpretable-topics-with-bertopic"><span class="header-section-number">6</span> Detect interpretable topics with BERTopic</a>
<ul class="collapse">
<li><a href="#function-to-create-bertopics-with-custom-embeddings" id="toc-function-to-create-bertopics-with-custom-embeddings" class="nav-link" data-scroll-target="#function-to-create-bertopics-with-custom-embeddings"><span class="header-section-number">6.1</span> Function to create BERTopics with custom embeddings</a></li>
<li><a href="#function-to-visualize-bertopics" id="toc-function-to-visualize-bertopics" class="nav-link" data-scroll-target="#function-to-visualize-bertopics"><span class="header-section-number">6.2</span> Function to visualize BERTopics</a></li>
<li><a href="#create-topics-for-a-list-of-embeddings-model" id="toc-create-topics-for-a-list-of-embeddings-model" class="nav-link" data-scroll-target="#create-topics-for-a-list-of-embeddings-model"><span class="header-section-number">6.3</span> Create topics for a list of embeddings model</a></li>
</ul></li>
<li><a href="#topics-results" id="toc-topics-results" class="nav-link" data-scroll-target="#topics-results"><span class="header-section-number">7</span> Topics Results</a>
<ul class="collapse">
<li><a href="#representative-documents-of-topics" id="toc-representative-documents-of-topics" class="nav-link" data-scroll-target="#representative-documents-of-topics"><span class="header-section-number">7.1</span> Representative documents of topics</a></li>
<li><a href="#distribution-of-topics" id="toc-distribution-of-topics" class="nav-link" data-scroll-target="#distribution-of-topics"><span class="header-section-number">7.2</span> Distribution of topics</a></li>
<li><a href="#visualize-how-each-token-contributes-to-a-specific-topic." id="toc-visualize-how-each-token-contributes-to-a-specific-topic." class="nav-link" data-scroll-target="#visualize-how-each-token-contributes-to-a-specific-topic."><span class="header-section-number">7.3</span> Visualize how each token contributes to a specific topic.</a></li>
<li><a href="#some-visualizations-of-the-topics" id="toc-some-visualizations-of-the-topics" class="nav-link" data-scroll-target="#some-visualizations-of-the-topics"><span class="header-section-number">7.4</span> Some visualizations of the topics</a></li>
<li><a href="#wordclouds-of-bertopics" id="toc-wordclouds-of-bertopics" class="nav-link" data-scroll-target="#wordclouds-of-bertopics"><span class="header-section-number">7.5</span> Wordclouds of BERTopics</a></li>
</ul></li>
<li><a href="#bertopic-with-custom-embeddings-model-xlnet" id="toc-bertopic-with-custom-embeddings-model-xlnet" class="nav-link" data-scroll-target="#bertopic-with-custom-embeddings-model-xlnet"><span class="header-section-number">8</span> BERTopic with Custom Embeddings model (xlnet)</a></li>
<li><a href="#word2vec-model-tokenization-with-nltk" id="toc-word2vec-model-tokenization-with-nltk" class="nav-link" data-scroll-target="#word2vec-model-tokenization-with-nltk"><span class="header-section-number">9</span> Word2Vec model (tokenization with nltk)</a>
<ul class="collapse">
<li><a href="#plot-similar-words-for-learning" id="toc-plot-similar-words-for-learning" class="nav-link" data-scroll-target="#plot-similar-words-for-learning"><span class="header-section-number">9.1</span> Plot similar words for “learning”</a></li>
<li><a href="#plot-word2vec-embeddings-in-3d-with-plotly" id="toc-plot-word2vec-embeddings-in-3d-with-plotly" class="nav-link" data-scroll-target="#plot-word2vec-embeddings-in-3d-with-plotly"><span class="header-section-number">9.2</span> Plot Word2Vec embeddings in 3D with plotly</a></li>
<li><a href="#plot-word2vec-embeddings-in-2d-with-plotly" id="toc-plot-word2vec-embeddings-in-2d-with-plotly" class="nav-link" data-scroll-target="#plot-word2vec-embeddings-in-2d-with-plotly"><span class="header-section-number">9.3</span> Plot Word2Vec embeddings in 2D with plotly</a></li>
</ul></li>
<li><a href="#plotting-authors-and-text-based-on-bertopic" id="toc-plotting-authors-and-text-based-on-bertopic" class="nav-link" data-scroll-target="#plotting-authors-and-text-based-on-bertopic"><span class="header-section-number">10</span> Plotting authors and text based on BERTopic</a>
<ul class="collapse">
<li><a href="#d-plot-of-authors-t-sne-bertopic-clustering" id="toc-d-plot-of-authors-t-sne-bertopic-clustering" class="nav-link" data-scroll-target="#d-plot-of-authors-t-sne-bertopic-clustering"><span class="header-section-number">10.1</span> 3D plot of authors (t-SNE + BERTopic clustering)</a></li>
<li><a href="#d-plot-of-authors-t-sne-bertopic-clustering-1" id="toc-d-plot-of-authors-t-sne-bertopic-clustering-1" class="nav-link" data-scroll-target="#d-plot-of-authors-t-sne-bertopic-clustering-1"><span class="header-section-number">10.2</span> 2D plot of authors (t-SNE + BERTopic clustering)</a></li>
</ul></li>
</ul>
</nav>
</div>
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
</div>
<main class="content quarto-banner-title-block page-columns page-full" id="quarto-document-content">
<section id="libraries-r" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="libraries-r"><span class="header-section-number">1</span> Libraries R</h2>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gt)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="loading-data-r" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="loading-data-r"><span class="header-section-number">2</span> Loading data R</h2>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-2"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-2-1"><a href="#annotated-cell-2-1" aria-hidden="true" tabindex="-1"></a>list_articles <span class="ot"><-</span> <span class="fu">read.csv2</span>(<span class="st">"nlp_full_data_final_18-08-2023.csv"</span>, <span class="at">encoding =</span> <span class="st">"UTF-8"</span>) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-2"><a href="#annotated-cell-2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="st">"entry_number"</span> <span class="ot">=</span> <span class="dv">1</span>)</span>
<span id="annotated-cell-2-3"><a href="#annotated-cell-2-3" aria-hidden="true" tabindex="-1"></a>list_references <span class="ot"><-</span> <span class="fu">read.csv2</span>(<span class="st">"nlp_references_final_18-08-2023.csv"</span>, <span class="at">encoding =</span> <span class="st">"UTF-8"</span>) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-4"><a href="#annotated-cell-2-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="st">"citing_art"</span> <span class="ot">=</span> <span class="dv">1</span>)</span>
<span id="annotated-cell-2-5"><a href="#annotated-cell-2-5" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(list_articles) <span class="ot"><-</span> <span class="fu">gsub</span>(<span class="st">"</span><span class="sc">\\</span><span class="st">.+"</span>, <span class="st">"_"</span>, <span class="fu">colnames</span>(list_articles))</span>
<span id="annotated-cell-2-6"><a href="#annotated-cell-2-6" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(list_articles) <span class="ot"><-</span> <span class="fu">gsub</span>(<span class="st">"^[[:punct:]]+|[[:punct:]]+$"</span>, <span class="st">""</span>, <span class="fu">colnames</span>(list_articles))</span>
<span id="annotated-cell-2-7"><a href="#annotated-cell-2-7" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(list_references) <span class="ot"><-</span> <span class="fu">gsub</span>(<span class="st">"</span><span class="sc">\\</span><span class="st">.+"</span>, <span class="st">"_"</span>, <span class="fu">colnames</span>(list_references))</span>
<span id="annotated-cell-2-8"><a href="#annotated-cell-2-8" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(list_references) <span class="ot"><-</span> <span class="fu">gsub</span>(<span class="st">"^[[:punct:]]+|[[:punct:]]+$"</span>, <span class="st">""</span>, <span class="fu">colnames</span>(list_references))</span>
<span id="annotated-cell-2-9"><a href="#annotated-cell-2-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-2-10"><a href="#annotated-cell-2-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-2-11"><a href="#annotated-cell-2-11" aria-hidden="true" tabindex="-1"></a>data_embeddings <span class="ot"><-</span> list_articles <span class="sc">%>%</span></span>
<span id="annotated-cell-2-12"><a href="#annotated-cell-2-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">distinct</span>(entry_number, <span class="at">.keep_all =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-13"><a href="#annotated-cell-2-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(marketing <span class="sc">==</span> <span class="dv">1</span>) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-14"><a href="#annotated-cell-2-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="st">"year"</span> <span class="ot">=</span> <span class="fu">substr</span>(prism_coverDate, <span class="dv">7</span>, <span class="dv">10</span>)) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-15"><a href="#annotated-cell-2-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">keywords =</span> <span class="fu">str_replace_all</span>(authkeywords, <span class="st">"</span><span class="sc">\\</span><span class="st">|"</span>, <span class="st">""</span>)) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-16"><a href="#annotated-cell-2-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">keywords =</span> <span class="fu">str_squish</span>(keywords)) <span class="sc">%>%</span></span>
<span id="annotated-cell-2-17"><a href="#annotated-cell-2-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="st">"combined_text"</span> <span class="ot">=</span> <span class="fu">paste0</span>(dc_title,<span class="st">". "</span>, dc_description, <span class="st">". "</span>, keywords))</span>
<span id="annotated-cell-2-18"><a href="#annotated-cell-2-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-2-19"><a href="#annotated-cell-2-19" aria-hidden="true" tabindex="-1"></a><span class="co">#write.csv(data_embeddings,"data_for_embeddings.csv")</span></span>
<span id="annotated-cell-2-20"><a href="#annotated-cell-2-20" aria-hidden="true" tabindex="-1"></a><span class="co">#data_embeddings <- read.csv("data_for_embeddings.csv")</span></span>
<span id="annotated-cell-2-21"><a href="#annotated-cell-2-21" aria-hidden="true" tabindex="-1"></a><span class="co">#embeddings <- read.csv("embeddings_bge.csv")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="a-glimpse-of-data" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="a-glimpse-of-data"><span class="header-section-number">3</span> A glimpse of data</h2>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>data_embeddings <span class="sc">%>%</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">2</span>) <span class="sc">%>%</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(entry_number, dc_creator, combined_text, year) <span class="sc">%>%</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">gt</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<div id="mjauxfpjii" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#mjauxfpjii table {
font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
#mjauxfpjii thead, #mjauxfpjii tbody, #mjauxfpjii tfoot, #mjauxfpjii tr, #mjauxfpjii td, #mjauxfpjii th {
border-style: none;
}
#mjauxfpjii p {
margin: 0;
padding: 0;
}
#mjauxfpjii .gt_table {
display: table;
border-collapse: collapse;
line-height: normal;
margin-left: auto;
margin-right: auto;
color: #333333;
font-size: 16px;
font-weight: normal;
font-style: normal;
background-color: #FFFFFF;
width: auto;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #A8A8A8;
border-right-style: none;
border-right-width: 2px;
border-right-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #A8A8A8;
border-left-style: none;
border-left-width: 2px;
border-left-color: #D3D3D3;
}
#mjauxfpjii .gt_caption {
padding-top: 4px;
padding-bottom: 4px;
}
#mjauxfpjii .gt_title {
color: #333333;
font-size: 125%;
font-weight: initial;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 5px;
padding-right: 5px;
border-bottom-color: #FFFFFF;
border-bottom-width: 0;
}
#mjauxfpjii .gt_subtitle {
color: #333333;
font-size: 85%;
font-weight: initial;
padding-top: 3px;
padding-bottom: 5px;
padding-left: 5px;
padding-right: 5px;
border-top-color: #FFFFFF;
border-top-width: 0;
}
#mjauxfpjii .gt_heading {
background-color: #FFFFFF;
text-align: center;
border-bottom-color: #FFFFFF;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
}
#mjauxfpjii .gt_bottom_border {
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
}
#mjauxfpjii .gt_col_headings {
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
}
#mjauxfpjii .gt_col_heading {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: normal;
text-transform: inherit;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
vertical-align: bottom;
padding-top: 5px;
padding-bottom: 6px;
padding-left: 5px;
padding-right: 5px;
overflow-x: hidden;
}
#mjauxfpjii .gt_column_spanner_outer {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: normal;
text-transform: inherit;
padding-top: 0;
padding-bottom: 0;
padding-left: 4px;
padding-right: 4px;
}
#mjauxfpjii .gt_column_spanner_outer:first-child {
padding-left: 0;
}
#mjauxfpjii .gt_column_spanner_outer:last-child {
padding-right: 0;
}
#mjauxfpjii .gt_column_spanner {
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
vertical-align: bottom;
padding-top: 5px;
padding-bottom: 5px;
overflow-x: hidden;
display: inline-block;
width: 100%;
}
#mjauxfpjii .gt_spanner_row {
border-bottom-style: hidden;
}
#mjauxfpjii .gt_group_heading {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
text-transform: inherit;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
vertical-align: middle;
text-align: left;
}
#mjauxfpjii .gt_empty_group_heading {
padding: 0.5px;
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
vertical-align: middle;
}
#mjauxfpjii .gt_from_md > :first-child {
margin-top: 0;
}
#mjauxfpjii .gt_from_md > :last-child {
margin-bottom: 0;
}
#mjauxfpjii .gt_row {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
margin: 10px;
border-top-style: solid;
border-top-width: 1px;
border-top-color: #D3D3D3;
border-left-style: none;
border-left-width: 1px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 1px;
border-right-color: #D3D3D3;
vertical-align: middle;
overflow-x: hidden;
}
#mjauxfpjii .gt_stub {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
text-transform: inherit;
border-right-style: solid;
border-right-width: 2px;
border-right-color: #D3D3D3;
padding-left: 5px;
padding-right: 5px;
}
#mjauxfpjii .gt_stub_row_group {
color: #333333;
background-color: #FFFFFF;
font-size: 100%;
font-weight: initial;
text-transform: inherit;
border-right-style: solid;
border-right-width: 2px;
border-right-color: #D3D3D3;
padding-left: 5px;
padding-right: 5px;
vertical-align: top;
}
#mjauxfpjii .gt_row_group_first td {
border-top-width: 2px;
}
#mjauxfpjii .gt_row_group_first th {
border-top-width: 2px;
}
#mjauxfpjii .gt_summary_row {
color: #333333;
background-color: #FFFFFF;
text-transform: inherit;
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
}
#mjauxfpjii .gt_first_summary_row {
border-top-style: solid;
border-top-color: #D3D3D3;
}
#mjauxfpjii .gt_first_summary_row.thick {
border-top-width: 2px;
}
#mjauxfpjii .gt_last_summary_row {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
}
#mjauxfpjii .gt_grand_summary_row {
color: #333333;
background-color: #FFFFFF;
text-transform: inherit;
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
}
#mjauxfpjii .gt_first_grand_summary_row {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
border-top-style: double;
border-top-width: 6px;
border-top-color: #D3D3D3;
}
#mjauxfpjii .gt_last_grand_summary_row_top {
padding-top: 8px;
padding-bottom: 8px;
padding-left: 5px;
padding-right: 5px;
border-bottom-style: double;
border-bottom-width: 6px;
border-bottom-color: #D3D3D3;
}
#mjauxfpjii .gt_striped {
background-color: rgba(128, 128, 128, 0.05);
}
#mjauxfpjii .gt_table_body {
border-top-style: solid;
border-top-width: 2px;
border-top-color: #D3D3D3;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
}
#mjauxfpjii .gt_footnotes {
color: #333333;
background-color: #FFFFFF;
border-bottom-style: none;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 2px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 2px;
border-right-color: #D3D3D3;
}
#mjauxfpjii .gt_footnote {
margin: 0px;
font-size: 90%;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 5px;
padding-right: 5px;
}
#mjauxfpjii .gt_sourcenotes {
color: #333333;
background-color: #FFFFFF;
border-bottom-style: none;
border-bottom-width: 2px;
border-bottom-color: #D3D3D3;
border-left-style: none;
border-left-width: 2px;
border-left-color: #D3D3D3;
border-right-style: none;
border-right-width: 2px;
border-right-color: #D3D3D3;
}
#mjauxfpjii .gt_sourcenote {
font-size: 90%;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 5px;
padding-right: 5px;
}
#mjauxfpjii .gt_left {
text-align: left;
}
#mjauxfpjii .gt_center {
text-align: center;
}
#mjauxfpjii .gt_right {
text-align: right;
font-variant-numeric: tabular-nums;
}
#mjauxfpjii .gt_font_normal {
font-weight: normal;
}
#mjauxfpjii .gt_font_bold {
font-weight: bold;
}
#mjauxfpjii .gt_font_italic {
font-style: italic;
}
#mjauxfpjii .gt_super {
font-size: 65%;
}
#mjauxfpjii .gt_footnote_marks {
font-size: 75%;
vertical-align: 0.4em;
position: initial;
}
#mjauxfpjii .gt_asterisk {
font-size: 100%;
vertical-align: 0;
}
#mjauxfpjii .gt_indent_1 {
text-indent: 5px;
}
#mjauxfpjii .gt_indent_2 {
text-indent: 10px;
}
#mjauxfpjii .gt_indent_3 {
text-indent: 15px;
}
#mjauxfpjii .gt_indent_4 {
text-indent: 20px;
}
#mjauxfpjii .gt_indent_5 {
text-indent: 25px;
}
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings">
<th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="entry_number">entry_number</th>
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="dc_creator">dc_creator</th>
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="combined_text">combined_text</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="year">year</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr><td headers="entry_number" class="gt_row gt_right">1</td>
<td headers="dc_creator" class="gt_row gt_left">Loupos P.</td>
<td headers="combined_text" class="gt_row gt_left">What reviews foretell about opening weekend box office revenue: the harbinger of failure effect in the movie industry. We empirically investigate the harbinger of failure phenomenon in the motion picture industry by analyzing the pre-release reviews written on movies by film critics. We find that harbingers of failure do exist. Their positive (negative) pre-release movie reviews provide a strong predictive signal that the movie will turn out to be a flop (success). This signal persists even for the top critic category, which usually consists of professional critics, indicating that having expertise in a professional domain does not necessarily lead to correct predictions. Our findings challenge the current belief that positive reviews always help enhance box office revenue and shed new light on the influencer-predictor hypothesis. We further analyze the writing style of harbingers and provide new insights into their personality traits and cognitive biases.. Harbingers of failure Movies Preference heterogeneity Reviews Text analytics</td>
<td headers="year" class="gt_row gt_right">2023</td></tr>
<tr><td headers="entry_number" class="gt_row gt_right">2</td>
<td headers="dc_creator" class="gt_row gt_left">Krefeld-Schwalb A.</td>
<td headers="combined_text" class="gt_row gt_left">Tighter nets for smaller fishes? Mapping the development of statistical practices in consumer research between 2008 and 2020. During the last decade, confidence in many social sciences, including consumer research, has been undermined by doubts about the replicability of empirical research findings. These doubts have led to increased calls to improve research practices and adopt new measures to increase the replicability of published work from various stakeholders such as funding agencies, journals, and scholars themselves. Despite these demands, it is unclear to which the research published in the leading consumer research journals has adhered to these calls for change. This article provides the first systematic empirical analysis of this question by surveying three crucial statistics of published consumer research over time: sample sizes, effect sizes, and the distribution of published p values. The authors compile a hand-coded sample of N = 258 articles published between 2008 and 2020 in the Journal of Consumer Psychology, the Journal of Consumer Research, and the Journal of Marketing Research. An automated text analysis across all publications in these three journals corroborates the representativeness of the hand-coded sample. Results reveal a substantial increase in sample sizes above and beyond the use of online samples along with a decrease in reported effect sizes. Effect and samples sizes are highly correlated which at least partially explains the reduction in reported effect sizes.. Experimental research methods False-positive results Review</td>
<td headers="year" class="gt_row gt_right">2023</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</section>
<section id="python-libraries-and-loading-data" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="python-libraries-and-loading-data"><span class="header-section-number">4</span> Python libraries and loading data</h2>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> warnings</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>warnings.filterwarnings(<span class="st">"ignore"</span>, message<span class="op">=</span><span class="st">".*The 'nopython' keyword.*"</span>)</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> nltk</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> os</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> palettable</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> plotly.express <span class="im">as</span> px</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> plotly.io <span class="im">as</span> pio</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> string</span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> stylecloud</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> time</span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch</span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> umap.umap_ <span class="im">as</span> umap</span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> bertopic <span class="im">import</span> BERTopic</span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> bertopic.vectorizers <span class="im">import</span> ClassTfidfTransformer</span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> bertopic.representation <span class="im">import</span> MaximalMarginalRelevance</span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> gensim.models <span class="im">import</span> Word2Vec</span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> nltk.corpus <span class="im">import</span> stopwords</span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> nltk.tokenize <span class="im">import</span> word_tokenize</span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> palettable <span class="im">import</span> colorbrewer</span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sentence_transformers <span class="im">import</span> SentenceTransformer, util</span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.cluster <span class="im">import</span> DBSCAN</span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.decomposition <span class="im">import</span> PCA</span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.feature_extraction.text <span class="im">import</span> CountVectorizer</span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.manifold <span class="im">import</span> TSNE</span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.metrics <span class="im">import</span> davies_bouldin_score, silhouette_score, silhouette_samples</span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tabulate <span class="im">import</span> tabulate</span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tqdm <span class="im">import</span> tqdm</span>
<span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> transformers <span class="im">import</span> XLNetTokenizer, XLNetModel</span>
<span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> yellowbrick.cluster <span class="im">import</span> SilhouetteVisualizer</span>
<span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> wordcloud <span class="im">import</span> WordCloud</span>
<span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.read_csv(<span class="st">"data_for_embeddings.csv"</span>)</span>
<span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a><span class="co">#df['title_abstract'] = df['dc_title'].astype(str) + '. ' + df['dc_description'].astype(str)</span></span>
<span id="cb3-42"><a href="#cb3-42" aria-hidden="true" tabindex="-1"></a>docs_marketing <span class="op">=</span> df[<span class="st">"combined_text"</span>].tolist()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="cuda-status-and-device-info" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="cuda-status-and-device-info"><span class="header-section-number">5</span> CUDA Status and Device Info</h2>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Is CUDA supported by this system? </span><span class="sc">{</span>torch<span class="sc">.</span>cuda<span class="sc">.</span>is_available()<span class="sc">}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Is CUDA supported by this system? True</code></pre>
</div>
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"CUDA version: </span><span class="sc">{</span>torch<span class="sc">.</span>version<span class="sc">.</span>cuda<span class="sc">}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>CUDA version: 12.1</code></pre>
</div>
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Storing ID of the current CUDA device</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>cuda_id <span class="op">=</span> torch.cuda.current_device()</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"ID of the current CUDA device: </span><span class="sc">{</span>cuda_id<span class="sc">}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>ID of the current CUDA device: 0</code></pre>
</div>
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Name of the current CUDA device: </span><span class="sc">{</span>torch<span class="sc">.</span>cuda<span class="sc">.</span>get_device_name(cuda_id)<span class="sc">}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Name of the current CUDA device: NVIDIA GeForce RTX 3070</code></pre>
</div>
</div>
</section>
<section id="detect-interpretable-topics-with-bertopic" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="detect-interpretable-topics-with-bertopic"><span class="header-section-number">6</span> Detect interpretable topics with BERTopic</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>We use a CountVectorizer which enables us to specify the range of the ngram we want in our topic model. We can use it before or after the topic modelling (update topic).<br>
Here we use it before the topic modelling to exclude english stopwords, but after the embeddings process so that the foundation provided by stopwords in sentences is preserved in context.</p>
</div>
</div>
<section id="function-to-create-bertopics-with-custom-embeddings" class="level3" data-number="6.1">
<h3 data-number="6.1" class="anchored" data-anchor-id="function-to-create-bertopics-with-custom-embeddings"><span class="header-section-number">6.1</span> Function to create BERTopics with custom embeddings</h3>
<p>The aim of this function is to swiftly create various BERTopic experiments while maintaining the same parameters, except for the choice of the embedding model. This enables the generation of distinct BERTopic results, facilitating meaningful comparisons among them.</p>
<p>Some explanations:</p>
<table class="table">
<thead>
<tr class="header">
<th>Parameter name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>docs</td>
<td>The documents we want to analyze (list).</td>
</tr>
<tr class="even">
<td>embeddings_model</td>
<td>Specifies the embeddings model we want to load and use.</td>
</tr>
<tr class="odd">
<td>min_topic_size</td>
<td>It is used to specify what the minimum size of a topic can be. See <a href="https://maartengr.github.io/BERTopic/getting_started/parameter%20tuning/parametertuning.html#min_topic_size" title="min_topic_size">BERTopic documentation</a>.</td>
</tr>
<tr class="even">
<td>nr_topics</td>
<td>The number of topics we want to reduce our results to. See <a href="https://maartengr.github.io/BERTopic/getting_started/parameter%20tuning/parametertuning.html#nr_topics" title="min_topic_size">BERTopic documentation</a>.</td>
</tr>
</tbody>
</table>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> create_bertopic(docs, embeddings_model, min_topic_size, nr_topics):</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># initialize a count-based tf-idf transformer</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> ctfidf_model <span class="op">=</span> ClassTfidfTransformer(reduce_frequent_words<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># initialize a sentence transformer model for embeddings</span></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> sentence_model <span class="op">=</span> SentenceTransformer(embeddings_model, device<span class="op">=</span><span class="st">'cuda'</span>)</span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># generate embeddings for the input documents</span></span>
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a> embeddings <span class="op">=</span> sentence_model.encode(docs, show_progress_bar<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># create the representation model</span></span>
<span id="cb12-13"><a href="#cb12-13" aria-hidden="true" tabindex="-1"></a> <span class="co">#representation_model = MaximalMarginalRelevance(diversity=1)</span></span>
<span id="cb12-14"><a href="#cb12-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># create a bertopic model with specified parameters</span></span>
<span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a> topic_model <span class="op">=</span> BERTopic(</span>
<span id="cb12-17"><a href="#cb12-17" aria-hidden="true" tabindex="-1"></a> ctfidf_model<span class="op">=</span>ctfidf_model,</span>
<span id="cb12-18"><a href="#cb12-18" aria-hidden="true" tabindex="-1"></a> calculate_probabilities<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a> verbose<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb12-20"><a href="#cb12-20" aria-hidden="true" tabindex="-1"></a> min_topic_size<span class="op">=</span>min_topic_size,</span>
<span id="cb12-21"><a href="#cb12-21" aria-hidden="true" tabindex="-1"></a> nr_topics<span class="op">=</span>nr_topics,</span>
<span id="cb12-22"><a href="#cb12-22" aria-hidden="true" tabindex="-1"></a> top_n_words<span class="op">=</span><span class="dv">20</span></span>
<span id="cb12-23"><a href="#cb12-23" aria-hidden="true" tabindex="-1"></a> <span class="co">#representation_model=representation_model</span></span>
<span id="cb12-24"><a href="#cb12-24" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb12-25"><a href="#cb12-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-26"><a href="#cb12-26" aria-hidden="true" tabindex="-1"></a> <span class="co"># fit the bertopic model to the input documents and embeddings</span></span>
<span id="cb12-27"><a href="#cb12-27" aria-hidden="true" tabindex="-1"></a> topics, probs <span class="op">=</span> topic_model.fit_transform(docs, embeddings)</span>
<span id="cb12-28"><a href="#cb12-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-29"><a href="#cb12-29" aria-hidden="true" tabindex="-1"></a> <span class="co"># update the vectorizer model used by bertopic</span></span>
<span id="cb12-30"><a href="#cb12-30" aria-hidden="true" tabindex="-1"></a> <span class="co"># `min_df` is the minimum document frequency for terms (words or n-grams) in the CountVectorizer.</span></span>
<span id="cb12-31"><a href="#cb12-31" aria-hidden="true" tabindex="-1"></a> updated_vectorizer_model <span class="op">=</span> CountVectorizer(stop_words<span class="op">=</span><span class="st">"english"</span>, ngram_range<span class="op">=</span>(<span class="dv">1</span>, <span class="dv">3</span>), min_df<span class="op">=</span><span class="dv">3</span>)</span>
<span id="cb12-32"><a href="#cb12-32" aria-hidden="true" tabindex="-1"></a> topic_model.update_topics(docs, vectorizer_model<span class="op">=</span>updated_vectorizer_model)</span>
<span id="cb12-33"><a href="#cb12-33" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-34"><a href="#cb12-34" aria-hidden="true" tabindex="-1"></a> <span class="co"># return the trained bertopic model</span></span>
<span id="cb12-35"><a href="#cb12-35" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> topic_model</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="function-to-visualize-bertopics" class="level3" data-number="6.2">
<h3 data-number="6.2" class="anchored" data-anchor-id="function-to-visualize-bertopics"><span class="header-section-number">6.2</span> Function to visualize BERTopics</h3>
<p>Creates a folder in Images/ with the model_name input with various plots in html files.</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> generate_topics_table(topic_model):</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># get topic information from the model</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> topics_info <span class="op">=</span> topic_model.get_topic_info()</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># check if topics_info is empty or None</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> topics_info <span class="kw">is</span> <span class="va">None</span> <span class="kw">or</span> topics_info.empty:</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"No topics found."</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># convert the data into a list</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a> data_as_list <span class="op">=</span> topics_info.values.tolist()</span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># get column names as headers</span></span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true" tabindex="-1"></a> headers <span class="op">=</span> topics_info.columns.tolist()</span>
<span id="cb13-14"><a href="#cb13-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-15"><a href="#cb13-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># generate the table in HTML format</span></span>
<span id="cb13-16"><a href="#cb13-16" aria-hidden="true" tabindex="-1"></a> table <span class="op">=</span> tabulate(data_as_list, headers, tablefmt<span class="op">=</span><span class="st">'html'</span>)</span>
<span id="cb13-17"><a href="#cb13-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-18"><a href="#cb13-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> table</span>
<span id="cb13-19"><a href="#cb13-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-20"><a href="#cb13-20" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb13-21"><a href="#cb13-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-22"><a href="#cb13-22" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> visualize_bertopic(topic_model, model_name, nr_topics):</span>
<span id="cb13-23"><a href="#cb13-23" aria-hidden="true" tabindex="-1"></a> <span class="co"># create the "images" folder if it doesn't exist already</span></span>
<span id="cb13-24"><a href="#cb13-24" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="kw">not</span> os.path.exists(<span class="st">"images"</span>):</span>
<span id="cb13-25"><a href="#cb13-25" aria-hidden="true" tabindex="-1"></a> os.makedirs(<span class="st">"images"</span>)</span>
<span id="cb13-26"><a href="#cb13-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-27"><a href="#cb13-27" aria-hidden="true" tabindex="-1"></a> <span class="co"># create a subfolder for the specific topic model</span></span>
<span id="cb13-28"><a href="#cb13-28" aria-hidden="true" tabindex="-1"></a> model_folder <span class="op">=</span> os.path.join(<span class="st">"images"</span>, model_name<span class="op">+</span><span class="st">"-"</span><span class="op">+</span><span class="bu">str</span>(nr_topics)<span class="op">+</span><span class="st">"topics"</span>)</span>
<span id="cb13-29"><a href="#cb13-29" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb13-30"><a href="#cb13-30" aria-hidden="true" tabindex="-1"></a> <span class="co"># create the model folder if it doesn't exist already</span></span>
<span id="cb13-31"><a href="#cb13-31" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="kw">not</span> os.path.exists(model_folder):</span>
<span id="cb13-32"><a href="#cb13-32" aria-hidden="true" tabindex="-1"></a> os.makedirs(model_folder)</span>
<span id="cb13-33"><a href="#cb13-33" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>:</span>
<span id="cb13-34"><a href="#cb13-34" aria-hidden="true" tabindex="-1"></a> <span class="co"># delete existing files in the model folder if it exists</span></span>
<span id="cb13-35"><a href="#cb13-35" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> <span class="bu">file</span> <span class="kw">in</span> os.listdir(model_folder):</span>
<span id="cb13-36"><a href="#cb13-36" aria-hidden="true" tabindex="-1"></a> os.remove(os.path.join(model_folder, <span class="bu">file</span>))</span>
<span id="cb13-37"><a href="#cb13-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-38"><a href="#cb13-38" aria-hidden="true" tabindex="-1"></a> <span class="co"># generate topics information table</span></span>
<span id="cb13-39"><a href="#cb13-39" aria-hidden="true" tabindex="-1"></a> topics_table <span class="op">=</span> generate_topics_table(topic_model)</span>
<span id="cb13-40"><a href="#cb13-40" aria-hidden="true" tabindex="-1"></a> <span class="cf">with</span> <span class="bu">open</span>(os.path.join(model_folder, <span class="st">'table_topics.html'</span>), <span class="st">'w'</span>) <span class="im">as</span> f:</span>
<span id="cb13-41"><a href="#cb13-41" aria-hidden="true" tabindex="-1"></a> f.write(topics_table)</span>
<span id="cb13-42"><a href="#cb13-42" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb13-43"><a href="#cb13-43" aria-hidden="true" tabindex="-1"></a> <span class="co"># visualize topics</span></span>
<span id="cb13-44"><a href="#cb13-44" aria-hidden="true" tabindex="-1"></a> fig_topics <span class="op">=</span> topic_model.visualize_topics()</span>
<span id="cb13-45"><a href="#cb13-45" aria-hidden="true" tabindex="-1"></a> fig_topics.write_html(os.path.join(model_folder, <span class="st">"topicsinfo.html"</span>))</span>
<span id="cb13-46"><a href="#cb13-46" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-47"><a href="#cb13-47" aria-hidden="true" tabindex="-1"></a> <span class="co"># visualize hierarchy</span></span>
<span id="cb13-48"><a href="#cb13-48" aria-hidden="true" tabindex="-1"></a> fig_hierarchy <span class="op">=</span> topic_model.visualize_hierarchy()</span>
<span id="cb13-49"><a href="#cb13-49" aria-hidden="true" tabindex="-1"></a> fig_hierarchy.write_html(os.path.join(model_folder, <span class="st">"hierarchy.html"</span>))</span>
<span id="cb13-50"><a href="#cb13-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-51"><a href="#cb13-51" aria-hidden="true" tabindex="-1"></a> <span class="co"># visualize hierarchical topics</span></span>
<span id="cb13-52"><a href="#cb13-52" aria-hidden="true" tabindex="-1"></a> hierarchical_topics <span class="op">=</span> topic_model.hierarchical_topics(docs_marketing)</span>
<span id="cb13-53"><a href="#cb13-53" aria-hidden="true" tabindex="-1"></a> fig_hierarchical_topics <span class="op">=</span> topic_model.visualize_hierarchy(hierarchical_topics<span class="op">=</span>hierarchical_topics)</span>
<span id="cb13-54"><a href="#cb13-54" aria-hidden="true" tabindex="-1"></a> fig_hierarchical_topics.write_html(os.path.join(model_folder, <span class="st">"hierarchical.html"</span>))</span>
<span id="cb13-55"><a href="#cb13-55" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-56"><a href="#cb13-56" aria-hidden="true" tabindex="-1"></a> <span class="co"># visualize the bar chart</span></span>
<span id="cb13-57"><a href="#cb13-57" aria-hidden="true" tabindex="-1"></a> fig_barchart <span class="op">=</span> topic_model.visualize_barchart(width<span class="op">=</span><span class="dv">300</span>, height<span class="op">=</span><span class="dv">300</span>, n_words<span class="op">=</span><span class="dv">10</span>, topics<span class="op">=</span><span class="va">None</span>, top_n_topics<span class="op">=</span><span class="dv">20</span>)</span>
<span id="cb13-58"><a href="#cb13-58" aria-hidden="true" tabindex="-1"></a> fig_barchart.write_html(os.path.join(model_folder, <span class="st">"barchart.html"</span>))</span>
<span id="cb13-59"><a href="#cb13-59" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-60"><a href="#cb13-60" aria-hidden="true" tabindex="-1"></a> <span class="co"># visualize the heatmap</span></span>
<span id="cb13-61"><a href="#cb13-61" aria-hidden="true" tabindex="-1"></a> fig_heatmap <span class="op">=</span> topic_model.visualize_heatmap()</span>
<span id="cb13-62"><a href="#cb13-62" aria-hidden="true" tabindex="-1"></a> fig_heatmap.write_html(os.path.join(model_folder, <span class="st">"heatmap.html"</span>))</span>
<span id="cb13-63"><a href="#cb13-63" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb13-64"><a href="#cb13-64" aria-hidden="true" tabindex="-1"></a> <span class="co"># topics over time</span></span>
<span id="cb13-65"><a href="#cb13-65" aria-hidden="true" tabindex="-1"></a> years <span class="op">=</span> df[<span class="st">'year'</span>].to_list()</span>
<span id="cb13-66"><a href="#cb13-66" aria-hidden="true" tabindex="-1"></a> topics_over_time <span class="op">=</span> topic_model.topics_over_time(docs_marketing, years)</span>
<span id="cb13-67"><a href="#cb13-67" aria-hidden="true" tabindex="-1"></a> fig_topics_over_time <span class="op">=</span> topic_model.visualize_topics_over_time(topics_over_time, top_n_topics<span class="op">=</span><span class="dv">20</span>, normalize_frequency<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb13-68"><a href="#cb13-68" aria-hidden="true" tabindex="-1"></a> fig_topics_over_time.write_html(os.path.join(model_folder, <span class="st">"topicsovertime.html"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="create-topics-for-a-list-of-embeddings-model" class="level3" data-number="6.3">
<h3 data-number="6.3" class="anchored" data-anchor-id="create-topics-for-a-list-of-embeddings-model"><span class="header-section-number">6.3</span> Create topics for a list of embeddings model</h3>
<p>We must specify the number of topics we want to create (<code>nbtopics</code>) and the minimum number of documents to form a topic (<code>nbmintopicsize</code>).</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co">#09/26/2023 -----------------------------------------</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="co">#to-do clean code : embeddings are charged twice for viz purposes (document viz) because it is loaded a first time in the create_bertopic function and again in the loop (can't use just the model name in visualize_bertopic function.)</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="co">#---------------------------------------------------</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a><span class="co"># list of embeddings models</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a>list_embeddings <span class="op">=</span> [<span class="st">"all-mpnet-base-v2"</span>]</span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a><span class="co">#list_embeddings = ["all-mpnet-base-v2","multi-qa-mpnet-base-dot-v1","all-roberta-large-v1","all-MiniLM-L12-v2"]</span></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a><span class="co"># create a list to store model information</span></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a>table_data <span class="op">=</span> []</span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a>topic_models <span class="op">=</span> {}</span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a><span class="co">#nbtopics is the number of topics we want to create/reduce to</span></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a><span class="co">#nbmintopicsize is the minimum number of documents to form a topic</span></span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a>nbtopics <span class="op">=</span> <span class="dv">17</span></span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a>nbmintopicsize <span class="op">=</span> <span class="dv">5</span></span>
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-17"><a href="#cb14-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-18"><a href="#cb14-18" aria-hidden="true" tabindex="-1"></a><span class="co"># loop through the list of embeddings models and create topic_model + viz in images</span></span>
<span id="cb14-19"><a href="#cb14-19" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> embeddings_model <span class="kw">in</span> list_embeddings:</span>
<span id="cb14-20"><a href="#cb14-20" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Creating BERTopics with the </span><span class="sc">{</span>embeddings_model<span class="sc">}</span><span class="ss"> Sentence-Transformers pretrained model."</span>)</span>
<span id="cb14-21"><a href="#cb14-21" aria-hidden="true" tabindex="-1"></a> topic_model <span class="op">=</span> create_bertopic(docs_marketing, embeddings_model, nbmintopicsize, nbtopics)</span>
<span id="cb14-22"><a href="#cb14-22" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Creating BERTopic visualizations in the `images</span><span class="ch">\\</span><span class="sc">{</span>embeddings_model<span class="sc">}</span><span class="ss">-</span><span class="sc">{</span>nbtopics<span class="sc">}</span><span class="ss">topics` folder."</span>)</span>
<span id="cb14-23"><a href="#cb14-23" aria-hidden="true" tabindex="-1"></a> visualize_bertopic(topic_model, embeddings_model, nbtopics)</span>
<span id="cb14-24"><a href="#cb14-24" aria-hidden="true" tabindex="-1"></a> chargedmodel <span class="op">=</span> SentenceTransformer(embeddings_model, device<span class="op">=</span><span class="st">'cuda'</span>)</span>
<span id="cb14-25"><a href="#cb14-25" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb14-26"><a href="#cb14-26" aria-hidden="true" tabindex="-1"></a> <span class="co"># visualize the documents</span></span>
<span id="cb14-27"><a href="#cb14-27" aria-hidden="true" tabindex="-1"></a> model_folder <span class="op">=</span> os.path.join(<span class="st">"images"</span>, embeddings_model<span class="op">+</span><span class="st">"-"</span><span class="op">+</span><span class="bu">str</span>(nbtopics)<span class="op">+</span><span class="st">"topics"</span>)</span>
<span id="cb14-28"><a href="#cb14-28" aria-hidden="true" tabindex="-1"></a> embeddings <span class="op">=</span> chargedmodel.encode(docs_marketing, show_progress_bar<span class="op">=</span><span class="va">False</span>)</span>
<span id="cb14-29"><a href="#cb14-29" aria-hidden="true" tabindex="-1"></a> fig_documents <span class="op">=</span> topic_model.visualize_documents(docs_marketing, embeddings<span class="op">=</span>embeddings)</span>
<span id="cb14-30"><a href="#cb14-30" aria-hidden="true" tabindex="-1"></a> fig_documents.write_html(os.path.join(model_folder, <span class="st">"documents_topics.html"</span>))</span>
<span id="cb14-31"><a href="#cb14-31" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb14-32"><a href="#cb14-32" aria-hidden="true" tabindex="-1"></a> <span class="co"># to summarize embeddings' models</span></span>
<span id="cb14-33"><a href="#cb14-33" aria-hidden="true" tabindex="-1"></a> dimensions <span class="op">=</span> chargedmodel.get_sentence_embedding_dimension()</span>
<span id="cb14-34"><a href="#cb14-34" aria-hidden="true" tabindex="-1"></a> max_tokens <span class="op">=</span> chargedmodel.max_seq_length</span>
<span id="cb14-35"><a href="#cb14-35" aria-hidden="true" tabindex="-1"></a> <span class="co"># store the topic_model in the dictionary with the embeddings name as key</span></span>
<span id="cb14-36"><a href="#cb14-36" aria-hidden="true" tabindex="-1"></a> topic_models[embeddings_model] <span class="op">=</span> topic_model</span>