Skip to content

Commit

Permalink
review suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chintu619 committed May 13, 2022
1 parent 650c733 commit ea44663
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
59 changes: 59 additions & 0 deletions egs2/TEMPLATE/asr1/pyscripts/utils/rotate_logfile.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 12 additions & 6 deletions egs2/TEMPLATE/st1/st.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ea44663

Please sign in to comment.