parser.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import re
  2. class parser:
  3. def __init__(self):
  4. print("Address of self = ",id(self))
  5. def check_image_count(self,image_list, text):
  6. pair_obj = findTag(text)
  7. if pair_obj['code'] == 1:
  8. if len(image_list)!=len(pair_obj['pair']):
  9. return {'msg':'圖片與標籤數量不一致!','code':-1}
  10. else:
  11. return {code:1}
  12. else:
  13. return pair_obj
  14. def findTag(self,text):
  15. left_tag = [m.start() for m in re.finditer('{', text)]
  16. right_tag = [m.start() for m in re.finditer('}', text)]
  17. if len(left_tag)!=len(right_tag):
  18. return {'msg':'圖片標籤錯誤,左右數量不符','code':-1}
  19. pair = []
  20. for idx in range(len(left_tag)):
  21. pair.append({'left':left_tag[idx],'right':right_tag[idx]})
  22. obj = {'code':1,'pair':pair}
  23. return obj
  24. def replace_list(self,text):
  25. rep_ls = []
  26. pair_obj = self.findTag(text)
  27. for p in pair_obj['pair']:
  28. rep_ls.append(text[p['left']:p['right']+1])
  29. return rep_ls
  30. def image_clip_info(self,dict_in):
  31. #if 'image_idx' in dic:
  32. # new_dic['image_obj'] = {'start':dic['start'],'idx':dic['image_idx']}
  33. stopPoint = 0 # sec
  34. time_info = []
  35. img_idx = 1 #start from 1
  36. added_idx = []
  37. for dic in dict_in:
  38. if 'image_obj' in dic :
  39. if dic['image_obj']['idx'] not in added_idx:
  40. added_idx.append(dic['image_obj']['idx'])
  41. time_info.append({'index':img_idx,'start':dic['start']})
  42. img_idx += 1
  43. if dic['start']+dic['duration'] > stopPoint:
  44. stopPoint = dic['start']+dic['duration']
  45. for idx in range(len(time_info)-1):
  46. time_info[idx]['duration'] = time_info[idx+1]['start']-time_info[idx]['start']
  47. time_info[-1]['duration'] = stopPoint - time_info[-1]['start']
  48. #index start duration
  49. return time_info