1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// product-weight.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// Product weight set and associated semiring operation definitions. 20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#ifndef FST_LIB_PRODUCT_WEIGHT_H__ 22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define FST_LIB_PRODUCT_WEIGHT_H__ 23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <stack> 25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <string> 26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/pair-weight.h> 28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/weight.h> 29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonnamespace fst { 32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Product semiring: W1 * W2 34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate<class W1, class W2> 35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass ProductWeight : public PairWeight<W1, W2> { 36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public: 37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson using PairWeight<W1, W2>::Zero; 38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson using PairWeight<W1, W2>::One; 39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson using PairWeight<W1, W2>::NoWeight; 40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson using PairWeight<W1, W2>::Quantize; 41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson using PairWeight<W1, W2>::Reverse; 42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson typedef ProductWeight<typename W1::ReverseWeight, typename W2::ReverseWeight> 44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ReverseWeight; 45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductWeight() {} 47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductWeight(const PairWeight<W1, W2>& w) : PairWeight<W1, W2>(w) {} 49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductWeight(W1 w1, W2 w2) : PairWeight<W1, W2>(w1, w2) {} 51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const ProductWeight<W1, W2> &Zero() { 53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const ProductWeight<W1, W2> zero(PairWeight<W1, W2>::Zero()); 54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return zero; 55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const ProductWeight<W1, W2> &One() { 58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const ProductWeight<W1, W2> one(PairWeight<W1, W2>::One()); 59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return one; 60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const ProductWeight<W1, W2> &NoWeight() { 63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const ProductWeight<W1, W2> no_weight( 64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson PairWeight<W1, W2>::NoWeight()); 65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return no_weight; 66f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 67f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string &Type() { 69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static const string type = W1::Type() + "_X_" + W2::Type(); 70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return type; 71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 72f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson static uint64 Properties() { 74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson uint64 props1 = W1::Properties(); 75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson uint64 props2 = W2::Properties(); 76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return props1 & props2 & (kLeftSemiring | kRightSemiring | 77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson kCommutative | kIdempotent); 78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 80f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ProductWeight<W1, W2> Quantize(float delta = kDelta) const { 81f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return PairWeight<W1, W2>::Quantize(delta); 82f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 84f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson ReverseWeight Reverse() const { 85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return PairWeight<W1, W2>::Reverse(); 86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson } 87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 89f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}; 90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class W1, class W2> 92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsoninline ProductWeight<W1, W2> Plus(const ProductWeight<W1, W2> &w, 93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson const ProductWeight<W1, W2> &v) { 94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return ProductWeight<W1, W2>(Plus(w.Value1(), v.Value1()), 95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Plus(w.Value2(), v.Value2())); 96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson} 97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 98f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class W1, class W2> 99f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsoninline ProductWeight<W1, W2> Times(const ProductWeight<W1, W2> &w, 100f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson const ProductWeight<W1, W2> &v) { 101f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return ProductWeight<W1, W2>(Times(w.Value1(), v.Value1()), 102f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Times(w.Value2(), v.Value2())); 103f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson} 104f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 105f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class W1, class W2> 106f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsoninline ProductWeight<W1, W2> Divide(const ProductWeight<W1, W2> &w, 107f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson const ProductWeight<W1, W2> &v, 108f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson DivideType typ = DIVIDE_ANY) { 109f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson return ProductWeight<W1, W2>(Divide(w.Value1(), v.Value1(), typ), 110f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson Divide(w.Value2(), v.Value2(), typ)); 111f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson} 112f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 113f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson} // namespace fst 114f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson 115f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#endif // FST_LIB_PRODUCT_WEIGHT_H__ 116