Mostafa-Saeed commited on
Commit
f4ea4e7
·
verified ·
1 Parent(s): 02d9457

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +106 -2
README.md CHANGED
@@ -1,3 +1,107 @@
 
 
 
 
 
 
 
1
  ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - ar
5
+ pipeline_tag: text-generation
6
+ tags:
7
+ - Arabic_Dialectal_Lemmatization
8
  ---
9
+ # EGY-S2S-lemmatizer
10
+ # Model Description
11
+
12
+ The model is developed for Arabic dialect lemmatization, focusing on Egyptian (EGY) Arabic. It follows a sequence-to-sequence formulation of lemmatization, where the model generates the lemma of a given word knowing 2 words before and 2 words after the current word rather than treating lemmas as fixed classification labels.
13
+
14
+
15
+ The model is evaluated using lemma accuracy as the main metric, with an additional normalized lemma accuracy metric that accounts for orthographic and diacritic variation. The full methodology, training setup, hyperparameters, and evaluation results are described in our paper [“Lemmatizing Dialectal Arabic with Sequence-to-Sequence Models”](https://aclanthology.org/2025.arabicnlp-main.10/)
16
+
17
+ # Standalone Usage
18
+
19
+
20
+ The model can also be used independently without the full lemmatization workflow on the GitHub repository (https://github.com/CAMeL-Lab/seq2seq-arabic-dialect-lemmatization). In this case, the input should contain the target word surrounded by the special token `<target>`, with up to two words before and two words after the target word.
21
+
22
+ ```python
23
+ import re
24
+ import math
25
+ import pandas as pd
26
+ import torch
27
+ from tqdm import tqdm
28
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
29
+
30
+ DIALECT_MODELS = {
31
+ "egy": "CAMeL-Lab/EGY-S2S-lemmatizer",
32
+ }
33
+
34
+ def load_model(s2s_dialect: str):
35
+ model_name = DIALECT_MODELS[s2s_dialect]
36
+ tokenizer = T5Tokenizer.from_pretrained(model_name, use_fast=True, legacy=False)
37
+ model = T5ForConditionalGeneration.from_pretrained(model_name)
38
+
39
+ tokenizer.add_special_tokens({"additional_special_tokens": ["<target>"]})
40
+ model.resize_token_embeddings(len(tokenizer))
41
+
42
+ return tokenizer, model
43
+
44
+ def extract_target_word(text: str) -> str:
45
+ match = re.search(r"<target>(.*?)<target>", str(text))
46
+ return match.group(1).strip() if match else ""
47
+
48
+ def predict(tokenizer, model, texts: list[str], device=None, batch_size: int = 16) -> list[str]:
49
+ if device is None:
50
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
51
+
52
+ model.to(device).eval()
53
+ all_preds = []
54
+ total_batches = math.ceil(len(texts) / batch_size)
55
+
56
+ for i in tqdm(range(0, len(texts), batch_size), total=total_batches, desc="Predicting"):
57
+ batch = texts[i:i + batch_size]
58
+
59
+ enc = tokenizer(
60
+ batch,
61
+ return_tensors="pt",
62
+ padding=True,
63
+ truncation=True,
64
+ max_length=64
65
+ )
66
+ enc = {k: v.to(device) for k, v in enc.items()}
67
+
68
+ with torch.no_grad():
69
+ out = model.generate(
70
+ **enc,
71
+ max_length=50,
72
+ num_beams=1,
73
+ do_sample=False
74
+ )
75
+
76
+ all_preds.extend(tokenizer.batch_decode(out, skip_special_tokens=True))
77
+
78
+ return all_preds
79
+
80
+ # Example:
81
+ # df should contain an input_text column with the target word marked using <target>
82
+ # Example input: "أنا عايز <target>أروح<target> البيت دلوقتي"
83
+
84
+ df["word"] = df["input_text"].apply(extract_target_word)
85
+
86
+ tokenizer, model = load_model("egy")
87
+ df["predicted_lex"] = predict(tokenizer, model, df["input_text"].tolist())
88
+ ```
89
+
90
+
91
+ ## 📖 Citation
92
+
93
+
94
+
95
+ If you use this model in your research, please cite the following paper:
96
+
97
+ ```bibtex
98
+ @inproceedings{saeed-habash-2025-lemmatizing,
99
+ title = {Lemmatizing Dialectal Arabic with Sequence-to-Sequence Models},
100
+ author = {Saeed, Mostafa and Habash, Nizar},
101
+ booktitle = {Proceedings of the Third Arabic Natural Language Processing Conference},
102
+ year = {2025},
103
+ address = {Suzhou, China},
104
+ url = {https://aclanthology.org/2025.arabicnlp-main.10/}
105
+ }
106
+ ```
107
+