8 #include "../HfstExceptionDefs.h"
12 namespace implementations {
14 struct string_comparison {
15 bool operator() (
const std::string &str1,
const std::string &str2)
const {
16 return (str1.compare(str2) < 0);
43 typedef std::vector<SymbolType>
45 typedef std::map<SymbolType, unsigned int, string_comparison>
66 static Number2SymbolVector number2symbol_map;
67 static Symbol2NumberMap symbol2number_map;
69 static unsigned int max_number;
72 static unsigned int get_max_number() {
93 static std::vector<unsigned int> get_harmonization_vector
94 (
const std::vector<SymbolType> &symbols)
96 std::vector<unsigned int> harmv;
97 harmv.reserve(symbols.size());
98 harmv.resize(symbols.size(), 0);
99 for (
unsigned int i=0; i<symbols.size(); i++)
101 if (symbols.at(i) !=
"")
102 harmv.at(i) = get_number(symbols.at(i));
107 static std::vector<unsigned int> get_reverse_harmonization_vector
108 (
const std::map<SymbolType, unsigned int> &symbols)
110 std::vector<unsigned int> harmv;
111 harmv.reserve(max_number+1);
112 harmv.resize(max_number+1, 0);
113 for (
unsigned int i=0; i<harmv.size(); i++)
115 std::map<SymbolType, unsigned int>::const_iterator it
116 = symbols.find(get_symbol(i));
117 if (it != symbols.end())
118 harmv.at(i) = it->second;
125 static const std::string &get_symbol(
unsigned int number)
127 if (number >= number2symbol_map.size()) {
128 std::string message(
"HfstTropicalTransducerTransitionData: "
130 std::ostringstream oss;
132 message.append(oss.str());
133 message.append(
" is not mapped to any symbol");
137 return number2symbol_map[number];
141 static unsigned int get_number(
const std::string &symbol)
144 Symbol2NumberMap::iterator it = symbol2number_map.find(symbol);
145 if (it == symbol2number_map.end()) {
146 std::cerr <<
"ERROR: No number for the empty symbol\n"
150 std::cerr <<
"ERROR: The empty symbol corresdponds to number "
151 << it->second << std::endl;
156 Symbol2NumberMap::iterator it = symbol2number_map.find(symbol);
157 if (it == symbol2number_map.end())
160 symbol2number_map[symbol] = max_number;
161 number2symbol_map.push_back(symbol);
170 unsigned int input_number;
171 unsigned int output_number;
175 void print_transition_data()
177 fprintf(stderr,
"%i:%i %f\n",
178 input_number, output_number, weight);
186 input_number(0), output_number(0), weight(0) {}
192 input_number = data.input_number;
193 output_number = data.output_number;
194 weight = data.weight;
203 if (isymbol ==
"" || osymbol ==
"")
205 (EmptyStringException,
206 "HfstTropicalTransducerTransitionData"
207 "(SymbolType, SymbolType, WeightType)");
209 input_number = get_number(isymbol);
210 output_number = get_number(osymbol);
211 this->weight = weight;
215 (
unsigned int inumber,
216 unsigned int onumber,
218 input_number = inumber;
219 output_number = onumber;
220 this->weight = weight;
225 return get_symbol(input_number);
230 return get_symbol(output_number);
233 unsigned int get_input_number()
const {
237 unsigned int get_output_number()
const {
238 return output_number;
247 static bool is_epsilon(
const SymbolType &symbol) {
248 return (symbol.compare(
"@_EPSILON_SYMBOL_@") == 0);
250 static bool is_unknown(
const SymbolType &symbol) {
251 return (symbol.compare(
"@_UNKNOWN_SYMBOL_@") == 0);
253 static bool is_identity(
const SymbolType &symbol) {
254 return (symbol.compare(
"@_IDENTITY_SYMBOL_@") == 0);
256 static bool is_valid_symbol(
const SymbolType &symbol) {
274 if (input_number < another.input_number )
276 if (input_number > another.input_number)
278 if (output_number < another.output_number)
280 if (output_number > another.output_number)
282 return (weight < another.weight);
287 input_number = another.input_number;
288 output_number = another.output_number;
289 weight = another.weight;
292 friend class Number2SymbolVectorInitializer;
293 friend class Symbol2NumberMapInitializer;
295 friend class ComposeIntersectFst;
296 friend class ComposeIntersectLexicon;
297 friend class ComposeIntersectRule;
298 friend class ComposeIntersectRulePair;
299 template <
class C>
friend class HfstTransitionGraph;
305 class Number2SymbolVectorInitializer {
307 Number2SymbolVectorInitializer
308 (HfstTropicalTransducerTransitionData::Number2SymbolVector &vect) {
309 vect.push_back(std::string(
"@_EPSILON_SYMBOL_@"));
310 vect.push_back(std::string(
"@_UNKNOWN_SYMBOL_@"));
311 vect.push_back(std::string(
"@_IDENTITY_SYMBOL_@"));
315 class Symbol2NumberMapInitializer {
317 Symbol2NumberMapInitializer
318 (HfstTropicalTransducerTransitionData::Symbol2NumberMap &map) {
319 map[
"@_EPSILON_SYMBOL_@"] = 0;
320 map[
"@_UNKNOWN_SYMBOL_@"] = 1;
321 map[
"@_IDENTITY_SYMBOL_@"] = 2;
bool operator<(const HfstTropicalTransducerTransitionData &another) const
Whether this transition is less than transition another.
Definition: HfstTropicalTransducerTransitionData.h:272
HfstTropicalTransducerTransitionData()
Create a HfstTropicalTransducerTransitionData with epsilon input and output strings and weight zero...
Definition: HfstTropicalTransducerTransitionData.h:185
std::set< SymbolType > SymbolTypeSet
A set of symbols.
Definition: HfstTropicalTransducerTransitionData.h:41
float WeightType
The weight type.
Definition: HfstTropicalTransducerTransitionData.h:39
HfstTropicalTransducerTransitionData(SymbolType isymbol, SymbolType osymbol, WeightType weight)
Create a HfstTropicalTransducerTransitionData with input symbol isymbol, output symbol osymbol and we...
Definition: HfstTropicalTransducerTransitionData.h:200
std::string SymbolType
The input and output symbol type.
Definition: HfstTropicalTransducerTransitionData.h:37
An error happened probably due to a bug in the HFST code.
Definition: HfstExceptionDefs.h:378
#define HFST_THROW_MESSAGE(E, M)
Macro to throw an exception of type E with message M. Use THROW instead of regular throw with subclas...
Definition: HfstExceptionDefs.h:35
const SymbolType & get_input_symbol() const
Get the input symbol.
Definition: HfstTropicalTransducerTransitionData.h:224
One implementation of template class C in HfstTransition.
Definition: HfstTropicalTransducerTransitionData.h:34
const SymbolType & get_output_symbol() const
Get the output symbol.
Definition: HfstTropicalTransducerTransitionData.h:229
WeightType get_weight() const
Get the weight.
Definition: HfstTropicalTransducerTransitionData.h:242