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> 37dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin#include <sstream> 38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/string-weight.h> 39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonnamespace fst { 42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class W> 44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass ArcTpl { 45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public: 46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef W Weight; 47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ArcTpl(Label i, Label o, const Weight& w, StateId s) 51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ArcTpl() {} 54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type(void) { 56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = 57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (Weight::Type() == "tropical") ? "standard" : Weight::Type(); 58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; 62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; 63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; 64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; 65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 66f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 67f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<TropicalWeight> StdArc; 68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<LogWeight> LogArc; 69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<Log64Weight> Log64Arc; 70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<SignedLogWeight> SignedLogArc; 71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<SignedLog64Weight> SignedLog64Arc; 72f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef ArcTpl<MinMaxWeight> MinMaxArc; 73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with integer labels and state Ids and string weights. 76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <StringType S = STRING_LEFT> 77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass StringArc { 78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public: 79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 80f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef StringWeight<int, S> Weight; 81f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 82f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StringArc(Label i, Label o, Weight w, StateId s) 84f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StringArc() {} 87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 89f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = 90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson S == STRING_LEFT ? "standard_string" : 91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_RIGHT ? "right_standard_string" : 92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_LEFT_RESTRICT ? "restricted_string" : 93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson "right_restricted_string")); 94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 98f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 99f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 100f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 101f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 102f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 103f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 104f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as template arg and with 105f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// weights over the Gallic semiring w.r.t the output labels and weights of A. 106f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, StringType S = STRING_LEFT> 107f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct GallicArc { 108f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 109f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 110f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 111f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef GallicWeight<Label, typename A::Weight, S> Weight; 112f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 113f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson GallicArc() {} 114f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 115f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson GallicArc(Label i, Label o, Weight w, StateId s) 116f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 117f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 118f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson GallicArc(const A &arc) 119f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(arc.ilabel), olabel(arc.ilabel), 120f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson weight(arc.olabel, arc.weight), nextstate(arc.nextstate) {} 121f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 122f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 123f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = 124f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_LEFT ? "gallic_" : 125f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_RIGHT ? "right_gallic_" : 126f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson (S == STRING_LEFT_RESTRICT ? "restricted_gallic_" : 127f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson "right_restricted_gallic_"))) + A::Type(); 128f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 129f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 130f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 131f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 132f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 133f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 134f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 135f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 136f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 137f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 138f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with the reverse of the weight found in its template arg. 139f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A> struct ReverseArc { 140f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 141f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 142f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Weight AWeight; 143f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename AWeight::ReverseWeight Weight; 144f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 145f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 146f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ReverseArc(Label i, Label o, Weight w, StateId s) 147f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 148f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 149f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ReverseArc() {} 150f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 151f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 152f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = "reverse_" + Arc::Type(); 153f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 154f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 155f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 156f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 157f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 158f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 159f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 160f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 161f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 162f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 163f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with integer labels and state Ids and lexicographic weights. 164f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate<class W1, class W2> 165f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct LexicographicArc { 166f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 167f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef LexicographicWeight<W1, W2> Weight; 168f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 169f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 170f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson LexicographicArc(Label i, Label o, Weight w, StateId s) 171f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 172f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 173f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson LexicographicArc() {} 174f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 175f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 176f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = Weight::Type(); 177f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 178f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 179f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 180f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 181f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 182f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 183f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 184f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 185f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 186f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 187f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with integer labels and state Ids and product weights. 188f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate<class W1, class W2> 189f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct ProductArc { 190f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int Label; 191f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef ProductWeight<W1, W2> Weight; 192f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef int StateId; 193f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 194f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductArc(Label i, Label o, Weight w, StateId s) 195f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 196f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 197f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductArc() {} 198f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 199f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 200f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = Weight::Type(); 201f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 202f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 203f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 204f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 205f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 206f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 207f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 208f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 209f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 210f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 211f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as first template arg and with 212f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// weights over the n-th cartesian power of the weight type of the 213f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// template arg. 214f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, unsigned int n> 215f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct PowerArc { 216f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 217f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 218f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 219f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef PowerWeight<typename A::Weight, n> Weight; 220f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 221f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson PowerArc() {} 222f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 223f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson PowerArc(Label i, Label o, Weight w, StateId s) 224f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 225f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 226f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 227f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static string type; 228f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if (type.empty()) { 229f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson string power; 230f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Int64ToStr(n, &power); 231f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson type = A::Type() + "_^" + power; 232f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 233f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 234f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 235f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 236f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 237f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 238f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 239f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 240f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 241f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 242f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 243f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as first template arg and with 244f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// weights over the arbitrary cartesian power of the weight type. 245f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, class K = int> 246f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct SparsePowerArc { 247f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 248f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 249f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 250f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef SparsePowerWeight<typename A::Weight, K> Weight; 251f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 252f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson SparsePowerArc() {} 253f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 254f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson SparsePowerArc(Label i, Label o, Weight w, StateId s) 255f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 256f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 257f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 258f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static string type; 259f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if (type.empty()) { type = A::Type() + "_^n"; } 260f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if(sizeof(K) != sizeof(uint32)) { 261f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson string size; 262f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Int64ToStr(8 * sizeof(K), &size); 263f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson type += "_" + size; 264f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 265f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 266f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 267f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 268f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 269f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 270f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 271f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 272f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 273f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 274f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 275f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc with label and state Id type the same as first template arg and with 276f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// expectation weight over the first template arg weight type and the 277f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// second template arg. 278f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class A, class X2> 279f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstruct ExpectationArc { 280f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef A Arc; 281f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Label Label; 282f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::StateId StateId; 283f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef typename A::Weight X1; 284f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef ExpectationWeight<X1, X2> Weight; 285f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 286f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ExpectationArc() {} 287f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 288f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ExpectationArc(Label i, Label o, Weight w, StateId s) 289f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson : ilabel(i), olabel(o), weight(w), nextstate(s) {} 290f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 291f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { // Arc type name 292f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static string type; 293f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson if (type.empty()) { 294f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson type = "expectation_" + A::Type() + "_" + X2::Type(); 295f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 296f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 297f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 298f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 299f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label ilabel; // Transition input label 300f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Label olabel; // Transition output label 301f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Weight weight; // Transition weight 302f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson StateId nextstate; // Transition destination state 303f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 304f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 305f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson} // namespace fst 306f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 307f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#endif // FST_LIB_ARC_H__ 308