parser.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. for dic in dict_in:
  37. if 'image_obj' in dic:
  38. time_info.append({'index':img_idx,'start':dic['start']})
  39. img_idx += 1
  40. stopPoint = dic['start']+dic['duration']
  41. for idx in range(len(time_info)-1):
  42. time_info[idx]['duration'] = time_info[idx+1]['start']-time_info[idx]['start']
  43. time_info[:-1][0]['duration'] = stopPoint
  44. #index start duration
  45. return time_info