[Python-il] Determining if a string is RTL
oavtal at bezeqint.net
Sat Jan 3 20:27:38 IST 2009
> In order to determine if a string is RTL this (untested) PHP regex was
> suggested: preg_match( "|[ا-يא-ת]|", substr($text, 0, 1))
> 1) Is grepping the first letter of a string for a Hebrew or Arabic
> character considered good enough to determine if a string should be
> treated as RTL for purposes of setting directionality in HTML output?
You have to skip things like whitespace and punctuation at the beginning
of the string. These are "weak" characters that do not define
Only the first "strong" character in the string, such as a letter,
controls the directionality.
For example, assuming SHALOM is in Hebrew, this string is RTL, even
though it doesn't begin with a hebrew letter:
You can add a few of those punctuation characters to the regex but that
will be a hack. For "true" directionality, you should have a database of
all unicode characters and their directionality.
Fribidi has such a database, and is embedded into PHP, but unfortunately
there is no interface to the fribidi_get_type() function. Nothing a
feature request can't fix :)
And as a sidenote, you can use "^" in your regex instead of
substr($text, 0, 1).. it may be a bit slower, but it's cleaner.
For a better algorithm that covers languages other than Hebrew, based on
such a database, Pango has a simple implementation:
or via google code search:
pango_unichar_direction is taken from a trimmed-down fribidi, found here:
> 2) Does Python have a better way of doing this?
There's no function to quickly give you an answer.
It does have the unicode character database, but you have to work a bit
to write a function similar to the one in Pango:
The function "bidirectional" in the unicodedata module will return
one of the codes in the table in . Run it for each character in the
string, until the answer is one of ['L', 'LRE' or 'LRO'] (text is LTR)
or ['R', 'AL', 'RLE', 'RLO']. If no such character is found, pick a
default direction :)
On 01/03/2009 01:08 PM, Dotan Cohen wrote:
> Please keep this post in English, as I am forwarding it to a developer
> who does not speak Hebrew. Thanks.
> Here is the related bug:
More information about the Python-il