Swedish grammaticality scorer (KB-BERT, fine-tuned)

Reads Swedish text and returns one score between 0 and 1. Well-formed Swedish scores high; text with grammatical errors scores low.

Built for the AI Sweden master's thesis task on Swedish grammaticality scoring.

Important: how to use it

This model uses mean pooling and a sigmoid head, stored in config.json as score_pooling and score_head. The standard Hugging Face pipeline ignores these settings: it uses the [CLS] path and gives raw logits, so its scores will be wrong.

Use predict.py and scoring.py from the GitHub repository, which read both settings:

pip install -r requirements.txt
python predict.py --input texts.jsonl --output scores.jsonl

Each input line is {"id": ..., "text": "..."}; each output line is {"id": ..., "score": ...}.

Model

Base model KB/bert-base-swedish-cased (about 0.1B parameters)
Pooling mean of the token vectors, padding ignored
Head dropout + linear layer, giving one logit; sigmoid gives the score
Loss binary cross-entropy with soft targets (the target is the score, e.g. 0.4)
Training 2 epochs, learning rate 2e-5, batch size 32, max length 128 tokens, seed 42, bfloat16
Hardware NVIDIA GeForce RTX 5050 Laptop GPU

Training data

About 56,000 texts from three sources (see the dataset card for details):

  • DaLAJ-GED-Superlim 2.0: real learner errors and correct sentences
  • ScaLA-sv: correct treebank sentences and copies with swapped neighbouring words or one deleted word
  • Generated errors (about 13,400): pronoun case, reflexives, prepositions, missing formal subject det, missing sig, extra att/för, and texts with 2 to 3 errors

Training targets: correct text 0.95, one error 0.40, two errors 0.25, three or more 0.10.

Results

Measured with the tasks's evaluate.py on its 120 starter test texts. Timing includes model loading.

Model Spearman ROC-AUC Samples/min
Starter baseline (frozen ModernBERT-large + SVR) 0.528 0.783 234
This model 0.826 0.976 843

The two speed numbers were measured on different hardware and are not directly comparable.

Limitations

  • Misses some error types. On 478 labelled starter texts, the model still scores some poor texts above 0.5, most often compound errors written as two words (vinter jacka), verb forms and missing or extra words.
  • Mostly single sentences. Training texts are mostly one sentence, so long multi-sentence texts are less tested. Texts longer than 128 tokens are cut.
  • Some noisy labels. A few generated "errors" are still acceptable Swedish. A check of 100 random generated rows by an LLM judged 98 as real errors.
  • Domain. Correct training text comes from learner essays, teaching texts and an older treebank, not modern news.
  • This is not a grammar checker. It gives one score per text and does not say where the error is.

License and credits

  • Base model: KBLab, National Library of Sweden. Malmsten, M., Börjeson, L. and Haffenden, C. (2020). Playing with Words at the National Library of Sweden: Making a Swedish BERT. arXiv:2007.01658.
  • Training data: see the dataset card.
Downloads last month
28
Safetensors
Model size
0.1B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for TheRealNaksu/swedish-grammaticality-kb-bert

Finetuned
(17)
this model

Dataset used to train TheRealNaksu/swedish-grammaticality-kb-bert

Paper for TheRealNaksu/swedish-grammaticality-kb-bert