fraction.py 927 B

1234567891011121314151617181920212223242526272829303132333435
  1. # -*- coding: utf-8 -*-
  2. """FRACTION类
  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 Fraction:
  10. """
  11. FRACTION类
  12. """
  13. def __init__(self, fraction=None, chntext=None):
  14. self.fraction = fraction
  15. self.chntext = chntext
  16. def chntext2fraction(self):
  17. denominator, numerator = self.chntext.split("分之")
  18. return chn2num(numerator) + "/" + chn2num(denominator)
  19. def fraction2chntext(self):
  20. numerator, denominator = self.fraction.split("/")
  21. return num2chn(denominator) + "分之" + num2chn(numerator)
  22. if __name__ == "__main__":
  23. # 测试程序
  24. print(Fraction(fraction="2135/7230").fraction2chntext())
  25. print(Fraction(chntext="五百八十一分之三百六十九").chntext2fraction())