Views
No views yet
1from transformers import AutoTokenizer, BertForTokenClassification, MBartForConditionalGeneration
2from camel_tools.disambig.bert import BERTUnfactoredDisambiguator
3from camel_tools.utils.dediac import dediac_ar
4import torch.nn.functional as F
5import torch
6
7bert_disambig = BERTUnfactoredDisambiguator.pretrained()
8
9ged_tokenizer = AutoTokenizer.from_pretrained('CAMeL-Lab/camelbert-msa-zaebuc-ged-13')
10ged_model = BertForTokenClassification.from_pretrained('CAMeL-Lab/camelbert-msa-zaebuc-ged-13')
11
12gec_tokenizer = AutoTokenizer.from_pretrained('CAMeL-Lab/arabart-zaebuc-gec-ged-13')
13gec_model = MBartForConditionalGeneration.from_pretrained('CAMeL-Lab/arabart-zaebuc-gec-ged-13')
14
15text = 'و قال له انه يحب اكل الطعام بكثره .'
16
17# morph processing the input text
18text_disambig = bert_disambig.disambiguate(text.split())
19morph_pp_text = [dediac_ar(w_disambig.analyses[0].analysis['diac']) for w_disambig in text_disambig]
20morph_pp_text = ' '.join(morph_pp_text)
21
22# GED tagging
23inputs = ged_tokenizer([morph_pp_text], return_tensors='pt')
24logits = ged_model(**inputs).logits
25preds = F.softmax(logits, dim=-1).squeeze()[1:-1]
26pred_ged_labels = [ged_model.config.id2label[p.item()] for p in torch.argmax(preds, -1)]
27
28# Extending GED label to GEC-tokenized input
29ged_label2ids = gec_model.config.ged_label2id
30tokens, ged_labels = [], []
31
32for word, label in zip(morph_pp_text.split(), pred_ged_labels):
33 word_tokens = gec_tokenizer.tokenize(word)
34 if len(word_tokens) > 0:
35 tokens.extend(word_tokens)
36 ged_labels.extend([label for _ in range(len(word_tokens))])
37
38
39input_ids = gec_tokenizer.convert_tokens_to_ids(tokens)
40input_ids = [gec_tokenizer.bos_token_id] + input_ids + [gec_tokenizer.eos_token_id]
41
42label_ids = [ged_label2ids.get(label, ged_label2ids['<pad>']) for label in ged_labels]
43label_ids = [ged_label2ids['UC']] + label_ids + [ged_label2ids['UC']]
44attention_mask = [1 for _ in range(len(input_ids))]
45
46
47gen_kwargs = {'num_beams': 5, 'max_length': 100,
48 'num_return_sequences': 1,
49 'no_repeat_ngram_size': 0, 'early_stopping': False,
50 'ged_tags': torch.tensor([label_ids]),
51 'attention_mask': torch.tensor([attention_mask])
52 }
53
54# GEC generation
55generated = gec_model.generate(torch.tensor([input_ids]), **gen_kwargs)
56
57generated_text = gec_tokenizer.batch_decode(generated,
58 skip_special_tokens=True,
59 clean_up_tokenization_spaces=False
60 )[0]
61
62print(generated_text) # وقال له أنه يحب أكل الطعام بكثرة .1@inproceedings{alhafni-etal-2023-advancements,
2 title = "Advancements in {A}rabic Grammatical Error Detection and Correction: An Empirical Investigation",
3 author = "Alhafni, Bashar and
4 Inoue, Go and
5 Khairallah, Christian and
6 Habash, Nizar",
7 editor = "Bouamor, Houda and
8 Pino, Juan and
9 Bali, Kalika",
10 booktitle = "Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing",
11 month = dec,
12 year = "2023",
13 address = "Singapore",
14 publisher = "Association for Computational Linguistics",
15 url = "https://aclanthology.org/2023.emnlp-main.396",
16 doi = "10.18653/v1/2023.emnlp-main.396",
17 pages = "6430--6448",
18 abstract = "Grammatical error correction (GEC) is a well-explored problem in English with many existing models and datasets. However, research on GEC in morphologically rich languages has been limited due to challenges such as data scarcity and language complexity. In this paper, we present the first results on Arabic GEC using two newly developed Transformer-based pretrained sequence-to-sequence models. We also define the task of multi-class Arabic grammatical error detection (GED) and present the first results on multi-class Arabic GED. We show that using GED information as auxiliary input in GEC models improves GEC performance across three datasets spanning different genres. Moreover, we also investigate the use of contextual morphological preprocessing in aiding GEC systems. Our models achieve SOTA results on two Arabic GEC shared task datasets and establish a strong benchmark on a recently created dataset. We make our code, data, and pretrained models publicly available.",
19}