28 int main(
int argc,
char *argv[]) {
30 using namespace kaldi;
32 typedef kaldi::int64 int64;
39 "Compute sentence-level lattice confidence measures for each lattice.\n" 40 "The output is simly the difference between the total costs of the best and\n" 41 "second-best paths in the lattice (or a very large value if the lattice\n" 42 "had only one path). Caution: this is not necessarily a very good confidence\n" 43 "measure. You almost certainly want to specify the acoustic scale.\n" 44 "If the input is a state-level lattice, you need to specify\n" 45 "--read-compact-lattice=false, or the confidences will be very small\n" 46 "(and wrong). You can get word-level confidence info from lattice-mbr-decode.\n" 48 "Usage: lattice-confidence <lattice-rspecifier> <confidence-wspecifier>\n" 49 "E.g.: lattice-confidence --acoustic-scale=0.08333 ark:- ark,t:-\n";
53 bool read_compact_lattice =
true;
55 po.
Register(
"acoustic-scale", &acoustic_scale,
56 "Scaling factor for acoustic likelihoods");
58 "Scaling factor for \"graph costs\" (including LM costs)");
59 po.
Register(
"read-compact-lattice", &read_compact_lattice,
60 "If true, read CompactLattice format; else, read Lattice format " 61 "(necessary for state-level lattices that were written in that " 71 std::string lats_rspecifier = po.
GetArg(1);
72 std::string confidence_wspecifier = po.
GetArg(2);
79 int64 num_done = 0, num_empty = 0,
80 num_one_sentence = 0, num_same_sentence = 0;
81 double sum_neg_exp = 0.0;
84 if (read_compact_lattice) {
87 for (; !clat_reader.
Done(); clat_reader.
Next()) {
89 std::string key = clat_reader.
Key();
93 if (acoustic_scale != 1.0 || lm_scale != 1.0)
97 std::vector<int32> best_sentence, second_best_sentence;
101 &second_best_sentence);
102 if (num_paths == 0) {
103 KALDI_WARN <<
"Lattice for utterance " << key <<
" is equivalent to " 104 <<
"the empty lattice.";
107 }
else if (num_paths == 1) {
109 }
else if (num_paths == 2 && best_sentence == second_best_sentence) {
110 KALDI_WARN <<
"Best and second-best sentences were identical: " 111 <<
"confidence is meaningless. You should call with " 112 <<
"--read-compact-lattice=false.";
116 confidence = std::min(max_output, confidence);
117 sum_neg_exp +=
Exp(-confidence);
118 confidence_writer.
Write(key, confidence);
123 for (; !lat_reader.
Done(); lat_reader.
Next()) {
125 std::string key = lat_reader.
Key();
129 if (acoustic_scale != 1.0 || lm_scale != 1.0)
132 std::vector<int32> best_sentence, second_best_sentence;
136 &second_best_sentence);
137 if (num_paths == 0) {
138 KALDI_WARN <<
"Lattice for utterance " << key <<
" is equivalent to " 139 <<
"the empty lattice.";
142 }
else if (num_paths == 1) {
144 }
else if (num_paths == 2 && best_sentence == second_best_sentence) {
146 KALDI_ERR <<
"Best and second-best sentences were identical.";
149 confidence = std::min(max_output, confidence);
150 sum_neg_exp +=
Exp(-confidence);
151 confidence_writer.
Write(key, confidence);
155 KALDI_LOG <<
"Done " << num_done <<
" lattices, of which " 156 << num_one_sentence <<
" contained only one sentence. " 157 << num_empty <<
" were equivalent to the empty lattice.";
159 KALDI_LOG <<
"Average confidence (averaged in negative-log space) is " 160 << -
Log(sum_neg_exp / num_done);
162 if (num_same_sentence != 0) {
163 KALDI_WARN << num_same_sentence <<
" lattices had the same sentence on " 164 <<
"different paths (likely an error)";
167 return (num_done != 0 ? 0 : 1);
168 }
catch (
const std::exception &e) {
169 std::cerr << e.what();
fst::StdArc::StateId StateId
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
BaseFloat SentenceLevelConfidence(const CompactLattice &clat, int32 *num_paths, std::vector< int32 > *best_sentence, std::vector< int32 > *second_best_sentence)
Caution: this function is not the only way to get confidences in Kaldi.
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
A templated class for writing objects to an archive or script file; see The Table concept...
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
fst::VectorFst< CompactLatticeArc > CompactLattice
int NumArgs() const
Number of positional parameters (c.f. argc-1).