Instructions to use dvschan/swedish-grammaticality-scorer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use dvschan/swedish-grammaticality-scorer with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="dvschan/swedish-grammaticality-scorer")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("dvschan/swedish-grammaticality-scorer") model = AutoModelForSequenceClassification.from_pretrained("dvschan/swedish-grammaticality-scorer", device_map="auto") - Notebooks
- Google Colab
- Kaggle
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
Model tree for dvschan/swedish-grammaticality-scorer
Base model
answerdotai/ModernBERT-large