FST  openfst-1.8.2.post1
OpenFst Library
read_write_utils.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Definition of ReadLabelTriples based on ReadLabelPairs, like that in
19 // nlp/fst/lib/util.h for pairs, and similarly for WriteLabelTriples.
20 
21 #ifndef FST_EXTENSIONS_MPDT_READ_WRITE_UTILS_H_
22 #define FST_EXTENSIONS_MPDT_READ_WRITE_UTILS_H_
23 
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include <fstream>
29 #include <fst/test-properties.h>
30 #include <string_view>
31 
32 namespace fst {
33 
34 // Returns true on success.
35 template <typename Label>
36 bool ReadLabelTriples(const std::string &source,
37  std::vector<std::pair<Label, Label>> *pairs,
38  std::vector<Label> *assignments,
39  bool allow_negative = false) {
40  std::ifstream fstrm(source);
41  if (!fstrm) {
42  LOG(ERROR) << "ReadIntTriples: Can't open file: " << source;
43  return false;
44  }
45  static constexpr auto kLineLen = 8096;
46  char line[kLineLen];
47  size_t nline = 0;
48  pairs->clear();
49  while (fstrm.getline(line, kLineLen)) {
50  ++nline;
51  std::vector<std::string_view> col =
52  StrSplit(line, ByAnyChar("\n\t "), SkipEmpty());
53  // Empty line or comment?
54  if (col.empty() || col[0].empty() || col[0][0] == '#') continue;
55  if (col.size() != 3) {
56  LOG(ERROR) << "ReadLabelTriples: Bad number of columns, "
57  << "file = " << source << ", line = " << nline;
58  return false;
59  }
60  bool err;
61  const Label i1 = StrToInt64(col[0], source, nline, allow_negative, &err);
62  if (err) return false;
63  const Label i2 = StrToInt64(col[1], source, nline, allow_negative, &err);
64  if (err) return false;
65  using Level = Label;
66  const Level i3 = StrToInt64(col[2], source, nline, allow_negative, &err);
67  if (err) return false;
68  pairs->push_back(std::make_pair(i1, i2));
69  assignments->push_back(i3);
70  }
71  return true;
72 }
73 
74 // Returns true on success.
75 template <typename Label>
76 bool WriteLabelTriples(const std::string &source,
77  const std::vector<std::pair<Label, Label>> &pairs,
78  const std::vector<Label> &assignments) {
79  if (pairs.size() != assignments.size()) {
80  LOG(ERROR) << "WriteLabelTriples: Pairs and assignments of different sizes";
81  return false;
82  }
83  std::ofstream fstrm(source);
84  if (!fstrm) {
85  LOG(ERROR) << "WriteLabelTriples: Can't open file: " << source;
86  return false;
87  }
88  for (size_t n = 0; n < pairs.size(); ++n)
89  fstrm << pairs[n].first << "\t" << pairs[n].second << "\t" << assignments[n]
90  << "\n";
91  if (!fstrm) {
92  LOG(ERROR) << "WriteLabelTriples: Write failed: "
93  << (source.empty() ? "standard output" : source);
94  return false;
95  }
96  return true;
97 }
98 
99 } // namespace fst
100 
101 #endif // FST_EXTENSIONS_MPDT_READ_WRITE_UTILS_H_
bool ReadLabelTriples(const std::string &source, std::vector< std::pair< Label, Label >> *pairs, std::vector< Label > *assignments, bool allow_negative=false)
#define LOG(type)
Definition: log.h:49
constexpr int kLineLen
Definition: symbol-table.h:63
internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim)
Definition: compat.cc:81
bool WriteLabelTriples(const std::string &source, const std::vector< std::pair< Label, Label >> &pairs, const std::vector< Label > &assignments)
int64_t StrToInt64(std::string_view s, std::string_view source, size_t nline, bool allow_negative, bool *error=nullptr)
Definition: util.cc:58