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