1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// arc.h 2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Licensed under the Apache License, Version 2.0 (the "License"); 4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// you may not use this file except in compliance with the License. 5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// You may obtain a copy of the License at 6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// 7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// http://www.apache.org/licenses/LICENSE-2.0 8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// 9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Unless required by applicable law or agreed to in writing, software 10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// distributed under the License is distributed on an "AS IS" BASIS, 11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// See the License for the specific language governing permissions and 13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// limitations under the License. 14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// 15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Copyright 2005-2010 Google, Inc. 16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Author: riley@google.com (Michael Riley) 17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// 18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// \file 19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// 20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Commonly used Fst arc types. 21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#ifndef FST_LIB_ARC_H__ 23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define FST_LIB_ARC_H__ 24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <string> 26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/expectation-weight.h> 29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/float-weight.h> 30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/lexicographic-weight.h> 31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/power-weight.h> 32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/product-weight.h> 33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/signed-log-weight.h> 34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/sparse-power-weight.h> 35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <iostream> 36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fstream> 37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/string-weight.h> 38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonnamespace fst { 41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class W> 43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass ArcTpl { 44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public: 45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef W Weight; 46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ArcTpl(Label i, Label o, const Weight& w, StateId s) 50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ArcTpl() {} 53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type(void) { 55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = 56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (Weight::Type() == "tropical") ? "standard" : Weight::Type(); 57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; 61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; 62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; 63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; 64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 66f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<TropicalWeight> StdArc; 67f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<LogWeight> LogArc; 68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<Log64Weight> Log64Arc; 69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<SignedLogWeight> SignedLogArc; 70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<SignedLog64Weight> SignedLog64Arc; 71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<MinMaxWeight> MinMaxArc; 72f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with integer labels and state Ids and string weights. 75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <StringType S = STRING_LEFT> 76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass StringArc { 77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public: 78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef StringWeight<int, S> Weight; 80f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 81f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 82f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StringArc(Label i, Label o, Weight w, StateId s) 83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 84f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StringArc() {} 86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = 89f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson S == STRING_LEFT ? "standard_string" : 90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_RIGHT ? "right_standard_string" : 91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_LEFT_RESTRICT ? "restricted_string" : 92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson "right_restricted_string")); 93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 98f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 99f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 100f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 101f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 102f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 103f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as template arg and with 104f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// weights over the Gallic semiring w.r.t the output labels and weights of A. 105f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, StringType S = STRING_LEFT> 106f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct GallicArc { 107f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 108f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 109f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 110f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef GallicWeight<Label, typename A::Weight, S> Weight; 111f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 112f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson GallicArc() {} 113f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 114f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson GallicArc(Label i, Label o, Weight w, StateId s) 115f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 116f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 117f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson GallicArc(const A &arc) 118f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(arc.ilabel), olabel(arc.ilabel), 119f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson weight(arc.olabel, arc.weight), nextstate(arc.nextstate) {} 120f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 121f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 122f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = 123f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_LEFT ? "gallic_" : 124f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_RIGHT ? "right_gallic_" : 125f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_LEFT_RESTRICT ? "restricted_gallic_" : 126f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson "right_restricted_gallic_"))) + A::Type(); 127f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 128f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 129f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 130f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 131f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 132f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 133f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 134f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 135f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 136f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 137f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with the reverse of the weight found in its template arg. 138f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A> struct ReverseArc { 139f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 140f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 141f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Weight AWeight; 142f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename AWeight::ReverseWeight Weight; 143f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 144f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 145f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ReverseArc(Label i, Label o, Weight w, StateId s) 146f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 147f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 148f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ReverseArc() {} 149f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 150f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 151f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = "reverse_" + Arc::Type(); 152f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 153f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 154f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 155f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 156f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 157f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 158f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 159f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 160f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 161f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 162f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with integer labels and state Ids and lexicographic weights. 163f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate<class W1, class W2> 164f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct LexicographicArc { 165f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 166f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef LexicographicWeight<W1, W2> Weight; 167f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 168f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 169f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson LexicographicArc(Label i, Label o, Weight w, StateId s) 170f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 171f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 172f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson LexicographicArc() {} 173f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 174f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 175f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = Weight::Type(); 176f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 177f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 178f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 179f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 180f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 181f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 182f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 183f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 184f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 185f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 186f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with integer labels and state Ids and product weights. 187f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate<class W1, class W2> 188f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct ProductArc { 189f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 190f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef ProductWeight<W1, W2> Weight; 191f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 192f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 193f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductArc(Label i, Label o, Weight w, StateId s) 194f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 195f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 196f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductArc() {} 197f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 198f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 199f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = Weight::Type(); 200f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 201f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 202f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 203f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 204f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 205f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 206f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 207f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 208f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 209f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 210f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as first template arg and with 211f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// weights over the n-th cartesian power of the weight type of the 212f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// template arg. 213f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, unsigned int n> 214f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct PowerArc { 215f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 216f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 217f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 218f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef PowerWeight<typename A::Weight, n> Weight; 219f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 220f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson PowerArc() {} 221f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 222f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson PowerArc(Label i, Label o, Weight w, StateId s) 223f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 224f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 225f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 226f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static string type; 227f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if (type.empty()) { 228f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson string power; 229f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Int64ToStr(n, &power); 230f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson type = A::Type() + "_^" + power; 231f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 232f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 233f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 234f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 235f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 236f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 237f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 238f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 239f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 240f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 241f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 242f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as first template arg and with 243f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// weights over the arbitrary cartesian power of the weight type. 244f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, class K = int> 245f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct SparsePowerArc { 246f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 247f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 248f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 249f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef SparsePowerWeight<typename A::Weight, K> Weight; 250f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 251f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson SparsePowerArc() {} 252f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 253f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson SparsePowerArc(Label i, Label o, Weight w, StateId s) 254f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 255f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 256f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 257f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static string type; 258f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if (type.empty()) { type = A::Type() + "_^n"; } 259f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if(sizeof(K) != sizeof(uint32)) { 260f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson string size; 261f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Int64ToStr(8 * sizeof(K), &size); 262f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson type += "_" + size; 263f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 264f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 265f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 266f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 267f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 268f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 269f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 270f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 271f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 272f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 273f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 274f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as first template arg and with 275f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// expectation weight over the first template arg weight type and the 276f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// second template arg. 277f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, class X2> 278f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct ExpectationArc { 279f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 280f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 281f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 282f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Weight X1; 283f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef ExpectationWeight<X1, X2> Weight; 284f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 285f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ExpectationArc() {} 286f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 287f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ExpectationArc(Label i, Label o, Weight w, StateId s) 288f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 289f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 290f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 291f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static string type; 292f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if (type.empty()) { 293f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson type = "expectation_" + A::Type() + "_" + X2::Type(); 294f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 295f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 296f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 297f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 298f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 299f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 300f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 301f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 302f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 303f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 304f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson} // namespace fst 305f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 306f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#endif // FST_LIB_ARC_H__ 307