How to use the pypinyin.converter.DefaultConverter function in pypinyin

To help you get started, we’ve selected a few pypinyin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_pre_handle_nopinyin_return_value():
    class A(DefaultConverter):
        def pre_handle_nopinyin(self, chars, style, heteronym, errors,
                                strict, **kwargs):
            return 'abc'

    han = 'test'
    assert DefaultConverter().convert(
        han, Style.TONE2, False, 'default', True) == [['test']]
    assert A().convert(
        han, Style.TONE2, False, 'default', True) == [['abc']]
github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_pre_convert_style_return_value():
    class A(DefaultConverter):
        def pre_convert_style(self, han, orig_pinyin, style, strict, **kwargs):
            return 'test'

    han = '测试'
    assert DefaultConverter().convert(
        han, Style.TONE2, False, 'ignore', True) == [['ce4'], ['shi4']]
    assert A().convert(
        han, Style.TONE2, False, 'ignore', True) == [['test'], ['test']]
github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_post_handle_nopinyin_return_value():
    class A(DefaultConverter):
        def post_handle_nopinyin(self, chars, style, heteronym, errors,
                                 strict, pinyin, **kwargs):
            return 'abc'

    han = 'test'
    assert DefaultConverter().convert(
        han, Style.TONE2, False, 'default', True) == [['test']]
    assert A().convert(
        han, Style.TONE2, False, 'default', True) == [['abc']]
github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_post_pinyin_return_value_single_pinyin():
    class A(DefaultConverter):
        def post_pinyin(self, han, heteronym, pinyin, **kwargs):
            return {
                '测': [['zhāo']],
                '试': [['yáng']],
                '测试': [['zhāo'], ['yáng']],
            }[han]

    han = '测试'
    assert DefaultConverter().convert(
        han, Style.TONE3, False, 'ignore', True) == [['ce4'], ['shi4']]
    assert A().convert(
        han, Style.TONE3, False, 'ignore', True) == [['zhao1'], ['yang2']]
github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_post_convert_style_return_value():
    class A(DefaultConverter):
        def post_convert_style(self, han, orig_pinyin, converted_pinyin,
                               style, strict, **kwargs):
            return 'test'

    han = '测试'
    assert DefaultConverter().convert(
        han, Style.TONE2, False, 'ignore', True) == [['ce4'], ['shi4']]
    assert A().convert(
        han, Style.TONE2, False, 'ignore', True) == [['test'], ['test']]
github mozillazg / python-pinyin / tests / contrib / test_neutral_tone.py View on Github external
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from pytest import mark

from pypinyin import lazy_pinyin, Style
from pypinyin.contrib.neutral_tone import NeutralToneWith5Mixin
from pypinyin.contrib.uv import V2UMixin
from pypinyin.converter import DefaultConverter
from pypinyin.core import Pinyin


class MyConverter(NeutralToneWith5Mixin, DefaultConverter):
    pass


class HerConverter(NeutralToneWith5Mixin, V2UMixin, DefaultConverter):
    pass


my_pinyin = Pinyin(MyConverter())
her_pinyin = Pinyin(HerConverter())


def test_neutral_tone_with_5():
    assert lazy_pinyin('好了', style=Style.TONE2) == ['ha3o', 'le']
    assert my_pinyin.lazy_pinyin('好了', style=Style.TONE2) == ['ha3o', 'le5']
    assert her_pinyin.lazy_pinyin('好了', style=Style.TONE2) == ['ha3o', 'le5']
github mozillazg / python-pinyin / tests / contrib / test_neutral_tone.py View on Github external
from __future__ import unicode_literals

from pytest import mark

from pypinyin import lazy_pinyin, Style
from pypinyin.contrib.neutral_tone import NeutralToneWith5Mixin
from pypinyin.contrib.uv import V2UMixin
from pypinyin.converter import DefaultConverter
from pypinyin.core import Pinyin


class MyConverter(NeutralToneWith5Mixin, DefaultConverter):
    pass


class HerConverter(NeutralToneWith5Mixin, V2UMixin, DefaultConverter):
    pass


my_pinyin = Pinyin(MyConverter())
her_pinyin = Pinyin(HerConverter())


def test_neutral_tone_with_5():
    assert lazy_pinyin('好了', style=Style.TONE2) == ['ha3o', 'le']
    assert my_pinyin.lazy_pinyin('好了', style=Style.TONE2) == ['ha3o', 'le5']
    assert her_pinyin.lazy_pinyin('好了', style=Style.TONE2) == ['ha3o', 'le5']

    assert lazy_pinyin('好了') == ['hao', 'le']
    assert my_pinyin.lazy_pinyin('好了') == ['hao', 'le']
    assert her_pinyin.lazy_pinyin('好了') == ['hao', 'le']
github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_post_pinyin_return_value_phrase_pinyin():
    class A(DefaultConverter):
        def post_pinyin(self, han, heteronym, pinyin, **kwargs):
            return {
                '北': [['zhāo']],
                '京': [['yáng']],
                '北京': [['zhāo'], ['yáng']],
            }[han]

    han = '北京'
    assert DefaultConverter().convert(
        han, Style.TONE3, False, 'ignore', True) == [['bei3'], ['jing1']]
    assert A().convert(
        han, Style.TONE3, False, 'ignore', True) == [['zhao1'], ['yang2']]
github mozillazg / python-pinyin / tests / test_converter.py View on Github external
def test_pre_convert_style_return_value():
    class A(DefaultConverter):
        def pre_convert_style(self, han, orig_pinyin, style, strict, **kwargs):
            return 'test'

    han = '测试'
    assert DefaultConverter().convert(
        han, Style.TONE2, False, 'ignore', True) == [['ce4'], ['shi4']]
    assert A().convert(
        han, Style.TONE2, False, 'ignore', True) == [['test'], ['test']]