ft-SA-LID: language identification for South African languages

A fastText classifier built to find Southern African Bantu-language text in web crawl and transcripts, including text that code-switches with English. It predicts 26 labels: the 9 official SA Bantu languages, Afrikaans, English, 13 neighbouring African languages, Dutch, and a pooled other class.

The model is designed to be used at the language-group level. isiZulu, isiXhosa, isiNdebele and siSwati are often indistinguishable in a short line, but they are always Nguni. Group decisions stay reliable (β‰₯ 0.98 group accuracy on 3–6-word snippets) even when the exact language is uncertain.

Group Labels
nguni zul (isiZulu), xho (isiXhosa), nbl (South African isiNdebele), ssw (siSwati), nde (Zimbabwean isiNdebele)
sotho_tswana sot (Sesotho), tsn (Setswana), nso (Sepedi / Sesotho sa Leboa), loz (Silozi)
venda ven (Tshivenda)
tsonga tso (Xitsonga), tsc (Xitswa), rng (Ronga)
afrikaans afr
english eng
shona sna (Shona), ndc (Ndau), kck (Kalanga)
other_bantu nya (Chichewa), toi (Tonga), bem (Bemba), kqn (Kaonde), lue (Luvale), swh (Swahili)
other nld (Dutch), other (46 other languages, see config.json)

SA-African score = total probability of nguni + sotho_tswana + venda + tsonga. This is the score to threshold when the question is "does this text contain SA Bantu-language content?".

Usage

pip install fasttext-numpy2-wheel numpy huggingface_hub    # or: pip install fasttext (numpy<2)
from huggingface_hub import hf_hub_download
import importlib.util, sys

repo = "janmbuys/ft-SA-LID"
spec = importlib.util.spec_from_file_location("sa_lid", hf_hub_download(repo, "sa_lid.py"))
sa_lid = importlib.util.module_from_spec(spec); spec.loader.exec_module(sa_lid)

lid = sa_lid.SALID(hf_hub_download(repo, "model.bin"))   # 1.07 GB, cached after the first download
texts = ["Sawubona, unjani namhlanje?", "Ke a leboha haholo", "Ek is baie dankie",
         "Thank you very much, see you tomorrow", "eish, I told you mos, angifuni ukuya khona"]

lid.predict(texts, k=2)          # [[('zul', 0.730), ('xho', 0.225)], [('sot', 1.0), ...], ...]
lid.predict_group(texts)         # [('nguni', 0.996), ('sotho_tswana', 1.0), ('afrikaans', 1.0), ('english', 0.948), ('nguni', 0.989)]
lid.sa_african_score(texts)      # array([0.999, 1.0, 0.0, 0.013, 0.998])
lid.is_sa_african(texts)         # array([ True, True, False, False, True])   threshold 0.755

Or from the command line, after downloading the repository: python sa_lid.py "Ndi a livhuwa" "Thank you".

Using the fastText model directly

The model was trained on normalised text. If you call fasttext yourself, apply the same normalisation first (sa_lid.normalize). Otherwise, punctuation, digits and case change the predictions. Normalisation steps:

  1. NFC
  2. lowercase
  3. remove URLs and e-mail addresses
  4. replace digits, underscores and all punctuation except ', ’ and - with spaces
  5. collapse whitespace
import fasttext
from sa_lid import normalize
m = fasttext.load_model("model.bin")
labels, probs = m.predict([normalize(t) for t in texts], k=-1)   # labels look like "__label__zul"

Use the probabilities of all labels (k=-1), not just the top label, when summing to groups.

Thresholds

sa_african_score thresholds, calibrated for whole-text scoring (config.json):

threshold false positives on English web pages from .za domains on English web (global) on English-only soap-opera utterances recall: soap-opera code-switched (eng + SA) recall: SA web lines, held-out domains recall: 3–6-word Vuk'uzenzele snippets
0.894 0.5% 0.04% 3.0% 0.783 0.963 0.968
0.755 (default) 1.0% 0.3% 4.3% 0.856 0.971 0.985
0.567 2.0% 0.8% 6.9% 0.902 0.979 0.992
0.5 2.8% 1.2% 7.8% 0.915 0.982 0.993

Choose a threshold by the cost of false positives in your data. South African English contains many SA names and loanwords ("eish", "lekker", place names), which are the main source of false positives. For conversational transcripts, recalibrate on a sample of your own English-only utterances if you can.

windowed_sa_african_score also scores every 5-word window and takes the maximum. It can find a short SA-language passage inside a long English document. At a matched false-positive rate it was not better than whole-text scoring in our tests, and the thresholds above do not apply to it.

Evaluation

All test data is disjoint from training. Texts are blocked by exact match after normalisation, and the web, edu and Vuk'uzenzele tests are also split by document or domain.

Language and group accuracy (argmax; fine = exact label, group = language group):

test set n ft-SA-LID fine ft-SA-LID group GlotLID v3 fine GlotLID v3 group
Vuk'uzenzele lines (gov. newspaper, 11 languages) 3,770 0.993 0.999 0.985 0.997
Vuk'uzenzele 3–6-word snippets 3,770 0.906 0.978 0.850 0.949
FLORES-200 / SIB-200 (incl. neighbours, other) 22,088 0.997 0.999 0.993 0.996
School textbooks (DBE, held-out docs), 6–40-word segments 16,500 0.981 0.998 0.929 0.987
School textbooks, 3–6-word snippets 16,500 0.939 0.988 0.831 0.938
Web lines from held-out domains (HPLT 3 + FineWeb-2, 11 languages incl. nde) 11,819 0.944 0.979 0.879 0.933

Per language (fine / group accuracy):

Vuk'uzenzele lines Vuk'uzenzele snippets textbook segments textbook snippets held-out web
zul 0.989 / 1.000 0.863 / 0.994 0.949 / 1.000 0.899 / 0.997 0.916 / 0.996
xho 0.991 / 1.000 0.897 / 0.991 0.982 / 0.999 0.961 / 0.996 0.913 / 0.967
nbl 0.971 / 1.000 0.830 / 0.988 0.962 / 1.000 0.912 / 0.994 0.909 / 0.984
ssw 0.997 / 1.000 0.981 / 0.997 0.989 / 0.999 0.973 / 0.998 0.757 / 0.825 *
nde – – – – 0.914 / 0.998
sot 0.997 / 1.000 0.889 / 0.991 0.970 / 0.999 0.905 / 0.995 0.976 / 0.988
tsn 0.995 / 1.000 0.849 / 0.977 0.977 / 0.997 0.867 / 0.987 0.957 / 0.992
nso 0.998 / 1.000 0.899 / 0.991 0.985 / 1.000 0.915 / 0.995 0.962 / 0.993
ven 1.000 / 1.000 0.980 / 0.980 0.999 / 0.999 0.987 / 0.984 0.975 / 0.974
tso 0.995 / 0.995 0.943 / 0.969 0.996 / 0.999 0.968 / 0.985 0.988 / 0.996
afr 1.000 / 1.000 0.936 / 0.936 0.998 / 0.998 0.982 / 0.981 0.985 / 0.985
eng 0.990 / 0.990 0.922 / 0.912 0.986 / 0.984 0.957 / 0.951 –

* Most of these errors are test-label noise, not model errors. 81 of the 103 misses are Luo-family Bible text from one site that the source corpus labelled siSwati, and the model correctly rejects them.

Detecting SA content (default threshold 0.755):

ft-SA-LID GlotLID v3 (threshold also at 1% FPR on .za web)
Recall: code-switched English–isiZulu/isiXhosa/Sesotho/Setswana soap-opera utterances 0.856 0.698
False positives: English-only soap-opera utterances 0.044 0.017
Recall: code-switched soap utterances at a matched 3.4% English-only false-positive rate 0.810 0.824
Recall: utterances with β‰₯ 3 African tokens 0.952 0.886
False positives: English template/boilerplate lines from SA-language websites 0.014 0.017
Speed (lines/sec, one CPU, all-label probabilities) ~8,000 ~175

The soap-opera test set is from the SADiLaR code-switched soap opera speech corpus (transcripts only). It is used for evaluation only.

Training data

3.82M training lines (81M words), at most 120k lines per label before augmentation (other: 375k). Sources:

  • Web text: HPLT 3.0 and FineWeb-2, filtered for SA languages. Each document was relabelled with an earlier internal version of this model, and auto-translated sites (domain and URL rules), spam, mostly-English and boilerplate-heavy documents were removed. FineWeb-2 documents duplicating HPLT were removed.
  • Department of Basic Education school materials for all 11 official languages: workbooks, graded readers and CAPS curriculum documents from education.gov.za. Extracted from PDFs (with OCR), reflowed into paragraphs, with headers and boilerplate removed.
  • Vuk'uzenzele government newspaper (dsfsi/vukuzenzele-monolingual), train split only.
  • GlotLID-C (cis-lmu/glotlid-corpus): Bible, JW, story books, news, Wikipedia and other sources for the SA and neighbouring languages. Its Vuk'uzenzele and FLORES files were not used.
  • FineWeb-2 for the neighbouring languages, Dutch and the other class; FineWeb for English.

Recipe:

  • Balancing. Curated text (textbooks, Vuk'uzenzele) fills up to 40% of each label. Web text is capped at 40% per topic and 20% per domain, with square-root sampling. This keeps jw.org and Bible text from dominating the smaller languages.
  • Line-level noise filter. Web and GlotLID-C lines that an earlier internal version of this model assigns to a different language group with p β‰₯ 0.9 were dropped. Among them were ~10k lines of Swahili Bible text in GlotLID-C's siSwati file.
  • Synthetic code-switching. 330k examples interleave SA-language chunks with English (80%) or another SA language, and are labelled with the African language.
  • Hard negatives. 60k English sentences with 1–2 inserted SA names or words, and 40k English template lines from SA-language websites, all labelled eng.
  • Short chunks. Random 1–12-word chunks of training lines are added, so short inputs are in distribution.

fastText: loss=softmax dim=64 epoch=5 lr=0.5 minn=2 maxn=5 wordNgrams=1 bucket=2000000 minCount=2. Dev precision@1: 0.980.

Limitations

  • Fine labels on short text. Within the Nguni and Sotho-Tswana groups, exact-language accuracy drops to 0.83–0.90 on 3–6-word snippets. Prefer group decisions; treat fine labels on short text as a best guess.
  • Neighbouring languages (tsc, rng, kck, kqn, lue, toi, bem, loz) were trained mostly on religious text (jw.org, Bibles) with 5k–30k lines each. No test set covers them except FLORES (bem, nya, sna, swh). Expect weaker accuracy on other genres.
  • English with SA names is the main false-positive source. A single African word ("Sawubona") in English text can be scored as SA-African. Utterances with exactly one African token are inherently ambiguous.
  • Latin script only for the SA languages. Non-Latin scripts fall into other.
  • Machine-translated text in SA languages was removed from training only by domain and URL rules. The model does not detect MT, and MT sites in SA languages are classified as those languages.
  • Text in the other class covers 46 languages. Other unseen languages can be assigned to any label.

License

The model and code are released under CC BY-SA 3.0, the same licence as the fastText models. This repository contains only the model and inference code; it does not redistribute any training data.

Citation

TODO

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support