simple-io-funcs.cc
Go to the documentation of this file.
1 // util/simple-io-funcs.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 #include "util/simple-io-funcs.h"
20 #include "util/text-utils.h"
21 
22 namespace kaldi {
23 
24 bool WriteIntegerVectorSimple(const std::string &wxfilename,
25  const std::vector<int32> &list) {
26  kaldi::Output ko;
27  // false, false is: text-mode, no Kaldi header.
28  if (!ko.Open(wxfilename, false, false)) return false;
29  for (size_t i = 0; i < list.size(); i++) ko.Stream() << list[i] << '\n';
30  return ko.Close();
31 }
32 
33 bool ReadIntegerVectorSimple(const std::string &rxfilename,
34  std::vector<int32> *list) {
35  kaldi::Input ki;
36  if (!ki.OpenTextMode(rxfilename)) return false;
37  std::istream &is = ki.Stream();
38  int32 i;
39  list->clear();
40  while ( !(is >> i).fail() )
41  list->push_back(i);
42  is >> std::ws;
43  return is.eof(); // should be eof, or junk at end of file.
44 }
45 
46 bool WriteIntegerVectorVectorSimple(const std::string &wxfilename,
47  const std::vector<std::vector<int32> > &list) {
48  kaldi::Output ko;
49  // false, false is: text-mode, no Kaldi header.
50  if (!ko.Open(wxfilename, false, false)) return false;
51  std::ostream &os = ko.Stream();
52  for (size_t i = 0; i < list.size(); i++) {
53  for (size_t j = 0; j < list[i].size(); j++) {
54  os << list[i][j];
55  if (j+1 < list[i].size()) os << ' ';
56  }
57  os << '\n';
58  }
59  return ko.Close();
60 }
61 
62 bool ReadIntegerVectorVectorSimple(const std::string &rxfilename,
63  std::vector<std::vector<int32> > *list) {
64  kaldi::Input ki;
65  if (!ki.OpenTextMode(rxfilename)) return false;
66  std::istream &is = ki.Stream();
67  list->clear();
68  std::string line;
69  while (std::getline(is, line)) {
70  std::vector<int32> v;
71  if (!SplitStringToIntegers(line, " \t\r", true, &v)) {
72  list->clear();
73  return false;
74  }
75  list->push_back(v);
76  }
77  return is.eof(); // if we're not at EOF, something weird happened.
78 }
79 
80 
81 } // end namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
kaldi::int32 int32
bool OpenTextMode(const std::string &rxfilename)
Definition: kaldi-io-inl.h:30
std::istream & Stream()
Definition: kaldi-io.cc:826
std::ostream & Stream()
Definition: kaldi-io.cc:701
bool ReadIntegerVectorVectorSimple(const std::string &rxfilename, std::vector< std::vector< int32 > > *list)
bool Open(const std::string &wxfilename, bool binary, bool write_header)
This opens the stream, with the given mode (binary or text).
Definition: kaldi-io.cc:707
bool WriteIntegerVectorVectorSimple(const std::string &wxfilename, const std::vector< std::vector< int32 > > &list)
bool WriteIntegerVectorSimple(const std::string &wxfilename, const std::vector< int32 > &list)
WriteToList attempts to write this list of integers, one per line, to the given file, in text format.
bool ReadIntegerVectorSimple(const std::string &rxfilename, std::vector< int32 > *list)
ReadFromList attempts to read this list of integers, one per line, from the given file...
bool Close()
Definition: kaldi-io.cc:677