1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
4 #include <fst/script/text-io.h>
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);
24 LOG(ERROR) << "ReadPotentials: Can't open file: " << filename;
27 static constexpr int kLineLen = 8096;
31 while (!istrm.getline(line, kLineLen).fail()) {
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;
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));
46 potentials->back() = weight;
51 // Writes vector of weights; returns true on success.
52 bool WritePotentials(const string &filename,
53 const std::vector<WeightClass> &potentials) {
55 if (!filename.empty()) {
58 LOG(ERROR) << "WritePotentials: Can't open file: " << filename;
62 std::ostream &strm = ostrm.is_open() ? ostrm : std::cout;
64 for (size_t s = 0; s < potentials.size(); ++s) {
65 strm << s << "\t" << potentials[s] << "\n";
68 LOG(ERROR) << "WritePotentials: Write failed: "
69 << (filename.empty() ? "standard output" : filename);