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

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

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