FST  openfst-1.8.3
OpenFst Library
weight.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 #include <fst/weight.h>
16 
17 #include <cctype>
18 #include <cstdio>
19 #include <ios>
20 #include <istream>
21 #include <ostream>
22 #include <string>
23 #include <utility>
24 
25 #include <fst/flags.h>
26 #include <fst/log.h>
27 #include <fst/util.h>
28 
29 DEFINE_string(fst_weight_separator, ",",
30  "Character separator between printed composite weights; "
31  "must be a single character");
32 
33 DEFINE_string(fst_weight_parentheses, "",
34  "Characters enclosing the first weight of a printed composite "
35  "weight (e.g., pair weight, tuple weight and derived classes) to "
36  "ensure proper I/O of nested composite weights; "
37  "must have size 0 (none) or 2 (open and close parenthesis)");
38 
39 namespace fst {
40 
41 namespace internal {
42 
44  std::pair<char, char> parentheses)
45  : separator_(separator),
46  open_paren_(parentheses.first),
47  close_paren_(parentheses.second),
48  error_(false) {
49  if ((open_paren_ == 0 || close_paren_ == 0) && open_paren_ != close_paren_) {
50  FSTERROR() << "Invalid configuration of weight parentheses: "
51  << static_cast<int>(open_paren_) << " "
52  << static_cast<int>(close_paren_);
53  error_ = true;
54  }
55 }
56 
59  FST_FLAGS_fst_weight_separator.empty()
60  ? 0
61  : FST_FLAGS_fst_weight_separator.front(),
62  {FST_FLAGS_fst_weight_parentheses.empty()
63  ? 0
64  : FST_FLAGS_fst_weight_parentheses[0],
65  FST_FLAGS_fst_weight_parentheses.size() < 2
66  ? 0
67  : FST_FLAGS_fst_weight_parentheses[1]}) {
68  if (FST_FLAGS_fst_weight_separator.size() != 1) {
69  FSTERROR() << "CompositeWeight: "
70  << "FST_FLAGS_fst_weight_separator.size() is not equal to 1";
71  error_ = true;
72  }
73  if (!FST_FLAGS_fst_weight_parentheses.empty() &&
74  FST_FLAGS_fst_weight_parentheses.size() != 2) {
75  FSTERROR() << "CompositeWeight: "
76  << "FST_FLAGS_fst_weight_parentheses.size() is not equal to 2";
77  error_ = true;
78  }
79 }
80 
81 } // namespace internal
82 
84  : ostrm_(ostrm) {
85  if (error()) ostrm.clear(std::ios::badbit);
86 }
87 
89  char separator,
90  std::pair<char, char> parentheses)
91  : internal::CompositeWeightIO(separator, parentheses), ostrm_(ostrm) {
92  if (error()) ostrm_.clear(std::ios::badbit);
93 }
94 
96  if (open_paren_ != 0) {
97  ostrm_ << open_paren_;
98  }
99 }
100 
102  if (close_paren_ != 0) {
103  ostrm_ << close_paren_;
104  }
105 }
106 
108  : istrm_(istrm) {
109  if (error()) istrm_.clear(std::ios::badbit);
110 }
111 
113  char separator,
114  std::pair<char, char> parentheses)
115  : internal::CompositeWeightIO(separator, parentheses), istrm_(istrm) {
116  if (error()) istrm_.clear(std::ios::badbit);
117 }
118 
120  do { // Skips whitespace.
121  c_ = istrm_.get();
122  } while (std::isspace(c_));
123  if (open_paren_ != 0) {
124  if (c_ != open_paren_) {
125  FSTERROR() << "CompositeWeightReader: Open paren missing: "
126  << "fst_weight_parentheses flag set correcty?";
127  istrm_.clear(std::ios::badbit);
128  return;
129  }
130  ++depth_;
131  c_ = istrm_.get();
132  }
133 }
134 
136  if (c_ != EOF && !std::isspace(c_)) {
137  FSTERROR() << "CompositeWeightReader: excess character: '"
138  << static_cast<char>(c_)
139  << "': fst_weight_parentheses flag set correcty?";
140  istrm_.clear(std::ios::badbit);
141  }
142 }
143 
144 } // namespace fst
#define FSTERROR()
Definition: util.h:56
std::pair< char, char > parentheses() const
Definition: weight.h:279
DEFINE_string(fst_weight_separator,",","Character separator between printed composite weights; ""must be a single character")
CompositeWeightReader(std::istream &istrm)
Definition: weight.cc:107
CompositeWeightWriter(std::ostream &ostrm)
Definition: weight.cc:83