All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
copy-vector.cc File Reference
Include dependency graph for copy-vector.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file copy-vector.cc.

References kaldi::ClassifyRspecifier(), kaldi::ClassifyWspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, kaldi::kCopyData, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, kaldi::kNoWspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), Vector< Real >::Resize(), Output::Stream(), SequentialTableReader< Holder >::Value(), VectorBase< Real >::Write(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30 
31  const char *usage =
32  "Copy vectors, or archives of vectors (e.g. transition-accs; speaker vectors)\n"
33  "\n"
34  "Usage: copy-vector [options] (<vector-in-rspecifier>|<vector-in-rxfilename>) (<vector-out-wspecifier>|<vector-out-wxfilename>)\n"
35  " e.g.: copy-vector --binary=false 1.mat -\n"
36  " copy-vector ark:2.trans ark,t:-\n"
37  "see also: dot-weights, append-vector-to-feats\n";
38 
39  bool binary = true;
40  int32 change_dim = -1;
41  ParseOptions po(usage);
42 
43  po.Register("binary", &binary, "Write in binary mode (only "
44  "relevant if output is a wxfilename)");
45  po.Register("change_dim", &change_dim,
46  "Use this option to truncate or zero-pad the vectors.");
47 
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() != 2) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55 
56  std::string vector_in_fn = po.GetArg(1),
57  vector_out_fn = po.GetArg(2);
58 
59  // all these "fn"'s are either rspecifiers or filenames.
60 
61  bool in_is_rspecifier =
62  (ClassifyRspecifier(vector_in_fn, NULL, NULL)
63  != kNoRspecifier),
64  out_is_wspecifier =
65  (ClassifyWspecifier(vector_out_fn, NULL, NULL, NULL)
66  != kNoWspecifier);
67 
68  if (in_is_rspecifier != out_is_wspecifier)
69  KALDI_ERR << "Cannot mix archives with regular files (copying vectors)";
70 
71  if (!in_is_rspecifier) {
73  ReadKaldiObject(vector_in_fn, &vec);
74  Output ko(vector_out_fn, binary);
75  if (change_dim >= 0) vec.Resize(change_dim, kCopyData);
76  vec.Write(ko.Stream(), binary);
77  KALDI_LOG << "Copied vector to " << vector_out_fn;
78  return 0;
79  } else {
80  int num_done = 0;
81  BaseFloatVectorWriter writer(vector_out_fn);
82  SequentialBaseFloatVectorReader reader(vector_in_fn);
83  if (change_dim < 0) {
84  for (; !reader.Done(); reader.Next(), num_done++)
85  writer.Write(reader.Key(), reader.Value());
86  KALDI_LOG << "Copied " << num_done << " vectors.";
87  } else {
88  for (; !reader.Done(); reader.Next(), num_done++) {
89  Vector<BaseFloat> vec (reader.Value());
90  vec.Resize(change_dim, kCopyData);
91  writer.Write(reader.Key(), reader.Value());
92  }
93  KALDI_LOG << "Copied " << num_done << " vectors, setting dim to "
94  << change_dim;
95  }
96  return (num_done != 0 ? 0 : 1);
97  }
98  } catch(const std::exception &e) {
99  std::cerr << e.what();
100  return -1;
101  }
102 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:127
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
#define KALDI_LOG
Definition: kaldi-error.h:133