tobraille程序

下面我将放出tobraille的程序了,虽然做得一塌糊涂。


# -*- coding:utf-8 -*-
# !/usr/bin/python

import re
import sys
from xpinyin import Pinyin
from dotsBraille import braille2dots


class KanjiToPinyin(object):
    def __init__(self):
        self.shengmu = None
        self.yunmu = None
        self.tone = None
        self.character = None
        self.pinyin = None
        self.braille = str
        self.break_flag = False

        self.tone_table = [
            (0, u"aoeiuv"),
            (1, u"\u0101\u014d\u0113\u012b\u016b\u01d6\u01d6"),
            (2, u"\u00e1\u00f3\u00e9\u00ed\u00fa\u01d8\u01d8"),
            (3, u"\u01ce\u01d2\u011b\u01d0\u01d4\u01da\u01da"),
            (4, u"\u00e0\u00f2\u00e8\u00ec\u00f9\u01dc\u01dc"),
        ]

        self.yunmu_table = ['a', 'o', 'e', 'i', 'u', 'v', 'ai', 'ei', 'ui', 'ao', 'ou', 'iu', 'ie', 'ue', 'er', 'an', 'en', 'in', 'un',
         'vn', 'ang', 'eng', 'ing', 'ong', 'ia', 'ian', 'iang', 'iao', 'iong', 'uai', 'ua', 'ue', 'uan', 'uang', 'uo', 'van']

    def get_pinyin(self, character):
        self.__init__()
        my_re = re.compile(r'[A-Za-z]', re.S)
        res = re.findall(my_re, character)
        my_re2 = re.compile(r'[0-9]', re.S)
        res2 = re.findall(my_re2, character)
        if len(res) or len(res2):
            return
        else:
            pass
        self.break_flag = False
        p = Pinyin()
        self.character = Character(character)
        pinyin_with_tone = p.get_pinyin(self.character.string, show_tone_marks=True, convert='lower')
        self.character.pinyin = pinyin_with_tone
        for letter in self.character.pinyin:
            n = 1
            for tones in self.tone_table[1:]:
                if letter in tones[1]:
                    self.character.tone = tones[0]
                    i = self.character.pinyin.index(letter)
                    j = tones[1].index(letter)
                    if i != 0:
                        if i + 1 != len(self.character.pinyin):
                            self.character.pinyin_notone = self.character.pinyin[0:i] + self.tone_table[0][1][j] + self.character.pinyin[i+1:]
                        else:
                            self.character.pinyin_notone = self.character.pinyin[0:i] + self.tone_table[0][1][j]
                    else:
                        if i + 1 != len(self.character.pinyin):
                            self.character.pinyin_notone = self.tone_table[0][1][j] +self.character.pinyin[i+1:]
                        else:
                            self.character.pinyin_notone = self.tone_table[0][1][j]
                    self.pinyin = self.character.pinyin_notone
                    self.break_flag = True

                if self.break_flag:
                    break
                n = n + 1
            if self.break_flag:
                break
        if n == 5:
            self.pinyin = self.character.pinyin
            self.tone = 0
        else:
            self.tone = n

    def divide_pinyin(self):
        if self.pinyin is None:
            return
        else:
            pass
        for letter in self.pinyin:
            if letter in self.yunmu_table:
                pos = self.pinyin.index(letter)
                break
        if pos != 0:
            self.shengmu = self.pinyin[0: pos]
            self.yunmu = self.pinyin[pos:]
        else:
            self.yunmu = self.pinyin

    def get_modified(self):
        if self.pinyin is None:
            return
        else:
            pass
        if self.character.string == '是':
            self.shengmu = 'sh'
            self.yunmu = None
            self.tone = None
        elif self.character.string == '的':
            self.shengmu = 'd'
            self.yunmu = None
            self.tone = None
        if self.shengmu == 'y':
            self.shengmu = None
            if self.yunmu[0] == 'u':
                self.yunmu = 'v'
            elif self.yunmu[0] == 'i':
                pass
            else:
                self.yunmu = 'i' + self.yunmu
        elif self.shengmu == 'w':
            self.shengmu = None
            if self.yunmu[0] == 'u':
                pass
            else:
                self.yunmu = 'u' + self.yunmu

        if self.yunmu in ['iou', 'uei', 'uen']:
            self.yunmu = self.yunmu[0] + self.yunmu[2]

        if self.yunmu == 'u':
            if self.shengmu in ['j', 'q', 'x']:
                self.yunmu = 'v'
            else:
                pass

        try:
            self.pinyin = self.shengmu + self.yunmu
        except:
            if self.shengmu is None:
                self.pinyin = self.yunmu
            elif self.yunmu is None:
                self.pinyin = self.shengmu
            else:
                pass

    def transfer(self, character):
        if character != '':
            self.get_pinyin(character)
            self.divide_pinyin()
            self.get_modified()
        else:
            print('The word in empty!')
            sys.exit()
        return (self.shengmu, self.yunmu, self.tone)


class Pinyin2Braille:
    def __init__(self):
        self.shengmu = None
        self.shengmu_symbol = None
        self.yunmu = None
        self.yunmu_symbol = None
        self.tone = None
        self.tone_symbol = None
        self.braille = None
        self.dots = None

        self.shengmu_trans_table = [
            ('b', 'b'), ('p', 'p'), ('m', 'm'), ('f', 'f'), ('d', 'd'), ('t', 't'), ('n', 'n'), ('l', 'l'), ('g', 'g'),
            ('k', 'k'), ('h', 'h'), ('j', 'g'), ('q', 'k'), ('x', 'h'), ('zh', '/'), ('ch', 'q'), ('sh', ':'),
            ('r', 'j'), ('z', 'z'), ('c', 'c'), ('s', 's'), ('y', 'i'), ('w', 'u'),
        ]

        self.yunmu_trans_table = [
            ('a', '9'), ('o', '5'), ('e', '5'), ('i', 'i'), ('u', 'u'), ('v', '+'), ('ai', '['), ('ei', '!'),
            ('ui', 'w'), ('ao', '6'), ('ou', '('), ('iu', '\\'), ('ie', 'e'), ('ue', ')'), ('er', 'r'), ('an', 'v'),
            ('en', '0'), ('in', '<'), ('un', '3'), ('vn', '_'), ('ang', '8'), ('eng', '#'), ('ing', '*'),
            ('ong', '4'), ('ian', '%'),('ia', '$'), ('iang', 'x'), ('iao', '>'), ('iong', '?'), ('uai', 'y'), ('ua', '='),
            ('uan', ']'), ('uang', '7'), ('uo', 'o'), ('van', '&'),
        ]

        self.tone_trans_table = [(0, ''), (1, 'a'), (2, '1'), (3, '3'), (4, '2')]

        self.figure_dots_table = [
            ('a', '1'), ('1', '2'), ('b', '12'), ("'", '3'), ('k', '13'), ('2', '23'), ('l', '123'), ('`', '4'),
            ('c', '14'), ('i', '24'), ('f', '124'), ('/', '34'), ('m', '134'), ('s', '234'), ('p', '1234'), ('"', '5'),
            ('e', '15'), ('3', '25'), ('h', '125'), ('9', '35'), ('o', '135'), ('6', '235'), ('r', '1235'), ('^', '45'),
            ('d', '145'), ('j', '245'), ('g', '1245'), ('>', '345'), ('n', '1345'), ('t', '2345'), ('q', '12345'),
            (',', '6'), ('*', '16'), ('5', '26'), ('<', '126'), ('-', '36'), ('u', '136'), ('8', '236'), ('v', '1236'),
            ('.', '46'), ('%', '146'), ('[', '246'), ('$', '1246'), ('+', '346'), ('x', '1346'), ('!', '2346'),
            ('&', '12346'), (';', '56'), (':', '156'), ('4', '256'), ('\\', '1256'), ('0', '356'), ('z', '1356'),
            ('7', '2356'), ('(', '12356'), ('_', '456'), ('?', '1456'), ('w', '2456'), (']', '12456'), ('#', '3456'),
            ('y', '13456'), (')', '23456'), ('=', '123456'),
        ]

        self.figure_table = ['a', '1', 'b', "'", 'k', '2', 'l', '`', 'c', 'i', 'f', '/', 'm', 's', 'p', '"', 'e', '3', 'h', '9', 'o', '6', 'r', '^', 'd', 'j', 'g', '>', 'n', 't', 'q', ',', '*', '5', '<', '-', 'u', '8', 'v', '.', '%', '[', '$', '+', 'x', '!', '&', ';', ':', '4', '\\', '0', 'z', '7', '(', '_', '?', 'w', ']', '#', 'y', ')', '=']


    def handle(self):

        shengmu_table = []
        for group in self.shengmu_trans_table:
            shengmu_table.append(group[0])

        yunmu_table = []
        for group in self.yunmu_trans_table:
            yunmu_table.append(group[0])

        if self.shengmu is None and self.yunmu is None:
            self.braille = ''

        elif self.shengmu is None:
            self.shengmu_symbol = ''
            self.yunmu_symbol = self.yunmu_trans_table[yunmu_table.index(self.yunmu)][1]
            self.tone_symbol = self.tone_trans_table[self.tone][1]
            self.braille = self.yunmu_symbol + self.tone_symbol

        elif self.shengmu in shengmu_table:
            if self.yunmu is None:
                self.shengmu_symbol = self.shengmu_trans_table[shengmu_table.index(self.shengmu)][1]
                self.braille = self.shengmu_symbol

            else:
                if (self.shengmu in ['zh', 'ch', 'sh', 'z', 'c', 's']) and (self.yunmu == 'i'):
                    self.shengmu_symbol = self.shengmu_trans_table[shengmu_table.index(self.shengmu)][1]
                    self.tone_symbol = self.tone_trans_table[self.tone][1]
                    self.braille = self.shengmu_symbol + self.tone_symbol
                else:
                    self.shengmu_symbol = self.shengmu_trans_table[shengmu_table.index(self.shengmu)][1]
                    self.yunmu_symbol = self.yunmu_trans_table[yunmu_table.index(self.yunmu)][1]
                    self.braille = self.shengmu_symbol + self.yunmu_symbol

    def set_dots(self):
        self.dots = ''
        for i in self.braille:
            self.dots = self.dots + braille2dots(i) + '-'

    def transfer(self, combination = (None, None, None)):
        self.__init__()
        self.shengmu = combination[0]
        self.yunmu = combination[1]
        self.tone = combination[2]
        self.handle()
        self.set_dots()
        return (self.braille, self.dots)


class Character(object):
    def __init__(self, character):
        self.string = character
        self.pinyin = str
        self.tone = 0
        self.pinyin_notone = str


def toBraille(string=''):
    punctuation_dict = {
        '。': '"2',
        ',': '"',
        '?': '"\'',
        '!': ';1',
        ':': '-',
        '、': '`',
        ';': ';',
        '——': '36',
        '(': ';\'',
        ')': '\';',
        '【': ';2',
        '】': '2;',
        '“': '^',
        '”': '^',
        '’': '^^',
        '‘': '^^',
        '《': '"-',
        '》': '-"',
        '<': '"\'',
        '>': '\'"',
        '.': '"2',
        ',': '"',
        '?': '"\'',
        '!': ';1',
        ':': '-',
        ';': ';',
        '--': '36',
        '(': ';\'',
        ')': '\';',
        '[': ';2',
        ']': '2;',
        '"': '^',
        '': '^^',
    }

    output_string = ''
    output_dots = ''
    k = KanjiToPinyin()
    p = Pinyin2Braille()
    for character in string:
        if character in punctuation_dict.keys():
            output_string += punctuation_dict[character]
            t = punctuation_dict[character]
            for i in t:
                output_dots += braille2dots(i) + '-'

        elif character == ' ':
            output_string += character
            output_dots += 'blank-'
        else:
            output_string += p.transfer(k.transfer(character))[0]
            output_dots += p.transfer(k.transfer(character))[1]
    return output_string, output_dots[0:-1]

def toPinyin(string = ''):
    output_string = []
    k = KanjiToPinyin()
    for character in string:
        if character == ' ':
            output_string.append(character)
        else:
            output_string.append(k.transfer(character))
    return output_string

if __name__ == '__main__':
    print(toBraille('猫2sfaf'))
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,444评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,421评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,363评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,460评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,502评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,511评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,280评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,736评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,014评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,190评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,848评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,531评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,159评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,411评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,067评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,078评论 2 352

推荐阅读更多精彩内容

  • 因为什刹海的名字过于好听,我几乎可以对里面的一切宽容。 对蹩脚爆肚炒肝宽容,对吵闹游人宽容,对乘坐10分钟即需80...
    KK87阅读 794评论 1 2
  • 厉害的姑娘是手腕很厉害, 不是脑袋很厉害。
    殁沫阅读 108评论 0 0
  • 雨淅沥, 遥望云边千万里。 情归去, 君似磐石妾似雨。 千年磐石风中立, 细雨如丝入海底。 若不离, 沧海桑田定不...
    黎若雪阅读 290评论 0 0