17 #include "../HfstExceptionDefs.h"
19 #include "../hfstdll.h"
23 namespace implementations {
25 struct string_comparison {
26 bool operator() (
const std::string &str1,
const std::string &str2)
const {
27 return (str1.compare(str2) < 0);
54 typedef std::vector<SymbolType>
56 typedef std::map<SymbolType, unsigned int, string_comparison>
77 HFSTDLL
static Number2SymbolVector number2symbol_map;
78 HFSTDLL
static Symbol2NumberMap symbol2number_map;
80 HFSTDLL
static unsigned int max_number;
83 HFSTDLL
static unsigned int get_max_number() {
104 static std::vector<unsigned int> get_harmonization_vector
105 (
const std::vector<SymbolType> &symbols)
107 std::vector<unsigned int> harmv;
108 harmv.reserve(symbols.size());
109 harmv.resize(symbols.size(), 0);
110 for (
unsigned int i=0; i<symbols.size(); i++)
112 if (symbols.at(i) !=
"")
113 harmv.at(i) = get_number(symbols.at(i));
118 static std::vector<unsigned int> get_reverse_harmonization_vector
119 (
const std::map<SymbolType, unsigned int> &symbols)
121 std::vector<unsigned int> harmv;
122 harmv.reserve(max_number+1);
123 harmv.resize(max_number+1, 0);
124 for (
unsigned int i=0; i<harmv.size(); i++)
126 std::map<SymbolType, unsigned int>::const_iterator it
127 = symbols.find(get_symbol(i));
128 if (it != symbols.end())
129 harmv.at(i) = it->second;
136 static const std::string &get_symbol(
unsigned int number)
138 if (number >= number2symbol_map.size()) {
139 std::string message(
"HfstTropicalTransducerTransitionData: "
141 std::ostringstream oss;
143 message.append(oss.str());
144 message.append(
" is not mapped to any symbol");
148 return number2symbol_map[number];
152 static unsigned int get_number(
const std::string &symbol)
155 Symbol2NumberMap::iterator it = symbol2number_map.find(symbol);
156 if (it == symbol2number_map.end()) {
157 std::cerr <<
"ERROR: No number for the empty symbol\n"
161 std::cerr <<
"ERROR: The empty symbol corresdponds to number "
162 << it->second << std::endl;
167 Symbol2NumberMap::iterator it = symbol2number_map.find(symbol);
168 if (it == symbol2number_map.end())
171 symbol2number_map[symbol] = max_number;
172 number2symbol_map.push_back(symbol);
181 unsigned int input_number;
182 unsigned int output_number;
186 HFSTDLL
void print_transition_data()
188 fprintf(stderr,
"%i:%i %f\n",
189 input_number, output_number, weight);
197 input_number(0), output_number(0), weight(0) {}
203 input_number = data.input_number;
204 output_number = data.output_number;
205 weight = data.weight;
214 if (isymbol ==
"" || osymbol ==
"")
216 (EmptyStringException,
217 "HfstTropicalTransducerTransitionData"
218 "(SymbolType, SymbolType, WeightType)");
220 input_number = get_number(isymbol);
221 output_number = get_number(osymbol);
222 this->weight = weight;
226 (
unsigned int inumber,
227 unsigned int onumber,
229 input_number = inumber;
230 output_number = onumber;
231 this->weight = weight;
236 return get_symbol(input_number);
241 return get_symbol(output_number);
244 HFSTDLL
unsigned int get_input_number()
const {
248 HFSTDLL
unsigned int get_output_number()
const {
249 return output_number;
264 HFSTDLL
static bool is_epsilon(
const SymbolType &symbol) {
265 return (symbol.compare(
"@_EPSILON_SYMBOL_@") == 0);
267 HFSTDLL
static bool is_unknown(
const SymbolType &symbol) {
268 return (symbol.compare(
"@_UNKNOWN_SYMBOL_@") == 0);
270 HFSTDLL
static bool is_identity(
const SymbolType &symbol) {
271 return (symbol.compare(
"@_IDENTITY_SYMBOL_@") == 0);
273 HFSTDLL
static bool is_valid_symbol(
const SymbolType &symbol) {
291 if (input_number < another.input_number )
293 if (input_number > another.input_number)
295 if (output_number < another.output_number)
297 if (output_number > another.output_number)
299 return (weight < another.weight);
305 if (input_number < another.input_number )
307 if (input_number > another.input_number)
309 if (output_number < another.output_number)
311 if (output_number > another.output_number)
318 input_number = another.input_number;
319 output_number = another.output_number;
320 weight = another.weight;
323 friend class Number2SymbolVectorInitializer;
324 friend class Symbol2NumberMapInitializer;
326 friend class ComposeIntersectFst;
327 friend class ComposeIntersectLexicon;
328 friend class ComposeIntersectRule;
329 friend class ComposeIntersectRulePair;
330 template <
class C>
friend class HfstTransitionGraph;
336 class Number2SymbolVectorInitializer {
338 HFSTDLL Number2SymbolVectorInitializer
339 (HfstTropicalTransducerTransitionData::Number2SymbolVector &vect) {
340 vect.push_back(std::string(
"@_EPSILON_SYMBOL_@"));
341 vect.push_back(std::string(
"@_UNKNOWN_SYMBOL_@"));
342 vect.push_back(std::string(
"@_IDENTITY_SYMBOL_@"));
346 class Symbol2NumberMapInitializer {
348 HFSTDLL Symbol2NumberMapInitializer
349 (HfstTropicalTransducerTransitionData::Symbol2NumberMap &map) {
350 map[
"@_EPSILON_SYMBOL_@"] = 0;
351 map[
"@_UNKNOWN_SYMBOL_@"] = 1;
352 map[
"@_IDENTITY_SYMBOL_@"] = 2;
HFSTDLL const SymbolType & get_input_symbol() const
Get the input symbol.
Definition: HfstTropicalTransducerTransitionData.h:235
std::set< SymbolType > SymbolTypeSet
A set of symbols.
Definition: HfstTropicalTransducerTransitionData.h:52
float WeightType
The weight type.
Definition: HfstTropicalTransducerTransitionData.h:50
std::string SymbolType
The input and output symbol type.
Definition: HfstTropicalTransducerTransitionData.h:48
HFSTDLL bool operator<(const HfstTropicalTransducerTransitionData &another) const
Whether this transition is less than transition another.
Definition: HfstTropicalTransducerTransitionData.h:289
HFSTDLL HfstTropicalTransducerTransitionData()
Create a HfstTropicalTransducerTransitionData with epsilon input and output strings and weight zero...
Definition: HfstTropicalTransducerTransitionData.h:196
HFSTDLL void set_weight(WeightType w)
Set the weight.
Definition: HfstTropicalTransducerTransitionData.h:258
HFSTDLL HfstTropicalTransducerTransitionData(SymbolType isymbol, SymbolType osymbol, WeightType weight)
Create a HfstTropicalTransducerTransitionData with input symbol isymbol, output symbol osymbol and we...
Definition: HfstTropicalTransducerTransitionData.h:211
An error happened probably due to a bug in the HFST code.
Definition: HfstExceptionDefs.h:390
HFSTDLL const SymbolType & get_output_symbol() const
Get the output symbol.
Definition: HfstTropicalTransducerTransitionData.h:240
#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:47
One implementation of template class C in HfstTransition.
Definition: HfstTropicalTransducerTransitionData.h:45
HFSTDLL WeightType get_weight() const
Get the weight.
Definition: HfstTropicalTransducerTransitionData.h:253