Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for English to Braille Translator by UFO665
from math import ceil
from string import ascii_lowercase
def convert(code):
bin_code = bin(code)[2:].zfill(6)[::-1]
return [[int(bin_code[j + i * 3]) for i in range(2)] for j in range(3)]
lstLettersNumbers = list(map(convert, [1, 3, 9, 25, 17, 11, 27, 19, 10, 26,
5, 7, 13, 29, 21, 15, 31, 23, 14, 30,
37, 39, 62, 45, 61, 53]))
lstCapitalFormat = convert(32)
lstNumberFormat = convert(60)
dctSymbols = {" ": convert(0), ",": convert(2), "-": convert(18), "?": convert(38),
"!": convert(22), ".": convert(50), "_": convert(36)}
class Braille(object):
def __init__(self, text):
self._lstValue = [[], [], []]
for char in text:
self._addChar(char)
self._endRows()
def _addChar(self, char):
if char in dctSymbols:
lst = dctSymbols[char]
elif char.isdigit():
self._append(lstNumberFormat)
lst = lstLettersNumbers[int(char) - 1 if char != "0" else 9]
else:
if char.isupper():
self._append(lstCapitalFormat)
lst = lstLettersNumbers[ascii_lowercase.index(char.lower())]
self._append(lst)
def _append(self, lst):
if len(self._lstValue[-1]) == 29:
self._lstValue.append([0] * 29)
for i in range(3):
self._lstValue.append([])
if len(self._lstValue[-1]) != 0:
for i in range(-3, 0):
self._lstValue[i].append(0)
for i in range(-3, 0):
self._lstValue[i] += lst[i + 3]
def _endRows(self):
if len(self._lstValue) > 3:
for i in range(29 - len(self._lstValue[-1])):
for i in range(-3, 0):
self._lstValue[i].append(0)
def value(self):
return self._lstValue
def braille_page(text):
braille = Braille(text)
return braille.value()
Feb. 6, 2016