Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert text to Braille (Unicode) in Python

Tags:

python

According to Wikipedia, the Unicode block for braille is U+2800 .. U+28FF.

I'm trying to convert normal text to Braille symbols (dots). To do so, I'm mapping this string:

" A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)="

The reason for mapping this specific string is mentioned on this Wikipedia page

My code:

def toBraille(c):
unic=2800
mapping = " A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)="
i = mapping.index(c.upper())
if (i>0):
    unic+=i 
    unichex = hex(unic)
    return unichr(unichex))
if (i==0):
    return '_'
if (i<O):
    return '?'

def converter(txt):
tmp=""
for x in txt:
    tmp+=str(toBraille(x))
return tmp

txt = raw_input("Please insert text: \n")
print(converter(txt))

I want to print braille characters like this

input = hello world
output = ⠓⠑⠇⠇⠕ ⠺⠕⠗⠇⠙

The problem is my output looks like this

Input = A
Output = 2801
like image 725
Pryda Avatar asked Jun 11 '26 08:06

Pryda


2 Answers

Preferably, this should be defined as a dictionary, not as two arrays.

For Braille Dots:

code_table = {
    'a': '100000',
    'b': '110000',
    'c': '100100',
    'd': '100110',
    'e': '100010',
    'f': '110100',
    'g': '110110',
    'h': '110010',
    'i': '010100',
    'j': '010110',
    'k': '101000',
    'l': '111000',
    'm': '101100',
    'n': '101110',
    'o': '101010',
    'p': '111100',
    'q': '111110',
    'r': '111010',
    's': '011100',
    't': '011110',
    'u': '101001',
    'v': '111001',
    'w': '010111',
    'x': '101101',
    'y': '101111',
    'z': '101011',
    '#': '001111',
    '1': '100000',
    '2': '110000',
    '3': '100100',
    '4': '100110',
    '5': '100010',
    '6': '110100',
    '7': '110110',
    '8': '110010',
    '9': '010100',
    '0': '010110',
    ' ': '000000'}

Similarly can be done for Braille Glyph.

like image 69
Apurv Sibal Avatar answered Jun 12 '26 22:06

Apurv Sibal


Remapping strings is built-in to python. With str.maketrans and str.translate you could do this:

intab = "helo"  # ...add the full alphabet and other characters
outtab = "⠓⠑⠇⠕" # and the characters you want them translated to
transtab = str.maketrans(intab, outtab)

strg = "hello"
print(strg.translate(transtab)) # ⠓⠑⠇⠇⠕

Note that the length of intab must match the length of outtab if you pass 2 arguments to maketrans only (you could pass a third argument; see doc).

like image 25
hiro protagonist Avatar answered Jun 12 '26 20:06

hiro protagonist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!