ArabicTextHelper Module¶
Helpers for Arabic text processing.
- class HMB.ArabicTextHelper.ArabicTextHelper[source]¶
Bases:
TextHelperArabicTextHelper: Utilities for Arabic text preprocessing.
This helper provides common Arabic-specific text preprocessing routines such as regex-based normalization and cleaning, ISRI stemming, Qalsadi lemmatization, and stopword removal. The methods are thin wrappers around NLTK and qalsadi functionality where applicable and try to preserve the original behavior while providing clear documentation.
Notes
Some methods require external packages (nltk, qalsadi) to be installed.
Methods accept and return lists of strings for batch processing.
Can be installed via pip install emoji nltk qalsadi.
- ArabicRegexPreprocessing(data, normalizeChars=True, removeHashes=True, removeUsernames=True, removeEmojis=True, removeLinks=True, removeDiacritics=True)[source]¶
Apply regex-based normalization and cleaning to Arabic text documents.
- Parameters:
data (list-like) – Iterable of strings to preprocess.
normalizeChars (bool) – Normalize common Arabic character variants.
removeHashes (bool) – Remove or normalize hashtags (#tag).
removeUsernames (bool) – Remove @user mentions.
removeEmojis (bool) – Remove emoji characters.
removeLinks (bool) – Remove URLs and web-like tokens.
removeDiacritics (bool) – Remove Arabic diacritics (tashkeel).
- Returns:
Preprocessed documents as strings.
- Return type:
Note
This function performs character normalization, optional diacritics stripping, removal of usernames/hashtags/links/emojis, and token cleaning (removing non-Arabic letters and digits).
- ArabicISRIStemmerPreprocessing(data)[source]¶
Apply ISRI stemming to each document using NLTK’s ISRIStemmer.
- Parameters:
data (list-like) – Iterable of strings to stem. Each string is tokenized using the helper’s SentenceTokeize method before stemming.
- Returns:
Stemmed documents as joined strings.
- Return type:
- ArabicQalsadiLemmatizerPreprocessing(data)[source]¶
Lemmatize Arabic text using the Qalsadi lemmatizer.
- Parameters:
data (list-like) – Iterable of strings to lemmatize.
- Returns:
Lemmatized documents as joined strings.
- Return type:
Note
Requires the qalsadi package to be installed.
- ArabicStopwordsRemovalPreprocessing(data)[source]¶
Remove Arabic stopwords from tokenized documents.
- Parameters:
data (list-like) – Iterable of strings to remove stopwords from. Each string is tokenized using the helper’s SentenceTokeize method.
- Returns:
Documents with stopwords removed and joined back into strings.
- Return type:
- RemovePunctuations(text, extra_punct=None)[source]¶
Remove punctuation characters from a text string, including Arabic punctuation.
- NormalizeArabic(text, removeElongation=True, normalizeHamza=True)[source]¶
Normalize common Arabic characters and optionally remove elongation.
- ArabicToEnglishNumbers(text)[source]¶
Convert Arabic-Indic and Persian digits to Western digits (0-9).
- TokenizeArabic(text)[source]¶
Simple Arabic tokenizer: extract contiguous Arabic letter sequences as tokens.
Note
This is an intentionally lightweight tokenizer. For advanced tokenization consider using language-specific tokenizers (Farasa, Camel Tools, etc.).
- CleanAndNormalize(text, removeDiacritics=True, removePunct=True, normalizeHamza=True, removeElongation=True, convertNumbers=True)[source]¶
Run a compact cleaning pipeline on a single text string.
This helper composes commonly used operations: normalize characters, optionally remove diacritics, strip punctuation, convert Arabic numbers to Western digits, collapse whitespace, and trim the result.
- Parameters:
text (str) – Input text.
removeDiacritics (bool) – Remove Arabic diacritics if True.
removePunct (bool) – Remove punctuation if True.
normalizeHamza (bool) – Normalize hamza/alef variants if True.
removeElongation (bool) – Remove repeated character elongation if True.
convertNumbers (bool) – Convert Arabic/Persian digits to ASCII digits if True.
- Returns:
Cleaned and normalized text.
- Return type:
- StripNonArabic(text, keepSpaces=True)[source]¶
Remove characters that are not in the Arabic Unicode block.
- ArabicCharRatio(text)[source]¶
Compute the ratio of Arabic characters to all characters in the string.
- ArabicWordCount(text)[source]¶
Count Arabic words/tokens in a string using the lightweight tokenizer.
- GetArabicCharNGrams(text, n=3)[source]¶
Return character n-grams from the Arabic-only content of the text.
- Parameters:
- Returns:
List of character n-gram strings. Returns empty list if no Arabic text found.
- Return type:
Note
This operates on the concatenated Arabic tokens (spaces removed) to produce contiguous character n-grams.