FST  openfst-1.7.1
OpenFst Library
read_write_utils.h
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 // Definition of ReadLabelTriples based on ReadLabelPairs, like that in
5 // nlp/fst/lib/util.h for pairs, and similarly for WriteLabelTriples.
6 
7 #ifndef FST_EXTENSIONS_MPDT_READ_WRITE_UTILS_H_
8 #define FST_EXTENSIONS_MPDT_READ_WRITE_UTILS_H_
9 
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include <fstream>
15 #include <fst/test-properties.h>
16 
17 namespace fst {
18 
19 // Returns true on success.
20 template <typename Label>
21 bool ReadLabelTriples(const string &filename,
22  std::vector<std::pair<Label, Label>> *pairs,
23  std::vector<Label> *assignments,
24  bool allow_negative = false) {
25  std::ifstream fstrm(filename);
26  if (!fstrm) {
27  LOG(ERROR) << "ReadIntTriples: Can't open file: " << filename;
28  return false;
29  }
30  static constexpr auto kLineLen = 8096;
31  char line[kLineLen];
32  size_t nline = 0;
33  pairs->clear();
34  while (fstrm.getline(line, kLineLen)) {
35  ++nline;
36  std::vector<char *> col;
37  SplitString(line, "\n\t ", &col, true);
38  // Empty line or comment?
39  if (col.empty() || col[0][0] == '\0' || col[0][0] == '#') continue;
40  if (col.size() != 3) {
41  LOG(ERROR) << "ReadLabelTriples: Bad number of columns, "
42  << "file = " << filename << ", line = " << nline;
43  return false;
44  }
45  bool err;
46  const Label i1 = StrToInt64(col[0], filename, nline, allow_negative, &err);
47  if (err) return false;
48  const Label i2 = StrToInt64(col[1], filename, nline, allow_negative, &err);
49  if (err) return false;
50  using Level = Label;
51  const Level i3 = StrToInt64(col[2], filename, nline, allow_negative, &err);
52  if (err) return false;
53  pairs->push_back(std::make_pair(i1, i2));
54  assignments->push_back(i3);
55  }
56  return true;
57 }
58 
59 // Returns true on success.
60 template <typename Label>
61 bool WriteLabelTriples(const string &filename,
62  const std::vector<std::pair<Label, Label>> &pairs,
63  const std::vector<Label> &assignments) {
64  if (pairs.size() != assignments.size()) {
65  LOG(ERROR) << "WriteLabelTriples: Pairs and assignments of different sizes";
66  return false;
67  }
68  std::ofstream fstrm(filename);
69  if (!fstrm) {
70  LOG(ERROR) << "WriteLabelTriples: Can't open file: " << filename;
71  return false;
72  }
73  for (size_t n = 0; n < pairs.size(); ++n)
74  fstrm << pairs[n].first << "\t" << pairs[n].second << "\t" << assignments[n]
75  << "\n";
76  if (!fstrm) {
77  LOG(ERROR) << "WriteLabelTriples: Write failed: "
78  << (filename.empty() ? "standard output" : filename);
79  return false;
80  }
81  return true;
82 }
83 
84 } // namespace fst
85 
86 #endif // FST_EXTENSIONS_MPDT_READ_WRITE_UTILS_H_
#define LOG(type)
Definition: log.h:48
bool ReadLabelTriples(const string &filename, std::vector< std::pair< Label, Label >> *pairs, std::vector< Label > *assignments, bool allow_negative=false)
void SplitString(char *line, const char *delim, std::vector< char * > *vec, bool omit_empty_strings)
Definition: util.cc:22
bool WriteLabelTriples(const string &filename, const std::vector< std::pair< Label, Label >> &pairs, const std::vector< Label > &assignments)
int64 StrToInt64(const string &s, const string &src, size_t nline, bool allow_negative, bool *error=nullptr)
Definition: util.cc:34