money.py 1018 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. """MONEY类
  3. 金钱 <=> 中文字符串 方法
  4. 中文字符串 <=> 金钱 方法
  5. """
  6. import re
  7. __author__ = "Zhiyang Zhou <zyzhou@stu.xmu.edu.cn>"
  8. __data__ = "2019-05-08"
  9. from fish_speech.text.chn_text_norm.cardinal import Cardinal
  10. class Money:
  11. """
  12. MONEY类
  13. """
  14. def __init__(self, money=None, chntext=None):
  15. self.money = money
  16. self.chntext = chntext
  17. # def chntext2money(self):
  18. # return self.money
  19. def money2chntext(self):
  20. money = self.money
  21. pattern = re.compile(r"(\d+(\.\d+)?)")
  22. matchers = pattern.findall(money)
  23. if matchers:
  24. for matcher in matchers:
  25. money = money.replace(
  26. matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext()
  27. )
  28. self.chntext = money
  29. return self.chntext
  30. if __name__ == "__main__":
  31. # 测试
  32. print(Money(money="21.5万元").money2chntext())
  33. print(Money(money="230块5毛").money2chntext())