Blame view

src/util/simple-io-funcs.cc 2.61 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  // util/simple-io-funcs.cc
  
  // Copyright 2009-2011  Microsoft Corporation
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  
  //  http://www.apache.org/licenses/LICENSE-2.0
  
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  #include "util/simple-io-funcs.h"
  #include "util/text-utils.h"
  
  namespace kaldi {
  
  bool WriteIntegerVectorSimple(const std::string &wxfilename,
                                const std::vector<int32> &list) {
    kaldi::Output ko;
    // false, false is: text-mode, no Kaldi header.
    if (!ko.Open(wxfilename, false, false)) return false;
    for (size_t i = 0; i < list.size(); i++) ko.Stream() << list[i] << '
  ';
    return ko.Close();
  }
  
  bool ReadIntegerVectorSimple(const std::string &rxfilename,
                               std::vector<int32> *list) {
    kaldi::Input ki;
    if (!ki.OpenTextMode(rxfilename)) return false;
    std::istream &is = ki.Stream();
    int32 i;
    list->clear();
    while ( !(is >> i).fail() )
      list->push_back(i);
    is >> std::ws;
    return is.eof();  // should be eof, or junk at end of file.
  }
  
  bool WriteIntegerVectorVectorSimple(const std::string &wxfilename,
                                   const std::vector<std::vector<int32> > &list) {
    kaldi::Output ko;
    // false, false is: text-mode, no Kaldi header.
    if (!ko.Open(wxfilename, false, false)) return false;
    std::ostream &os = ko.Stream();
    for (size_t i = 0; i < list.size(); i++) {
      for (size_t j = 0; j < list[i].size(); j++) {
        os << list[i][j];
        if (j+1 < list[i].size()) os << ' ';
      }
      os << '
  ';
    }
    return ko.Close();
  }
  
  bool ReadIntegerVectorVectorSimple(const std::string &rxfilename,
                                     std::vector<std::vector<int32> > *list) {
    kaldi::Input ki;
    if (!ki.OpenTextMode(rxfilename)) return false;
    std::istream &is = ki.Stream();
    list->clear();
    std::string line;
    while (std::getline(is, line)) {
      std::vector<int32> v;
      if (!SplitStringToIntegers(line, " \t\r", true, &v)) {
        list->clear();
        return false;
      }
      list->push_back(v);
    }
    return is.eof();  // if we're not at EOF, something weird happened.
  }
  
  
  }  // end namespace kaldi