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