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