Arabic NLP - Tutorial - الدورة التعليمية

NLP in Arabic with HF and Beyond

Overview

Arabic language consists of 28 basic letters in addition to extra letters that can be concatenated with Hamza (ء) like أ ، ؤ ، ئ that are used to make emphasis on the letter. Moreover, there are special characters called diacritics to compensate for the lack of short vowels in the language. This increases the number of letters to reach over 40 if we consider diacritics. In Arabic writing script, the words are usually connected together (ligature) which makes it challenging for many tasks like OCR. From a language construction perspective, Arabic is morphologically rich where multiple prefixes, suffixes and infixes can be added to a single stem. There are two types of morphology in Arabic: derivational and inflectional. Derivational morphology is common in Arabic which allows deriving multiple words from a single stem. On the other hand inflectional morphology can add multiple morphemes to a single stem to create much longer and complex words. As a result, Arabic verbs can have up to 5,400 different forms. This rich morphology gave rise to completely different and complex dialects around the Arabic world. Moreover, the complexity of the language makes it challenging for language modelling in general.

State of NLP in Arabic

Before the recent advances in NLP there was some focus on the analysis of morphology in Arabic. Notably, a lot of tools have been created for morphological analysis like segmentation : FARASA and MADAMIRA. Later, after the success of word embeddings like Word2Vec, AraVec was created as a very large word embedding for modern standard arabic and dialects. Recently with the advances in transformers many models have been pre-trained for Arabic like BERT based model i.e AraBERT, and improved variants MARBERT & ARBERT, a GPT-2 based model AraGPT-2, ELECTRA based model AraELECTRA and GigaBERT using zero shot cross lingual understanding from English. Not to mention the task specific models for different tasks like dialect identification, fake news detection, sentiment analysis, translation, etc.

Datasets and Models in HF

Currently there are around 50 datasets for Arabic in different tasks like sentiment analysis, text classification, question answering, translation, unsupervised training, localization, etc. All the transformer models in the previous section are available in the huggingface model hub. In addition there are currently over 70 models for Arabic ranging from monolingual to multilingual models which tackle MSA and dialects.

Arabic Transformers in HF

We can use the datasets module to load any available dataset for Arabic. In this example we use the MetRec dataset which contains poems labeled in terms of meters. In the code snippet below we can load the dataset

from datasets import load_dataset
dataset = load_dataset('metrec')

As we can see if we print the variable we see the dataset contains 47,124 records for training and 8,316 records for testing

DatasetDict({
train: Dataset({
features: ['text', 'label'],
num_rows: 47124
})
test: Dataset({
features: ['text', 'label'],
num_rows: 8316
})
})

Transformers with tokenizers can be used also with ease. Here is a simple example for loading some pretrained Arabic models and tokenizers using transformers

from transformers import AutoTokenizer, AutoModel

PRE_TRAINED_MODEL_NAME = 'aubmindlab/bert-base-arabertv01'

tokenizer = AutoTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)
bert_model = AutoModel.from_pretrained(PRE_TRAINED_MODEL_NAME)

You can do some complex tasks like training and fine-tuning as you can see in this tutorial.

Relevant Resources

As the transformers and tools of hugging face expand to multiple languages, the community is building tools that build on top of that. In Arabic, Tnkeeh (تنقيح) is an Arabic text cleaning library that can be used for removing diacritics, normalization and segmentation and many other preprocessing tasks. The library can process instances of the datasets module. Also CAMeL Tools have incorporated transformers in their api that can be used for many tasks like sentiment classification.

12 Likes

معالجة اللغة الطبيعية باستخدام مكتبة هقنق فيس huggingface

تمت الترجمة بواسطة عبد الإله المسفر

المقدمة

تتكون أحرف اللغة العربية من ٢٨ حرفا بالإضافة الى أحرف أخرى تتشكل عند جمعها مع الهمزة (ء). مثلا: أ، ؤ، ئ. مايميز اللغة العربية آيضا هو وجود التشكيل، مايجعل للكلمات معان مختلفة على الرغم من مشاركتها نفس الأحرف (عِقْد، عَقّد، عُقْد). مما يجعل عدد الأحرف يتخطى ٤٠ حرفا اذا ما اخذنا التشكيل بعين الإعتبار. عند كتابة اللغة العربية، تتصل الأحرف ببعضها بطريقة تختلف عن الشكل الأساسي للأحرف على حدة. مثلا: (ح ر ف) تتشكل عند الكتابة الى (حرف)، مما يجعل بعض مهام تعلم الآلة صعبة، مثل التعرف على الأشكال (OCR). من منظور بناء اللغة ، تعتبر اللغة العربية غنية من الناحية الشكلية, حيث يمكن إضافة العديد من البادئات واللواحق إلى جذع واحد. هناك نوعان من التشكل في اللغة العربية: الاشتقاقي والتصريف. علم التشكل الاشتقاقي شائع في اللغة العربية والذي يسمح باشتقاق كلمات متعددة من جذع واحد. من ناحية أخرى ، يمكن أن تضيف مورفولوجيا التصريف العديد من الأشكال إلى جذع واحد لإنشاء كلمات أطول ومعقدة. نتيجة لذلك، يمكن أن تحتوي الأفعال العربية على 5400 صيغة مختلفة. أدت هذه التركيبة الغنية إلى ظهور لهجات مختلفة تمامًا ومعقدة في جميع أنحاء العالم العربي. علاوة على ذلك ، فإن تعقيد اللغة يجعل نمذجة اللغة مهمة صعبة.

تطورات معالجة اللغة الطبيعية في اللغة العربية

قبل التطورات الحديثة في معالجة اللغة الطبيعية، كان هناك بعض التركيز على تحليل التشكل في اللغة العربية. والجدير بالذكر أنه تم إنشاء الكثير من الأدوات للتحليل الصرفي مثل التجزئة في فراسة FARASA ومداميرا MADAMIRA. في وقت لاحق، بعد نجاح تقنيات تضمين الكلمات مثل Word2Vec ، تم إنشاء AraVec ككلمة كبيرة جدًا يتم تضمينها في اللغة العربية واللهجات القياسية الحديثة. في الآونة الأخيرة مع التقدم في المحولات TRANSFORMERS ، تم تدريب العديد من النماذج مسبقًا على اللغة العربية مثل النموذج القائم على BERT ، مثل AraBERT، والمتغيرات المحسنة MARBERT & ARBERT ، وهو نموذج يعتمد على GPT-2 AraGPT-2 ، ونموذج يعتمد على ELECTRA AraELECTRAو GigaBERT باستخدام لغة بدون طلقة فهم من اللغة الإنجليزية. ناهيك عن نماذج المهام المحددة لمهام مختلفة مثل تحديد اللهجة واكتشاف الأخبار المزيفة وتحليل المشاعر والترجمة وما إلى ذلك.

بيانات ونماذج اللغة العربية في مكتبة هقنق فيس huggingface

يوجد حاليًا حوالي 50 مجموعة بيانات للغة العربية تختص في مهام مختلفة مثل تحليل المشاعر ، تصنيف النص ، والإجابة على الأسئلة ، والترجمة ، والتدريب غير الخاضع للإشراف ، والتخصيص ، وما إلى ذلك. جميع نماذج المحولات في القسم السابق متوفرة في محور نماذج مكتبة هقنق فيس huggingface. بالإضافة إلى ذلك ، يوجد حاليًا أكثر من 70 نموذجًا للغة العربية تتراوح من النماذج أحادية اللغة إلى النماذج متعددة اللغات التي تتعامل مع اللغة العربية الفصحى واللهجات.

المحولات العربية في مكتبة هقنق فيس huggingface

يمكننا استخدام وحدة مجموعات البيانات لتحميل أي مجموعة بيانات متاحة للغة العربية. في هذا المثال ، نستخدم مجموعة بيانات MetRec التي تحتوي على قصائد مصنفة من حيث الأمتار. في مقتطف الشفرة أدناه يمكننا تحميل مجموعة البيانات.

from datasets import load_dataset
dataset = load_dataset('metrec')
كما يمكننا أن نرى أن مجموعة البيانات تحتوي على 47124 سجلًا للتدريب و 8316 سجلًا للاختبار
DatasetDict({
train: Dataset({
features: ['text', 'label'],
num_rows: 47124
})
test: Dataset({
features: ['text', 'label'],
num_rows: 8316
})
})
يمكن أيضًا استخدام المحولات ذات الرموز المميزة بسهولة. فيما يلي مثال بسيط لتحميل بعض النماذج العربية التي تم اختبارها مسبقًا والرموز المميزة باستخدام المحولات.
from transformers import AutoTokenizer, AutoModel
PRE_TRAINED_MODEL_NAME = 'aubmindlab/bert-base-arabertv01'

tokenizer = AutoTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)
bert_model = AutoModel.from_pretrained(PRE_TRAINED_MODEL_NAME)

يمكنك القيام ببعض المهام المعقدة مثل التدريب والضبط كما يظهر في هذه الشروحات.

مصادر ذات صلة

نظرًا لتوسع محولات وأدوات تعانق الوجه إلى لغات متعددة ، يقوم المجتمع ببناء أدوات تعتمد على ذلك. في اللغة العربية ، مكتبة تنقيح لتنظيف النصوص العربية يمكن استخدامها لإزالة علامات التشكيل والتطبيع والتجزئة والعديد من مهام المعالجة المسبقة الأخرى. يمكن للمكتبة معالجة مثيلات وحدة مجموعات البيانات. كما قامت CAMeL Tools بدمج المحولات في واجهات برمجة التطبيقات الخاصة بها والتي يمكن استخدامها في العديد من المهام مثل تصنيف المشاعر.

5 Likes

Here is a colab notebook created by @abdulelahsm.

2 Likes