diff --git a/egs2/TEMPLATE/asr1/pyscripts/utils/rotate_logfile.py b/egs2/TEMPLATE/asr1/pyscripts/utils/rotate_logfile.py new file mode 100755 index 00000000000..e30c7a1e682 --- /dev/null +++ b/egs2/TEMPLATE/asr1/pyscripts/utils/rotate_logfile.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2022 Chaitanya Narisetty +# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0) + +"""Rotate log-file.""" + +import argparse +from pathlib import Path +import shutil + + +def rotate(path, max_num_log_files=1000): + """Rotate a log-file while retaining past `max_num_log_files` files. + Examples: + /some/path/ + ├──logfile.txt + ├──logfile.1.txt + ├──logfile.2.txt + >>> rotate('/some/path/logfile.txt') + /some/path/ + ├──logfile.1.txt + ├──logfile.2.txt + ├──logfile.3.txt + """ + for i in range(max_num_log_files - 1, -1, -1): + if i == 0: + p = Path(path) + pn = p.parent / (p.stem + ".1" + p.suffix) + else: + _p = Path(path) + p = _p.parent / (_p.stem + f".{i}" + _p.suffix) + pn = _p.parent / (_p.stem + f".{i + 1}" + _p.suffix) + + if p.exists(): + if i == max_num_log_files - 1: + p.unlink() + else: + shutil.move(p, pn) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "log_filepath", type=str, help="Path to log-file to be rotated." + ) + parser.add_argument( + "--max-num-log-files", + type=int, + help="Maximum number of log-files to be kept.", + default=1000, + ) + args = parser.parse_args() + + rotate(args.log_filepath, args.max_num_log_files) + + +if __name__ == "__main__": + main() diff --git a/egs2/TEMPLATE/st1/st.sh b/egs2/TEMPLATE/st1/st.sh index a43e6aef9a9..bc0f57e4173 100755 --- a/egs2/TEMPLATE/st1/st.sh +++ b/egs2/TEMPLATE/st1/st.sh @@ -1472,12 +1472,18 @@ if ! "${skip_eval}"; then detokenizer.perl -l ${tgt_lang} -q < "${_scoredir}/ref.trn" > "${_scoredir}/ref.trn.detok" detokenizer.perl -l ${tgt_lang} -q < "${_scoredir}/hyp.trn" > "${_scoredir}/hyp.trn.detok" + # rotate result files + if [ ${tgt_case} = "tc" ]; then + pyscripts/utils/rotate_logfile.py ${_scoredir}/result.tc.txt + fi + pyscripts/utils/rotate_logfile.py ${_scoredir}/result.lc.txt + if [ ${tgt_case} = "tc" ]; then echo "Case sensitive BLEU result (single-reference)" > ${_scoredir}/result.tc.txt sacrebleu "${_scoredir}/ref.trn.detok" \ -i "${_scoredir}/hyp.trn.detok" \ -m bleu chrf ter \ - > ${_scoredir}/result.tc.txt + >> ${_scoredir}/result.tc.txt log "Write a case-sensitive BLEU (single-reference) result in ${_scoredir}/result.tc.txt" fi @@ -1489,7 +1495,7 @@ if ! "${skip_eval}"; then sacrebleu -lc "${_scoredir}/ref.trn.detok.lc.rm" \ -i "${_scoredir}/hyp.trn.detok.lc.rm" \ -m bleu chrf ter \ - > ${_scoredir}/result.lc.txt + >> ${_scoredir}/result.lc.txt log "Write a case-insensitve BLEU (single-reference) result in ${_scoredir}/result.lc.txt" # process multi-references cases @@ -1520,17 +1526,17 @@ if ! "${skip_eval}"; then done if [ ${tgt_case} = "tc" ]; then - echo "Case sensitive BLEU result (multi-references)" > ${_scoredir}/result.tc.txt + echo "Case sensitive BLEU result (multi-references)" >> ${_scoredir}/result.tc.txt sacrebleu ${case_sensitive_refs} \ -i ${_scoredir}/hyp.trn.detok.lc.rm -m bleu chrf ter \ - > ${_scoredir}/result.tc.txt + >> ${_scoredir}/result.tc.txt log "Write a case-sensitve BLEU (multi-reference) result in ${_scoredir}/result.tc.txt" fi - echo "Case insensitive BLEU result (multi-references)" > ${_scoredir}/result.lc.txt + echo "Case insensitive BLEU result (multi-references)" >> ${_scoredir}/result.lc.txt sacrebleu -lc ${case_insensitive_refs} \ -i ${_scoredir}/hyp.trn.detok.lc.rm -m bleu chrf ter \ - > ${_scoredir}/result.lc.txt + >> ${_scoredir}/result.lc.txt log "Write a case-insensitve BLEU (multi-reference) result in ${_scoredir}/result.lc.txt" fi done