sum-tree-stats.cc
Go to the documentation of this file.
1 // bin/sum-tree-stats.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation, GoVivace Inc.
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "tree/context-dep.h"
24 #include "tree/build-tree-utils.h"
25 
26 int main(int argc, char *argv[]) {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29  try {
30  const char *usage =
31  "Sum statistics for phonetic-context tree building.\n"
32  "Usage: sum-tree-stats [options] tree-accs-out tree-accs-in1 tree-accs-in2 ...\n"
33  "e.g.: \n"
34  " sum-tree-stats treeacc 1.treeacc 2.treeacc 3.treeacc\n";
35 
36  ParseOptions po(usage);
37  bool binary = true;
38 
39  po.Register("binary", &binary, "Write output in binary mode");
40  po.Read(argc, argv);
41 
42  if (po.NumArgs() < 2) {
43  po.PrintUsage();
44  exit(1);
45  }
46 
47  std::map<EventType, Clusterable*> tree_stats;
48 
49  std::string tree_stats_wxfilename = po.GetArg(1);
50 
51  // A reminder on what BuildTreeStatsType is:
52  // typedef std::vector<std::pair<EventType, Clusterable*> > BuildTreeStatsType;
53 
54  for (int32 arg = 2; arg <= po.NumArgs(); arg++) {
55  std::string tree_stats_rxfilename = po.GetArg(arg);
56  bool binary_in;
57  Input ki(tree_stats_rxfilename, &binary_in);
58  BuildTreeStatsType stats_array;
59  GaussClusterable example; // Lets ReadBuildTreeStats know which type to read..
60  ReadBuildTreeStats(ki.Stream(), binary_in, example, &stats_array);
61  for (BuildTreeStatsType::iterator iter = stats_array.begin();
62  iter != stats_array.end(); ++iter) {
63  EventType e = iter->first;
64  Clusterable *c = iter->second;
65  std::map<EventType, Clusterable*>::iterator map_iter = tree_stats.find(e);
66  if (map_iter == tree_stats.end()) { // Not already present.
67  tree_stats[e] = c;
68  } else {
69  map_iter->second->Add(*c);
70  delete c;
71  }
72  }
73  }
74 
75  BuildTreeStatsType stats; // vectorized form.
76 
77  for (std::map<EventType, Clusterable*>::const_iterator iter = tree_stats.begin();
78  iter != tree_stats.end();
79  ++iter) {
80  stats.push_back(std::make_pair(iter->first, iter->second));
81  }
82  tree_stats.clear();
83 
84  {
85  Output ko(tree_stats_wxfilename, binary);
86  WriteBuildTreeStats(ko.Stream(), binary, stats);
87  }
88  KALDI_LOG << "Wrote summed accs ( " << stats.size() << " individual stats)";
89  DeleteBuildTreeStats(&stats);
90  return (stats.size() != 0 ? 0 : 1);
91  } catch(const std::exception &e) {
92  std::cerr << e.what();
93  return -1;
94  }
95 }
96 
97 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
virtual void Add(const Clusterable &other)=0
Add other stats.
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
void DeleteBuildTreeStats(BuildTreeStatsType *stats)
This frees the Clusterable* pointers in "stats", where non-NULL, and sets them to NULL...
void ReadBuildTreeStats(std::istream &is, bool binary, const Clusterable &example, BuildTreeStatsType *stats)
Reads BuildTreeStats object.
std::istream & Stream()
Definition: kaldi-io.cc:826
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
std::ostream & Stream()
Definition: kaldi-io.cc:701
int main(int argc, char *argv[])
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
std::vector< std::pair< EventType, Clusterable * > > BuildTreeStatsType
GaussClusterable wraps Gaussian statistics in a form accessible to generic clustering algorithms...
#define KALDI_LOG
Definition: kaldi-error.h:153
void WriteBuildTreeStats(std::ostream &os, bool binary, const BuildTreeStatsType &stats)
Writes BuildTreeStats object. This works even if pointers are NULL.