date.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. """DATE类
  3. 日期 <=> 中文字符串 方法
  4. 中文字符串 <=> 日期 方法
  5. """
  6. __author__ = "Zhiyang Zhou <zyzhou@stu.xmu.edu.cn>"
  7. __data__ = "2019-05-07"
  8. from fish_speech.text.chn_text_norm.cardinal import Cardinal
  9. from fish_speech.text.chn_text_norm.digit import Digit
  10. class Date:
  11. """
  12. DATE类
  13. """
  14. def __init__(self, date=None, chntext=None):
  15. self.date = date
  16. self.chntext = chntext
  17. # def chntext2date(self):
  18. # chntext = self.chntext
  19. # try:
  20. # year, other = chntext.strip().split('年', maxsplit=1)
  21. # year = Digit(chntext=year).digit2chntext() + '年'
  22. # except ValueError:
  23. # other = chntext
  24. # year = ''
  25. # if other:
  26. # try:
  27. # month, day = other.strip().split('月', maxsplit=1)
  28. # month = Cardinal(chntext=month).chntext2cardinal() + '月'
  29. # except ValueError:
  30. # day = chntext
  31. # month = ''
  32. # if day:
  33. # day = Cardinal(chntext=day[:-1]).chntext2cardinal() + day[-1]
  34. # else:
  35. # month = ''
  36. # day = ''
  37. # date = year + month + day
  38. # self.date = date
  39. # return self.date
  40. def date2chntext(self):
  41. date = self.date
  42. try:
  43. year, other = date.strip().split("年", maxsplit=1)
  44. year = Digit(digit=year).digit2chntext() + "年"
  45. except ValueError:
  46. other = date
  47. year = ""
  48. if other:
  49. try:
  50. month, day = other.strip().split("月", maxsplit=1)
  51. month = Cardinal(cardinal=month).cardinal2chntext() + "月"
  52. except ValueError:
  53. day = date
  54. month = ""
  55. if day:
  56. day = Cardinal(cardinal=day[:-1]).cardinal2chntext() + day[-1]
  57. else:
  58. month = ""
  59. day = ""
  60. chntext = year + month + day
  61. self.chntext = chntext
  62. return self.chntext
  63. if __name__ == "__main__":
  64. # 测试
  65. print(Date(date="09年3月16日").date2chntext())