Dialect Identification: Language Variation and NLP
Module: Natural Language Processing | Difficulty: Advanced
What You'll Learn
- 1Assign a text to a named dialect variety
- 2Compute precision, recall, F1, and macro-F1 on an imbalanced test set
- 3Explain why character n-grams beat word features on unstandardized spelling
- 4Name the main dialect corpora and their strongest scores
Dialect identification assigns a regional or social variety to a text when all candidate varieties share one script and much of their vocabulary. The task matters because NLP tuned on the standard variety silently fails on user text: the winning NADI 2020 system reached only 26.78 macro-F1 over 21 Arabic dialects, far below human reliability.
The Identification Task
Arabic is the textbook case of diglossia: Modern Standard Arabic (MSA) is written and broadcast, while roughly 300 million speakers use regional varieties only now entering written form. Every variety uses the same abjad, so the classifier cannot lean on script and must exploit morphology, function words, and orthographic habit.
The label inventory is a design choice, and it drives every reported score.
| Inventory | Levels | Example labels | Source |
|---|---|---|---|
| 5 macro-varieties | region | Maghrebi, Egyptian, Levantine, Gulf, Iraqi | AOC, 2011 |
| 21 countries | country | Egypt, Saudi Arabia, Tunisia, Iraq | NADI 2020 |
| 18 countries | country | balanced MENA tweets | QADI, 2021 |
| 26 cities | city | Cairo, Beirut, Riyadh, Fez | MADAR, 2019 |
Fine granularity shrinks per-class data, which is why country-level macro-F1 collapsed to 26.78 while a five-way regional task on the same language clears 85 percent accuracy.
Features and Models
Character n-gram evidence
Word-level models fail because dialect spelling is unstandardized: one Egyptian word may appear as "2ashan", "3ashan", or "3ashn". Character n-grams fold all three into one surface form, which is why strong dialect systems are built on them. For a text a multinomial model scores a variety by
where is the set of character 3-grams of word , its training count in dialect , and the smoothing constant. Feature selection uses the chi-square statistic against the label:
with the total count and the joint counts. Keeping only grams above a threshold cuts the feature space by about 90 percent while raising macro-F1, because it discards grams frequent in every dialect, such as shared news vocabulary.
Evaluation Under Class Imbalance
Dialect corpora are skewed: an estimated 20 percent of NADI 2020 training tweets were MSA rather than dialectal, and country support varied. Micro accuracy hides failure on rare dialects, so the official metric is macro-F1.
Macro-F1 then gives every dialect an equal vote:
Datasets and Leaderboards
| Corpus | Size | Labels | Strongest reported result |
|---|---|---|---|
| AOC, 2011 | 52M words, 108K annotated | 5 dialects | 85.7 accuracy vs 88.0 human |
| NADI 2020 | 21,000 train, 5,000 test, 10M unlabeled | 21 countries | 26.78 macro-F1, 42.86 accuracy |
| QADI, 2021 | 540K tweets, 2,525 users | 18 countries | 60.6 macro-F1 on 91.5 accurate labels |
| NADI 2023 | 23,400 tweets | 18 countries | 87.27 macro-F1, closed track |
| MADAR, 2019 | 1,000 travel sentences per city set | 26 cities | city ID trails country ID |
The jump from 26.78 to 87.27 macro-F1 came from cleaner, balanced data plus transformer pre-training, not a new architecture.
Worked Code
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
train = [ # Arabizi transliteration, ASCII-safe
("esh hada ya sadiqi ma fehash shi", "LEV"),
("leish ma tiji shway khalina nzid", "LEV"),
("2ay 7aga 2olha tab3an ha2olha", "EGY"),
("el gaw gamed awi naharda kda", "EGY"),
("shlonak ya rafiq shlon el omor", "GLF"),
("wach raktek ya khoya nta lih", "MAG"),
("ach dert fih a3tini chwiya", "MAG"),
]
vec = TfidfVectorizer(analyzer="char", ngram_range=(3, 5), min_df=1)
X = vec.fit_transform([t for t, _ in train])
clf = LogisticRegression(max_iter=2000, C=4.0).fit(X, [y for _, y in train])
test = ["khalina nzid shway ya sadiqi", "2ay 7aga 2olha yalla"]
pred = clf.predict(vec.transform(test))
for sent, lab in zip(test, pred):
print(f"{lab:4s} {sent}")
print("macro-F1:", f1_score(["LEV", "EGY"], pred, average="macro"))
The second implementation is a from-scratch character n-gram naive Bayes, the generative model whose log-probability appears above.
import math
from collections import defaultdict, Counter
def ngrams(word, n=3):
pad = "^" + word + "$"
return [pad[i:i + n] for i in range(len(pad) - n + 1)]
class DialectNB:
def __init__(self, alpha=1.0):
self.alpha = alpha
self.gc = defaultdict(Counter)
self.cc = Counter()
self.ct = Counter()
self.vocab = set()
def fit(self, data):
for text, label in data:
grams = [g for w in text.split() for g in ngrams(w)]
self.cc[label] += 1
self.ct[label] += len(grams)
self.gc[label].update(grams)
self.vocab.update(grams)
def log_score(self, text, label):
prior = math.log(self.cc[label] / sum(self.cc.values()))
denom = self.ct[label] + self.alpha * len(self.vocab)
grams = [g for w in text.split() for g in ngrams(w)]
return prior + sum(math.log((self.gc[label][g] + self.alpha) / denom) for g in grams)
def predict(self, text):
return max(self.cc, key=lambda d: self.log_score(text, d))
data = [
("esh hada ya sadiqi leish ktir", "LEV"),
("leish ma tiji shway hal time", "LEV"),
("2ay 7aga 2olha yalla kda", "EGY"),
("el gaw gamed awi naharda", "EGY"),
("wach raktek ya khoya a3tini", "MAG"),
("ach dert fih chwiya ya khouya", "MAG"),
]
model = DialectNB(alpha=1.0)
model.fit(data)
for s in ["yalla kda el gaw gamed", "leish ma tiji shway", "ach dert fih khoya"]:
print(model.predict(s), "|", s)
Real-World Applications
- Search and ranking: QADI-scale models label 540K tweets at 60.6 macro-F1 to route Arabic queries to dialect-tuned indexes.
- Machine translation: NADI 2023 dialect-to-MSA translation reached 14.76 BLEU closed and 21.10 BLEU open, gated on identifying the dialect.
- Speech technology: ASR and TTS pick an acoustic model by dialect, since Egyptian and Maghrebi pronunciation do not transfer.
- Equity: an MSA-only classifier scores 66.6 accuracy where a dialect-trained one scores 85.7, against a human ceiling of 88.0.
đĄThe script hides the dialect
Common Mistakes
Reporting plain accuracy on a 21-class test set
Fix: Report macro-F1 so rare countries are not hidden
Training word features on raw dialect spellings
Fix: Normalize diacritics and Arabizi digits, then use char grams
Assuming labeled data is pure dialect
Fix: Filter MSA first, since about 20 percent of NADI 2020 was MSA
Best Practices
- Normalize orthography and strip MSA contamination before training
- Use char TF-IDF plus chi-square selection as the cheap baseline
- Publish macro-F1 with the label inventory
Key Takeaways
- NADI 2020 peaked at 26.78 macro-F1 on 21 dialects, so label granularity dominates
- Macro-F1 weights classes equally: 62.5 macro versus 68.3 support-weighted
- Character n-grams absorb spelling variation that word features cannot
- QADI labels are only 91.5 accurate, which bounds any system trained on them
Frequently Asked Questions
Why is dialect ID harder than language ID?
Because the varieties share a script and most content words, so class-conditioned token distributions overlap heavily. Language ID exceeds 99 percent accuracy on 176 languages, while 21-way NADI peaked at 26.78 macro-F1.
Does more data fix it?
Substantially. NADI 2020 released 21,000 labeled plus 10M unlabeled tweets and still peaked at 26.78; by NADI 2023, with 23,400 balanced tweets and transformer pre-training, the closed track reached 87.27.
How do I evaluate with skewed classes?
Convert per-class precision and recall to F1, then average over classes for macro-F1, and report per-class support.
Interview Questions
Q1: Why does macro-F1 beat accuracy for dialect ID?
It weights every dialect equally, which matters when support ranges from 400 to 1,000 tweets per country
Q2: What is the most useful feature for Arabic dialect ID?
Character 3-gram TF-IDF, because Arabizi spellings make word features brittle
Q3: How would you handle MSA tweets in the test set?
Add MSA as a reject class or filter it first