| 33 | |
| 34 | |
| 35 | def convert(jsonf, dic, refs, hyps, num_spkrs=1): |
| 36 | n_ref = len(refs) |
| 37 | n_hyp = len(hyps) |
| 38 | assert n_ref == n_hyp |
| 39 | assert n_ref == num_spkrs |
| 40 | |
| 41 | # logging info |
| 42 | logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s" |
| 43 | logging.basicConfig(level=logging.INFO, format=logfmt) |
| 44 | logging.info(get_commandline_args()) |
| 45 | |
| 46 | logging.info("reading %s", jsonf) |
| 47 | with codecs.open(jsonf, "r", encoding="utf-8") as f: |
| 48 | j = json.load(f) |
| 49 | |
| 50 | logging.info("reading %s", dic) |
| 51 | with codecs.open(dic, "r", encoding="utf-8") as f: |
| 52 | dictionary = f.readlines() |
| 53 | char_list = [entry.split(" ")[0] for entry in dictionary] |
| 54 | char_list.insert(0, "<blank>") |
| 55 | char_list.append("<eos>") |
| 56 | |
| 57 | for ns in range(num_spkrs): |
| 58 | hyp_file = codecs.open(hyps[ns], "w", encoding="utf-8") |
| 59 | ref_file = codecs.open(refs[ns], "w", encoding="utf-8") |
| 60 | |
| 61 | for x in j["utts"]: |
| 62 | # recognition hypothesis |
| 63 | if num_spkrs == 1: |
| 64 | seq = [ |
| 65 | char_list[int(i)] |
| 66 | for i in j["utts"][x]["output"][0]["rec_tokenid"].split() |
| 67 | ] |
| 68 | else: |
| 69 | seq = [ |
| 70 | char_list[int(i)] |
| 71 | for i in j["utts"][x]["output"][ns][0]["rec_tokenid"].split() |
| 72 | ] |
| 73 | # In the recognition hypothesis, |
| 74 | # the <eos> symbol is usually attached in the last part of the sentence |
| 75 | # and it is removed below. |
| 76 | hyp_file.write(" ".join(seq).replace("<eos>", "")), |
| 77 | hyp_file.write( |
| 78 | " (" + j["utts"][x]["utt2spk"].replace("-", "_") + "-" + x + ")\n" |
| 79 | ) |
| 80 | |
| 81 | # reference |
| 82 | if num_spkrs == 1: |
| 83 | seq = j["utts"][x]["output"][0]["token"] |
| 84 | else: |
| 85 | seq = j["utts"][x]["output"][ns][0]["token"] |
| 86 | # Unlike the recognition hypothesis, |
| 87 | # the reference is directly generated from a token without dictionary |
| 88 | # to avoid to include <unk> symbols in the reference to make scoring normal. |
| 89 | # The detailed discussion can be found at |
| 90 | # https://github.com/espnet/espnet/issues/993 |
| 91 | ref_file.write( |
| 92 | seq + " (" + j["utts"][x]["utt2spk"].replace("-", "_") + "-" + x + ")\n" |