This commit is contained in:
游雁 2024-02-29 16:07:42 +08:00
parent 1ef8117213
commit ad99b15120

View File

@ -2,6 +2,8 @@ import os
import numpy as np
import sys
import hydra
from omegaconf import DictConfig, OmegaConf, ListConfig
def compute_wer(ref_file,
hyp_file,
@ -28,18 +30,20 @@ def compute_wer(ref_file,
key = line.strip().split()[0]
value = line.strip().split()[1:]
if cn_postprocess:
value = value.replace(" ", "")
value = [x for x in value]
value = " ".join(value)
value = value.replace(" ", "")
if value[0] == "":
value = value[1:]
value = [x for x in value]
hyp_dict[key] = value
with open(ref_file, 'r') as ref_reader:
for line in ref_reader:
key = line.strip().split()[0]
value = line.strip().split()[1:]
if cn_postprocess:
value = " ".join(value)
value = value.replace(" ", "")
value = [x for x in value]
value = " ".join(value)
ref_dict[key] = value
cer_detail_writer = open(cer_file, 'w')
@ -66,13 +70,17 @@ def compute_wer(ref_file,
rst['S.Err'] = round(rst['wrong_sentences'] * 100 / rst['Snt'], 2)
cer_detail_writer.write('\n')
cer_detail_writer.write("%WER " + str(rst['Err']) + " [ " + str(rst['wrong_words'])+ " / " + str(rst['Wrd']) +
", " + str(rst['Ins']) + " ins, " + str(rst['Del']) + " del, " + str(rst['Sub']) + " sub ]" + '\n')
cer_detail_writer.write("%SER " + str(rst['S.Err']) + " [ " + str(rst['wrong_sentences']) + " / " + str(rst['Snt']) + " ]" + '\n')
cer_detail_writer.write("Scored " + str(len(hyp_dict)) + " sentences, " + str(len(hyp_dict) - rst['Snt']) + " not present in hyp." + '\n')
cer_detail_writer.write("%WER " + str(rst['Err']) + " [ " + str(rst['wrong_words']) + " / " + str(rst['Wrd']) +
", " + str(rst['Ins']) + " ins, " + str(rst['Del']) + " del, " + str(
rst['Sub']) + " sub ]" + '\n')
cer_detail_writer.write(
"%SER " + str(rst['S.Err']) + " [ " + str(rst['wrong_sentences']) + " / " + str(rst['Snt']) + " ]" + '\n')
cer_detail_writer.write("Scored " + str(len(hyp_dict)) + " sentences, " + str(
len(hyp_dict) - rst['Snt']) + " not present in hyp." + '\n')
cer_detail_writer.close()
def compute_wer_by_line(hyp,
ref):
hyp = list(map(lambda x: x.lower(), hyp))
@ -153,11 +161,12 @@ def compute_wer_by_line(hyp,
return rst
def print_cer_detail(rst):
return ("(" + "nwords=" + str(rst['nwords']) + ",cor=" + str(rst['cor'])
+ ",ins=" + str(rst['ins']) + ",del=" + str(rst['del']) + ",sub="
+ str(rst['sub']) + ") corr:" + '{:.2%}'.format(rst['cor']/rst['nwords'])
+ ",cer:" + '{:.2%}'.format(rst['wrong']/rst['nwords']))
+ str(rst['sub']) + ") corr:" + '{:.2%}'.format(rst['cor'] / rst['nwords'])
+ ",cer:" + '{:.2%}'.format(rst['wrong'] / rst['nwords']))
@hydra.main(config_name=None, version_base=None)
@ -167,11 +176,13 @@ def main_hydra(cfg: DictConfig):
cer_file = cfg.get("cer_file", None)
cn_postprocess = cfg.get("cn_postprocess", False)
if ref_file is None or hyp_file is None or cer_file is None:
print("usage : python -m funasr.metrics.wer ++ref_file=test.ref ++hyp_file=test.hyp ++cer_file=test.wer ++cn_postprocess=false")
print(
"usage : python -m funasr.metrics.wer ++ref_file=test.ref ++hyp_file=test.hyp ++cer_file=test.wer ++cn_postprocess=false")
sys.exit(0)
compute_wer(ref_file, hyp_file, cer_file, cn_postprocess)
if __name__ == '__main__':
main_hydra()