Logo

Arabic Corpora: A Key Resource for Arabic NLP

10 min read
Lesson slides
1 / 13

Arabic Corpora: A Key Resource for Arabic NLP

Understanding corpora, tokenization, segmentation, and stopword removal for Arabic text

📌 Arabic Corpora: A Key Resource for Arabic NLP 🌍📖

🔹 What is an Arabic Corpus?

An Arabic corpus (plural: corpora) is a large collection of Arabic text used for training and evaluating Natural Language Processing (NLP) models. These corpora help in tasks such as tokenization, part-of-speech tagging, named entity recognition, sentiment analysis, and machine translation.


🔹 Types of Arabic Corpora (For reference only) 🏛️

Arabic corpora vary based on their domain, purpose, and structure. Here are some common types:

1️⃣ General-Purpose Arabic Corpora

📌 Used for general NLP tasks like tokenization, parsing, and word embeddings.

  • 📖 Arabic Gigaword → Large-scale collection of Arabic news articles.
  • 🌎 Arabic Web Corpus → Crawled Arabic text from various websites.
  • 📜 Tashkeela Corpus → Focuses on Arabic diacritics (Tashkeel) for vowelization.

2️⃣ Arabic Speech & Conversational Corpora 🗣️

📌 Used for speech recognition, chatbot training, and dialogue systems.

  • 🎤 MGB-2 (Multi-Genre Broadcast Corpus) → Transcribed TV broadcasts.
  • ☎️ CALLHOME Arabic Corpus → Recorded and transcribed phone conversations.
  • 🗣️ ArzEn → A bilingual Egyptian Arabic-English corpus for speech processing.

3️⃣ Arabic Sentiment & Opinion Corpora 😊😡

📌 Used for sentiment analysis and opinion mining.

  • 📊 ASTD (Arabic Sentiment Twitter Dataset) → Annotated tweets with sentiment labels.
  • 📝 LABR (Large-Scale Arabic Book Reviews Corpus) → Sentiment analysis for book reviews.
  • 💬 OCA (Opinion Corpus for Arabic) → Movie review sentiment dataset.

4️⃣ Quranic & Religious Texts Corpora 📜

📌 Used for linguistic analysis, translation, and search tools for religious texts.

  • 📖 Quranic Arabic Corpus → Annotated corpus of the Holy Quran.
  • 📚 Al-Hadith Corpus → Collection of Hadith texts for NLP research.

5️⃣ Historical & Literary Arabic Corpora 🏺

📌 Used for classical Arabic research and historical text processing.

  • 📜 Al-Khalil Corpus → Collection of classical Arabic texts.
  • 📚 The Doha Historical Dictionary of Arabic → Covers Arabic language evolution.

✨ Text Normalization Tasks 📖

Text normalization is an essential preprocessing step in NLP. It includes:

  • 🔹 Tokenization: Splitting text into words or subwords.
  • 🔹 Segmentation: Dividing text into meaningful units such as sentences or phrases.
  • 🔹 Stemming: Reducing words to their root form by removing affixes.
  • 🔹 Lemmatization: Converting words to their base dictionary form.

🔹 Arabic Text

إن التاريخ العسكري سوف يتوقف طويلاً أمام عملية يوم ٦ أكتوبر ١٩٧٣، ولست أتجاوز إذا قلت أن التاريخ العسكري سوف يتوقف طويلاً بالفحص والدرس أمام عملية يوم ٦ أكتوبر سنة ٧٣، حين تمكنت القوات المسلحة المصرية من اقتحام مانع قناة السويس الصعب واجتياز خط بارليف المنيع وإقامة رؤوس جسور لها بعد أن أفقدت العدو توازنه في 6 ساعات، لقد كانت المخاطرة كبيرة والتضحيات عظيمة لمعركة ٦ أكتوبر خلال الساعات الست الأولى من حربنا كانت هائلة فقد العدو توازنه إلى هذه اللحظة.

Q1: Tokenization Using Three Methods

Tokenization is the process of breaking down a phrase, sentence, paragraph, or entire document into smaller units called tokens. These tokens can be individual words, subwords, or punctuation marks.

For this task, apply three different tokenization methods:

  1. Two traditional Python-based methods
  2. One NLP library-based method
text = ' إن التاريخ العسكري سوف يتوقف طويلاً أمام عملية يوم ٦ أكتوبر ١٩٧٣, ولست اتجاوز اذا قلت أن التاريخ العسكري سوف يتوقف طويلاً بالفحص والدرس أمام عملية يوم ٦ أكتوبر سنة ٧٣, حين تمكنت القوات المسلحة المصرية من إقتحام مانع قناة السويس الصعب واجتياز خط بارليف المنيع وإقامة رؤس جسور لها بعد أن أفقدت العدو توازنه فى 6 ساعات, لقد كانت المخاطرة كبيرة والتضحيات عظيمة لمعركة ٦ أكتوبر خلال الساعات الست الأولي من حربنا كانت هائلة فقد العدو توازنه إلي هذه اللحظة'
# Preprocessing
import re
pre_processed = re.sub(r'[^\w\s]', '', text) # Keep only whitespaces & words
pre_processed
' إن التاريخ العسكري سوف يتوقف طويلا أمام عملية يوم ٦ أكتوبر ١٩٧٣ ولست اتجاوز اذا قلت أن التاريخ العسكري سوف يتوقف طويلا بالفحص والدرس أمام عملية يوم ٦ أكتوبر سنة ٧٣ حين تمكنت القوات المسلحة المصرية من إقتحام مانع قناة السويس الصعب واجتياز خط بارليف المنيع وإقامة رؤس جسور لها بعد أن أفقدت العدو توازنه فى 6 ساعات لقد كانت المخاطرة كبيرة والتضحيات عظيمة لمعركة ٦ أكتوبر خلال الساعات الست الأولي من حربنا كانت هائلة فقد العدو توازنه إلي هذه اللحظة'

🔸 Tokenization Methods

1️⃣ Python’s Built-in split() Method (Basic, whitespace-based tokenization)
2️⃣ Regular Expressions (re.findall(PATTERN, TEXT),re.split())
3️⃣ NLP Library (nltk.word_tokenize for English, Farasa for Arabic) (Context-aware, language-specific tokenization)

1️⃣ Using Python’s split() Method (Traditional Method)

Python's built-in split() method can be used to tokenize text based on spaces. However, it does not handle punctuation or special cases.

tokens_1 = pre_processed.split()
tokens_1
['إن',
 'التاريخ',
 'العسكري',
 'سوف',
 'يتوقف',
 'طويلا',
 'أمام',
 'عملية',
 'يوم',
 '٦',
 'أكتوبر',
 '١٩٧٣',
 'ولست',
 'اتجاوز',
 'اذا',
 'قلت',
 'أن',
 'التاريخ',
 'العسكري',
 'سوف',
 'يتوقف',
 'طويلا',
 'بالفحص',
 'والدرس',
 'أمام',
 'عملية',
 'يوم',
 '٦',
 'أكتوبر',
 'سنة',
 '٧٣',
 'حين',
 'تمكنت',
 'القوات',
 'المسلحة',
 'المصرية',
 'من',
 'إقتحام',
 'مانع',
 'قناة',
 'السويس',
 'الصعب',
 'واجتياز',
 'خط',
 'بارليف',
 'المنيع',
 'وإقامة',
 'رؤس',
 'جسور',
 'لها',
 'بعد',
 'أن',
 'أفقدت',
 'العدو',
 'توازنه',
 'فى',
 '6',
 'ساعات',
 'لقد',
 'كانت',
 'المخاطرة',
 'كبيرة',
 'والتضحيات',
 'عظيمة',
 'لمعركة',
 '٦',
 'أكتوبر',
 'خلال',
 'الساعات',
 'الست',
 'الأولي',
 'من',
 'حربنا',
 'كانت',
 'هائلة',
 'فقد',
 'العدو',
 'توازنه',
 'إلي',
 'هذه',
 'اللحظة']

2️⃣ Using Regular Expressions

We can use regular expressions (re) to split text.

PATTERN = r"\w+"
tokens_2 = re.findall(PATTERN, pre_processed)
tokens_2
['إن',
 'التاريخ',
 'العسكري',
 'سوف',
 'يتوقف',
 'طويلا',
 'أمام',
 'عملية',
 'يوم',
 '٦',
 'أكتوبر',
 '١٩٧٣',
 'ولست',
 'اتجاوز',
 'اذا',
 'قلت',
 'أن',
 'التاريخ',
 'العسكري',
 'سوف',
 'يتوقف',
 'طويلا',
 'بالفحص',
 'والدرس',
 'أمام',
 'عملية',
 'يوم',
 '٦',
 'أكتوبر',
 'سنة',
 '٧٣',
 'حين',
 'تمكنت',
 'القوات',
 'المسلحة',
 'المصرية',
 'من',
 'إقتحام',
 'مانع',
 'قناة',
 'السويس',
 'الصعب',
 'واجتياز',
 'خط',
 'بارليف',
 'المنيع',
 'وإقامة',
 'رؤس',
 'جسور',
 'لها',
 'بعد',
 'أن',
 'أفقدت',
 'العدو',
 'توازنه',
 'فى',
 '6',
 'ساعات',
 'لقد',
 'كانت',
 'المخاطرة',
 'كبيرة',
 'والتضحيات',
 'عظيمة',
 'لمعركة',
 '٦',
 'أكتوبر',
 'خلال',
 'الساعات',
 'الست',
 'الأولي',
 'من',
 'حربنا',
 'كانت',
 'هائلة',
 'فقد',
 'العدو',
 'توازنه',
 'إلي',
 'هذه',
 'اللحظة']

3️⃣ Using NLP Libraries (nltk.word_tokenize)

The NLTK tokenizer provides a smart way to split words while handling punctuation and contractions properly.

import nltk
# Punkt is a pretrained model that helps in splitting text into sentences and words.
# It supports multiple languages, including English and Arabic.
# The "punkt" package consists in unsafe pickles, so it is deprecated and not used in NLTK 3.8.2. It is replaced by "punkt_tab".
 
# nltk.download('punkt')
nltk.download('punkt_tab')
nltk.download('stopwords')
[nltk_data] Downloading package punkt_tab to
[nltk_data]     C:\Users\mmaba\AppData\Roaming\nltk_data...
[nltk_data]   Package punkt_tab is already up-to-date!
[nltk_data] Downloading package stopwords to
[nltk_data]     C:\Users\mmaba\AppData\Roaming\nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
True
from nltk.tokenize import word_tokenize
tokens_3 = word_tokenize(pre_processed)
tokens_3
['إن',
 'التاريخ',
 'العسكري',
 'سوف',
 'يتوقف',
 'طويلا',
 'أمام',
 'عملية',
 'يوم',
 '٦',
 'أكتوبر',
 '١٩٧٣',
 'ولست',
 'اتجاوز',
 'اذا',
 'قلت',
 'أن',
 'التاريخ',
 'العسكري',
 'سوف',
 'يتوقف',
 'طويلا',
 'بالفحص',
 'والدرس',
 'أمام',
 'عملية',
 'يوم',
 '٦',
 'أكتوبر',
 'سنة',
 '٧٣',
 'حين',
 'تمكنت',
 'القوات',
 'المسلحة',
 'المصرية',
 'من',
 'إقتحام',
 'مانع',
 'قناة',
 'السويس',
 'الصعب',
 'واجتياز',
 'خط',
 'بارليف',
 'المنيع',
 'وإقامة',
 'رؤس',
 'جسور',
 'لها',
 'بعد',
 'أن',
 'أفقدت',
 'العدو',
 'توازنه',
 'فى',
 '6',
 'ساعات',
 'لقد',
 'كانت',
 'المخاطرة',
 'كبيرة',
 'والتضحيات',
 'عظيمة',
 'لمعركة',
 '٦',
 'أكتوبر',
 'خلال',
 'الساعات',
 'الست',
 'الأولي',
 'من',
 'حربنا',
 'كانت',
 'هائلة',
 'فقد',
 'العدو',
 'توازنه',
 'إلي',
 'هذه',
 'اللحظة']

📝 Q2: Implement Sentence Segmentation Using nltk ✂️📖

🔹 Task Description

Sentence segmentation is the process of dividing a paragraph into individual sentences. In this task, you will use NLTK’s Punkt Sentence Tokenizer to segment text into sentences.

par=" في نفس المساحة، لقد تم توليد هذا النص من مولد النص العربى، حيث يمكنك أن تولد مثل هذا النص أو العديد من النصوص الأخرى إضافة إلى زيادة عدد الحروف التى يولدها التطبيق. إذا كنت تحتاج إلى عدد أكبر من الفقرات يتيح لك مولد النص العربى زيادة عدد الفقرات كما تريد، النص لن يبدو مقسما ولا يحوي أخطاء لغوية، مولد النص العربى مفيد لمصممي المواقع على وجه الخصوص، حيث يحتاج العميل فى كثير من الأحيان أن يطلع على صورة حقيقية لتصميم الموقع.ومن هنا وجب على المصمم أن يضع نصوصا مؤقتة على التصميم ليظهر للعميل الشكل كاملاً،دور مولد النص العربى أن يوفر على المصمم عناء البحث عن نص بديل لا علاقة له بالموضوع "
#============= Segmentation ==========
def segment(text):
    segmenter = nltk.data.load("tokenizers/punkt/english.pickle")
    sents = segmenter.tokenize(text)
    for sent in sents:
        print(sent)
        print('-'*30)
 
    print(f"Number of sentences: {len(sents)}")
segment(par)
 في نفس المساحة، لقد تم توليد هذا النص من مولد النص العربى، حيث يمكنك أن تولد مثل هذا النص أو العديد من النصوص الأخرى إضافة إلى زيادة عدد الحروف التى يولدها التطبيق.
------------------------------
إذا كنت تحتاج إلى عدد أكبر من الفقرات يتيح لك مولد النص العربى زيادة عدد الفقرات كما تريد، النص لن يبدو مقسما ولا يحوي أخطاء لغوية، مولد النص العربى مفيد لمصممي المواقع على وجه الخصوص، حيث يحتاج العميل فى كثير من الأحيان أن يطلع على صورة حقيقية لتصميم الموقع.ومن هنا وجب على المصمم أن يضع نصوصا مؤقتة على التصميم ليظهر للعميل الشكل كاملاً،دور مولد النص العربى أن يوفر على المصمم عناء البحث عن نص بديل لا علاقة له بالموضوع
------------------------------
Number of sentences: 2

📝 Q3: Removing Stopwords Using nltk.corpus 🚀

🔹 Task Description

Stopwords are common words (such as "the", "and", "is") that usually do not add significant meaning in text analysis. Using NLTK's corpus module, remove stopwords from the following Arabic sentences.


X = [
    "أحب المشي على الشاطئ",
    "أنا أحب الأفلام",
    "أحب الأفلام",
    "القط على القبعة",
]
Y = [
    "الشاطئ للبحر مثل السحابة للسماء",
    "أحب الأفلام",
    "انا احب الافلام",
    "القطة فوق القبعة",
]
# print stopwords
from nltk.corpus import stopwords
stopwords = stopwords.words('arabic')
print(f"Stop words length : {len(stopwords)}")
print(f"Example : {stopwords[0:10]}")
Stop words length : 754
Example : ['إذ', 'إذا', 'إذما', 'إذن', 'أف', 'أقل', 'أكثر', 'ألا', 'إلا', 'التي']
from nltk.tokenize import word_tokenize
def RemoveStopwords(X,Y):
    
    print("X:", X)
    print("Y:", Y)
    
    # tokenization
    X_list = word_tokenize(X)
    Y_list = word_tokenize(Y)
    
    # remove stop words from the string
    X_set = {w for w in X_list if not w in stopwords}
    Y_set = {w for w in Y_list if not w in stopwords}
 
    # X_set = {w for w in X_list if not w in stopwords}
    # Is the same as ---------------------------------------------------
    # for w in X_list:  # Loop through each word in X_list
    #     if w not in stopwords:  # If the word is NOT a stopword
    #         X_set.add(w)  # Add it to the set
    # ------------------------------------------------------------------
    print("X_set:", X_set)
    print("Y_set:", Y_set)
for item in zip(X,Y):
    print(item)
('أحب المشي على الشاطئ', 'الشاطئ للبحر مثل السحابة للسماء')
('أنا أحب الأفلام', 'أحب الأفلام')
('أحب الأفلام', 'انا احب الافلام')
('القط على القبعة', 'القطة فوق القبعة')
for x,y in zip(X, Y):
    RemoveStopwords(x, y)
    print('-'*30)
X: أحب المشي على الشاطئ
Y: الشاطئ للبحر مثل السحابة للسماء
X_set: {'المشي', 'الشاطئ', 'أحب'}
Y_set: {'للسماء', 'السحابة', 'الشاطئ', 'للبحر'}
------------------------------
X: أنا أحب الأفلام
Y: أحب الأفلام
X_set: {'أحب', 'الأفلام'}
Y_set: {'أحب', 'الأفلام'}
------------------------------
X: أحب الأفلام
Y: انا احب الافلام
X_set: {'أحب', 'الأفلام'}
Y_set: {'الافلام', 'انا', 'احب'}
------------------------------
X: القط على القبعة
Y: القطة فوق القبعة
X_set: {'القط', 'القبعة'}
Y_set: {'القطة', 'القبعة'}
------------------------------

🔹 What is an Arabic Corpus?

An Arabic corpus (plural: corpora) is a large collection of Arabic text used for training and evaluating Natural Language Processing (NLP) models. These corpora help in tasks such as tokenization, part-of-speech tagging, named entity recognition, sentiment analysis, and machine translation.


# Where to put your corpus ?  
# nltk.data.path
# Loading it
# nltk.data.load('PATH')