Skip to main content

Pyarabic: python package for Arabic language

 Introduction:

 In languages which are non-english and non-european as well, NLP work has progressed slowly in the last few decades because of the lesser number of scholars working on them as well as a lack of global interest in them. But now the time has changed and people from all over the world are collaborating on these lesser explored libraries and they are building resources for working on these languages with the same ease with that of english. 

Pyarabic is a package created from such a similar effort which deals with the intricate details of the arabic language and helps processing all kinds of arabic texts. While trying to learn it, being from a non-arab background, I couldn't read lots of parts of the main readthedocs site and had to work my around it. So in this blog post, I will summarize my learnings in english language, so that you can learn it and use the package with much more ease than me.

[Credit where credit is due: this article heavily uses the actual documentation provided in the reference link in the end. I am very much grateful to the authors for being able to work on arab nlp.]

Features:

First things first; lets get our basics known. The pyarabic package is written by taha zerrouki (http://tahadz.com/). Here are the following features mentioned for this library:

  • Arabic letters classification
  • Text tokenization into words or sentences
  • Strip Harakat ( all, except Shadda, tatweel, last_haraka)
  • Sperate and join Letters and Harakat
  • Reduce tashkeel
  • Mesure tashkeel similarity ( Harakats, fully or partially vocalized, similarity with a template)
  • Letters normalization ( Ligatures and Hamza)
  • Numbers to words
  • Extract numerical phrases
  • Pre-vocalization of numerical phrases
  • Unshiping texts

So, basically all the basic NLP stuff. But to know more into what these tasks stand for, we have to know what harakat, tashkeel and all other terms stand for. 

Harakat/tashkeel:

Harakat translates to short vowel in arab. There are different vowels such as shadda, tanween etc which sit on the arabic letters and change their meaning. 

Cases:

There are no upper case or lower cases in arabic.  

How to tokenize arabic text?

I think in NLP, the most bare minimum thing to achieve is to do tokenization of texts. For arabic, you can simply do the following:

from pyarabic import araby

text_tokenized = araby.tokenize(arabic_text)

and simply the text_tokenized will have the list of tokens.

The tokenize function also allows different morph ( transformation and modification of the text) and conditions. The morph and conditions take both strings of function names which are built in to the pyarabic package or callable/lambda functions directly passed to be used for tokenization. Let's explore it with 2 examples from each.

(a) example of built in functions being passed:

tokenize with built in condition morphs pyarabic

(b) example of callable being used:

callable condition morphs in pyarabic tokenization

Also, one can use the sentence_tokenize method to break down a paragraph or so into the different sentences. Checkout the below example for the same:

tokenize sentence using sentence_tokenize in pyarabic

There are other utility functions related to tokenization; but you can check the documentation or figure out using simple python code yourself only. Next lets check how to handle numbers in arabic using this package.

how to extract numbers and deal with numbers: 

There is a specific number.py file in the pyarabic language. All the number manipulations are done using this script from the package. 

Let's take a look into a few use cases.

(a) how to turn arabic numbers into strings using pyarabic?

Here we use int2str function from the ArNumbers class of the number.py file of the pyarabic package. Check out the example below how it spells out 125.


(b) how to turn arabic text into numbers using pyarabic?

For this we use text2number function from the number.py script. In this example, it changes the spelled out number into western digits 523.

arabicstrings into numbers pyarabic

(c) How to count or find out number words in a text?

The necessary function for this is extract_number_phrases. Check the example below. It finds out 2 different number phrases from the given text string.

pyarabic arabic count number phrases from text

(d) How to normalize all types of numerals into same numeral format?

For this we will have to use a different script which is for translation, named as trans. You can use it like

from pyarabic.number import *

Check the following example how it works. 


So these are the different kind of number manipulations one can do with pyarabic. Let's focus on other things.

It is known that, in arabic, it is often normal to write without the tashkeel or the harakats. A bunch of different functions are there to help us normalize our texts therefore by stripping it off those vowels and thereby normalizing them.

So let's take into a look into

Normalizing arabic texts by removing vowels:

(a) stripping diacritics:

Most of the times, the vowels are applied as diacritic marks on the arabic text. To strip diacritic therefore means removing harakat and small letters. You can use strip_diacritics function for the same just like below:

stripping diacritics pyarabic using strip_diacritics

(b) reducing tashkeel :

In this case we only delete the tashkeels. The function to use for this is reduce_tashkeel. Check how we are using it below:

pyarabic reducing tashkeels

(c) strip_tatweel and strip_shadda are similarly used to strip the text off tatweel and shadda.

(d) other than these, there are normalizing functions such as normalize_hamda and so on. For learning about them and what specific cases they are used in, you can follow the official documentation.

Conclusion:

In this article, we have briefly explored what pyarabic is, how to manipulate and tokenize text, count or transform numbers into string and vice versa; and how to handle vowel normalization and vowel stripping which is a specific characteristic to the arabic language itself. This is good enough to get you started with some real nlp in arabic. But note that further critical attributes such as sentence parsing, parts of speech tagging are not provided by this library currently; which makes this library significantly weaker to the general comparison in other language's dominant libraries. But withstanding that, pyarabic stands for the first big nlp library for arabic natural language processing toolkits.

Please comment, share and let me know if any mistake, improvement, change or addition is needed to be made to the article.

References:

(1) pyarabic documentation

(2) arabic alphabet

Comments

Popular posts from this blog

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme