1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
4 #include <fst/script/text-io.h>
12 #include <fst/types.h>
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 istrm(filename);
25 LOG(ERROR) << "ReadPotentials: Can't open file: " << filename;
28 static const int kLineLen = 8096;
32 while (!istrm.getline(line, kLineLen).fail()) {
34 std::vector<char *> col;
35 SplitToVector(line, "\n\t ", &col, true);
36 if (col.empty() || col[0][0] == '\0') continue;
37 if (col.size() != 2) {
38 FSTERROR() << "ReadPotentials: Bad number of columns, "
39 << "file = " << filename << ", line = " << nline;
42 const ssize_t s = StrToInt64(col[0], filename, nline, false);
43 const WeightClass weight(weight_type, col[1]);
44 while (potentials->size() <= s) {
45 potentials->push_back(WeightClass::Zero(weight_type));
47 potentials->back() = weight;
52 // Writes vector of weights; returns true on success.
53 bool WritePotentials(const string &filename,
54 const std::vector<WeightClass> &potentials) {
56 if (!filename.empty()) {
59 LOG(ERROR) << "WritePotentials: Can't open file: " << filename;
63 std::ostream &strm = ostrm.is_open() ? ostrm : std::cout;
65 for (size_t s = 0; s < potentials.size(); ++s) {
66 strm << s << "\t" << potentials[s] << "\n";
69 LOG(ERROR) << "WritePotentials: Write failed: "
70 << (filename.empty() ? "standard output" : filename);