FST  openfst-1.8.2.post1
OpenFst Library
text-io.cc
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 #include <fst/script/text-io.h>
19 
20 #include <cstring>
21 #include <ostream>
22 #include <sstream>
23 #include <string>
24 #include <utility>
25 
26 #include <fst/log.h>
27 #include <fstream>
28 #include <fst/util.h>
29 #include <fst/windows_defs.inc>
30 #include <string_view>
31 
32 namespace fst {
33 namespace script {
34 
35 // Reads vector of weights; returns true on success.
36 bool ReadPotentials(std::string_view weight_type, const std::string &source,
37  std::vector<WeightClass> *potentials) {
38  std::ifstream istrm(source);
39  if (!istrm) {
40  LOG(ERROR) << "ReadPotentials: Can't open file: " << source;
41  return false;
42  }
43  static constexpr int kLineLen = 8096;
44  char line[kLineLen];
45  size_t nline = 0;
46  potentials->clear();
47  while (!istrm.getline(line, kLineLen).fail()) {
48  ++nline;
49  std::vector<std::string_view> col =
50  StrSplit(line, ByAnyChar("\n\t "), SkipEmpty());
51  if (col.empty() || col[0].empty()) continue;
52  if (col.size() != 2) {
53  FSTERROR() << "ReadPotentials: Bad number of columns, "
54  << "file = " << source << ", line = " << nline;
55  return false;
56  }
57  const ssize_t s = StrToInt64(col[0], source, nline, false);
58  const WeightClass weight(weight_type, col[1]);
59  while (potentials->size() <= s) {
60  potentials->push_back(WeightClass::Zero(weight_type));
61  }
62  potentials->back() = weight;
63  }
64  return true;
65 }
66 
67 // Writes vector of weights; returns true on success.
68 bool WritePotentials(const std::string &source,
69  const std::vector<WeightClass> &potentials) {
70  std::ofstream ostrm;
71  if (!source.empty()) {
72  ostrm.open(source);
73  if (!ostrm) {
74  LOG(ERROR) << "WritePotentials: Can't open file: " << source;
75  return false;
76  }
77  }
78  std::ostream &strm = ostrm.is_open() ? ostrm : std::cout;
79  strm.precision(9);
80  for (size_t s = 0; s < potentials.size(); ++s) {
81  strm << s << "\t" << potentials[s] << "\n";
82  }
83  if (strm.fail()) {
84  LOG(ERROR) << "WritePotentials: Write failed: "
85  << (source.empty() ? "standard output" : source);
86  return false;
87  }
88  return true;
89 }
90 
91 } // namespace script
92 } // namespace fst
bool WritePotentials(const std::string &source, const std::vector< WeightClass > &potentials)
Definition: text-io.cc:68
#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
#define FSTERROR()
Definition: util.h:53
static WeightClass Zero(std::string_view weight_type)
Definition: weight-class.cc:41
bool ReadPotentials(std::string_view weight_type, const std::string &source, std::vector< WeightClass > *potentials)
Definition: text-io.cc:36
int64_t StrToInt64(std::string_view s, std::string_view source, size_t nline, bool allow_negative, bool *error=nullptr)
Definition: util.cc:58