post_decorators.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. '''
  48. predict_line = args[1]
  49. predict_line = f(*args, **kwargs)
  50. predict_line = [re.sub('患直质', '蛋白质', s) for s in predict_line]
  51. predict_line = [re.sub('脂扇', '脂肪', s) for s in predict_line]
  52. predict_line = [re.sub('勿(.*克)', '\\1', s) for s in predict_line]
  53. predict_line = [re.sub('毫 克', '毫克', s) for s in predict_line]
  54. return predict_line
  55. @decorator
  56. def rule4_decorator(f, *args, **kwargs):
  57. '''
  58. ['', '项目每一百克', '营养素参考值']
  59. '''
  60. predict_line = args[1]
  61. predict_line = f(*args, **kwargs)
  62. try:
  63. if '项目' in predict_line[1] and predict_line[0]=='':
  64. predict_line[0] = '项目'
  65. predict_line[1] = predict_line[1].replace('项目', '')
  66. except IndexError as e:
  67. print('rule4_decorator', e)
  68. return predict_line
  69. @decorator
  70. def rule5_decorator(f, *args, **kwargs):
  71. predict_line = args[1]
  72. predict_line = f(*args, **kwargs)
  73. try:
  74. if '项目' in predict_line[0] and '营养素参考值' in predict_line[1] and len(predict_line[1])>7 and predict_line[2] == '':
  75. predict_line[2] = '营养素参考值'
  76. if len(predict_line[1].split('营养素参考值'))>1:
  77. predict_line[2] = predict_line[2]+predict_line[1].split('营养素参考值')[1]
  78. predict_line[1] = predict_line[1].split('营养素参考值')[0]
  79. except IndexError as e:
  80. print('rule5_decorator', e)
  81. return predict_line
  82. @decorator
  83. def rule6_decorator(f, *args, **kwargs):
  84. '''
  85. predict_line = ['项目 ', '', '每份(70g)营养素参考值%', '']
  86. '''
  87. predict_line = args[1]
  88. predict_line = f(*args, **kwargs)
  89. idx = 0
  90. if '' in predict_line:
  91. idx = predict_line.index('')
  92. try:
  93. if idx == 1:
  94. if '项目' in predict_line[0] and '每份' in predict_line[2] and '营养素参考值' in predict_line[2]:
  95. predict_line[1] = predict_line[2].split('营养素参考值')[0]
  96. r = predict_line[2].split('营养素参考值')
  97. if len(r) == 2:
  98. predict_line[2] = '营养素参考值'+r[1]
  99. except IndexError as e:
  100. print('rule6_decorator', e)
  101. return predict_line