Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Aug 21, 2022
2 parents e1f34db + 77bd383 commit 81f4669
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ Changelog
---------


`0.47.1`_ (2022-08-21)
+++++++++++++++++++++++++

* **[Bugfixed]** 修复无声母和韵母的场景下指定 neutral_tone_with_five=True 会返回 ``5`` 作为拼音的问题:

.. code-block:: python
# 修复前
>>> lazy_pinyin('', style=Style.FINALS_TONE3, neutral_tone_with_five=True)
['5']
# 修复后
>>> lazy_pinyin('', style=Style.FINALS_TONE3, neutral_tone_with_five=True)
['']
`0.47.0`_ (2022-07-30)
+++++++++++++++++++++++++

Expand Down Expand Up @@ -993,3 +1008,4 @@ __ https://github.com/mozillazg/python-pinyin/issues/8
.. _0.45.0: https://github.com/mozillazg/python-pinyin/compare/v0.44.0...v0.45.0
.. _0.46.0: https://github.com/mozillazg/python-pinyin/compare/v0.45.0...v0.46.0
.. _0.47.0: https://github.com/mozillazg/python-pinyin/compare/v0.46.0...v0.47.0
.. _0.47.1: https://github.com/mozillazg/python-pinyin/compare/v0.47.0...v0.47.1
2 changes: 2 additions & 0 deletions pypinyin/contrib/neutral_tone.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def post_convert_style(self, han, orig_pinyin, converted_pinyin,

if pre_data is not None:
converted_pinyin = pre_data
if not converted_pinyin: # 空字符串
return converted_pinyin
# 有声调,跳过
if _re_number.search(converted_pinyin):
return converted_pinyin
Expand Down
26 changes: 17 additions & 9 deletions pypinyin/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ def convert(self, words, style, heteronym, errors, strict, **kwargs):
if RE_HANS.match(words):
pys = self._phrase_pinyin(words, style=style, heteronym=heteronym,
errors=errors, strict=strict)
return _remove_dup_and_empty(pys)
post_data = self.post_pinyin(words, heteronym, pys)
if post_data is not None:
pys = post_data

pys = self.convert_styles(
pys, words, style, heteronym, errors, strict)

else:
py = self.handle_nopinyin(words, style=style, errors=errors,
heteronym=heteronym, strict=strict)
if py:
pys.extend(py)

py = self.handle_nopinyin(words, style=style, errors=errors,
heteronym=heteronym, strict=strict)
if py:
pys.extend(py)
return _remove_dup_and_empty(pys)

def pre_convert_style(self, han, orig_pinyin, style, strict, **kwargs):
Expand All @@ -84,7 +91,7 @@ def convert_style(self, han, orig_pinyin, style, strict, **kwargs):
转换风格前会调用 ``pre_convert_style`` 方法,
转换后会调用 ``post_convert_style`` 方法。
:param han: 要处理的汉字
:param han: 要处理的单个汉字
:param orig_pinyin: 汉字对应的原始带声调拼音
:param style: 拼音风格
:param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
Expand Down Expand Up @@ -251,10 +258,11 @@ def _phrase_pinyin(self, phrase, style, heteronym, errors, strict):
py = self._single_pinyin(han, style, heteronym, errors, strict)
pinyin_list.extend(py)

post_data = self.post_pinyin(phrase, heteronym, pinyin_list)
if post_data is not None:
pinyin_list = post_data
return pinyin_list

def convert_styles(self, pinyin_list, phrase, style, heteronym, errors,
strict, **kwargs):
"""转换多个汉字的拼音结果的风格"""
for idx, item in enumerate(pinyin_list):
han = phrase[idx]
if heteronym:
Expand Down
5 changes: 5 additions & 0 deletions pypinyin/converter.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class DefaultConverter(Converter):
pinyin: TPinyinResult,
**kwargs: Any) -> Union[TPinyinResult, None]: ...

def convert_styles(self, pinyin_list: TPinyinResult,
phrase: Text, style: TStyle, heteronym: bool,
errors: TErrors, strict: bool, **kwargs: Any,
) -> TPinyinResult: ...

def _phrase_pinyin(self, phrase: Text, style: TStyle, heteronym: bool,
errors: TErrors, strict: bool
) -> TPinyinResult: ...
Expand Down
18 changes: 18 additions & 0 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from pytest import mark

from pypinyin import pinyin, Style, lazy_pinyin, slug
from pypinyin.seg.simpleseg import simple_seg

Expand Down Expand Up @@ -49,6 +51,22 @@ def test_issue_266():
assert lazy_pinyin('呣', style=Style.FINALS, strict=False) == ['m']


@mark.parametrize('neutral_tone_with_five', [True, False])
def test_issue_284(neutral_tone_with_five):
assert lazy_pinyin('嗯', style=Style.FINALS_TONE3,
neutral_tone_with_five=neutral_tone_with_five) == ['']
assert lazy_pinyin('呣', style=Style.FINALS_TONE3,
neutral_tone_with_five=neutral_tone_with_five) == ['']
assert lazy_pinyin('嗯', style=Style.FINALS,
neutral_tone_with_five=neutral_tone_with_five) == ['']
assert lazy_pinyin('呣', style=Style.FINALS,
neutral_tone_with_five=neutral_tone_with_five) == ['']
assert lazy_pinyin('嗯', style=Style.INITIALS,
neutral_tone_with_five=neutral_tone_with_five) == ['']
assert lazy_pinyin('呣', style=Style.INITIALS,
neutral_tone_with_five=neutral_tone_with_five) == ['']


if __name__ == '__main__':
import pytest
pytest.cmdline.main()

0 comments on commit 81f4669

Please sign in to comment.