Braille Converter
Convert text to Unicode Braille patterns and back.
常见问题
代码实现
# Grade 1 Braille (Unicode Braille Patterns U+2800)
LETTER_DOTS = {
'a': 0b000001, 'b': 0b000011, 'c': 0b001001, 'd': 0b011001, 'e': 0b010001,
'f': 0b001011, 'g': 0b011011, 'h': 0b010011, 'i': 0b001010, 'j': 0b011010,
'k': 0b000101, 'l': 0b000111, 'm': 0b001101, 'n': 0b011101, 'o': 0b010101,
'p': 0b001111, 'q': 0b011111, 'r': 0b010111, 's': 0b001110, 't': 0b011110,
'u': 0b100101, 'v': 0b100111, 'w': 0b111010, 'x': 0b101101, 'y': 0b111101,
'z': 0b110101,
}
DIGIT_DOTS = {str(i): v for i, v in enumerate(
[0b011010, 0b000001, 0b000011, 0b001001, 0b011001, 0b010001,
0b001011, 0b011011, 0b010011, 0b001010], 0)}
NUMBER_INDICATOR = 0b111100
def text_to_braille(text: str) -> str:
result = []
num_mode = False
for ch in text.lower():
if ch.isdigit():
if not num_mode:
result.append(chr(0x2800 + NUMBER_INDICATOR))
num_mode = True
result.append(chr(0x2800 + DIGIT_DOTS[ch]))
elif ch in LETTER_DOTS:
num_mode = False
result.append(chr(0x2800 + LETTER_DOTS[ch]))
elif ch == ' ':
num_mode = False
result.append(chr(0x2800))
else:
result.append(ch)
return ''.join(result)
print(text_to_braille("Hello 123"))
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.