mirror of
https://github.com/modelscope/FunASR
synced 2025-09-15 14:48:36 +08:00
cer
This commit is contained in:
parent
1ef8117213
commit
ad99b15120
@ -2,6 +2,8 @@ import os
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import sys
|
import sys
|
||||||
import hydra
|
import hydra
|
||||||
|
from omegaconf import DictConfig, OmegaConf, ListConfig
|
||||||
|
|
||||||
|
|
||||||
def compute_wer(ref_file,
|
def compute_wer(ref_file,
|
||||||
hyp_file,
|
hyp_file,
|
||||||
@ -28,18 +30,20 @@ def compute_wer(ref_file,
|
|||||||
key = line.strip().split()[0]
|
key = line.strip().split()[0]
|
||||||
value = line.strip().split()[1:]
|
value = line.strip().split()[1:]
|
||||||
if cn_postprocess:
|
if cn_postprocess:
|
||||||
value = value.replace(" ", "")
|
|
||||||
value = [x for x in value]
|
|
||||||
value = " ".join(value)
|
value = " ".join(value)
|
||||||
|
value = value.replace(" ", "")
|
||||||
|
if value[0] == "请":
|
||||||
|
value = value[1:]
|
||||||
|
value = [x for x in value]
|
||||||
hyp_dict[key] = value
|
hyp_dict[key] = value
|
||||||
with open(ref_file, 'r') as ref_reader:
|
with open(ref_file, 'r') as ref_reader:
|
||||||
for line in ref_reader:
|
for line in ref_reader:
|
||||||
key = line.strip().split()[0]
|
key = line.strip().split()[0]
|
||||||
value = line.strip().split()[1:]
|
value = line.strip().split()[1:]
|
||||||
if cn_postprocess:
|
if cn_postprocess:
|
||||||
|
value = " ".join(value)
|
||||||
value = value.replace(" ", "")
|
value = value.replace(" ", "")
|
||||||
value = [x for x in value]
|
value = [x for x in value]
|
||||||
value = " ".join(value)
|
|
||||||
ref_dict[key] = value
|
ref_dict[key] = value
|
||||||
|
|
||||||
cer_detail_writer = open(cer_file, 'w')
|
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)
|
rst['S.Err'] = round(rst['wrong_sentences'] * 100 / rst['Snt'], 2)
|
||||||
|
|
||||||
cer_detail_writer.write('\n')
|
cer_detail_writer.write('\n')
|
||||||
cer_detail_writer.write("%WER " + str(rst['Err']) + " [ " + str(rst['wrong_words'])+ " / " + str(rst['Wrd']) +
|
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')
|
", " + str(rst['Ins']) + " ins, " + str(rst['Del']) + " del, " + str(
|
||||||
cer_detail_writer.write("%SER " + str(rst['S.Err']) + " [ " + str(rst['wrong_sentences']) + " / " + str(rst['Snt']) + " ]" + '\n')
|
rst['Sub']) + " sub ]" + '\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(
|
||||||
|
"%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()
|
cer_detail_writer.close()
|
||||||
|
|
||||||
|
|
||||||
def compute_wer_by_line(hyp,
|
def compute_wer_by_line(hyp,
|
||||||
ref):
|
ref):
|
||||||
hyp = list(map(lambda x: x.lower(), hyp))
|
hyp = list(map(lambda x: x.lower(), hyp))
|
||||||
@ -153,11 +161,12 @@ def compute_wer_by_line(hyp,
|
|||||||
|
|
||||||
return rst
|
return rst
|
||||||
|
|
||||||
|
|
||||||
def print_cer_detail(rst):
|
def print_cer_detail(rst):
|
||||||
return ("(" + "nwords=" + str(rst['nwords']) + ",cor=" + str(rst['cor'])
|
return ("(" + "nwords=" + str(rst['nwords']) + ",cor=" + str(rst['cor'])
|
||||||
+ ",ins=" + str(rst['ins']) + ",del=" + str(rst['del']) + ",sub="
|
+ ",ins=" + str(rst['ins']) + ",del=" + str(rst['del']) + ",sub="
|
||||||
+ str(rst['sub']) + ") corr:" + '{:.2%}'.format(rst['cor']/rst['nwords'])
|
+ str(rst['sub']) + ") corr:" + '{:.2%}'.format(rst['cor'] / rst['nwords'])
|
||||||
+ ",cer:" + '{:.2%}'.format(rst['wrong']/rst['nwords']))
|
+ ",cer:" + '{:.2%}'.format(rst['wrong'] / rst['nwords']))
|
||||||
|
|
||||||
|
|
||||||
@hydra.main(config_name=None, version_base=None)
|
@hydra.main(config_name=None, version_base=None)
|
||||||
@ -167,11 +176,13 @@ def main_hydra(cfg: DictConfig):
|
|||||||
cer_file = cfg.get("cer_file", None)
|
cer_file = cfg.get("cer_file", None)
|
||||||
cn_postprocess = cfg.get("cn_postprocess", False)
|
cn_postprocess = cfg.get("cn_postprocess", False)
|
||||||
if ref_file is None or hyp_file is None or cer_file is None:
|
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)
|
sys.exit(0)
|
||||||
|
|
||||||
compute_wer(ref_file, hyp_file, cer_file, cn_postprocess)
|
compute_wer(ref_file, hyp_file, cer_file, cn_postprocess)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main_hydra()
|
main_hydra()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user