123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import jiwer
- import glob
- import pandas as pd
- import tqdm
- import os
- import re
- from collections import defaultdict
- import matplotlib.pyplot as plt
- root_path = ""
- sub_path = [
- "", ""
- ]
- def readNumber(n):
- units = [''] + list('십백천만') + ['십만', '백만', '천만', '억']
- # units = [''] + list('십백천만')
- nums = '일이삼사오육칠팔구'
- result = []
- i = 0
- while n > 0:
- n, r = divmod(n, 10)
- if r > 0:
- result.append(nums[r-1] + units[i])
- i += 1
- return ''.join(result[::-1])
- def convert(str):
- iter = re.finditer(r'\d+', str)
- L = sum(1 for _ in re.finditer(r'\d+', str))
- res = ''
- prev = 0
- if L == 0:
- return str
- for idx, match in enumerate(iter):
- s, e = match.span()
- if prev < s:
- res += str[prev:s]
-
- a = str[s : e]
- a = readNumber(int(a))
- res += a
- prev = e
- if idx == L - 1:
- res += str[e:]
- return res
- def create_total(total_name):
- for sub in sub_path:
- srt_list = glob.glob(root_path + sub + "*.srt")
- with open(root_path + f"total/{total_name}", 'a') as tt:
- for srt in srt_list:
- base_name = srt.rsplit('_', 2)[0]
- folder_name, file_name = base_name.rsplit('/', 1)
- with open(srt, 'r') as f:
- split_text = ' '
- for idx, org_text in enumerate(f):
- if idx%4 == 2:
- str_num = str(idx//4)
- str_num = '_' + ('0' * (3 - len(str_num))) + str_num
- split_file = folder_name + "/split/" + file_name + str_num + ".txt"
- if not os.path.exists(split_file):
- split_text = ''
- else:
- with open(split_file, 'r') as f:
- split_text = f.read()
-
-
- org_text = re.sub(r"[^\uAC00-\uD7A30-9a-zA-Z\s]", "", org_text.strip())
- split_text = re.sub(r"[^\uAC00-\uD7A30-9a-zA-Z\s]", "", split_text.strip())
- # org_text = re.sub(r"[^\uAC00-\uD7A30-9a-zA-Z]", "", org_text.strip())
- # split_text = re.sub(r"[^\uAC00-\uD7A30-9a-zA-Z]", "", split_text.strip())
- if len(org_text) == 0 or len(split_text) == 0:
- cer, wer, c_cer, c_wer = [ 1 for _ in range(4) ]
- else:
- cer = jiwer.cer(org_text, split_text)
- wer = jiwer.wer(org_text, split_text)
- try:
- c_org = convert(org_text)
- c_split = convert(split_text)
- c_cer = jiwer.cer(c_org, c_split)
- c_wer = jiwer.wer(c_org, c_split)
- except:
- c_cer = cer
- c_wer = wer
-
- tt.write(f"{file_name + str_num}\t{org_text}\t{split_text}\t{cer}\t{wer}\t{c_cer}\t{c_wer}\n")
- def create_dataframe(total_name):
- with open(root_path + f"total/{total_name}", 'r') as f:
- data = f.readlines()
-
- columns = ['path', 'script1', 'script2', 'cer', 'wer', 'c_cer', 'c_wer']
- data = [ d.strip().split('\t') for d in data ]
- df = pd.DataFrame(data, columns=columns)
- threshold = '0.2'
- print(df[(df['cer'] < threshold) | (df['wer'] < threshold) | (df['c_cer'] < threshold) | (df['c_wer'] < threshold) ][['path', 'cer', 'wer', 'c_cer', 'c_wer']])
- print('-'*50)
- print(df[(df['cer'] < threshold) | (df['wer'] < threshold) | (df['c_cer'] < threshold) | (df['c_wer'] < threshold) ][['path', 'script1', 'script2']])
- print('-'*50)
- threshold = '0.0'
- print(df[(df['cer'] <= threshold) | (df['wer'] <= threshold) | (df['c_cer'] <= threshold) | (df['c_wer'] <= threshold) ][['path', 'script1', 'script2']])
- print('-'*50)
-
- def create_plt_file(d, name):
- plt.title(name)
- plt.xlim([0, 3])
- plt.bar(d.keys(), d.values(), 0.01, color='g')
- plt.savefig(f'plot/{name}.png')
- def create_hist():
- hist_cer = defaultdict(int)
- hist_wer = defaultdict(int)
- hist_c_cer = defaultdict(int)
- hist_c_wer = defaultdict(int)
- error_cnt = 0
- for sub in sub_path:
- srt_list = glob.glob(root_path + sub + "*.srt")
- for srt in srt_list:
- base_name = srt.rsplit('_', 2)[0]
- folder_name, file_name = base_name.rsplit('/', 1)
- with open(srt, 'r') as f:
- split_text = ' '
- for idx, org_text in enumerate(f):
- cer, wer, c_cer, c_wer = [ 0 for _ in range(4) ]
- if idx%4 == 2:
- str_num = str(idx//4)
- str_num = '_' + ('0' * (3 - len(str_num))) + str_num
- split_file = folder_name + "/split/" + file_name + str_num + ".txt"
- if not os.path.exists(split_file):
- split_text = ''
- else:
- with open(split_file, 'r') as f:
- split_text = f.read()
-
- org_text = org_text.strip()
- split_text = split_text.strip()
- if len(org_text) == 0 or len(split_text) == 0:
- e = 1.0
- hist_cer[e] += 1
- hist_wer[e] += 1
- hist_c_cer[e] += 1
- hist_c_wer[e] += 1
- else:
- cer = round(jiwer.cer(org_text, split_text), 4)
- wer = round(jiwer.wer(org_text, split_text), 4)
- hist_cer[cer] += 1
- hist_wer[wer] += 1
- try:
- c_org = convert(org_text)
- c_split = convert(split_text)
- c_cer = round(jiwer.cer(c_org, c_split), 4)
- c_wer = round(jiwer.wer(c_org, c_split), 4)
- hist_c_cer[c_cer] += 1
- hist_c_wer[c_wer] += 1
- except:
- error_cnt += 1
- pass
-
- # create_plt_file(hist_cer, 'cer')
- # create_plt_file(hist_wer, 'wer')
- # create_plt_file(hist_c_cer, 'convert cer')
- # create_plt_file(hist_c_wer, 'convert wer')
- # print(error_cnt)
- width = 0.005
- xlim = [0, 1.1]
- # plt.subplot(2, 2, 1)
- # plt.title('cer')
- # plt.xlim(xlim)
- # plt.bar(hist_cer.keys(), hist_cer.values(), width, color='g')
- # plt.subplot(2, 2, 2)
- # plt.title('wer')
- # plt.xlim(xlim)
- # plt.bar(hist_wer.keys(), hist_wer.values(), width, color='g')
- # plt.subplot(2, 2, 3)
- # plt.title('convert cer')
- # plt.xlim(xlim)
- # plt.bar(hist_c_cer.keys(), hist_c_cer.values(), width, color='g')
- # plt.subplot(2, 2, 4)
- # plt.title('convert wer')
- # plt.xlim(xlim)
- # plt.bar(hist_c_wer.keys(), hist_c_wer.values(), width, color='g')
- # plt.tight_layout()
- # plt.savefig(f'plot/total.png', dpi=200)
-
- if __name__ == '__main__':
- total_name = "total_without_space.txt"
- # create_hist()
- create_total(total_name)
- create_dataframe(total_name)
|