【问题标题】:Convert text to Braille (Unicode) in Python在 Python 中将文本转换为盲文 (Unicode)
【发布时间】:2017-01-29 15:38:01
【问题描述】:

根据Wikipedia,盲文的Unicode块是U+2800 .. U+28FF。

我正在尝试将普通文本转换为盲文符号(点)。为此,我正在映射这个字符串:

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

提到了映射这个特定字符串的原因on this Wikipedia page

我的代码:

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))

我想打印这样的盲文字符

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

问题是我的输出看起来像这样

Input = A
Output = 2801

【问题讨论】:

  • 为什么要映射这个字符串?什么是特别的 w.r.t.你想要的转换?你希望你的普通同事理解它吗?
  • 我在维基百科上读过 [en.wikipedia.org/wiki/Braille_ASCII]: 这个 C 字符串(也可以在 Python 和其他接受 C 字符串文字的编程语言中使用)为 Unicode 盲文字符提供盲文 ASCII 映射从 U+2800 到 U+283F 的顺序,从字符串开头的 U+2800 开始:
  • 这是一种相当违反直觉的编码。破坏您的代码的一件事是 2800。它应该是十六进制的 0x2800。为什么不摆脱困惑,写一本普通的 Python 字典呢? {'A':u'⠁',...}?
  • 只需创建一个具有所需映射的字典并通过它运行每个字符。
  • @RadLexus 完成。

标签: python


【解决方案1】:

重映射字符串是 python 内置的:使用 str.maketransstr.translate 你可以这样做:

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)) # ⠓⠑⠇⠇⠕

请注意,如果您仅将 2 个参数传递给 maketransintab 的长度必须与 outtab 的长度匹配(您可以传递第三个参数;请参阅文档)。

【讨论】:

  • 请注意,这只是 Python 3。翻译表仅在 python 2 上为 ascii。
【解决方案2】:

最好将其定义为字典,而不是两个数组。

对于盲文点:

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'}

Braille Glyph 也可以这样做。

【讨论】:

    【解决方案3】:

    我使用了一个简单的 Python 字典,它可以工作,但它非常基础

    # ASCII
    asciicodes = [' ','!','"','#','$','%','&','','(',')','*','+',',','-','.','/',
              '0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
              'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
              'r','s','t','u','v','w','x','y','z','[','\\',']','^','_']
    
    # Braille symbols
    brailles = ['⠀','⠮','⠐','⠼','⠫','⠩','⠯','⠄','⠷','⠾','⠡','⠬','⠠','⠤','⠨','⠌','⠴','⠂','⠆','⠒','⠲','⠢',
            '⠖','⠶','⠦','⠔','⠱','⠰','⠣','⠿','⠜','⠹','⠈','⠁','⠃','⠉','⠙','⠑','⠋','⠛','⠓','⠊','⠚','⠅',
            '⠇','⠍','⠝','⠕','⠏','⠟','⠗','⠎','⠞','⠥','⠧','⠺','⠭','⠽','⠵','⠪','⠳','⠻','⠘','⠸']
    

    【讨论】:

    • 这应该被定义为字典而不是两个数组。当前版本不可维护。
    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2019-06-07
    • 2021-11-27
    • 2018-08-28
    • 2021-12-13
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多