post_decorators.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from decorator import decorator
  2. import re
  3. @decorator
  4. def rule1_decorator(f, *args, **kwargs):
  5. '''
  6. predict_line = ['项目 ', '', '每100克营养素参考值%', '']
  7. '''
  8. predict_line = args[1]
  9. predict_line = f(*args, **kwargs)
  10. idx = 0
  11. if '' in predict_line:
  12. idx = predict_line.index('')
  13. try:
  14. if idx == 1:
  15. if '项目' in predict_line[0] and '每100克' in predict_line[2]:
  16. predict_line[1] = '每100克'
  17. r = re.split('每100克', predict_line[2])
  18. if len(r) == 2 and r[1]:
  19. predict_line[2] = r[1]
  20. except IndexError as e:
  21. print('rule1_decorator', e)
  22. return predict_line
  23. @decorator
  24. def rule2_decorator(f, *args, **kwargs):
  25. '''
  26. predict_line = ['碳水化合物18.2克', '', '6%', '']
  27. '''
  28. predict_line = args[1]
  29. predict_line = f(*args, **kwargs)
  30. idx = predict_line.index('')
  31. try:
  32. if idx == 1:
  33. if '化合物' in predict_line[0]:
  34. r = re.split('化合物', predict_line[0])
  35. predict_line[0] = '碳水化合物'
  36. if len(r) == 2 and r[1]:
  37. predict_line[1] = r[1]
  38. except IndexError as e:
  39. print('rule2_decorator', e)
  40. return predict_line
  41. @decorator
  42. def rule3_decorator(f, *args, **kwargs):
  43. '''
  44. ['患直质', '1.6克', '3%', '']
  45. ['脂扇', '1.1', '19%', '']
  46. ['碳水化合物', '勿18.2克', '6%', '']
  47. ['能量.', '408千焦', '5%']
  48. ['——精', '2.9克']
  49. '''
  50. predict_line = args[1]
  51. predict_line = f(*args, **kwargs)
  52. predict_line = [re.sub('患直质', '蛋白质', s) for s in predict_line]
  53. predict_line = [re.sub('脂扇', '脂肪', s) for s in predict_line]
  54. predict_line = [re.sub('勿(.*克)', '\\1', s) for s in predict_line]
  55. predict_line = [re.sub('毫 克', '毫克', s) for s in predict_line]
  56. predict_line = [re.sub('——精', '——糖', s) for s in predict_line]
  57. return predict_line
  58. @decorator
  59. def rule4_decorator(f, *args, **kwargs):
  60. '''
  61. ['', '项目每一百克', '营养素参考值']
  62. '''
  63. predict_line = args[1]
  64. predict_line = f(*args, **kwargs)
  65. try:
  66. if '项目' in predict_line[1] and predict_line[0]=='':
  67. predict_line[0] = '项目'
  68. predict_line[1] = predict_line[1].replace('项目', '')
  69. except IndexError as e:
  70. print('rule4_decorator', e)
  71. return predict_line
  72. @decorator
  73. def rule5_decorator(f, *args, **kwargs):
  74. '''
  75. predict_line = ['项目 ', '每份(70g)营养素参考值%', '']
  76. '''
  77. predict_line = args[1]
  78. predict_line = f(*args, **kwargs)
  79. try:
  80. if '项目' in predict_line[0] and '营养素参考值' in predict_line[1] and len(predict_line[1])>7 and predict_line[2] == '':
  81. predict_line[2] = '营养素参考值'
  82. if len(predict_line[1].split('营养素参考值'))>1:
  83. predict_line[2] = predict_line[2]+predict_line[1].split('营养素参考值')[1]
  84. predict_line[1] = predict_line[1].split('营养素参考值')[0]
  85. except IndexError as e:
  86. print('rule5_decorator', e)
  87. return predict_line
  88. @decorator
  89. def rule6_decorator(f, *args, **kwargs):
  90. '''
  91. predict_line = ['项目 ', '', '每份(70g)营养素参考值%', '']
  92. '''
  93. predict_line = args[1]
  94. predict_line = f(*args, **kwargs)
  95. idx = 0
  96. if '' in predict_line:
  97. idx = predict_line.index('')
  98. try:
  99. if idx == 1:
  100. if '项目' in predict_line[0] and '每份' in predict_line[2] and '营养素参考值' in predict_line[2]:
  101. predict_line[1] = predict_line[2].split('营养素参考值')[0]
  102. r = predict_line[2].split('营养素参考值')
  103. if len(r) == 2:
  104. predict_line[2] = '营养素参考值'+r[1]
  105. except IndexError as e:
  106. print('rule6_decorator', e)
  107. return predict_line
  108. decorators = []
  109. def register_decorator(dtor):
  110. decorators.append(dtor)
  111. def combined_decorator(func):
  112. for dtor in reversed(decorators):
  113. func = dtor(func)
  114. return func
  115. register_decorator(rule1_decorator)
  116. register_decorator(rule2_decorator)
  117. register_decorator(rule3_decorator)
  118. register_decorator(rule4_decorator)
  119. register_decorator(rule5_decorator)
  120. register_decorator(rule6_decorator)