postprocess.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import re
  2. import glob
  3. import time
  4. import tqdm
  5. from datetime import datetime
  6. from shutil import copyfile
  7. import os
  8. a1 = "머리를 (()) 그담에 뭐 하트도 날리고 뽀뽀도 해주고 윙크도 해주고 토닥토닥 안아주고 이 두 가지를 적절하게 잘 사용하면 굉장히 인제 조금 칭찬 방법이 되는 거죠."
  9. a2 = "니가 이반에서 제일 머리가 좋아 그런 거는 쪼끔 안 좋습니다 왜냐면 인제 자기보다 예쁜 아이가 있는 다른 반에 가지 않을려고 {laughing} 다른 친구보다 다른 형제자매들 보다 훨씬 낫다는 식의 비교 칭찬은 가급적 하지 않는 것이 좋습니다."
  10. a3 = "그니까 아까 왜 너 얼마나 잘할 거 같애 칠십 퍼센트요 팔십 퍼센트요 그니까 (아이 캔 두잇)(i can do it 나 할 수 있을 거 같애요 라고 하는 건데요."
  11. a4 = f" "
  12. a = [a1, a2, a3, a4]
  13. pattern_1 = r'\(\(\)\)' ## (())
  14. pattern_2 = r'\{(\w*)\}' ## {@@@}
  15. pattern_3 = r'\((\w*)\)' ## (@@@) 찾기
  16. pattern_4 = r'[\(\)\{\}]' ## (, ), {, } 찾기
  17. pattern_4_1 = r'&([\w|-]*)&' ## &(id-id123)& 찾기
  18. pattern_5 = r"[^\uAC00-\uD7A30-9a-zA-Z\s]" ## 특문 repl=' '
  19. pattern_6 = r" {2,}" ## 빈 문자열 2개 이상
  20. def postprocess(origin):
  21. _a = re.sub(pattern=pattern_1, repl='', string = origin)
  22. _a = re.sub(pattern=pattern_2, repl='', string = _a)
  23. _a = re.sub(pattern=pattern_3, repl='', string = _a)
  24. tmp = re.search(pattern=pattern_4, string = _a)
  25. if tmp:
  26. return None
  27. tmp = re.search(pattern=pattern_4_1, string = _a)
  28. if tmp:
  29. return None
  30. _a = re.sub(pattern=pattern_5, repl = " ", string = _a)
  31. _a = re.sub(pattern=pattern_6, repl = " ", string = _a)
  32. _a = _a.strip()
  33. if len(_a) == 0:
  34. return None
  35. else:
  36. return _a
  37. # test = '{laughing} 기억하고 있어야지 후생에도 혹시 {laughing} (()) (()) (광)'
  38. # test = '저희 음악 한 곡 듣고 와서 조금 더 마지막 사연 이야기 나눠볼게요.여러분의 의견도 많이 보내주시길 바래요.&company-name225& &tel-num1& 오십 원 혹은 백 원의 유료문자'
  39. # test = '저희 음악 한 곡 듣고 와서 조금 더 마지막 사연 이야기 나눠볼게요.여러분의 의견도 많이 보내주시길 바래요.'
  40. # print(list(map(postprocess, a)))
  41. # print(re.sub(pattern=pattern_1, repl='', string = test))
  42. # exit()
  43. root_dir = "/root/sata/broadcast_split/data/"
  44. total_txt = "/root/mnt/data_aug/audio_augmentation/script/total.txt"
  45. # with open(total_txt, 'w+') as tf:
  46. # for idx, content in enumerate(a):
  47. # content = content.strip()
  48. # post = postprocess(content)
  49. # if post is not None:
  50. # print(content + "\n --> " + post)
  51. # tf.write(post + "\n")
  52. # exit()
  53. ## 1 = broadcast_split
  54. ## 2 = broadcast/split
  55. ## 1에 있는 ogg를 기준으로, txt2를 가져오고, 이를 후처리 해서 txt1에 적용함.
  56. def broadcast_move_split2_split1(time_str):
  57. total_txt_file = '/root/mnt/data_aug/audio_augmentation/script/txt/broadcast_total_2.txt'
  58. ## for test
  59. # total_txt_file = '/root/mnt/data_aug/audio_augmentation/script/txt/broadcast_total_test.txt'
  60. ogg_path = '/root/sata/etc/broadcast_split/data'
  61. ## for text
  62. # ogg_path = '/root/mnt/data_aug/audio_augmentation/sample/broadcast_split'
  63. post_none_list = []
  64. not_exist_list = []
  65. ogg_list = []
  66. print('glob start !!')
  67. if os.path.isfile(total_txt_file):
  68. with open(total_txt_file, 'r') as f:
  69. for ogg in f:
  70. tt = ogg.strip()
  71. ogg_list.append(tt)
  72. else:
  73. ogg_list = glob.glob(os.path.join(ogg_path, '**/*.ogg'), recursive=True)
  74. with open(total_txt_file, 'w+') as f:
  75. for ogg in ogg_list:
  76. f.write(ogg + '\n')
  77. print('glob end !!', len(ogg_list))
  78. # return
  79. for source_ogg in tqdm.tqdm(ogg_list):
  80. source_ogg = source_ogg.strip()
  81. split1_txt_file = source_ogg.strip().replace("ogg", "txt")
  82. split2_txt_file = source_ogg.strip().replace("ogg", "txt").replace("broadcast_split", "broadcast/split")
  83. if not os.path.isfile(split2_txt_file):
  84. not_exist_list.append([source_ogg, split2_txt_file])
  85. else:
  86. with open(split2_txt_file, 'r') as f:
  87. content = f.read().strip()
  88. post = postprocess(content)
  89. with open(split1_txt_file, 'w') as f:
  90. if post is None:
  91. post_none_list.append([split2_txt_file, content])
  92. if os.path.isfile(split1_txt_file):
  93. os.remove(split1_txt_file)
  94. tmp_ogg = split1_txt_file.replace('txt', 'ogg')
  95. if os.path.isfile(tmp_ogg):
  96. os.remove(tmp_ogg)
  97. else:
  98. f.write(post + '\n')
  99. if post_none_list:
  100. print('postprocess none = ', len(post_none_list))
  101. txt = f'log/post_none_{time_str}.txt'
  102. with open(txt, 'w+') as f:
  103. for item in post_none_list:
  104. f.write("\t".join(item) + "\n")
  105. if not_exist_list:
  106. print('not exist = ', len(not_exist_list))
  107. txt = f'log/not_exist_{time_str}.txt'
  108. for item in post_none_list:
  109. with open(txt, 'w+') as f:
  110. f.write("\t".join(item) + "\n")
  111. if __name__ == '__main__':
  112. now = datetime.now()
  113. time_str = now.astimezone().strftime('%Y_%m_%d_%H_%M_%S')
  114. # print(time_str)
  115. # # exit()
  116. broadcast_move_split2_split1(time_str)
  117. # failure = []
  118. # failure_txt = f"broadcast_failure_{time_str}.txt"
  119. # with open(total_txt, 'w+') as tf:
  120. # for file in tqdm.tqdm(glob.glob(f'{root_dir}**/*.txt', recursive=True)):
  121. # content = ''
  122. # with open(file, 'r') as f:
  123. # content = f.read().strip()
  124. # post = postprocess(content)
  125. # # print(str(idx) + " : " + content + "\n --> " + post)
  126. # with open(file, 'w') as f:
  127. # if post is not None:
  128. # # tf.write(file + "\n")
  129. # # f.write(post)
  130. # pass
  131. # else:
  132. # failure.append([file, content, post])
  133. # # print(failure)
  134. # print(len(failure))
  135. # with open(failure_txt, 'w+') as ft:
  136. # for fail in tqdm.tqdm(failure):
  137. # if fail[2] is None: fail[2] = 'None'
  138. # ft.write("\t".join(fail) + "\n")
  139. # else:
  140. # print(file)
  141. # cnt += 1