24 #include "fst/fstlib.h"    30 int main(
int argc, 
char *argv[]) {
    31   using namespace kaldi;
    33   typedef kaldi::uint64 uint64;
    36         "Computes element counts from integer vector table.\n"    37         "(e.g. get pdf-counts to estimate DNN-output priors "    38         "for data analysis)\n"    39         "Verbosity : level 1 => print frequencies and histogram\n"    41         "Usage: analyze-counts [options] <alignments-rspecifier> "    42         "<counts-wxfilname>\n"    44         " analyze-counts ark:1.ali prior.counts\n"    45         " Show phone counts by:\n"    46         " ali-to-phones --per-frame=true ark:1.ali ark:- |"    47         " analyze-counts --verbose=1 ark:- - >/dev/null\n"    48         "Note: this is deprecated, see post-to-tacc.\n";
    53     std::string symbol_table_filename = 
"";
    55     po.
Register(
"binary", &binary, 
"write in binary mode");
    56     po.
Register(
"symbol-table", &symbol_table_filename,
    57                 "Read symbol table for display of counts");
    60     po.
Register(
"counts-dim", &counts_dim,
    61                 "Output dimension of the counts, "    62                 "a hint for dimension auto-detection.");
    64     std::string frame_weights;
    65     po.
Register(
"frame-weights", &frame_weights,
    66                 "Per-frame weights (counting weighted frames).");
    67     std::string utt_weights;
    68     po.
Register(
"utt-weights", &utt_weights,
    69                 "Per-utterance weights (counting weighted frames).");
    78     std::string alignments_rspecifier = po.
GetArg(1),
    84     if (frame_weights != 
"") {
    85       weights_reader.
Open(frame_weights);
    88     if (utt_weights != 
"") {
    89       utt_weights_reader.
Open(utt_weights);
    95     int32 num_done = 0, num_other_error = 0;
    96     for (; !alignment_reader.
Done(); alignment_reader.
Next()) {
    97       std::string utt = alignment_reader.
Key();
    98       const std::vector<int32> &alignment = alignment_reader.
Value();
   102       if (utt_weights != 
"") {
   103         if (!utt_weights_reader.
HasKey(utt)) {
   104           KALDI_WARN << utt << 
", missing per-utterance weight";
   108           utt_w = utt_weights_reader.
Value(utt);
   114       if (frame_weights != 
"") {
   115         if (!weights_reader.
HasKey(utt)) {
   116           KALDI_WARN << utt << 
", missing per-frame weights";
   120           frame_w = weights_reader.
Value(utt);
   126       for (
size_t i = 0; 
i < alignment.size(); 
i++) {
   129         if (alignment[
i] >= counts.
Dim()) {
   132         if (frame_weights != 
"") {
   133           counts(alignment[
i]) += 1.0 * utt_w * frame_w(i);
   135           counts(alignment[
i]) += 1.0 * utt_w;
   142     for (
size_t i = 0; 
i < counts.
Dim(); 
i++) {
   143       if (0.0 == counts(
i)) {
   144         KALDI_WARN << 
"Zero count for label " << 
i << 
", this is suspicious.";
   151     counts_nozero.
Add(0.5);
   153     Output ko(wxfilename, binary);
   161       fst::SymbolTable *elem_syms = NULL;
   162       if (symbol_table_filename != 
"") {
   163           elem_syms = fst::SymbolTable::ReadText(symbol_table_filename);
   165             KALDI_ERR << 
"Could not read symbol table from file "   166                       << symbol_table_filename;
   170       std::vector<std::pair<double, int32> > sorted_counts;
   171       for (int32 
i = 0; 
i < counts.
Dim(); 
i++) {
   172         sorted_counts.push_back(
   173                         std::make_pair(static_cast<double>(counts(
i)), 
i));
   175       std::sort(sorted_counts.begin(), sorted_counts.end());
   176       std::ostringstream os;
   177       double sum = counts.
Sum();
   178       os << 
"Printing...\n### The sorted count table," << std::endl;
   179       os << 
"count\t(norm),\tid\t(symbol):" << std::endl;
   180       for (int32 
i = 0; 
i < sorted_counts.size(); 
i++) {
   181         os << sorted_counts[
i].first << 
"\t("   182            << 
static_cast<float>(sorted_counts[
i].first) / sum << 
"),\t"   183            << sorted_counts[
i].second << 
"\t"   184            << (elem_syms != NULL ? 
"(" +
   185                            elem_syms->Find(sorted_counts[
i].second) + 
")" : 
"")
   188       os << 
"\n#total " << sum
   189          << 
" (" << 
static_cast<float>(sum)/100/3600 << 
"h)"   194     KALDI_LOG << 
"Summed " << num_done << 
" int32 vectors to counts, "   195               << 
"skipped " << num_other_error << 
" vectors.";
   196     KALDI_LOG << 
"Counts written to " << wxfilename;
   198   } 
catch(
const std::exception &e) {
   199     std::cerr << e.what();
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
 
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor]. 
 
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary). 
 
bool Open(const std::string &rspecifier)
 
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero). 
 
void Register(const std::string &name, bool *ptr, const std::string &doc)
 
Allows random access to a collection of objects in an archive or script file; see The Table concept...
 
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
 
const T & Value(const std::string &key)
 
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
 
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. 
 
MatrixIndexT Dim() const
Returns the dimension of the vector. 
 
bool HasKey(const std::string &key)
 
Real Sum() const
Returns sum of the elements. 
 
int NumArgs() const
Number of positional parameters (c.f. argc-1). 
 
#define KALDI_ASSERT(cond)
 
int32 g_kaldi_verbose_level
This is set by util/parse-options. 
 
void Add(Real c)
Add a constant to each element of a vector. 
 
int main(int argc, char *argv[])
Sums the pdf vectors to counts, this is used to obtain prior counts for hybrid decoding.