• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // arc.h
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 //
16 // \file
17 // Commonly used Fst arc types.
18 
19 #ifndef FST_LIB_ARC_H__
20 #define FST_LIB_ARC_H__
21 
22 #include "fst/lib/float-weight.h"
23 #include "fst/lib/product-weight.h"
24 #include "fst/lib/string-weight.h"
25 
26 namespace fst {
27 
28 // Arc with integer labels and state Ids and float weights over the
29 // tropical semiring.
30 struct StdArc {
31   typedef int Label;
32   typedef TropicalWeight Weight;
33   typedef int StateId;
34 
StdArcStdArc35   StdArc(Label i, Label o, Weight w, StateId s)
36       : ilabel(i), olabel(o), weight(w), nextstate(s) {}
37 
StdArcStdArc38   StdArc() {}
39 
TypeStdArc40   static const string &Type() {  // Arc type name
41     static const string type = "standard";
42     return type;
43   }
44 
45   Label ilabel;       // Transition input label
46   Label olabel;       // Transition output label
47   Weight weight;      // Transition weight
48   StateId nextstate;  // Transition destination state
49 };
50 
51 
52 // Arc with integer labels and state Ids and float weights over the
53 // log semiring.
54 struct LogArc {
55   typedef int Label;
56   typedef LogWeight Weight;
57   typedef int StateId;
58 
LogArcLogArc59   LogArc(Label i, Label o, Weight w, StateId s)
60       : ilabel(i), olabel(o), weight(w), nextstate(s) {}
61 
LogArcLogArc62   LogArc() {}
63 
TypeLogArc64   static const string &Type() {  // Arc type name
65     static const string type = "log";
66     return type;
67   }
68 
69   Label ilabel;       // Transition input label
70   Label olabel;       // Transition output label
71   Weight weight;      // Transition weight
72   StateId nextstate;  // Transition destination state
73 };
74 
75 
76 // Arc with integer labels and state Ids and string weights.
77 template <StringType S = STRING_LEFT>
78 class StringArc {
79  public:
80   typedef int Label;
81   typedef StringWeight<int, S> Weight;
82   typedef int StateId;
83 
StringArc(Label i,Label o,Weight w,StateId s)84   StringArc(Label i, Label o, Weight w, StateId s)
85       : ilabel(i), olabel(o), weight(w), nextstate(s) {}
86 
StringArc()87   StringArc() {}
88 
Type()89   static const string &Type() {  // Arc type name
90     static const string type =
91         S == STRING_LEFT ? "standard_string" :
92         (S == STRING_RIGHT ? "right_standard_string" :
93          (S == STRING_LEFT_RESTRICT ? "restricted_string" :
94           "right_restricted_string"));
95     return type;
96   }
97 
98   Label ilabel;       // Transition input label
99   Label olabel;       // Transition output label
100   Weight weight;      // Transition weight
101   StateId nextstate;  // Transition destination state
102 };
103 
104 
105 // Arc with label and state Id type the same as template arg and with
106 // weights over the Gallic semiring w.r.t the output labels and weights of A.
107 template <class A, StringType S = STRING_LEFT>
108 struct GallicArc {
109   typedef A Arc;
110   typedef typename A::Label Label;
111   typedef typename A::StateId StateId;
112   typedef GallicWeight<Label, typename A::Weight, S> Weight;
113 
GallicArcGallicArc114   GallicArc() {}
115 
GallicArcGallicArc116   GallicArc(Label i, Label o, Weight w, StateId s)
117       : ilabel(i), olabel(o), weight(w), nextstate(s) {}
118 
GallicArcGallicArc119   GallicArc(const A &arc)
120       : ilabel(arc.ilabel), olabel(arc.ilabel),
121         weight(arc.olabel, arc.weight), nextstate(arc.nextstate) {}
122 
TypeGallicArc123   static const string &Type() {  // Arc type name
124     static const string type =
125         (S == STRING_LEFT ? "gallic_" :
126          (S == STRING_RIGHT ? "right_gallic_" :
127           (S == STRING_LEFT_RESTRICT ? "restricted_gallic_" :
128            "right_restricted_gallic_"))) + A::Type();
129     return type;
130   }
131 
132   Label ilabel;       // Transition input label
133   Label olabel;       // Transition output label
134   Weight weight;      // Transition weight
135   StateId nextstate;  // Transition destination state
136 };
137 
138 
139 // Arc with the reverse of the weight found in its template arg.
140 template <class A> struct ReverseArc {
141   typedef A Arc;
142   typedef typename A::Label Label;
143   typedef typename A::Weight AWeight;
144   typedef typename AWeight::ReverseWeight Weight;
145   typedef typename A::StateId StateId;
146 
ReverseArcReverseArc147   ReverseArc(Label i, Label o, Weight w, StateId s)
148       : ilabel(i), olabel(o), weight(w), nextstate(s) {}
149 
ReverseArcReverseArc150   ReverseArc() {}
151 
TypeReverseArc152   static const string &Type() {  // Arc type name
153     static const string type = "reverse_" + Arc::Type();
154     return type;
155   }
156 
157   Label ilabel;       // Transition input label
158   Label olabel;       // Transition output label
159   Weight weight;      // Transition weight
160   StateId nextstate;  // Transition destination state
161 };
162 
163 }  // namespace fst;
164 
165 #endif  // FST_LIB_ARC_H__
166