tone.py 917 B

12345678910111213141516171819202122232425262728293031323334
  1. # -*- coding: utf-8 -*-
  2. """TONE 相关的几个拼音风格实现:
  3. Style.TONE
  4. Style.TONE2
  5. Style.TONE3
  6. """
  7. from __future__ import unicode_literals
  8. from pypinyin.constants import Style
  9. from pypinyin.style import register
  10. from pypinyin.style._constants import RE_TONE3
  11. from pypinyin.style._utils import replace_symbol_to_number
  12. class ToneConverter(object):
  13. def to_tone(self, pinyin, **kwargs):
  14. return pinyin
  15. def to_tone2(self, pinyin, **kwargs):
  16. # 用数字表示声调
  17. pinyin = replace_symbol_to_number(pinyin)
  18. return pinyin
  19. def to_tone3(self, pinyin, **kwargs):
  20. pinyin = self.to_tone2(pinyin, **kwargs)
  21. # 将声调数字移动到最后
  22. return RE_TONE3.sub(r'\1\3\2', pinyin)
  23. converter = ToneConverter()
  24. register(Style.TONE, func=converter.to_tone)
  25. register(Style.TONE2, func=converter.to_tone2)
  26. register(Style.TONE3, func=converter.to_tone3)