Skip to content

Instantly share code, notes, and snippets.

@ShigekiKarita
Last active June 27, 2019 10:37
Show Gist options
  • Save ShigekiKarita/6f309c6625f306b1575023ec54384be9 to your computer and use it in GitHub Desktop.
Save ShigekiKarita/6f309c6625f306b1575023ec54384be9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# usage:
# $ ./score_abcd.sh ./exp/tri3a_dnn/decode_tgpr_5k_dev_0330/scoring/15.tra
# tmp/result.A.txt: | SPKR | # Snt # Wrd | Corr Sub Del Ins Err S.Err |
# tmp/result.A.txt: | Sum/Avg| 330 5353 | 94.2 5.4 0.4 2.5 8.3 52.4 |
# tmp/result.B.txt: | SPKR | # Snt # Wrd | Corr Sub Del Ins Err S.Err |
# tmp/result.B.txt: | Sum/Avg| 1980 32118 | 88.3 9.2 2.5 1.8 13.5 66.3 |
# tmp/result.C.txt: | SPKR | # Snt # Wrd | Corr Sub Del Ins Err S.Err |
# tmp/result.C.txt: | Sum/Avg| 330 5353 | 84.8 13.8 1.4 3.0 18.2 65.2 |
# tmp/result.D.txt: | SPKR | # Snt # Wrd | Corr Sub Del Ins Err S.Err |
# tmp/result.D.txt: | Sum/Avg| 1980 32118 | 73.4 20.1 6.4 2.5 29.1 76.2 |
hyp=$1
dir=$(dirname $hyp)/sclite
hyptxt=$dir/$(basename $hyp).txt
reftxt=$(dirname $hyp)/test_filt.txt
mkdir -p $dir
# to text
cat $hyp | utils/int2sym.pl -f 2- ./exp/tri3a_dnn/graph_tgpr_5k/words.txt | sed 's:<UNK>::g' > $hyptxt
# to sclite format
python3 <<EOF
def f(src, dst):
fd = {k: open(f"{dst}.{k}.trn", "w") for k in ["A", "B", "C", "D"]}
with open(src, "r") as f:
for line in f:
xs = line.split()
uid = xs[0]
txt = " ".join(xs[1:]).strip()
if uid[-1] == "0":
s = "A"
elif uid[-1] in "123456":
s = "B"
elif uid[-1] == "7":
s = "C"
elif uid[-1] in "89abcd":
s = "D"
else:
assert False, f"invalid uid: {uid}"
fd[s].write(f"{txt}({uid[:4]}_{uid[4:]})\n")
f("$hyptxt", "$dir/hyp")
f("$reftxt", "$dir/ref")
EOF
sclite -r ${dir}/ref.A.trn trn -h ${dir}/hyp.A.trn trn -i rm -o all stdout > ${dir}/result.A.txt
sclite -r ${dir}/ref.B.trn trn -h ${dir}/hyp.B.trn trn -i rm -o all stdout > ${dir}/result.B.txt
sclite -r ${dir}/ref.C.trn trn -h ${dir}/hyp.C.trn trn -i rm -o all stdout > ${dir}/result.C.txt
sclite -r ${dir}/ref.D.trn trn -h ${dir}/hyp.D.trn trn -i rm -o all stdout > ${dir}/result.D.txt
grep -e Avg -e SPKR -m 2 ${dir}/result.*.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment