def is_right_to_left(text):
'''Check whether a text is right-to-left text or not
:param text: The text to check
:type text: string
:rtype: boolean
See: http://unicode.org/reports/tr9/#P2
TR9> In each paragraph, find the first character of type L, AL, or R
TR9> while skipping over any characters between an isolate initiator
TR9> and its matching PDI or, if it has no matching PDI, the end of the
TR9> paragraph
Examples:
>>> is_right_to_left('Hallo!')
False
>>> is_right_to_left('?')
True
>>> is_right_to_left('???')
False
>>> is_right_to_left('????')
True
>>> is_right_to_left('a????')
False
>>> is_right_to_left('?a?????')
True
'''
skip = False
for char in text:
bidi_cat = unicodedata.bidirectional(char)
if skip and bidi_cat != 'PDI':
continue
skip = False
if bidi_cat in ('AL', 'R'):
return True
if bidi_cat == 'L':
return False
if bidi_cat in ('LRI', 'RLI', 'FSI'):
skip = True
return False
评论列表
文章目录