Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <fst/script/text-io.h>
00018
00019 #include <cstring>
00020 #include <sstream>
00021 #include <utility>
00022 using std::pair; using std::make_pair;
00023
00024 #include <fst/types.h>
00025 #include <fst/util.h>
00026
00027 namespace fst {
00028 namespace script {
00029
00030 void ReadPotentials(const string &weight_type,
00031 const string& filename,
00032 vector<WeightClass>* potential) {
00033 ifstream strm(filename.c_str());
00034 if (!strm)
00035 LOG(FATAL) << "ReadPotentials: Can't open file: " << filename;
00036
00037 const int kLineLen = 8096;
00038 char line[kLineLen];
00039 size_t nline = 0;
00040
00041 potential->clear();
00042 while (strm.getline(line, kLineLen)) {
00043 ++nline;
00044 vector<char *> col;
00045 SplitToVector(line, "\n\t ", &col, true);
00046 if (col.size() == 0 || col[0][0] == '\0')
00047 continue;
00048 if (col.size() != 2)
00049 LOG(FATAL) << "ReadPotentials: Bad number of columns, "
00050 << "file = " << filename << ", line = " << nline;
00051
00052 ssize_t s = StrToInt64(col[0], filename, nline, false);
00053 WeightClass weight(weight_type, col[1]);
00054
00055 while (potential->size() <= s)
00056 potential->push_back(WeightClass::Zero());
00057 (*potential)[s] = weight;
00058 }
00059 }
00060
00061 void WritePotentials(const string& filename,
00062 const vector<WeightClass>& potential) {
00063 ostream *strm = &std::cout;
00064 if (!filename.empty()) {
00065 strm = new ofstream(filename.c_str());
00066 if (!*strm)
00067 LOG(FATAL) << "WritePotentials: Can't open file: " << filename;
00068 }
00069
00070 strm->precision(9);
00071 for (ssize_t s = 0; s < potential.size(); ++s)
00072 *strm << s << "\t" << potential[s] << "\n";
00073
00074 if (!*strm)
00075 LOG(FATAL) << "WritePotentials: Write failed: "
00076 << (filename.empty() ? "standard output" : filename);
00077 if (strm != &std::cout)
00078 delete strm;
00079 }
00080
00081
00082 }
00083 }
00084