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