FST  openfst-1.6.1
OpenFst Library
text-io.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #include <fst/script/text-io.h>
5 
6 #include <cstring>
7 #include <fstream>
8 #include <ostream>
9 #include <sstream>
10 #include <utility>
11 
12 #include <fst/types.h>
13 #include <fst/log.h>
14 #include <fstream>
15 #include <fst/util.h>
16 
17 namespace fst {
18 namespace script {
19 
20 // Reads vector of weights; returns true on success.
21 bool ReadPotentials(const string &weight_type, const string &filename,
22  std::vector<WeightClass> *potentials) {
23  std::ifstream strm(filename.c_str());
24  if (!strm.good()) {
25  LOG(ERROR) << "ReadPotentials: Can't open file: " << filename;
26  return false;
27  }
28  static const int kLineLen = 8096;
29  char line[kLineLen];
30  size_t nline = 0;
31  potentials->clear();
32  while (!strm.getline(line, kLineLen).fail()) {
33  ++nline;
34  std::vector<char *> col;
35  SplitToVector(line, "\n\t ", &col, true);
36  if (col.size() == 0 || col[0][0] == '\0') continue;
37  if (col.size() != 2) {
38  FSTERROR() << "ReadPotentials: Bad number of columns, "
39  << "file = " << filename << ", line = " << nline;
40  return false;
41  }
42  ssize_t s = StrToInt64(col[0], filename, nline, false);
43  WeightClass weight(weight_type, col[1]);
44  while (potentials->size() <= s) {
45  potentials->push_back(WeightClass::Zero(weight_type));
46  }
47  potentials->back() = weight;
48  }
49  return true;
50 }
51 
52 // Writes vector of weights; returns true on success.
53 bool WritePotentials(const string &filename,
54  const std::vector<WeightClass> &potentials) {
55  std::ofstream fstrm;
56  if (!filename.empty()) {
57  fstrm.open(filename.c_str());
58  if (!fstrm) {
59  LOG(ERROR) << "WritePotentials: Can't open file: " << filename;
60  return false;
61  }
62  }
63  std::ostream &ostrm = fstrm.is_open() ? fstrm : std::cout;
64  ostrm.precision(9);
65  for (auto s = 0; s < potentials.size(); ++s)
66  ostrm << s << "\t" << potentials[s] << "\n";
67  if (ostrm.fail())
68  LOG(ERROR) << "WritePotentials: Write failed: "
69  << (filename.empty() ? "standard output" : filename);
70  return !ostrm;
71 }
72 
73 } // namespace script
74 } // namespace fst
#define LOG(type)
Definition: log.h:48
bool WritePotentials(const string &filename, const std::vector< WeightClass > &potentials)
Definition: text-io.cc:53
#define FSTERROR()
Definition: util.h:32
void SplitToVector(char *line, const char *delim, std::vector< char * > *vec, bool omit_empty_strings)
Definition: util.cc:21
bool ReadPotentials(const string &weight_type, const string &filename, std::vector< WeightClass > *potentials)
Definition: text-io.cc:21
static WeightClass Zero(const string &weight_type)
Definition: weight-class.cc:24
int64 StrToInt64(const string &s, const string &src, size_t nline, bool allow_negative, bool *error=nullptr)
Definition: util.cc:33