Skip to content

Commit 899e626

Browse files
committed
Built site for gh-pages
1 parent 9312a0c commit 899e626

4 files changed

Lines changed: 13 additions & 27 deletions

File tree

.nojekyll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
df92da26
1+
cc40726e

search.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"href": "tutorial.html",
1919
"title": "BERTopic Tutorial",
2020
"section": "",
21-
"text": "Warning\n\n\n\nNe pas se focaliser que sur l’exploratoire\nNeed to rerun it all and double check comments"
21+
"text": "What are the main issues adressed in a set of documents ? Are those documents similar or discussing different issues ? What is the most important topic ? Delineating the main topics in a set of documents is a common task in social sciences; especially when working with a new corpus. Of course, identifying the topics can be done by hand. But when dealing with large corpora, this task is almost impossible; hence, for quite some time now, social scientists have used topic modelling techniques to quickly extract the main themes in their corpus (Asmussen & Møller, 2019), either to explore them or to attribute a label to each document to conduct further analysis.\n\nTopic modelling is a natural language processing task that extracts latent topics structuring a corpora. For instance, Jockers & Mimno (2013) extracted broad themes in the English literature from the 19th century.\nNatural language processing (NLP) is a subfield of computer science that analyses textual data. Main NLP tasks are text generation (like chatGPT), text classification, or topic modelling.\n\n\n\n\n\n\n\nFigure 1: Topic Modelling in-out sketch\n\n\n\nUntil recently, topic modelling techniques — and natural language processing (NLP) in general — heavily relied on word counts, especially bag-of-words approaches. Amongst other limits, those approaches do not take into account the context in which a word is used — the order of the words in a sentence does not matter to those models, thus failing to grasp the complexity of the human language.\nTo tackle this limit, researchers developed transformations that generate richer and denser representations based on deep learning models. Those models take the text as an entry and generate a vector representation called embeddings. BERTopic is a topic model tool designed for Python that leverages transformer models1 to generate coherent topics based on the semantic similarity of texts. Since its inception in 2022, BERTopic has already proven to be useful in various research across social sciences. For instance, Bizel-Bizellot et al. (2024) used it to identify the main circumstances of infection with COVID-19 based on survey-free-text answers. In the context of climate misinformation, Törnberg & Törnberg (2025) analysed images and texts to highlight trends.\nBERTopic is a brilliant tool that unlocks diverse paths but requires time to master. In this tutorial, we focus on its general philosophy and how to use it in a social science project. We will demonstrate how to start with a text corpus and create a topic model that makes sense. As a use case, we will use the database of all the theses defended between 2010 and 2022 in France and create a topic model to describe what keeps French PhD students busy!\nBy the end of this tutorial, you should be able to:\n\nGet an idea of what you can do with topic modelling in social science\nSet up a topic model on your data and make sense of the results\nUnderstand each step of the BERTopic pipeline and customise it\n\nWe conclude this tutorial with a discussion about the evaluation of topic modelling and good practices for reproducibility.\nPython, Machine Learning and NLP prerequisites\nWe don’t assume that you have any knowledge about NLP and try our best to explain every step in an agnostic manner. We also provide numerous references if you want to dig deeper.\nNevertheless, you need to have some notions of Python. If you need to refresh your Python skills, you can use Lino Galina’s courses. We assume that:\n\nyou have a working environment and can install packages\nyou know the basic syntax of Python (functions, variables, if-statements, for loops) and you’re comfortable enough with Pandas to load your documents and proceed to simple manipulations such as creating, dropping and renaming columns and rows.\n\nIn this tutorial, we use python 3.12, and you can install the packages we will need with :\npip install -U bertopic pandas scikit-learn datasets plotly kaleido stopwordsiso nbformat ipykernel\n\n\n\n\n\n\nNote\n\n\n\nWe provide a detailed requirement file that should work for Linux and MacOS.\n\n\nMaterial\nThe tutorial comes with some material uploaded on Zenodo :\n\na notebook with all the code\nThe original dataset (which can be downloaded here).\nA clean dataset with the cleaning code."
2222
},
2323
{
2424
"objectID": "tutorial.html#the-bertopic-pipeline",

sitemap.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</url>
77
<url>
88
<loc>https://AXLMRIN.github.io/bertopic-tutorial/tutorial.html</loc>
9-
<lastmod>2025-11-13T11:03:20.015Z</lastmod>
9+
<lastmod>2025-11-13T11:06:15.337Z</lastmod>
1010
</url>
1111
</urlset>

tutorial.html

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,6 @@ <h1 class="title">BERTopic Tutorial</h1>
239239
</header>
240240

241241

242-
<div class="callout callout-style-default callout-warning callout-titled">
243-
<div class="callout-header d-flex align-content-center">
244-
<div class="callout-icon-container">
245-
<i class="callout-icon"></i>
246-
</div>
247-
<div class="callout-title-container flex-fill">
248-
Warning
249-
</div>
250-
</div>
251-
<div class="callout-body-container callout-body">
252-
<p>Ne pas se focaliser que sur l’exploratoire</p>
253-
<p>Need to rerun it all and double check comments</p>
254-
</div>
255-
</div>
256242
<section id="introduction" class="level1">
257243
<h1>Introduction</h1>
258244
<p>What are the main issues adressed in a set of documents ? Are those documents similar or discussing different issues ? What is the most important topic ? Delineating the main topics in a set of documents is a common task in social sciences; especially when working with a new corpus. Of course, identifying the topics can be done by hand. But when dealing with large corpora, this task is almost impossible; hence, for quite some time now, social scientists have used <strong>topic modelling</strong> techniques to quickly extract the main themes in their corpus (Asmussen &amp; Møller, 2019), either to explore them or to attribute a label to each document to conduct further analysis.</p>
@@ -466,7 +452,7 @@ <h2 class="anchored" data-anchor-id="breaking-down-the-process">Breaking down th
466452
<h3 class="anchored" data-anchor-id="how-to-generate-the-embeddings">How to generate the embeddings?</h3>
467453
<p>To generate the embeddings we use encoder models. Encoder models are a type of pre-trained transformer models<a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> whose job is to encapsulate the semantic of textual data. A good example of encoder is the BERT model and all it’s successors like RoBERTa, DeBERTa … For generating embeddings, BERTopic uses <strong>SBERT</strong>. Encoder models can take in a limitted number of tokens (parts of words), this is called the <strong>context window size</strong>. For smaller models, the context window size is of about 500 tokens (200 words on average) like for BERT and larger models like ModernBERT have a context window size of 8,000 tokens.</p>
468454
<div class="callout callout-style-default callout-tip callout-titled" title="Learn more about embedding techniques">
469-
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
455+
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
470456
<div class="callout-icon-container">
471457
<i class="callout-icon"></i>
472458
</div>
@@ -475,7 +461,7 @@ <h3 class="anchored" data-anchor-id="how-to-generate-the-embeddings">How to gene
475461
</div>
476462
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
477463
</div>
478-
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
464+
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
479465
<div class="callout-body-container callout-body">
480466
<ul>
481467
<li><a href="https://maartengr.github.io/BERTopic/getting_started/embeddings/embeddings.html">BERTopic documentation on embeddings</a></li>
@@ -487,7 +473,7 @@ <h3 class="anchored" data-anchor-id="how-to-generate-the-embeddings">How to gene
487473
</div>
488474
<p>The embeddings — ie the generated vectors, contain hundreds of dimensions (for instance, the dimension of BERT’s embeddings is 512). Clustering algorithms work poorly with this many dimensions so we need to reduce the dimensionality of the embedding space (typically between 2 and 10). To reduce the dimensionality, the BERTopic pipeline uses the <strong>UMAP</strong> algorithm for it’s ability to grasp local and global structures (McInnes et al., 2018)<a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"><sup>3</sup></a>. This mean that, despite moving from several hundreds of dimensions to only a couple, documents that are close together will stay close and distant ones will stay further apart. This is a critical step as we are heavily changing the structure of the data.</p>
489475
<div class="callout callout-style-default callout-tip callout-titled" title="Learn more about dimensionality reduction and UMAP">
490-
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
476+
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
491477
<div class="callout-icon-container">
492478
<i class="callout-icon"></i>
493479
</div>
@@ -496,7 +482,7 @@ <h3 class="anchored" data-anchor-id="how-to-generate-the-embeddings">How to gene
496482
</div>
497483
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
498484
</div>
499-
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
485+
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
500486
<div class="callout-body-container callout-body">
501487
<ul>
502488
<li><a href="https://www.ibm.com/think/topics/dimensionality-reduction">“What is dimensionality reduction?” by Eda Kavlakoglu (IBM)</a></li>
@@ -513,7 +499,7 @@ <h3 class="anchored" data-anchor-id="how-to-generate-clusters">How to generate c
513499
<p>The goal for the clustering algorithm is to create groups of documents that are semantically close. We are not certain that the output clusters will be “real” topics, but we tune the BERTopic pipeline in order for the clusters to be representatives of topics that are latent in our corpus.</p>
514500
<p>Different algorithms can be used for clustering. <strong>HDBSCAN</strong> was chosen for it’s ability to detect clusters based on their density, hence it is able to detect clusters of various shapes and density. HDBSCAN also allows for documents to be labeled as noise to primarily focus on dense and coherent groups.</p>
515501
<div class="callout callout-style-default callout-tip callout-titled" title="Learn more about clustering techniques and HDBSCAN">
516-
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
502+
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
517503
<div class="callout-icon-container">
518504
<i class="callout-icon"></i>
519505
</div>
@@ -522,7 +508,7 @@ <h3 class="anchored" data-anchor-id="how-to-generate-clusters">How to generate c
522508
</div>
523509
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
524510
</div>
525-
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
511+
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
526512
<div class="callout-body-container callout-body">
527513
<ul>
528514
<li><a href="https://scikit-learn.org/stable/modules/clustering.html">Clustering by Scikit-Learn</a></li>
@@ -540,7 +526,7 @@ <h3 class="anchored" data-anchor-id="how-to-retrieve-relevant-keywords-for-each-
540526
<p>To achieve that, it needs to come back to the text document and discompose it at the word level<a href="#fn4" class="footnote-ref" id="fnref4" role="doc-noteref"><sup>4</sup></a>. It uses word-count-based techniques that will count the number of occurrences of each word<a href="#fn5" class="footnote-ref" id="fnref5" role="doc-noteref"><sup>5</sup></a> in each document. However, there are plenty of words we don’t want to count because they do not carry much semantic information (ex: “the”, “I”, “is”, “but”, … ). These words are called stop words and are skipped.</p>
541527
<p>To do so, we use the <code>CountVectorizer</code> object that will create a word x document matrix.</p>
542528
<div class="callout callout-style-default callout-tip callout-titled" title="Example">
543-
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="false" aria-label="Toggle callout">
529+
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
544530
<div class="callout-icon-container">
545531
<i class="callout-icon"></i>
546532
</div>
@@ -549,7 +535,7 @@ <h3 class="anchored" data-anchor-id="how-to-retrieve-relevant-keywords-for-each-
549535
</div>
550536
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
551537
</div>
552-
<div id="callout-7" class="callout-7-contents callout-collapse collapse">
538+
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
553539
<div class="callout-body-container callout-body">
554540
<p>Given the two following documents:</p>
555541
<ul>
@@ -598,7 +584,7 @@ <h3 class="anchored" data-anchor-id="how-to-retrieve-relevant-keywords-for-each-
598584
</div>
599585
<p>The word x document matrix is never used as is and needs transformation to outline relevant words. The usual transformation is called TF-IDF. This transformation raises the score of words that appear often in a document and decreases the score of words that appear in many documents. In BERTopic, they use an alternative called <strong>c-TF-IDF</strong>. This transformation raises the score of words that appear often in documents of the same group and decreases the score of words appearing in other groups. With this transformation, we retrieve words that make a group unique!</p>
600586
<div class="callout callout-style-default callout-tip callout-titled" title="Learn more about bag-of-words and TF-IDF">
601-
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-8-contents" aria-controls="callout-8" aria-expanded="false" aria-label="Toggle callout">
587+
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="false" aria-label="Toggle callout">
602588
<div class="callout-icon-container">
603589
<i class="callout-icon"></i>
604590
</div>
@@ -607,7 +593,7 @@ <h3 class="anchored" data-anchor-id="how-to-retrieve-relevant-keywords-for-each-
607593
</div>
608594
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
609595
</div>
610-
<div id="callout-8" class="callout-8-contents callout-collapse collapse">
596+
<div id="callout-7" class="callout-7-contents callout-collapse collapse">
611597
<div class="callout-body-container callout-body">
612598
<ul>
613599
<li><a href="https://web.stanford.edu/~jurafsky/slp3/3.pdf">“N-gram Language Models” Chapter 3 of Speech and Language Processing by D.Jurafsky and J.H.Martin</a></li>

0 commit comments

Comments
 (0)