00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FST_LIB_ARC_H__
00023 #define FST_LIB_ARC_H__
00024
00025 #include <string>
00026
00027 #include <fst/expectation-weight.h>
00028 #include <fst/float-weight.h>
00029 #include <fst/lexicographic-weight.h>
00030 #include <fst/power-weight.h>
00031 #include <fst/product-weight.h>
00032 #include <fst/signed-log-weight.h>
00033 #include <fst/sparse-power-weight.h>
00034 #include <iostream>
00035 #include <fstream>
00036 #include <fst/string-weight.h>
00037
00038 namespace fst {
00039
00040 template <class W>
00041 class ArcTpl {
00042 public:
00043 typedef W Weight;
00044 typedef int Label;
00045 typedef int StateId;
00046
00047 ArcTpl(Label i, Label o, const Weight& w, StateId s)
00048 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00049
00050 ArcTpl() {}
00051
00052 static const string &Type(void) {
00053 static const string type =
00054 (Weight::Type() == "tropical") ? "standard" : Weight::Type();
00055 return type;
00056 }
00057
00058 Label ilabel;
00059 Label olabel;
00060 Weight weight;
00061 StateId nextstate;
00062 };
00063
00064 typedef ArcTpl<TropicalWeight> StdArc;
00065 typedef ArcTpl<LogWeight> LogArc;
00066 typedef ArcTpl<SignedLogWeight> SignedLogArc;
00067 typedef ArcTpl<MinMaxWeight> MinMaxArc;
00068
00069
00070
00071 template <StringType S = STRING_LEFT>
00072 class StringArc {
00073 public:
00074 typedef int Label;
00075 typedef StringWeight<int, S> Weight;
00076 typedef int StateId;
00077
00078 StringArc(Label i, Label o, Weight w, StateId s)
00079 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00080
00081 StringArc() {}
00082
00083 static const string &Type() {
00084 static const string type =
00085 S == STRING_LEFT ? "standard_string" :
00086 (S == STRING_RIGHT ? "right_standard_string" :
00087 (S == STRING_LEFT_RESTRICT ? "restricted_string" :
00088 "right_restricted_string"));
00089 return type;
00090 }
00091
00092 Label ilabel;
00093 Label olabel;
00094 Weight weight;
00095 StateId nextstate;
00096 };
00097
00098
00099
00100
00101 template <class A, StringType S = STRING_LEFT>
00102 struct GallicArc {
00103 typedef A Arc;
00104 typedef typename A::Label Label;
00105 typedef typename A::StateId StateId;
00106 typedef GallicWeight<Label, typename A::Weight, S> Weight;
00107
00108 GallicArc() {}
00109
00110 GallicArc(Label i, Label o, Weight w, StateId s)
00111 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00112
00113 GallicArc(const A &arc)
00114 : ilabel(arc.ilabel), olabel(arc.ilabel),
00115 weight(arc.olabel, arc.weight), nextstate(arc.nextstate) {}
00116
00117 static const string &Type() {
00118 static const string type =
00119 (S == STRING_LEFT ? "gallic_" :
00120 (S == STRING_RIGHT ? "right_gallic_" :
00121 (S == STRING_LEFT_RESTRICT ? "restricted_gallic_" :
00122 "right_restricted_gallic_"))) + A::Type();
00123 return type;
00124 }
00125
00126 Label ilabel;
00127 Label olabel;
00128 Weight weight;
00129 StateId nextstate;
00130 };
00131
00132
00133
00134 template <class A> struct ReverseArc {
00135 typedef A Arc;
00136 typedef typename A::Label Label;
00137 typedef typename A::Weight AWeight;
00138 typedef typename AWeight::ReverseWeight Weight;
00139 typedef typename A::StateId StateId;
00140
00141 ReverseArc(Label i, Label o, Weight w, StateId s)
00142 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00143
00144 ReverseArc() {}
00145
00146 static const string &Type() {
00147 static const string type = "reverse_" + Arc::Type();
00148 return type;
00149 }
00150
00151 Label ilabel;
00152 Label olabel;
00153 Weight weight;
00154 StateId nextstate;
00155 };
00156
00157
00158
00159 template<class W1, class W2>
00160 struct LexicographicArc {
00161 typedef int Label;
00162 typedef LexicographicWeight<W1, W2> Weight;
00163 typedef int StateId;
00164
00165 LexicographicArc(Label i, Label o, Weight w, StateId s)
00166 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00167
00168 LexicographicArc() {}
00169
00170 static const string &Type() {
00171 static const string type = Weight::Type();
00172 return type;
00173 }
00174
00175 Label ilabel;
00176 Label olabel;
00177 Weight weight;
00178 StateId nextstate;
00179 };
00180
00181
00182
00183 template<class W1, class W2>
00184 struct ProductArc {
00185 typedef int Label;
00186 typedef ProductWeight<W1, W2> Weight;
00187 typedef int StateId;
00188
00189 ProductArc(Label i, Label o, Weight w, StateId s)
00190 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00191
00192 ProductArc() {}
00193
00194 static const string &Type() {
00195 static const string type = Weight::Type();
00196 return type;
00197 }
00198
00199 Label ilabel;
00200 Label olabel;
00201 Weight weight;
00202 StateId nextstate;
00203 };
00204
00205
00206
00207
00208
00209 template <class A, unsigned int n>
00210 struct PowerArc {
00211 typedef A Arc;
00212 typedef typename A::Label Label;
00213 typedef typename A::StateId StateId;
00214 typedef PowerWeight<typename A::Weight, n> Weight;
00215
00216 PowerArc() {}
00217
00218 PowerArc(Label i, Label o, Weight w, StateId s)
00219 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00220
00221 static const string &Type() {
00222 static string type;
00223 if (type.empty()) {
00224 string power;
00225 Int64ToStr(n, &power);
00226 type = A::Type() + "_^" + power;
00227 }
00228 return type;
00229 }
00230
00231 Label ilabel;
00232 Label olabel;
00233 Weight weight;
00234 StateId nextstate;
00235 };
00236
00237
00238
00239
00240 template <class A, class K = int>
00241 struct SparsePowerArc {
00242 typedef A Arc;
00243 typedef typename A::Label Label;
00244 typedef typename A::StateId StateId;
00245 typedef SparsePowerWeight<typename A::Weight, K> Weight;
00246
00247 SparsePowerArc() {}
00248
00249 SparsePowerArc(Label i, Label o, Weight w, StateId s)
00250 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00251
00252 static const string &Type() {
00253 static string type;
00254 if (type.empty()) { type = A::Type() + "_^n"; }
00255 if(sizeof(K) != sizeof(uint32)) {
00256 string size;
00257 Int64ToStr(8 * sizeof(K), &size);
00258 type += "_" + size;
00259 }
00260 return type;
00261 }
00262
00263 Label ilabel;
00264 Label olabel;
00265 Weight weight;
00266 StateId nextstate;
00267 };
00268
00269
00270
00271
00272
00273 template <class A, class X2>
00274 struct ExpectationArc {
00275 typedef A Arc;
00276 typedef typename A::Label Label;
00277 typedef typename A::StateId StateId;
00278 typedef typename A::Weight X1;
00279 typedef ExpectationWeight<X1, X2> Weight;
00280
00281 ExpectationArc() {}
00282
00283 ExpectationArc(Label i, Label o, Weight w, StateId s)
00284 : ilabel(i), olabel(o), weight(w), nextstate(s) {}
00285
00286 static const string &Type() {
00287 static string type;
00288 if (type.empty()) {
00289 type = "expectation_" + A::Type() + "_" + X2::Type();
00290 }
00291 return type;
00292 }
00293
00294 Label ilabel;
00295 Label olabel;
00296 Weight weight;
00297 StateId nextstate;
00298 };
00299
00300 }
00301
00302 #endif /// FST_LIB_ARC_H__
00303