telephone.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -*-
  2. """TELEPHONE类
  3. 电话号码 <=> 中文字符串 方法
  4. 中文字符串 <=> 电话号码 方法
  5. """
  6. __author__ = "Zhiyang Zhou <zyzhou@stu.xmu.edu.cn>"
  7. __data__ = "2019-05-03"
  8. from fish_speech.text.chn_text_norm.basic_util import *
  9. class TelePhone:
  10. """
  11. TELEPHONE类
  12. """
  13. def __init__(self, telephone=None, raw_chntext=None, chntext=None):
  14. self.telephone = telephone
  15. self.raw_chntext = raw_chntext
  16. self.chntext = chntext
  17. # def chntext2telephone(self):
  18. # sil_parts = self.raw_chntext.split('<SIL>')
  19. # self.telephone = '-'.join([
  20. # str(chn2num(p)) for p in sil_parts
  21. # ])
  22. # return self.telephone
  23. def telephone2chntext(self, fixed=False):
  24. if fixed:
  25. sil_parts = self.telephone.split("-")
  26. self.raw_chntext = "<SIL>".join(
  27. [num2chn(part, alt_two=False, use_units=False) for part in sil_parts]
  28. )
  29. self.chntext = self.raw_chntext.replace("<SIL>", "")
  30. else:
  31. sp_parts = self.telephone.strip("+").split()
  32. self.raw_chntext = "<SP>".join(
  33. [num2chn(part, alt_two=False, use_units=False) for part in sp_parts]
  34. )
  35. self.chntext = self.raw_chntext.replace("<SP>", "")
  36. return self.chntext
  37. if __name__ == "__main__":
  38. # 测试程序
  39. print(TelePhone(telephone="0595-23980880").telephone2chntext())
  40. # print(TelePhone(raw_chntext='零五九五杠二三八六五零九八').chntext2telephone())