Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: ar
|
| 3 |
+
base_model: aubmindlab/bert-base-arabertv02-twitter
|
| 4 |
+
pipeline_tag: text-classification
|
| 5 |
+
tags:
|
| 6 |
+
- arabic
|
| 7 |
+
- dialect-identification
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Arabic dialect classifier (18 countries)
|
| 11 |
+
|
| 12 |
+
Fine-tuned AraBERTv0.2-Twitter that guesses which of 18 Arab countries a tweet's author is from.
|
| 13 |
+
Labels: OM, SD, SA, KW, QA, LB, JO, SY, IQ, MA, EG, PL, YE, BH, DZ, AE, TN, LY.
|
| 14 |
+
|
| 15 |
+
## How to use
|
| 16 |
+
Tweets must go through the same cleaning as in training, then the model.
|
| 17 |
+
|
| 18 |
+
```python
|
| 19 |
+
# pip install transformers torch arabert farasapy
|
| 20 |
+
import torch
|
| 21 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 22 |
+
from arabert.preprocess import ArabertPreprocessor
|
| 23 |
+
|
| 24 |
+
repo = "Majellan/arabic-dialect-classifier"
|
| 25 |
+
tok = AutoTokenizer.from_pretrained(repo)
|
| 26 |
+
model = AutoModelForSequenceClassification.from_pretrained(repo).eval()
|
| 27 |
+
prep = ArabertPreprocessor(model_name="aubmindlab/bert-base-arabertv02-twitter")
|
| 28 |
+
|
| 29 |
+
text = "..." # an Arabic tweet
|
| 30 |
+
enc = tok(prep.preprocess(text), return_tensors="pt", truncation=True, max_length=64)
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
probs = torch.softmax(model(**enc).logits, dim=-1)[0]
|
| 33 |
+
print(model.config.id2label[probs.argmax().item()], float(probs.max()))
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## Training
|
| 37 |
+
- Data: a Hugging Face copy of QADI (`Abdelrahman-Rezk/Arabic_Dialect_Identification`):
|
| 38 |
+
440,021 train / 9,164 validation / 8,981 test tweets (31 empty train tweets dropped).
|
| 39 |
+
- Preprocessing: `ArabertPreprocessor` for the Twitter model, max length 64 tokens.
|
| 40 |
+
- 4 epochs, learning rate 2e-5, batch size 32, fp16, one T4 GPU.
|
| 41 |
+
- Best validation macro F1 was at epoch 3 (0.645). This is the epoch-4 model (validation 0.643).
|
| 42 |
+
|
| 43 |
+
## Results (test set, evaluated once)
|
| 44 |
+
| Model | Macro F1 |
|
| 45 |
+
|---|---|
|
| 46 |
+
| TF-IDF + LinearSVC (untuned baseline) | 0.524 |
|
| 47 |
+
| This model | **0.631** (accuracy 0.655) |
|
| 48 |
+
|
| 49 |
+
With my own grouping of the 18 countries into 5 regions, region accuracy is 0.846.
|
| 50 |
+
Strongest: EG 0.856, LY 0.795, LB 0.756. Weakest: YE 0.454, BH 0.498, OM 0.515, JO 0.530.
|
| 51 |
+
The QADI paper reports 0.606 on its own official test set (about 3,300 tweets). That is a different
|
| 52 |
+
test set from this one, so the numbers are not directly comparable.
|
| 53 |
+
|
| 54 |
+
## Limitations
|
| 55 |
+
- - **Where it makes mistakes.** It picks the right region (my own grouping of the 18 countries into 5) for 84.6% of test tweets, but the right country for only 65.5%. The biggest mix-up is Jordan and Palestine (104 and 99 tweets), and six of the eight lowest-scoring countries are Gulf countries (counting Yemen as Gulf).
|
| 56 |
+
- **Iraq and the Gulf.** Iraq is the weakest of the five regions (F1 0.643; 181 of 304 tweets correct), but 10th of 18 as a country. 59% of its 123 mistakes go to Gulf countries (the Gulf is 41% of the other tweets), and 64% of the tweets wrongly predicted as Iraqi are Gulf tweets. I read 20 Iraqi tweets that the model sent to the Gulf: 15 sounded Gulf to me (I couldn't tell which country) and 5 had a clear Iraqi clue, so the model's guess was often reasonable. Two explanations fit, and I can't tell them apart: real overlap between Iraqi and Gulf speech (as a native Arabic speaker from eastern Syria, I know Iraqi dialects vary and parts are close to Kuwaiti and Bahraini), or label noise (see the next point). This is a small, subjective sample of mistakes only.
|
| 57 |
+
- QADI labels come from users' account descriptions, not from the tweets. The QADI authors estimate
|
| 58 |
+
91.5% label accuracy, so some labels are wrong.
|
| 59 |
+
- Scores are model outputs, not calibrated probabilities.
|
| 60 |
+
- Trained on tweets only. Short texts, formal Arabic and tweets without dialect clues are guesses.
|
| 61 |
+
- Do not use it to make decisions about people.
|
| 62 |
+
|
| 63 |
+
## License and data
|
| 64 |
+
No license set. Intended for research and education only. I could not find a clear license for
|
| 65 |
+
the training data (QADI), so please check it yourself before any other use.
|
| 66 |
+
Sources: QADI, "Arabic Dialect Identification in the Wild" (arXiv 2005.06557);
|
| 67 |
+
AraBERT, https://huggingface.co/aubmindlab/bert-base-arabertv02-twitter
|