lattice-lmrescore.cc
6 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// latbin/lattice-lmrescore.cc
// Copyright 2009-2011 Microsoft Corporation
// 2014 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 "fstext/kaldi-fst-io.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;
using fst::ReadFstKaldi;
const char *usage =
"Add lm_scale * [cost of best path through LM FST] to graph-cost of\n"
"paths through lattice. Does this by composing with LM FST, then\n"
"lattice-determinizing (it has to negate weights first if lm_scale<0)\n"
"Usage: lattice-lmrescore [options] <lattice-rspecifier> <lm-fst-in> <lattice-wspecifier>\n"
" e.g.: lattice-lmrescore --lm-scale=-1.0 ark:in.lats 'fstproject --project_output=true data/lang/G.fst|' ark:out.lats\n";
ParseOptions po(usage);
BaseFloat lm_scale = 1.0;
int32 num_states_cache = 50000;
po.Register("lm-scale", &lm_scale, "Scaling factor for language model costs; frequently 1.0 or -1.0");
po.Register("num-states-cache", &num_states_cache,
"Number of states we cache when mapping LM FST to lattice type. "
"More -> more memory but faster.");
po.Read(argc, argv);
if (po.NumArgs() != 3) {
po.PrintUsage();
exit(1);
}
std::string lats_rspecifier = po.GetArg(1),
fst_rxfilename = po.GetArg(2),
lats_wspecifier = po.GetArg(3);
VectorFst<StdArc> *std_lm_fst = ReadFstKaldi(fst_rxfilename);
if (std_lm_fst->Properties(fst::kILabelSorted, true) == 0) {
// Make sure LM is sorted on ilabel.
fst::ILabelCompare<StdArc> ilabel_comp;
fst::ArcSort(std_lm_fst, ilabel_comp);
}
// mapped_fst is the LM fst interpreted using the LatticeWeight semiring,
// with all the cost on the first member of the pair (since it's a graph
// weight).
fst::CacheOptions cache_opts(true, num_states_cache);
fst::MapFstOptions mapfst_opts(cache_opts);
fst::StdToLatticeMapper<BaseFloat> mapper;
fst::MapFst<StdArc, LatticeArc, fst::StdToLatticeMapper<BaseFloat> >
lm_fst(*std_lm_fst, mapper, mapfst_opts);
delete std_lm_fst;
// The next fifteen or so lines are a kind of optimization and
// can be ignored if you just want to understand what is going on.
// Change the options for TableCompose to match the input
// (because it's the arcs of the LM FST we want to do lookup
// on).
fst::TableComposeOptions compose_opts(fst::TableMatcherOptions(),
true, fst::SEQUENCE_FILTER,
fst::MATCH_INPUT);
// The following is an optimization for the TableCompose
// composition: it stores certain tables that enable fast
// lookup of arcs during composition.
fst::TableComposeCache<fst::Fst<LatticeArc> > lm_compose_cache(compose_opts);
// Read as regular lattice-- this is the form we need it in for efficient
// composition and determinization.
SequentialLatticeReader lattice_reader(lats_rspecifier);
// Write as compact lattice.
CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
int32 n_done = 0, n_fail = 0;
for (; !lattice_reader.Done(); lattice_reader.Next()) {
std::string key = lattice_reader.Key();
Lattice lat = lattice_reader.Value();
lattice_reader.FreeCurrent();
if (lm_scale != 0.0) {
// Only need to modify it if LM scale nonzero.
// Before composing with the LM FST, we scale the lattice weights
// by the inverse of "lm_scale". We'll later scale by "lm_scale".
// We do it this way so we can determinize and it will give the
// right effect (taking the "best path" through the LM) regardless
// of the sign of lm_scale.
fst::ScaleLattice(fst::GraphLatticeScale(1.0 / lm_scale), &lat);
ArcSort(&lat, fst::OLabelCompare<LatticeArc>());
Lattice composed_lat;
// Could just do, more simply: Compose(lat, lm_fst, &composed_lat);
// and not have lm_compose_cache at all.
// The command below is faster, though; it's constant not
// logarithmic in vocab size.
TableCompose(lat, lm_fst, &composed_lat, &lm_compose_cache);
Invert(&composed_lat); // make it so word labels are on the input.
CompactLattice determinized_lat;
DeterminizeLattice(composed_lat, &determinized_lat);
fst::ScaleLattice(fst::GraphLatticeScale(lm_scale), &determinized_lat);
if (determinized_lat.Start() == fst::kNoStateId) {
KALDI_WARN << "Empty lattice for utterance " << key << " (incompatible LM?)";
n_fail++;
} else {
compact_lattice_writer.Write(key, determinized_lat);
n_done++;
}
} else {
// zero scale so nothing to do.
n_done++;
CompactLattice compact_lat;
ConvertLattice(lat, &compact_lat);
compact_lattice_writer.Write(key, compact_lat);
}
}
KALDI_LOG << "Done " << n_done << " lattices, failed for " << n_fail;
return (n_done != 0 ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}