Search by

le-pave / press-topics

le-pave

Transparent topic classification of press articles with a weighted dictionary: no AI service, no third party, every result explained.

Package info

codeberg.org/le-pave/press-topics.git

Issues

pkg:composer/le-pave/press-topics

Statistics

Installs: 1

Dependents: 0

Suggesters: 1

v0.1.0 2026-11-28 22:51 UTC

This package is not auto-updated.

Last update: 2026-11-29 20:53:18 UTC


README

Transparent topic classification of press articles with a weighted dictionary. No AI service, no third party: the text of an article never leaves your server, and every result says which words decided it.

Built for Le Pavé, an aggregator of the independent French-language press, to replace the language model that suggested the topics of its contents.

Table of Contents

How it works

  • Terms. A text is lowercased, its accents, elisions and function words removed, its plural endings dropped. Its terms are its words and the pairs of words next to each other (extreme droite). A term of the title counts twice as much as a term of the summary.
  • Dictionary. For each topic, a list of terms with a weight: positive when the term speaks for the topic, negative when it speaks against it, plus a bias and a threshold. The score of a topic is the bias plus the weights of the terms found, turned into a probability; the topics above their threshold are kept.
  • Learning. The weights can be learnt from articles whose topics are known (one logistic regression per topic, a linear model: each term keeps a single readable weight), then reviewed and edited by hand. The weakest terms are pruned, so the dictionary stays short enough to read.

Usage

composer require le-pave/press-topics
use PressTopics\Classifier;
use PressTopics\Dictionary;
use PressTopics\Document;

$classifier = new Classifier(Dictionary::load('dictionary.json'));
$result = $classifier->classify(new Document(
    'Pesticides : la justice suspend un arrêté préfectoral',
    'Le tribunal administratif donne raison aux associations de défense de l’environnement.',
));

$result->topics;  // ['ecologie' => 0.99]
$result->reasons; // ['ecologie' => ['pesticide' => 3.07, 'environnement' => 2.24, …, 'justice' => -0.87]]

PHP 8.1 or later, no dependency.

Command line

# Learn a dictionary from a corpus (the first 2,000 documents after a shuffle are kept out)
bin/press-topics train corpus.jsonl topics.json dictionary.json

# Compare it with the known topics of the documents kept out
bin/press-topics evaluate dictionary.json corpus.jsonl

# Classify one text, with the terms that weighed
bin/press-topics classify dictionary.json "Title" "Summary"

corpus.jsonl holds one document per line: {"id": 1, "title": "…", "summary": "…", "categories": […], "topics": ["slug"]}. topics.json maps each slug to its label: {"ecologie": "Écologie", …}.

Dictionary format

{
    "version": 1,
    "meta": {"trainedOn": 12643, "date": "2026-11-28"},
    "fieldWeights": {"title": 2.0, "summary": 1.0, "categories": 2.0},
    "topics": {
        "ecologie": {
            "label": "Écologie",
            "bias": -0.21,
            "threshold": 0.45,
            "terms": {"climatique": 4.93, "pesticide": 2.81, "…": 0.0}
        }
    }
}

Terms are written like the normalized text: lowercase, without accents, singular.

Results on Le Pavé

First dictionary (28 November 2026), learnt from 12,643 articles classified by the language model, compared on 2,000 other articles:

At least one topic in common (or none on both sides)82.6 %
Exactly the same topics52.1 %
Precision / recall / F1 (all topics)0.715 / 0.726 / 0.720

These figures measure the agreement with the language model, not the truth: a good share of the disagreements are debatable, sometimes in favour of the dictionary.

Limits

  • One language. The tokenizer and its function words are French. A text in another language gets no relevant term: classify it apart (topics of its media, or a dictionary of its language).
  • Rare topics. A topic with few examples (a few dozen) is learnt poorly: its terms should be reviewed and completed by hand.
  • Inherited labels. A dictionary learnt from topics given by a model inherits its mistakes until people correct them.
  • Noise. Some common words still get a little weight: to prune when reviewing the dictionary. The words of the name of the media are left out (Document::$source), so that a topic is not learnt from the name of the media that covers it most.

Licence

EUPL-1.2, the open source licence of the European Union.

The dictionary of Le Pavé is not part of this repository: it is published apart, as data.