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