Tuesday, 29 January 2019

Encoding Arabic letters with their diacritics (if exists)

I'm working on a deep learning project in which we use RNN. I want to encode the data before it's fed to the network. Input is Arabic verses, which have diacritics that are treated as separate characters in Python. I should encode/represent the character with the character following it with a number if the character following it is a diacritic, else I only encode the character.

Doing so for millions of verses, was hoping to use lambda with map. However, I cannot iterate with two characters at a time, i.e., was hoping for:

map(lambda ch, next_ch: encode(ch + next_ch) if is_diacritic(next_ch) else encode(ch), verse)

My intention behind the question is finding the fastest way to accomplish the above. No restriction on lambda functions, but for loop answers are not what I'm looking for.

A close example for non-Arabians, assume you want to encode the following text:

 XXA)L_I!I%M<LLL>MMQ*Q

You want to encode the letter after concatenating it with the letter following it if it's a special character, else just encode the letter only.

Output:

['X', 'X', 'A)', 'L_', 'I!', 'I%', 'M<', 'L', 'L', 'L>', 'M', 'M', 'Q*', 'Q']

For Arabians:

Verse example:

"قفا نبك من ذِكرى حبيب ومنزل بسِقطِ اللّوى بينَ الدَّخول فحَوْمل"

Diacritics are these small symbols above the letter(i.e., ّ , ْ )


[Update]

Range of diacritics starts at 64B HEX or 1611 INT and ends at 652 HEX or 1618 INT.

And letters 621 HEX - 1569 INT to 63A HEX - 1594 INT and from 641 HEX - 1601 INT to 64A HEX - 1610 INT

A letter can have at most one diacritic.


Extra information:

A similar encoding methodology to what I'm doing is representing the binary form of the verse as a matrix with shape (number of bits needed, number of characters in a verse). Both the number of bits and number of characters are calculated after we combine each letter with its diacritic if it exists.

For example, assume the verse is the following, and diacritics are special characters:

X+Y_XX+YYYY_

The alphabet different combinations are:

['X', 'X+', 'X_', 'Y', 'Y+', 'Y_']  

Hence I need 3 bits (at least) to represent these 6 characters, so the number of bits needed is 3

Consider the following encodings:

{
'X' : 000,
'X+': 001,
'X_': 010,
'Y':  011,
'Y+': 100,
'Y_': 101,
}

And I get to represent the example in a matrix as (binary representation is vertical):

X+     Y_    X    X+    Y    Y    Y    Y_
0      1     0    0     0    0    0    1
0      0     0    0     1    1    1    0
1      1     0    1     1    1    1    1

Which is why I am looking to combine diacritics with letters first.


Note: Iterate over a string 2 (or n) characters at a time in Python and Iterating each character in a string using Python do not give the intended answer.



from Encoding Arabic letters with their diacritics (if exists)

No comments:

Post a Comment