MaartenGr / MaartenGr/BERTopic

Topic modelling for corpus comparison ('topics per class' on steroids?)

Open
#1,786 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

Has anybody ever looked into this? If so, would you be willing to share your experience?

Let me share a concrete use case of my own. I am currently playing with Parlamint 4.0, a dataset with the parliamentary debates of 26 European parliaments since ~1996 (coverage varies by country). It includes the full text of all contributions as well as a bunch of metadata - including, for instance, the speakers, their political parties and their political orientation (e.g. left, centre, right; or even 'right-to-far-right' vs 'far right') . One version of the dataset even contains English translations of everything. I have first of all filtered out all paragraphs that are relevant for my research topic (Russia) with a regex-based keyword search. I have then extracted key noun phrases from the paragraph text using spaCy' s 'en_core_web_lg' language model for the PoS parsing for KeyphraseCountVectorizer; the 'all-mpnet-base-v2' language model for the SentenceTransformer; and then just matplotlib and seaborn for the vizzes. Here's an example viz - it shows the normalized salience of these keyphrases for one random country (in this case the UK) per year ("what percentage of all paragraphs are Russia-relevant for that country and how has that changed over time?")
Austria_Top_Keywords_Heatmap

So yes, this is a 'dense' viz, BUT it is (IMO) still visually more 'chewable' for more than just a few ones than a line graph...

I am currently running BERTopic (using these KeyBERT key phrases) on my entire 'Russia'-corpus with the usual visualization options. I will then use some LLM to label these topics more 'intelligently' and to write a few paragraphs on them (for a write-up). I will definitely also (try to) make a landscape view like in this (CiteSpace) viz:
inflation+recession

But my question concerns comparing topics across subcorpora. I'll definitely generate a heatmap like the one I inserted here. [and btw - if Maarten reads this, I'd be happy to share my code with him]. The heatmap I shared here had keyphrases in the y-axis - this one will have topic labels, and you would see the relative salience of these topics over time. I then also plan to run it on each individual country ("what are the most salient Russia-related topics that are debated in country a?"); on the 'far right' and 'far left' political parties; - both overall and also for selected countries. I might think of more ones.

So the way I see it, for comparing subcorpora, we currently have the interactive horizontal 'Topic per class' histogram viz that's part of BERTopic (and it's great!) already; and we have the Heatmap viz I proposed here (it's not so great, but it DOES 'show' more of the big picture). But can anybody think of other ways of comparing the results of topic modelling across classes AND across time? Sorry for being so long-winded, but I'd be really grateful if somebody were willing to share some ideas.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name a repository file, test, or entry point to investigate. Start by reviewing the existing Topic per class and visualization APIs, then define a concrete cross-class and cross-time comparison with an agreed output and validation criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization, machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.