lattice-scale.cc
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// latbin/lattice-scale.cc
// Copyright 2009-2013 Microsoft Corporation
// Johns Hopkins University (author: Daniel Povey)
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "fstext/fstext-lib.h"
#include "lat/kaldi-lattice.h"
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
typedef kaldi::int32 int32;
typedef kaldi::int64 int64;
using fst::SymbolTable;
using fst::VectorFst;
using fst::StdArc;
const char *usage =
"Apply scaling to lattice weights\n"
"Usage: lattice-scale [options] lattice-rspecifier lattice-wspecifier\n"
" e.g.: lattice-scale --lm-scale=0.0 ark:1.lats ark:scaled.lats\n";
ParseOptions po(usage);
bool write_compact = true;
BaseFloat acoustic_scale = 1.0;
BaseFloat inv_acoustic_scale = 1.0;
BaseFloat lm_scale = 1.0;
BaseFloat acoustic2lm_scale = 0.0;
BaseFloat lm2acoustic_scale = 0.0;
po.Register("write-compact", &write_compact, "If true, write in normal (compact) form.");
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
po.Register("inv-acoustic-scale", &inv_acoustic_scale, "An alternative way "
"of setting the acoustic scale: you can set its inverse.");
po.Register("lm-scale", &lm_scale, "Scaling factor for graph/lm costs");
po.Register("acoustic2lm-scale", &acoustic2lm_scale, "Add this times original acoustic costs to LM costs");
po.Register("lm2acoustic-scale", &lm2acoustic_scale, "Add this times original LM costs to acoustic costs");
po.Read(argc, argv);
if (po.NumArgs() != 2) {
po.PrintUsage();
exit(1);
}
std::string lats_rspecifier = po.GetArg(1),
lats_wspecifier = po.GetArg(2);
int32 n_done = 0;
KALDI_ASSERT(acoustic_scale == 1.0 || inv_acoustic_scale == 1.0);
if (inv_acoustic_scale != 1.0)
acoustic_scale = 1.0 / inv_acoustic_scale;
std::vector<std::vector<double> > scale(2);
scale[0].resize(2);
scale[1].resize(2);
scale[0][0] = lm_scale;
scale[0][1] = acoustic2lm_scale;
scale[1][0] = lm2acoustic_scale;
scale[1][1] = acoustic_scale;
if (write_compact) {
SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
// Write as compact lattice.
CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
CompactLattice lat = compact_lattice_reader.Value();
ScaleLattice(scale, &lat);
compact_lattice_writer.Write(compact_lattice_reader.Key(), lat);
n_done++;
}
} else {
SequentialLatticeReader lattice_reader(lats_rspecifier);
// Write as regular lattice.
LatticeWriter lattice_writer(lats_wspecifier);
for (; !lattice_reader.Done(); lattice_reader.Next()) {
Lattice lat = lattice_reader.Value();
ScaleLattice(scale, &lat);
lattice_writer.Write(lattice_reader.Key(), lat);
n_done++;
}
}
KALDI_LOG << "Done " << n_done << " lattices.";
return (n_done != 0 ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}