5da4285d2da755cf305fb367001c9231da0dd905
[platform/upstream/openfst.git] / src / script / text-io.cc
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 string &weight_type, const 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     SplitToVector(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 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