Swedish grammaticality scorer

Fine-tuned AI-Sweden-Models/ModernBERT-large (395M parameters) that scores Swedish text from 0 (poor) to 1 (good). One regression output.

Calibration is required

The model was trained with MSE on the raw logit, so calibration.json in this repo maps the logit to the 0 to 1 scale: sigmoid(a * logit + b) with a = 5.29 and b = -2.62. Skipping it keeps the ranking but pushes most scores near 0.7.

Usage

import json
import torch
from huggingface_hub import hf_hub_download
from transformers import AutoModelForSequenceClassification, AutoTokenizer

repo = "dvschan/swedish-grammaticality-scorer"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForSequenceClassification.from_pretrained(repo).eval()
cal = json.load(open(hf_hub_download(repo, "calibration.json"), encoding="utf-8"))

text = "Kommunen har beslutat att bygga en ny förskola i stadsdelen."
enc = tok([text], return_tensors="pt", truncation=True, max_length=256)
with torch.no_grad():
    logit = model(**enc).logits[0, 0].item()
print(torch.sigmoid(torch.tensor(cal["a"] * logit + cal["b"])).item())

The project's predict.py does this for a JSONL file:

python predict.py --input test.jsonl --output scores.jsonl

Data and results

Training data: 8,325 clean/corrupted pairs built from 10,000 Swedish Wikipedia sentences, plus 478 starter examples, 17,128 rows in total. Each pair holds one clean sentence and one copy with 1 to 3 rule-based errors. Validation and synthetic test sets are 922 and 926 rows, published at https://huggingface.co/datasets/dvschan/swedish-grammaticality. The 478 starter rows are not redistributed because the starter set has no stated license.

Test set Spearman ROC-AUC
Starter 120-row test 0.686 0.891
Synthetic 926 rows 0.849 0.952

Spearman measures rank agreement from -1 to 1 and ignores the absolute score values. ROC-AUC is the area under the receiver operating characteristic curve.

Limitations

The nine error rules cover article gender, definiteness, verb-second order, word order, deletion, duplication, de/dem, verb form, and spelling. Article and verb-second errors are rare in the training data, and synthetic errors are cleaner than real learner mistakes, so the synthetic result is optimistic.

Very short inputs score low. "Hej." gets 0.66, because all training sentences have at least six words.

Downloads last month
68
Safetensors
Model size
0.4B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for dvschan/swedish-grammaticality-scorer

Finetuned
(5)
this model

Dataset used to train dvschan/swedish-grammaticality-scorer