copy-int-vector.cc
Go to the documentation of this file.
1 // bin/copy-int-vector.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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 "matrix/kaldi-vector.h"
24 
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Copy vectors of integers, or archives of vectors of integers \n"
32  "(e.g. alignments)\n"
33  "\n"
34  "Usage: copy-int-vector [options] (vector-in-rspecifier|vector-in-rxfilename) (vector-out-wspecifier|vector-out-wxfilename)\n"
35  " e.g.: copy-int-vector --binary=false foo -\n"
36  " copy-int-vector ark:1.ali ark,t:-\n";
37 
38  bool binary = true;
39  ParseOptions po(usage);
40 
41  po.Register("binary", &binary, "Write in binary mode (only relevant if output is a wxfilename)");
42 
43  po.Read(argc, argv);
44 
45  if (po.NumArgs() != 2) {
46  po.PrintUsage();
47  exit(1);
48  }
49 
50 
51  std::string vector_in_fn = po.GetArg(1),
52  vector_out_fn = po.GetArg(2);
53 
54  // all these "fn"'s are either rspecifiers or filenames.
55 
56  bool in_is_rspecifier =
57  (ClassifyRspecifier(vector_in_fn, NULL, NULL)
58  != kNoRspecifier),
59  out_is_wspecifier =
60  (ClassifyWspecifier(vector_out_fn, NULL, NULL, NULL)
61  != kNoWspecifier);
62 
63  if (in_is_rspecifier != out_is_wspecifier)
64  KALDI_ERR << "Cannot mix archives with regular files (copying vectors)";
65 
66  if (!in_is_rspecifier) {
67  std::vector<int32> vec;
68  {
69  bool binary_in;
70  Input ki(vector_in_fn, &binary_in);
71  ReadIntegerVector(ki.Stream(), binary_in, &vec);
72  }
73  Output ko(vector_out_fn, binary);
74  WriteIntegerVector(ko.Stream(), binary, vec);
75  KALDI_LOG << "Copied vector to " << vector_out_fn;
76  return 0;
77  } else {
78  int num_done = 0;
79  Int32VectorWriter writer(vector_out_fn);
80  SequentialInt32VectorReader reader(vector_in_fn);
81  for (; !reader.Done(); reader.Next(), num_done++)
82  writer.Write(reader.Key(), reader.Value());
83  KALDI_LOG << "Copied " << num_done << " vectors of int32.";
84  return (num_done != 0 ? 0 : 1);
85  }
86  } catch(const std::exception &e) {
87  std::cerr << e.what();
88  return -1;
89  }
90 }
91 
92 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
std::istream & Stream()
Definition: kaldi-io.cc:826
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
#define KALDI_LOG
Definition: kaldi-error.h:153