Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@register(style_value)
def func(pinyin, **kwargs):
return pinyin + str(len(pinyin))
def test_custom_style_with_call():
style_value = 'test_custom_style_with_call'
def func(pinyin, **kwargs):
return str(len(pinyin))
register(style_value, func=func)
hans = '北京'
origin_pinyin_s = pinyin(hans)
expected_pinyin_s = deepcopy(origin_pinyin_s)
for pinyin_s in expected_pinyin_s:
for index, py in enumerate(pinyin_s):
pinyin_s[index] = func(py)
assert pinyin(hans, style=style_value) == expected_pinyin_s
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)
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)
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)
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)
import pypinyin
from pypinyin.style import register
from .textnorm import textnorm
from .nonstd_pinyin import _nonstd_style
import jieba
jieba.initialize()
register('nonstd', _nonstd_style)
def _get_pinyin(text, std=True, pb=False):
'''
Params:
text: string, normalized sentences
std: boolean, standard pinyin stylc, default: standard pinyin style
pb: boolen, prosody label, default: False
Returns:
pinyin: string
'''
if std:
style = pypinyin.Style.TONE3
else:
style = 'nonstd'