Compute wer. Args: ref_file: TODO. hyp_file: TODO. cer_file: TODO. cn_postprocess: TODO.
(
ref_file,
hyp_file,
cer_file,
cn_postprocess=False,
)
| 6 | |
| 7 | |
| 8 | def compute_wer( |
| 9 | ref_file, |
| 10 | hyp_file, |
| 11 | cer_file, |
| 12 | cn_postprocess=False, |
| 13 | ): |
| 14 | """Compute wer. |
| 15 | |
| 16 | Args: |
| 17 | ref_file: TODO. |
| 18 | hyp_file: TODO. |
| 19 | cer_file: TODO. |
| 20 | cn_postprocess: TODO. |
| 21 | """ |
| 22 | rst = { |
| 23 | "Wrd": 0, |
| 24 | "Corr": 0, |
| 25 | "Ins": 0, |
| 26 | "Del": 0, |
| 27 | "Sub": 0, |
| 28 | "Snt": 0, |
| 29 | "Err": 0.0, |
| 30 | "S.Err": 0.0, |
| 31 | "wrong_words": 0, |
| 32 | "wrong_sentences": 0, |
| 33 | } |
| 34 | |
| 35 | hyp_dict = {} |
| 36 | ref_dict = {} |
| 37 | with open(hyp_file, "r") as hyp_reader: |
| 38 | for line in hyp_reader: |
| 39 | key = line.strip().split()[0] |
| 40 | value = line.strip().split()[1:] |
| 41 | if cn_postprocess: |
| 42 | value = " ".join(value) |
| 43 | value = value.replace(" ", "") |
| 44 | # if value[0] == "请": |
| 45 | # value = value[1:] |
| 46 | value = [x for x in value] |
| 47 | hyp_dict[key] = value |
| 48 | with open(ref_file, "r") as ref_reader: |
| 49 | for line in ref_reader: |
| 50 | key = line.strip().split()[0] |
| 51 | value = line.strip().split()[1:] |
| 52 | if cn_postprocess: |
| 53 | value = " ".join(value) |
| 54 | value = value.replace(" ", "") |
| 55 | value = [x for x in value] |
| 56 | ref_dict[key] = value |
| 57 | |
| 58 | cer_detail_writer = open(cer_file, "w") |
| 59 | for hyp_key in hyp_dict: |
| 60 | if hyp_key in ref_dict: |
| 61 | out_item = compute_wer_by_line(hyp_dict[hyp_key], ref_dict[hyp_key]) |
| 62 | rst["Wrd"] += out_item["nwords"] |
| 63 | rst["Corr"] += out_item["cor"] |
| 64 | rst["wrong_words"] += out_item["wrong"] |
| 65 | rst["Ins"] += out_item["ins"] |
no test coverage detected
searching dependent graphs…