Esempio n. 1
0
    (re.compile(r'1$'), ''),
)
BOPOMOFO_TABLE = dict(
    zip('bpmfdtnlgkhjqxZCSrzcsiuvaoeEAIOUMNKGR2340',
        'ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄧㄨㄩㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦˊˇˋ˙'))


class BopomofoConverter(object):
    def to_bopomofo(self, pinyin, **kwargs):
        pinyin = self._pre_convert(pinyin)
        # 查表替换成注音
        for find_re, replace in BOPOMOFO_REPLACE:
            pinyin = find_re.sub(replace, pinyin)
        pinyin = ''.join(BOPOMOFO_TABLE.get(x, x) for x in pinyin)
        return pinyin

    def to_bopomofo_first(self, pinyin, **kwargs):
        pinyin = self.to_bopomofo(pinyin, **kwargs)
        return pinyin[0]

    def _pre_convert(self, pinyin):
        # 用数字表示声调
        pinyin = replace_symbol_to_number(pinyin)
        # 将声调数字移动到最后
        return RE_TONE3.sub(r'\1\3\2', pinyin)


converter = BopomofoConverter()
register(Style.BOPOMOFO, func=converter.to_bopomofo)
register(Style.BOPOMOFO_FIRST, func=converter.to_bopomofo_first)
Esempio n. 2
0
        has_fi = has_finals(pinyin)

        # 用数字表示声调
        pinyin = replace_symbol_to_number(pinyin)
        if not has_fi:
            return pinyin
        # 获取韵母部分
        return get_finals(pinyin, strict=False)

    def to_finals_tone3(self, pinyin, **kwargs):
        if kwargs.get('strict'):
            pinyin = convert_finals(pinyin)
        has_fi = has_finals(pinyin)

        # 用数字表示声调
        pinyin = replace_symbol_to_number(pinyin)
        # 将声调数字移动到最后
        pinyin = RE_TONE3.sub(r'\1\3\2', pinyin)

        if not has_fi:
            return pinyin
        # 获取韵母部分
        return get_finals(pinyin, strict=False)


converter = FinalsConverter()
register(Style.FINALS, func=converter.to_finals)
register(Style.FINALS_TONE, func=converter.to_finals_tone)
register(Style.FINALS_TONE2, func=converter.to_finals_tone2)
register(Style.FINALS_TONE3, func=converter.to_finals_tone3)
Esempio n. 3
0
    (re.compile(r'^v(\d?$)'), 'vI'),
)
CYRILLIC_TABLE = dict(
    zip(u'abwgdEOrZiIklmnopRstufhqcCSHTMUevAV',
        u'абвгдеёжзийклмнопрстуфхццчшщъьыэюяю'))


class CyrillicfoConverter(object):
    def to_cyrillic(self, pinyin, **kwargs):
        pinyin = self._pre_convert(pinyin)
        # 查表替换成注音
        for find_re, replace in CYRILLIC_REPLACE:
            pinyin = find_re.sub(replace, pinyin)
        pinyin = ''.join(CYRILLIC_TABLE.get(x, x) for x in pinyin)
        return pinyin

    def to_cyrillic_first(self, pinyin, **kwargs):
        pinyin = self.to_cyrillic(pinyin, **kwargs)
        return pinyin[0]

    def _pre_convert(self, pinyin):
        # 用数字表示声调
        pinyin = replace_symbol_to_number(pinyin)
        # 将声调数字移动到最后
        return RE_TONE3.sub(r'\1\3\2', pinyin)


converter = CyrillicfoConverter()
register(Style.CYRILLIC, func=converter.to_cyrillic)
register(Style.CYRILLIC_FIRST, func=converter.to_cyrillic_first)
Esempio n. 4
0
Style.TONE2
Style.TONE3
"""
from __future__ import unicode_literals

from apps.pypinyin.constants import Style
from apps.pypinyin.style import register
from apps.pypinyin.style._constants import RE_TONE3
from apps.pypinyin.style._utils import replace_symbol_to_number


class ToneConverter(object):
    def to_tone(self, pinyin, **kwargs):
        return pinyin

    def to_tone2(self, pinyin, **kwargs):
        # 用数字表示声调
        pinyin = replace_symbol_to_number(pinyin)
        return pinyin

    def to_tone3(self, pinyin, **kwargs):
        pinyin = self.to_tone2(pinyin, **kwargs)
        # 将声调数字移动到最后
        return RE_TONE3.sub(r'\1\3\2', pinyin)


converter = ToneConverter()
register(Style.TONE, func=converter.to_tone)
register(Style.TONE2, func=converter.to_tone2)
register(Style.TONE3, func=converter.to_tone3)
Esempio n. 5
0
# -*- coding: utf-8 -*-
"""其他几个拼音风格实现:

Style.NORMAL
Style.FIRST_LETTER
"""
from __future__ import unicode_literals

from apps.pypinyin.constants import Style
from apps.pypinyin.style import register
from apps.pypinyin.style._utils import replace_symbol_to_no_symbol


class OthersConverter(object):
    def to_normal(self, pinyin, **kwargs):
        pinyin = replace_symbol_to_no_symbol(pinyin)
        return pinyin

    def to_first_letter(self, pinyin, **kwargs):
        # 用数字表示声调
        pinyin = self.to_normal(pinyin)
        return pinyin[0]


converter = OthersConverter()
register(Style.NORMAL, func=converter.to_normal)
register(Style.FIRST_LETTER, func=converter.to_first_letter)