diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 18:28:37 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 18:28:37 -0800 |
commit | e6733b78b94eb6701345e1a7244ab54dad60d47d (patch) | |
tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /tools/thirdparty/OpenFst | |
parent | abbfd2c3d63580a0fdde5a0480f4ec6dd34e1fcf (diff) | |
download | srec-e6733b78b94eb6701345e1a7244ab54dad60d47d.tar.gz |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'tools/thirdparty/OpenFst')
70 files changed, 0 insertions, 18343 deletions
diff --git a/tools/thirdparty/OpenFst/Android.mk b/tools/thirdparty/OpenFst/Android.mk deleted file mode 100644 index 5053e7d..0000000 --- a/tools/thirdparty/OpenFst/Android.mk +++ /dev/null @@ -1 +0,0 @@ -include $(call all-subdir-makefiles) diff --git a/tools/thirdparty/OpenFst/README b/tools/thirdparty/OpenFst/README deleted file mode 100644 index 4351e5d..0000000 --- a/tools/thirdparty/OpenFst/README +++ /dev/null @@ -1,36 +0,0 @@ -OpenFst - Beta release version. -http://128.122.80.210/~openfst/twiki/pub/FST/FstDownload -OpenFst-beta-20070801.tgz - -PLATFORMS: - - This version is known to work under linux using g++. - The make files are unlikely to work on other platforms - unmodified. Stay tuned for an autoconf build. - -BUILDING: - - To build (rudimentary Makefiles for now): - - cd fst ; make all - - To test: - - cd fst ; make test - # Note long compile on algo_test and pair-arc.so (many templates) - -USING WITH YOUR OWN PROGRAMS: - - Include "fst/lib/fstlib.h" (path must be relative to the OpenFst root - directory). You need to link to fst/lib/libfst.so as well as libpthread - and libdl. The linking is dynamic so that the Fst and Arc tpe - DSO extensions can be used (see e.g. test/dso_test.sh). - -DOCUMENTATION: - - At www.openfst.org. - -TO DO: - 1) remove/sanitize google-isms - e.g. flags and error handling in compat.* - 2) autoconf - 3) check portability diff --git a/tools/thirdparty/OpenFst/fst/Android.mk b/tools/thirdparty/OpenFst/fst/Android.mk deleted file mode 100644 index 5053e7d..0000000 --- a/tools/thirdparty/OpenFst/fst/Android.mk +++ /dev/null @@ -1 +0,0 @@ -include $(call all-subdir-makefiles) diff --git a/tools/thirdparty/OpenFst/fst/lib/Android.mk b/tools/thirdparty/OpenFst/fst/lib/Android.mk deleted file mode 100644 index 0593078..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/Android.mk +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2006 The Android Open Source Project - -LOCAL_PATH:= $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_SRC_FILES:= \ - fst.cpp \ - properties.cpp \ - symbol-table.cpp \ - compat.cpp \ - -LOCAL_C_INCLUDES := \ - $(LOCAL_PATH) \ - $(LOCAL_PATH)/../.. \ - -LOCAL_CFLAGS += \ - -DFST_DL \ - -LOCAL_SHARED_LIBRARIES := \ - -LOCAL_LDLIBS += -lpthread - -LOCAL_MODULE := libfst - -include $(BUILD_HOST_SHARED_LIBRARY) diff --git a/tools/thirdparty/OpenFst/fst/lib/arc.h b/tools/thirdparty/OpenFst/fst/lib/arc.h deleted file mode 100644 index 92d80a0..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/arc.h +++ /dev/null @@ -1,165 +0,0 @@ -// arc.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Commonly used Fst arc types. - -#ifndef FST_LIB_ARC_H__ -#define FST_LIB_ARC_H__ - -#include "fst/lib/float-weight.h" -#include "fst/lib/product-weight.h" -#include "fst/lib/string-weight.h" - -namespace fst { - -// Arc with integer labels and state Ids and float weights over the -// tropical semiring. -struct StdArc { - typedef int Label; - typedef TropicalWeight Weight; - typedef int StateId; - - StdArc(Label i, Label o, Weight w, StateId s) - : ilabel(i), olabel(o), weight(w), nextstate(s) {} - - StdArc() {} - - static const string &Type() { // Arc type name - static const string type = "standard"; - return type; - } - - Label ilabel; // Transition input label - Label olabel; // Transition output label - Weight weight; // Transition weight - StateId nextstate; // Transition destination state -}; - - -// Arc with integer labels and state Ids and float weights over the -// log semiring. -struct LogArc { - typedef int Label; - typedef LogWeight Weight; - typedef int StateId; - - LogArc(Label i, Label o, Weight w, StateId s) - : ilabel(i), olabel(o), weight(w), nextstate(s) {} - - LogArc() {} - - static const string &Type() { // Arc type name - static const string type = "log"; - return type; - } - - Label ilabel; // Transition input label - Label olabel; // Transition output label - Weight weight; // Transition weight - StateId nextstate; // Transition destination state -}; - - -// Arc with integer labels and state Ids and string weights. -template <StringType S = STRING_LEFT> -class StringArc { - public: - typedef int Label; - typedef StringWeight<int, S> Weight; - typedef int StateId; - - StringArc(Label i, Label o, Weight w, StateId s) - : ilabel(i), olabel(o), weight(w), nextstate(s) {} - - StringArc() {} - - static const string &Type() { // Arc type name - static const string type = - S == STRING_LEFT ? "standard_string" : - (S == STRING_RIGHT ? "right_standard_string" : - (S == STRING_LEFT_RESTRICT ? "restricted_string" : - "right_restricted_string")); - return type; - } - - Label ilabel; // Transition input label - Label olabel; // Transition output label - Weight weight; // Transition weight - StateId nextstate; // Transition destination state -}; - - -// Arc with label and state Id type the same as template arg and with -// weights over the Gallic semiring w.r.t the output labels and weights of A. -template <class A, StringType S = STRING_LEFT> -struct GallicArc { - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::StateId StateId; - typedef GallicWeight<Label, typename A::Weight, S> Weight; - - GallicArc() {} - - GallicArc(Label i, Label o, Weight w, StateId s) - : ilabel(i), olabel(o), weight(w), nextstate(s) {} - - GallicArc(const A &arc) - : ilabel(arc.ilabel), olabel(arc.ilabel), - weight(arc.olabel, arc.weight), nextstate(arc.nextstate) {} - - static const string &Type() { // Arc type name - static const string type = - (S == STRING_LEFT ? "gallic_" : - (S == STRING_RIGHT ? "right_gallic_" : - (S == STRING_LEFT_RESTRICT ? "restricted_gallic_" : - "right_restricted_gallic_"))) + A::Type(); - return type; - } - - Label ilabel; // Transition input label - Label olabel; // Transition output label - Weight weight; // Transition weight - StateId nextstate; // Transition destination state -}; - - -// Arc with the reverse of the weight found in its template arg. -template <class A> struct ReverseArc { - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::Weight AWeight; - typedef typename AWeight::ReverseWeight Weight; - typedef typename A::StateId StateId; - - ReverseArc(Label i, Label o, Weight w, StateId s) - : ilabel(i), olabel(o), weight(w), nextstate(s) {} - - ReverseArc() {} - - static const string &Type() { // Arc type name - static const string type = "reverse_" + Arc::Type(); - return type; - } - - Label ilabel; // Transition input label - Label olabel; // Transition output label - Weight weight; // Transition weight - StateId nextstate; // Transition destination state -}; - -} // namespace fst; - -#endif // FST_LIB_ARC_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/arcfilter.h b/tools/thirdparty/OpenFst/fst/lib/arcfilter.h deleted file mode 100644 index 2840ac6..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/arcfilter.h +++ /dev/null @@ -1,43 +0,0 @@ -// arcfilter.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function objects to restrict which arcs are traversed in an FST. - -#ifndef FST_LIB_ARCFILTER_H__ -#define FST_LIB_ARCFILTER_H__ - -namespace fst { - -// True for all arcs. -template <class A> -class AnyArcFilter { -public: - bool operator()(const A &arc) const { return true; } -}; - - -// True for (input/output) epsilon arcs. -template <class A> -class EpsilonArcFilter { -public: - bool operator()(const A &arc) const { - return arc.ilabel == 0 && arc.olabel == 0; - } -}; - -} // namespace fst - -#endif // FST_LIB_ARCFILTER_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/arcsort.h b/tools/thirdparty/OpenFst/fst/lib/arcsort.h deleted file mode 100644 index 4d15adb..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/arcsort.h +++ /dev/null @@ -1,317 +0,0 @@ -// arcsort.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to sort arcs in an FST. - -#ifndef FST_LIB_ARCSORT_H__ -#define FST_LIB_ARCSORT_H__ - -#include <algorithm> - -#include "fst/lib/cache.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -// Sorts the arcs in an FST according to function object 'comp' of -// type Compare. This version modifies its input. Comparison function -// objects IlabelCompare and OlabelCompare are provived by the -// library. In general, Compare must meet the requirements for an STL -// sort comparision function object. It must also have a member -// Properties(uint64) that specifies the known properties of the -// sorted FST; it takes as argument the input FST's known properties -// before the sort. -// -// Complexity: -// - Time: O(V + D log D) -// - Space: O(D) -// where V = # of states and D = maximum out-degree. -template<class Arc, class Compare> -void ArcSort(MutableFst<Arc> *fst, Compare comp) { - typedef typename Arc::StateId StateId; - - uint64 props = fst->Properties(kFstProperties, false); - - vector<Arc> arcs; - for (StateIterator< MutableFst<Arc> > siter(*fst); - !siter.Done(); - siter.Next()) { - StateId s = siter.Value(); - arcs.clear(); - for (ArcIterator< MutableFst<Arc> > aiter(*fst, s); - !aiter.Done(); - aiter.Next()) - arcs.push_back(aiter.Value()); - sort(arcs.begin(), arcs.end(), comp); - fst->DeleteArcs(s); - for (size_t a = 0; a < arcs.size(); ++a) - fst->AddArc(s, arcs[a]); - } - - fst->SetProperties(comp.Properties(props), kFstProperties); -} - -typedef CacheOptions ArcSortFstOptions; - -// Implementation of delayed ArcSortFst. -template<class A, class C> -class ArcSortFstImpl : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - using FstImpl<A>::InputSymbols; - using FstImpl<A>::OutputSymbols; - - using VectorFstBaseImpl<typename CacheImpl<A>::State>::NumStates; - - using CacheImpl<A>::HasArcs; - using CacheImpl<A>::HasFinal; - using CacheImpl<A>::HasStart; - - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - ArcSortFstImpl(const Fst<A> &fst, const C &comp, - const ArcSortFstOptions &opts) - : CacheImpl<A>(opts), fst_(fst.Copy()), comp_(comp) { - SetType("arcsort"); - uint64 props = fst_->Properties(kCopyProperties, false); - SetProperties(comp_.Properties(props)); - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - } - - ArcSortFstImpl(const ArcSortFstImpl& impl) - : fst_(impl.fst_->Copy()), comp_(impl.comp_) { - SetType("arcsort"); - SetProperties(impl.Properties(), kCopyProperties); - SetInputSymbols(impl.InputSymbols()); - SetOutputSymbols(impl.OutputSymbols()); - } - - ~ArcSortFstImpl() { delete fst_; } - - StateId Start() { - if (!HasStart()) - SetStart(fst_->Start()); - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) - SetFinal(s, fst_->Final(s)); - return CacheImpl<A>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitStateIterator(StateIteratorData<A> *data) const { - fst_->InitStateIterator(data); - } - - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - void Expand(StateId s) { - for (ArcIterator< Fst<A> > aiter(*fst_, s); !aiter.Done(); aiter.Next()) - AddArc(s, aiter.Value()); - SetArcs(s); - - if (s < NumStates()) { // ensure state exists - vector<A> &carcs = GetState(s)->arcs; - sort(carcs.begin(), carcs.end(), comp_); - } - } - - private: - const Fst<A> *fst_; - C comp_; - - void operator=(const ArcSortFstImpl<A, C> &impl); // Disallow -}; - - -// Sorts the arcs in an FST according to function object 'comp' of -// type Compare. This version is a delayed Fst. Comparsion function -// objects IlabelCompare and OlabelCompare are provided by the -// library. In general, Compare must meet the requirements for an STL -// comparision function object (e.g. as used for STL sort). It must -// also have a member Properties(uint64) that specifies the known -// properties of the sorted FST; it takes as argument the input FST's -// known properties. -// -// Complexity: -// - Time: O(v + d log d) -// - Space: O(v + d) -// where v = # of states visited, d = maximum out-degree of states -// visited. Constant time and space to visit an input state is assumed -// and exclusive of caching. -template <class A, class C> -class ArcSortFst : public Fst<A> { - public: - friend class CacheArcIterator< ArcSortFst<A, C> >; - friend class ArcIterator< ArcSortFst<A, C> >; - - typedef A Arc; - typedef C Compare; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - ArcSortFst(const Fst<A> &fst, const C &comp) - : impl_(new ArcSortFstImpl<A, C>(fst, comp, ArcSortFstOptions())) {} - - ArcSortFst(const Fst<A> &fst, const C &comp, const ArcSortFstOptions &opts) - : impl_(new ArcSortFstImpl<A, C>(fst, comp, opts)) {} - - ArcSortFst(const ArcSortFst<A, C> &fst) : - impl_(new ArcSortFstImpl<A, C>(*(fst.impl_))) {} - - virtual ~ArcSortFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual ArcSortFst<A, C> *Copy() const { - return new ArcSortFst<A, C>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual void InitStateIterator(StateIteratorData<A> *data) const { - impl_->InitStateIterator(data); - } - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - private: - ArcSortFstImpl<A, C> *impl_; - - void operator=(const ArcSortFst<A, C> &fst); // Disallow -}; - - -// Specialization for ArcSortFst. -template <class A, class C> -class ArcIterator< ArcSortFst<A, C> > - : public CacheArcIterator< ArcSortFst<A, C> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const ArcSortFst<A, C> &fst, StateId s) - : CacheArcIterator< ArcSortFst<A, C> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - - -// Compare class for comparing input labels of arcs. -template<class A> class ILabelCompare { - public: - bool operator() (A arc1, A arc2) const { - return arc1.ilabel < arc2.ilabel; - } - - uint64 Properties(uint64 props) const { - return props & kArcSortProperties | kILabelSorted; - } -}; - - -// Compare class for comparing output labels of arcs. -template<class A> class OLabelCompare { - public: - bool operator() (const A &arc1, const A &arc2) const { - return arc1.olabel < arc2.olabel; - } - - uint64 Properties(uint64 props) const { - return props & kArcSortProperties | kOLabelSorted; - } -}; - - -// Useful aliases when using StdArc. -template<class C> class StdArcSortFst : public ArcSortFst<StdArc, C> { - public: - typedef StdArc Arc; - typedef C Compare; -}; - -typedef ILabelCompare<StdArc> StdILabelCompare; - -typedef OLabelCompare<StdArc> StdOLabelCompare; - -} // namespace fst - -#endif // FST_LIB_ARCSORT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/arcsum.h b/tools/thirdparty/OpenFst/fst/lib/arcsum.h deleted file mode 100644 index 442e2c1..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/arcsum.h +++ /dev/null @@ -1,96 +0,0 @@ -// arcsum.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions to sum arcs (sum weights) in an fst. - -#ifndef FST_LIB_ARCSUM_H__ -#define FST_LIB_ARCSUM_H__ - -#include "fst/lib/mutable-fst.h" -#include "fst/lib/weight.h" - -namespace fst { - -template <class A> -class ArcSumCompare { - public: - bool operator()(const A& x, const A& y) { - if (x.ilabel < y.ilabel) return true; - if (x.ilabel > y.ilabel) return false; - if (x.olabel < y.olabel) return true; - if (x.olabel < y.olabel) return false; - if (x.nextstate < y.nextstate) return true; - if (x.nextstate > y.nextstate) return false; - return false; - } -}; - - -template <class A> -class ArcSumEqual { - public: - bool operator()(const A& x, const A& y) { - return (x.ilabel == y.ilabel && - x.olabel == y.olabel && - x.nextstate == y.nextstate); - } -}; - - -// Combines identically labeled arcs, summing weights. For each state -// we combine arcs with the same input and output label, summing their -// weights using Weight:::Plus(). -template <class A> -void ArcSum(MutableFst<A>* fst) { - typedef typename A::StateId StateId; - - vector<A> arcs; - for (StateIterator<Fst<A> > siter(*fst); !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - if (fst->NumArcs(s) == 0) continue; - - // Sums arcs into arcs array. - arcs.clear(); - arcs.reserve(fst->NumArcs(s)); - for (ArcIterator<Fst<A> > aiter(*fst, s); !aiter.Done(); - aiter.Next()) - arcs.push_back(aiter.Value()); - - // At each state, first sorts the exiting arcs by input label, output label - // and destination state and then combines arcs identical in these - // attributes. - ArcSumCompare<A> comp; - sort(arcs.begin(), arcs.end(), comp); - - // Deletes current arcs and copy in sumed arcs. - fst->DeleteArcs(s); - A current_arc = arcs[0]; - ArcSumEqual<A> equal; - for (size_t i = 1; i < arcs.size(); ++i) { - if (equal(current_arc, arcs[i])) { - current_arc.weight = Plus(current_arc.weight, arcs[i].weight); - } else { - fst->AddArc(s, current_arc); - current_arc = arcs[i]; - } - } - fst->AddArc(s, current_arc); - } -} - -} - -#endif // FST_LIB_ARCSUM_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/cache.h b/tools/thirdparty/OpenFst/fst/lib/cache.h deleted file mode 100644 index ec00d5a..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/cache.h +++ /dev/null @@ -1,461 +0,0 @@ -// cache.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// An Fst implementation that caches FST elements of a delayed -// computation. - -#ifndef FST_LIB_CACHE_H__ -#define FST_LIB_CACHE_H__ - -#include <list> - -#include "fst/lib/vector-fst.h" - -DECLARE_bool(fst_default_cache_gc); -DECLARE_int64(fst_default_cache_gc_limit); - -namespace fst { - -struct CacheOptions { - bool gc; // enable GC - size_t gc_limit; // # of bytes allowed before GC - - - CacheOptions(bool g, size_t l) : gc(g), gc_limit(l) {} - CacheOptions() - : gc(FLAGS_fst_default_cache_gc), - gc_limit(FLAGS_fst_default_cache_gc_limit) {} -}; - - -// This is a VectorFstBaseImpl container that holds a State similar to -// VectorState but additionally has a flags data member (see -// CacheState below). This class is used to cache FST elements with -// the flags used to indicate what has been cached. Use HasStart() -// HasFinal(), and HasArcs() to determine if cached and SetStart(), -// SetFinal(), AddArc(), and SetArcs() to cache. Note you must set the -// final weight even if the state is non-final to mark it as -// cached. If the 'gc' option is 'false', cached items have the extent -// of the FST - minimizing computation. If the 'gc' option is 'true', -// garbage collection of states (not in use in an arc iterator) is -// performed, in a rough approximation of LRU order, when 'gc_limit' -// bytes is reached - controlling memory use. When 'gc_limit' is 0, -// special optimizations apply - minimizing memory use. - -template <class S> -class CacheBaseImpl : public VectorFstBaseImpl<S> { - public: - using FstImpl<typename S::Arc>::Type; - using VectorFstBaseImpl<S>::NumStates; - using VectorFstBaseImpl<S>::AddState; - - typedef S State; - typedef typename S::Arc Arc; - typedef typename Arc::Weight Weight; - typedef typename Arc::StateId StateId; - - CacheBaseImpl() - : cache_start_(false), nknown_states_(0), min_unexpanded_state_id_(0), - cache_first_state_id_(kNoStateId), cache_first_state_(0), - cache_gc_(FLAGS_fst_default_cache_gc), cache_size_(0), - cache_limit_(FLAGS_fst_default_cache_gc_limit > kMinCacheLimit || - FLAGS_fst_default_cache_gc_limit == 0 ? - FLAGS_fst_default_cache_gc_limit : kMinCacheLimit) {} - - explicit CacheBaseImpl(const CacheOptions &opts) - : cache_start_(false), nknown_states_(0), - min_unexpanded_state_id_(0), cache_first_state_id_(kNoStateId), - cache_first_state_(0), cache_gc_(opts.gc), cache_size_(0), - cache_limit_(opts.gc_limit > kMinCacheLimit || opts.gc_limit == 0 ? - opts.gc_limit : kMinCacheLimit) {} - - ~CacheBaseImpl() { - delete cache_first_state_; - } - - // Gets a state from its ID; state must exist. - const S *GetState(StateId s) const { - if (s == cache_first_state_id_) - return cache_first_state_; - else - return VectorFstBaseImpl<S>::GetState(s); - } - - // Gets a state from its ID; state must exist. - S *GetState(StateId s) { - if (s == cache_first_state_id_) - return cache_first_state_; - else - return VectorFstBaseImpl<S>::GetState(s); - } - - // Gets a state from its ID; return 0 if it doesn't exist. - const S *CheckState(StateId s) const { - if (s == cache_first_state_id_) - return cache_first_state_; - else if (s < NumStates()) - return VectorFstBaseImpl<S>::GetState(s); - else - return 0; - } - - // Gets a state from its ID; add it if necessary. - S *ExtendState(StateId s) { - if (s == cache_first_state_id_) { - return cache_first_state_; // Return 1st cached state - } else if (cache_limit_ == 0 && cache_first_state_id_ == kNoStateId) { - cache_first_state_id_ = s; // Remember 1st cached state - cache_first_state_ = new S; - return cache_first_state_; - } else if (cache_first_state_id_ != kNoStateId && - cache_first_state_->ref_count == 0) { - cache_first_state_id_ = s; // Reuse 1st cached state - cache_first_state_->Reset(); - return cache_first_state_; // Return 1st cached state - } else { - while (NumStates() <= s) // Add state to main cache - AddState(0); - if (!VectorFstBaseImpl<S>::GetState(s)) { - SetState(s, new S); - if (cache_first_state_id_ != kNoStateId) { // Forget 1st cached state - while (NumStates() <= cache_first_state_id_) - AddState(0); - SetState(cache_first_state_id_, cache_first_state_); - if (cache_gc_) { - cache_states_.push_back(cache_first_state_id_); - cache_size_ += sizeof(S) + - cache_first_state_->arcs.capacity() * sizeof(Arc); - cache_limit_ = kMinCacheLimit; - } - cache_first_state_id_ = kNoStateId; - cache_first_state_ = 0; - } - if (cache_gc_) { - cache_states_.push_back(s); - cache_size_ += sizeof(S); - if (cache_size_ > cache_limit_) - GC(s, false); - } - } - return VectorFstBaseImpl<S>::GetState(s); - } - } - - void SetStart(StateId s) { - VectorFstBaseImpl<S>::SetStart(s); - cache_start_ = true; - if (s >= nknown_states_) - nknown_states_ = s + 1; - } - - void SetFinal(StateId s, Weight w) { - S *state = ExtendState(s); - state->final = w; - state->flags |= kCacheFinal | kCacheRecent; - } - - void AddArc(StateId s, const Arc &arc) { - S *state = ExtendState(s); - state->arcs.push_back(arc); - } - - // Marks arcs of state s as cached. - void SetArcs(StateId s) { - S *state = ExtendState(s); - vector<Arc> &arcs = state->arcs; - state->niepsilons = state->noepsilons = 0; - for (unsigned int a = 0; a < arcs.size(); ++a) { - const Arc &arc = arcs[a]; - if (arc.nextstate >= nknown_states_) - nknown_states_ = arc.nextstate + 1; - if (arc.ilabel == 0) - ++state->niepsilons; - if (arc.olabel == 0) - ++state->noepsilons; - } - ExpandedState(s); - state->flags |= kCacheArcs | kCacheRecent; - if (cache_gc_ && s != cache_first_state_id_) { - cache_size_ += arcs.capacity() * sizeof(Arc); - if (cache_size_ > cache_limit_) - GC(s, false); - } - }; - - void ReserveArcs(StateId s, size_t n) { - S *state = ExtendState(s); - state->arcs.reserve(n); - } - - // Is the start state cached? - bool HasStart() const { return cache_start_; } - // Is the final weight of state s cached? - - bool HasFinal(StateId s) const { - const S *state = CheckState(s); - if (state && state->flags & kCacheFinal) { - state->flags |= kCacheRecent; - return true; - } else { - return false; - } - } - - // Are arcs of state s cached? - bool HasArcs(StateId s) const { - const S *state = CheckState(s); - if (state && state->flags & kCacheArcs) { - state->flags |= kCacheRecent; - return true; - } else { - return false; - } - } - - Weight Final(StateId s) const { - const S *state = GetState(s); - return state->final; - } - - size_t NumArcs(StateId s) const { - const S *state = GetState(s); - return state->arcs.size(); - } - - size_t NumInputEpsilons(StateId s) const { - const S *state = GetState(s); - return state->niepsilons; - } - - size_t NumOutputEpsilons(StateId s) const { - const S *state = GetState(s); - return state->noepsilons; - } - - // Provides information needed for generic arc iterator. - void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const { - const S *state = GetState(s); - data->base = 0; - data->narcs = state->arcs.size(); - data->arcs = data->narcs > 0 ? &(state->arcs[0]) : 0; - data->ref_count = &(state->ref_count); - ++(*data->ref_count); - } - - // Number of known states. - StateId NumKnownStates() const { return nknown_states_; } - // Find the mininum never-expanded state Id - StateId MinUnexpandedState() const { - while (min_unexpanded_state_id_ < (StateId)expanded_states_.size() && - expanded_states_[min_unexpanded_state_id_]) - ++min_unexpanded_state_id_; - return min_unexpanded_state_id_; - } - - // Removes from cache_states_ and uncaches (not referenced-counted) - // states that have not been accessed since the last GC until - // cache_limit_/3 bytes are uncached. If that fails to free enough, - // recurs uncaching recently visited states as well. If still - // unable to free enough memory, then widens cache_limit_. - void GC(StateId current, bool free_recent) { - if (!cache_gc_) - return; - VLOG(2) << "CacheImpl: Enter GC: object = " << Type() << "(" << this - << "), free recently cached = " << free_recent - << ", cache size = " << cache_size_ - << ", cache limit = " << cache_limit_ << "\n"; - typename list<StateId>::iterator siter = cache_states_.begin(); - - size_t cache_target = (2 * cache_limit_)/3 + 1; - while (siter != cache_states_.end()) { - StateId s = *siter; - S* state = VectorFstBaseImpl<S>::GetState(s); - if (cache_size_ > cache_target && state->ref_count == 0 && - (free_recent || !(state->flags & kCacheRecent)) && s != current) { - cache_size_ -= sizeof(S) + state->arcs.capacity() * sizeof(Arc); - delete state; - SetState(s, 0); - cache_states_.erase(siter++); - } else { - state->flags &= ~kCacheRecent; - ++siter; - } - } - if (!free_recent && cache_size_ > cache_target) { - GC(current, true); - } else { - while (cache_size_ > cache_target) { - cache_limit_ *= 2; - cache_target *= 2; - } - } - VLOG(2) << "CacheImpl: Exit GC: object = " << Type() << "(" << this - << "), free recently cached = " << free_recent - << ", cache size = " << cache_size_ - << ", cache limit = " << cache_limit_ << "\n"; - } - - private: - static const uint32 kCacheFinal = 0x0001; // Final weight has been cached - static const uint32 kCacheArcs = 0x0002; // Arcs have been cached - static const uint32 kCacheRecent = 0x0004; // Mark as visited since GC - - static const size_t kMinCacheLimit; // Minimum (non-zero) cache limit - - void ExpandedState(StateId s) { - if (s < min_unexpanded_state_id_) - return; - while ((StateId)expanded_states_.size() <= s) - expanded_states_.push_back(false); - expanded_states_[s] = true; - } - - bool cache_start_; // Is the start state cached? - StateId nknown_states_; // # of known states - vector<bool> expanded_states_; // states that have been expanded - mutable StateId min_unexpanded_state_id_; // minimum never-expanded state Id - StateId cache_first_state_id_; // First cached state id - S *cache_first_state_; // First cached state - list<StateId> cache_states_; // list of currently cached states - bool cache_gc_; // enable GC - size_t cache_size_; // # of bytes cached - size_t cache_limit_; // # of bytes allowed before GC - - void InitStateIterator(StateIteratorData<Arc> *); // disallow - DISALLOW_EVIL_CONSTRUCTORS(CacheBaseImpl); -}; - -template <class S> -const size_t CacheBaseImpl<S>::kMinCacheLimit = 8096; - - -// Arcs implemented by an STL vector per state. Similar to VectorState -// but adds flags and ref count to keep track of what has been cached. -template <class A> -struct CacheState { - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - CacheState() : final(Weight::Zero()), flags(0), ref_count(0) {} - - void Reset() { - flags = 0; - ref_count = 0; - arcs.resize(0); - } - - Weight final; // Final weight - vector<A> arcs; // Arcs represenation - size_t niepsilons; // # of input epsilons - size_t noepsilons; // # of output epsilons - mutable uint32 flags; - mutable int ref_count; -}; - -// A CacheBaseImpl with a commonly used CacheState. -template <class A> -class CacheImpl : public CacheBaseImpl< CacheState<A> > { - public: - typedef CacheState<A> State; - - CacheImpl() {} - - explicit CacheImpl(const CacheOptions &opts) - : CacheBaseImpl< CacheState<A> >(opts) {} - - private: - DISALLOW_EVIL_CONSTRUCTORS(CacheImpl); -}; - - -// Use this to make a state iterator for a CacheBaseImpl-derived Fst. -// You'll need to make this class a friend of your derived Fst. -// Note this iterator only returns those states reachable from -// the initial state, so consider implementing a class-specific one. -template <class F> -class CacheStateIterator : public StateIteratorBase<typename F::Arc> { - public: - typedef typename F::Arc Arc; - typedef typename Arc::StateId StateId; - - explicit CacheStateIterator(const F &fst) : fst_(fst), s_(0) {} - - virtual bool Done() const { - if (s_ < fst_.impl_->NumKnownStates()) - return false; - fst_.Start(); // force start state - if (s_ < fst_.impl_->NumKnownStates()) - return false; - for (int u = fst_.impl_->MinUnexpandedState(); - u < fst_.impl_->NumKnownStates(); - u = fst_.impl_->MinUnexpandedState()) { - ArcIterator<F>(fst_, u); // force state expansion - if (s_ < fst_.impl_->NumKnownStates()) - return false; - } - return true; - } - - virtual StateId Value() const { return s_; } - - virtual void Next() { ++s_; } - - virtual void Reset() { s_ = 0; } - - private: - const F &fst_; - StateId s_; -}; - - -// Use this to make an arc iterator for a CacheBaseImpl-derived Fst. -// You'll need to make this class a friend of your derived Fst and -// define types Arc and State. -template <class F> -class CacheArcIterator { - public: - typedef typename F::Arc Arc; - typedef typename F::State State; - typedef typename Arc::StateId StateId; - - CacheArcIterator(const F &fst, StateId s) : i_(0) { - state_ = fst.impl_->ExtendState(s); - ++state_->ref_count; - } - - ~CacheArcIterator() { --state_->ref_count; } - - bool Done() const { return i_ >= state_->arcs.size(); } - - const Arc& Value() const { return state_->arcs[i_]; } - - void Next() { ++i_; } - - void Reset() { i_ = 0; } - - void Seek(size_t a) { i_ = a; } - - private: - const State *state_; - size_t i_; - - DISALLOW_EVIL_CONSTRUCTORS(CacheArcIterator); -}; - -} // namespace fst - -#endif // FST_LIB_CACHE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/closure.h b/tools/thirdparty/OpenFst/fst/lib/closure.h deleted file mode 100644 index 1322a6b..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/closure.h +++ /dev/null @@ -1,142 +0,0 @@ -// closure.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to compute the concatenative closure of an Fst. - -#ifndef FST_LIB_CLOSURE_H__ -#define FST_LIB_CLOSURE_H__ - -#include "fst/lib/mutable-fst.h" -#include "fst/lib/rational.h" - -namespace fst { - -// Computes the concatenative closure. This version modifies its -// MutableFst input. If FST transduces string x to y with weight a, -// then the closure transduces x to y with weight a, xx to yy with -// weight Times(a, a), xxx to yyy with with Times(Times(a, a), a), -// etc. If closure_type == CLOSURE_STAR, then the empty string is -// transduced to itself with weight Weight::One() as well. -// -// Complexity: -// - Time: O(V) -// - Space: O(V) -// where V = # of states. -template<class Arc> -void Closure(MutableFst<Arc> *fst, ClosureType closure_type) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Label Label; - typedef typename Arc::Weight Weight; - - uint64 props = fst->Properties(kFstProperties, false); - StateId start = fst->Start(); - for (StateIterator< MutableFst<Arc> > siter(*fst); - !siter.Done(); - siter.Next()) { - StateId s = siter.Value(); - Weight final = fst->Final(s); - if (final != Weight::Zero()) - fst->AddArc(s, Arc(0, 0, final, start)); - } - if (closure_type == CLOSURE_STAR) { - StateId nstart = fst->AddState(); - fst->SetStart(nstart); - fst->SetFinal(nstart, Weight::One()); - if (start != kNoLabel) - fst->AddArc(nstart, Arc(0, 0, Weight::One(), start)); - } - fst->SetProperties(ClosureProperties(props, closure_type == CLOSURE_STAR), - kFstProperties); -} - -// Computes the concatenative closure. This version modifies its -// RationalFst input. -template<class Arc> -void Closure(RationalFst<Arc> *fst, ClosureType closure_type) { - fst->Impl()->AddClosure(closure_type); -} - - -struct ClosureFstOptions : RationalFstOptions { - ClosureType type; - - ClosureFstOptions(const RationalFstOptions &opts, ClosureType t) - : RationalFstOptions(opts), type(t) {} - explicit ClosureFstOptions(ClosureType t) : type(t) {} - ClosureFstOptions() : type(CLOSURE_STAR) {} -}; - - -// Computes the concatenative closure. This version is a delayed -// Fst. If FST transduces string x to y with weight a, then the -// closure transduces x to y with weight a, xx to yy with weight -// Times(a, a), xxx to yyy with weight Times(Times(a, a), a), etc. If -// closure_type == CLOSURE_STAR, then The empty string is transduced -// to itself with weight Weight::One() as well. -// -// Complexity: -// - Time: O(v) -// - Space: O(v) -// where v = # of states visited. Constant time and space to visit an -// input state or arc is assumed and exclusive of caching. -template <class A> -class ClosureFst : public RationalFst<A> { - public: - using RationalFst<A>::Impl; - - typedef A Arc; - - ClosureFst(const Fst<A> &fst, ClosureType closure_type) { - Impl()->InitClosure(fst, closure_type); - } - - ClosureFst(const Fst<A> &fst, const ClosureFstOptions &opts) - : RationalFst<A>(opts) { - Impl()->InitClosure(fst, opts.type); - } - - ClosureFst(const ClosureFst<A> &fst) : RationalFst<A>(fst) {} - - virtual ClosureFst<A> *Copy() const { return new ClosureFst<A>(*this); } -}; - - -// Specialization for ClosureFst. -template <class A> -class StateIterator< ClosureFst<A> > : public StateIterator< RationalFst<A> > { - public: - explicit StateIterator(const ClosureFst<A> &fst) - : StateIterator< RationalFst<A> >(fst) {} -}; - - -// Specialization for ClosureFst. -template <class A> -class ArcIterator< ClosureFst<A> > : public ArcIterator< RationalFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const ClosureFst<A> &fst, StateId s) - : ArcIterator< RationalFst<A> >(fst, s) {} -}; - - -// Useful alias when using StdArc. -typedef ClosureFst<StdArc> StdClosureFst; - -} // namespace fst - -#endif // FST_LIB_CLOSURE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/compat.cpp b/tools/thirdparty/OpenFst/fst/lib/compat.cpp deleted file mode 100644 index 11302a8..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/compat.cpp +++ /dev/null @@ -1,135 +0,0 @@ -// compat.cc -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Google compatibility definitions. - -#include <cstring> - -#include "fst/lib/compat.h" - -DEFINE_int32(v, 0, "verbose level"); -DEFINE_bool(help, false, "verbose level"); -DEFINE_string(tmpdir, "/tmp/", "temporary directory"); - -static string prog_name; -static string flag_usage; - -static void out_of_memory() { - fprintf(stderr, "%s: Memory allocation failed\n", prog_name.c_str()); - exit(1); -} - -void InitFst(const char *usage, int *argc, char ***argv, bool remove_flags) { - prog_name = (*argv)[0]; - set_new_handler(out_of_memory); - - flag_usage = usage; - int index = 1; - for (; index < *argc; ++index) { - string argval = (*argv)[index]; - - if (argval[0] != '-' || argval == "-") - break; - while (argval[0] == '-') - argval = argval.substr(1); // remove initial '-'s - - string arg = argval; - string val = ""; - - // split argval (arg=val) into arg and val - size_t pos = argval.find("="); - if (pos != string::npos) { - arg = argval.substr(0, pos); - val = argval.substr(pos + 1); - } - - FlagRegister<bool> *bool_register = - FlagRegister<bool>::GetRegister(); - if (bool_register->InitFlag(arg, val)) - continue; - FlagRegister<string> *string_register = - FlagRegister<string>::GetRegister(); - if (string_register->InitFlag(arg, val)) - continue; - FlagRegister<int32> *int32_register = - FlagRegister<int32>::GetRegister(); - if (int32_register->InitFlag(arg, val)) - continue; - FlagRegister<int64> *int64_register = - FlagRegister<int64>::GetRegister(); - if (int64_register->InitFlag(arg, val)) - continue; - FlagRegister<double> *double_register = - FlagRegister<double>::GetRegister(); - if (double_register->InitFlag(arg, val)) - continue; - - LOG(FATAL) << "FlagInit: Bad option: " << (*argv)[index]; - } - - if (remove_flags) { - for (int i = 0; i < *argc - index; ++i) - (*argv)[i + 1] = (*argv)[i + index]; - *argc -= index - 1; - } - - if (FLAGS_help) { - ShowUsage(); - exit(1); - } -} - -void ShowUsage() { - std::cout << flag_usage << "\n"; - std::cout << " Flags Description:\n"; - FlagRegister<bool> *bool_register = FlagRegister<bool>::GetRegister(); - bool_register->ShowUsage(); - FlagRegister<string> *string_register = FlagRegister<string>::GetRegister(); - string_register->ShowUsage(); - FlagRegister<int32> *int32_register = FlagRegister<int32>::GetRegister(); - int32_register->ShowUsage(); - FlagRegister<int64> *int64_register = FlagRegister<int64>::GetRegister(); - int64_register->ShowUsage(); - FlagRegister<double> *double_register = FlagRegister<double>::GetRegister(); - double_register->ShowUsage(); -} - -void SplitToVector(char* full, const char* delim, vector<char*>* vec, - bool omit_empty_strings) { - char* next = full; - while((next = strsep(&full, delim)) != NULL) { - if (omit_empty_strings && next[0] == '\0') continue; - vec->push_back(next); - } - // Add last element (or full string if no delimeter found): - if (full != NULL) { - vec->push_back(full); - } -} - - -MD5::MD5() {} // ?OP? - -void MD5::Reset() {} // ?OP? - -void MD5::Update(void const *data, int size) {} // ?OP? - -string MD5::Digest() { return ""; } // every matches! ?OP? - - -Mutex::Mutex() {} // ?OP? - -MutexLock::MutexLock(Mutex *mutex) {} // ?OP? diff --git a/tools/thirdparty/OpenFst/fst/lib/compat.h b/tools/thirdparty/OpenFst/fst/lib/compat.h deleted file mode 100644 index 4c27ee3..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/compat.h +++ /dev/null @@ -1,314 +0,0 @@ -// compat.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Google compatibility declarations and inline definitions. - -#ifndef FST_COMPAT_H__ -#define FST_COMPAT_H__ - -// for STL -#include <cassert> -#include <cstdio> -#include <iostream> -#include <map> -#include <string> -#include <vector> - -#include <ext/hash_map> -#include <fcntl.h> -#include <pthread.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - -// exact size types -typedef short int16; -typedef int int32; -typedef long long int64; - -typedef unsigned short uint16; -typedef unsigned int uint32; -typedef unsigned long long uint64; - -using namespace std; - -// make copy constructor and operator= private -#define DISALLOW_EVIL_CONSTRUCTORS(type) \ - type(const type&); \ - void operator=(const type&) - -// thread control -class Mutex { - public: - Mutex(); - - private: - DISALLOW_EVIL_CONSTRUCTORS(Mutex); -}; - -class MutexLock { - public: - MutexLock(Mutex *); - - private: - DISALLOW_EVIL_CONSTRUCTORS(MutexLock); -}; - - -// flags -#define DECLARE_bool(name) extern bool FLAGS_ ## name -#define DECLARE_string(name) extern string FLAGS_ ## name -#define DECLARE_int32(name) extern int32 FLAGS_ ## name -#define DECLARE_int64(name) extern int64 FLAGS_ ## name -#define DECLARE_double(name) extern double FLAGS_ ## name - -template <typename T> -struct FlagDescription { - FlagDescription(T *addr, const char *doc, const char *type, const T val) - : address(addr), doc_string(doc), type_name(type), default_value(val) {} - - T *address; - const char *doc_string; - const char *type_name; - const T default_value; -}; - -template <typename T> -class FlagRegister { - public: - static FlagRegister<T> *GetRegister() { - pthread_once(®ister_init_, &FlagRegister<T>::Init); - return register_; - } - - const FlagDescription<T> &GetFlagDescription(const string &name) const { - MutexLock l(register_lock_); - typename map< string, FlagDescription<T> >::const_iterator it = - flag_table_.find(name); - return it != flag_table_.end() ? it->second : 0; - } - void SetDescription(const string &name, const FlagDescription<T> &desc) { - MutexLock l(register_lock_); - flag_table_.insert(make_pair(name, desc)); - } - - bool SetFlag(const string &val, bool *address) const { - if (val == "true" || val == "1" || val.empty()) { - *address = true; - return true; - } else if (val == "false" || val == "0") { - *address = false; - return true; - } - else { - return false; - } - } - bool SetFlag(const string &val, string *address) const { - *address = val; - return true; - } - bool SetFlag(const string &val, int32 *address) const { - char *p = 0; - *address = strtol(val.c_str(), &p, 0); - return !val.empty() && *p == '\0'; - } - bool SetFlag(const string &val, int64 *address) const { - char *p = 0; - *address = strtoll(val.c_str(), &p, 0); - return !val.empty() && *p == '\0'; - } - bool SetFlag(const string &val, double *address) const { - char *p = 0; - *address = strtod(val.c_str(), &p); - return !val.empty() && *p == '\0'; - } - - bool InitFlag(const string &arg, const string &val) const { - for (typename map< string, FlagDescription<T> >::const_iterator it = - flag_table_.begin(); - it != flag_table_.end(); - ++it) { - const string &name = it->first; - const FlagDescription<T> &desc = it->second; - if (arg == name) - return SetFlag(val, desc.address); - } - return false; - } - - void ShowDefault(bool default_value) const { - std::cout << ", default = "; - std::cout << (default_value ? "true" : "false"); - } - void ShowDefault(const string &default_value) const { - std::cout << ", default = "; - std::cout << "\"" << default_value << "\""; - } - template<typename V> void ShowDefault(const V& default_value) const { - std::cout << ", default = "; - std::cout << default_value; - } - void ShowUsage() const { - for (typename map< string, FlagDescription<T> >::const_iterator it = - flag_table_.begin(); - it != flag_table_.end(); - ++it) { - const string &name = it->first; - const FlagDescription<T> &desc = it->second; - std::cout << " --" << name - << ": type = " << desc.type_name; - ShowDefault(desc.default_value); - std::cout << "\n " << desc.doc_string << "\n"; - } - } - - private: - static void Init() { - register_lock_ = new Mutex; - register_ = new FlagRegister<T>; - } - static pthread_once_t register_init_; // ensures only called once - static Mutex* register_lock_; // multithreading lock - static FlagRegister<T> *register_; - - map< string, FlagDescription<T> > flag_table_; -}; - -template <class T> -pthread_once_t FlagRegister<T>::register_init_ = PTHREAD_ONCE_INIT; - -template <class T> -Mutex *FlagRegister<T>::register_lock_ = 0; - -template <class T> -FlagRegister<T> *FlagRegister<T>::register_ = 0; - - -template <typename T> -class FlagRegisterer { - public: - FlagRegisterer(const string &name, const FlagDescription<T> &desc) { - FlagRegister<T> *registr = FlagRegister<T>::GetRegister(); - registr->SetDescription(name, desc); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(FlagRegisterer); -}; - - -#define DEFINE_VAR(type, name, value, doc) \ - type FLAGS_ ## name = value; \ - static FlagRegisterer<type> \ - name ## _flags_registerer(#name, FlagDescription<type>(&FLAGS_ ## name, \ - doc, \ - #type, \ - value)) - -#define DEFINE_bool(name, value, doc) DEFINE_VAR(bool, name, value, doc) -#define DEFINE_string(name, value, doc) DEFINE_VAR(string, name, value, doc) -#define DEFINE_int32(name, value, doc) DEFINE_VAR(int32, name, value, doc) -#define DEFINE_int64(name, value, doc) DEFINE_VAR(int64, name, value, doc) -#define DEFINE_double(name, value, doc) DEFINE_VAR(double, name, value, doc) - -void InitFst(const char *usage, int *argc, char ***argv, bool remove_flags); - -void ShowUsage(); - - -// checking -#define CHECK(x) assert(x) -#define CHECK_EQ(x, y) assert((x) == (y)) - -// logging -DECLARE_int32(v); - -// tmp directory -DECLARE_string(tmpdir); - -class LogMessage { - public: - LogMessage(const string &type) : fatal_(type == "FATAL") { - std::cerr << type << ": "; - } - ~LogMessage() { - std::cerr << endl; - if(fatal_) - exit(1); - } - ostream &stream() { return std::cerr; } - - private: - bool fatal_; -}; - -#define LOG(type) LogMessage(#type).stream() -#define VLOG(level) if ((level) <= FLAGS_v) LOG(INFO) - - -// string utilities -void SplitToVector(char *line, const char *delim, - vector<char *> *vec, bool omit_empty_strings); - -// Downcasting -template<typename To, typename From> -inline To down_cast(From* f) { - return static_cast<To>(f); -} - -// Bitcasting -template <class Dest, class Source> -inline Dest bit_cast(const Source& source) { - // Compile time assertion: sizeof(Dest) == sizeof(Source) - // A compile error here means your Dest and Source have different sizes. - typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : - -1]; - Dest dest; - memcpy(&dest, &source, sizeof(dest)); - return dest; -} - -// MD5 checksums -class MD5 { - public: - MD5(); - void Reset(); - void Update(void const *data, int size); - string Digest(); - - private: - DISALLOW_EVIL_CONSTRUCTORS(MD5); -}; - -// Hashing functions -namespace __gnu_cxx { - -template<> struct hash<int64> { - size_t operator()(int64 x) const { - return x; - } -}; - -template<> struct hash<string> { - size_t operator()(const string &s) const { - return __stl_hash_string(s.c_str()); - } -}; - -} // namespace __gnu_cxx - -#endif // FST_COMPAT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/complement.h b/tools/thirdparty/OpenFst/fst/lib/complement.h deleted file mode 100644 index c727220..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/complement.h +++ /dev/null @@ -1,293 +0,0 @@ -// complement.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Class to complement an Fst. - -#ifndef FST_LIB_COMPLEMENT_H__ -#define FST_LIB_COMPLEMENT_H__ - -#include <algorithm> - -#include "fst/lib/fst.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -template <class A> class ComplementFst; - -// Implementation of delayed ComplementFst. The algorithm used -// completes the (deterministic) FSA and then exchanges final and -// non-final states. Completion, i.e. ensuring that all labels can be -// read from every state, is accomplished by using RHO labels, which -// match all labels that are otherwise not found leaving a state. The -// first state in the output is reserved to be a new state that is the -// destination of all RHO labels. Each remaining output state s -// corresponds to input state s - 1. The first arc in the output at -// these states is the rho label, the remaining arcs correspond to the -// input arcs. -template<class A> -class ComplementFstImpl : public FstImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - friend class StateIterator< ComplementFst<A> >; - friend class ArcIterator< ComplementFst<A> >; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - explicit ComplementFstImpl(const Fst<A> &fst) : fst_(fst.Copy()) { - SetType("complement"); - uint64 props = fst.Properties(kILabelSorted, false); - SetProperties(ComplementProperties(props), kCopyProperties); - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - } - - ~ComplementFstImpl() { delete fst_; } - - StateId Start() const { - StateId start = fst_->Start(); - if (start != kNoStateId) - return start + 1; - else - return 0; - } - - // Exchange final and non-final states; make rho destination state final. - Weight Final(StateId s) const { - if (s == 0 || fst_->Final(s - 1) == Weight::Zero()) - return Weight::One(); - else - return Weight::Zero(); - } - - size_t NumArcs(StateId s) const { - if (s == 0) - return 1; - else - return fst_->NumArcs(s - 1) + 1; - } - - size_t NumInputEpsilons(StateId s) const { - return s == 0 ? 0 : fst_->NumInputEpsilons(s - 1); - } - - size_t NumOutputEpsilons(StateId s) const { - return s == 0 ? 0 : fst_->NumOutputEpsilons(s - 1); - } - - private: - const Fst<A> *fst_; - - DISALLOW_EVIL_CONSTRUCTORS(ComplementFstImpl); -}; - - -// Complements an automaton; this is a library-internal operation -// that introduces the rho label. This version is a delayed Fst. -template <class A> -class ComplementFst : public Fst<A> { - public: - friend class StateIterator< ComplementFst<A> >; - friend class ArcIterator< ComplementFst<A> >; - - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - explicit ComplementFst(const Fst<A> &fst) - : impl_(new ComplementFstImpl<A>(fst)) { - uint64 props = kUnweighted | kNoEpsilons | kIDeterministic | kAcceptor; - if (fst.Properties(props, true) != props) - LOG(FATAL) << "ComplementFst: argument not an unweighted" - << " epsilon-free deterministic acceptor"; - } - - ComplementFst(const ComplementFst<A> &fst) : impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~ComplementFst() { if (!impl_->DecrRefCount()) { delete impl_; }} - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual ComplementFst<A> *Copy() const { - return new ComplementFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual inline void InitArcIterator(StateId s, - ArcIteratorData<A> *data) const; - - private: - ComplementFstImpl<A> *impl_; - - void operator=(const ComplementFst<A> &fst); // disallow -}; - - -// Specialization for ComplementFst. -template <class A> -class StateIterator< ComplementFst<A> > : public StateIteratorBase<A> { - public: - typedef typename A::StateId StateId; - typedef typename A::Label Label; - - explicit StateIterator(const ComplementFst<A> &fst) - : siter_(*fst.impl_->fst_), s_(0) { - } - - virtual bool Done() const { return s_ > 0 && siter_.Done(); } - virtual StateId Value() const { return s_; } - virtual void Next() { - if (s_ != 0) - siter_.Next(); - ++s_; - } - virtual void Reset() { - siter_.Reset(); - s_ = 0; - } - - private: - StateIterator< Fst<A> > siter_; - StateId s_; - - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - - -// Specialization for ComplementFst. -template <class A> -class ArcIterator< ComplementFst<A> > : public ArcIteratorBase<A> { - public: - typedef typename A::StateId StateId; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - - ArcIterator(const ComplementFst<A> &fst, StateId s) - : aiter_(0), s_(s), pos_(0) { - if (s_ != 0) - aiter_ = new ArcIterator< Fst<A> >(*fst.impl_->fst_, s - 1); - } - virtual ~ArcIterator() { delete aiter_; } - - virtual bool Done() const { - if (s_ != 0) - return pos_ > 0 && aiter_->Done(); - else - return pos_ > 0; - } - - // Adds the rho label to the rho destination state. - virtual const A& Value() const { - if (pos_ == 0) { - arc_.ilabel = arc_.olabel = kRhoLabel; - arc_.weight = Weight::One(); - arc_.nextstate = 0; - } else { - arc_ = aiter_->Value(); - ++arc_.nextstate; - } - return arc_; - } - virtual void Next() { - if (s_ != 0 && pos_ > 0) - aiter_->Next(); - ++pos_; - } - virtual void Reset() { - if (s_ != 0) - aiter_->Reset(); - pos_ = 0; - } - virtual void Seek(size_t a) { - if (s_ != 0) { - if (a == 0) { - aiter_->Reset(); - } else { - aiter_->Seek(a - 1); - } - } - pos_ = a; - } - - private: - ArcIterator< Fst<A> > *aiter_; - StateId s_; - size_t pos_; - mutable A arc_; - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - - -template <class A> inline void -ComplementFst<A>::InitStateIterator(StateIteratorData<A> *data) const { - data->base = new StateIterator< ComplementFst<A> >(*this); -} - -template <class A> inline void -ComplementFst<A>::InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - data->base = new ArcIterator< ComplementFst<A> >(*this, s); -} - - -// Useful alias when using StdArc. -typedef ComplementFst<StdArc> StdComplementFst; - -} // namespace fst - -#endif // FST_LIB_COMPLEMENT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/compose.h b/tools/thirdparty/OpenFst/fst/lib/compose.h deleted file mode 100644 index 24b169f..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/compose.h +++ /dev/null @@ -1,910 +0,0 @@ -// compose.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Class to compute the composition of two FSTs - -#ifndef FST_LIB_COMPOSE_H__ -#define FST_LIB_COMPOSE_H__ - -#include <algorithm> - -#include <ext/hash_map> -using __gnu_cxx::hash_map; - -#include "fst/lib/cache.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -// Enumeration of uint64 bits used to represent the user-defined -// properties of FST composition (in the template parameter to -// ComposeFstOptions<T>). The bits stand for extensions of generic FST -// composition. ComposeFstOptions<> (all the bits unset) is the "plain" -// compose without any extra extensions. -enum ComposeTypes { - // RHO: flags dealing with a special "rest" symbol in the FSTs. - // NB: at most one of the bits COMPOSE_FST1_RHO, COMPOSE_FST2_RHO - // may be set. - COMPOSE_FST1_RHO = 1ULL<<0, // "Rest" symbol on the output side of fst1. - COMPOSE_FST2_RHO = 1ULL<<1, // "Rest" symbol on the input side of fst2. - COMPOSE_FST1_PHI = 1ULL<<2, // "Failure" symbol on the output - // side of fst1. - COMPOSE_FST2_PHI = 1ULL<<3, // "Failure" symbol on the input side - // of fst2. - COMPOSE_FST1_SIGMA = 1ULL<<4, // "Any" symbol on the output side of - // fst1. - COMPOSE_FST2_SIGMA = 1ULL<<5, // "Any" symbol on the input side of - // fst2. - // Optimization related bits. - COMPOSE_GENERIC = 1ULL<<32, // Disables optimizations, applies - // the generic version of the - // composition algorithm. This flag - // is used for internal testing - // only. - - // ----------------------------------------------------------------- - // Auxiliary enum values denoting specific combinations of - // bits. Internal use only. - COMPOSE_RHO = COMPOSE_FST1_RHO | COMPOSE_FST2_RHO, - COMPOSE_PHI = COMPOSE_FST1_PHI | COMPOSE_FST2_PHI, - COMPOSE_SIGMA = COMPOSE_FST1_SIGMA | COMPOSE_FST2_SIGMA, - COMPOSE_SPECIAL_SYMBOLS = COMPOSE_RHO | COMPOSE_PHI | COMPOSE_SIGMA, - - // ----------------------------------------------------------------- - // The following bits, denoting specific optimizations, are - // typically set *internally* by the composition algorithm. - COMPOSE_FST1_STRING = 1ULL<<33, // fst1 is a string - COMPOSE_FST2_STRING = 1ULL<<34, // fst2 is a string - COMPOSE_FST1_DET = 1ULL<<35, // fst1 is deterministic - COMPOSE_FST2_DET = 1ULL<<36, // fst2 is deterministic - COMPOSE_INTERNAL_MASK = 0xffffffff00000000ULL -}; - - -template <uint64 T = 0ULL> -struct ComposeFstOptions : public CacheOptions { - explicit ComposeFstOptions(const CacheOptions &opts) : CacheOptions(opts) {} - ComposeFstOptions() { } -}; - - -// Abstract base for the implementation of delayed ComposeFst. The -// concrete specializations are templated on the (uint64-valued) -// properties of the FSTs being composed. -template <class A> -class ComposeFstImplBase : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - using CacheBaseImpl< CacheState<A> >::HasStart; - using CacheBaseImpl< CacheState<A> >::HasFinal; - using CacheBaseImpl< CacheState<A> >::HasArcs; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - ComposeFstImplBase(const Fst<A> &fst1, - const Fst<A> &fst2, - const CacheOptions &opts) - :CacheImpl<A>(opts), fst1_(fst1.Copy()), fst2_(fst2.Copy()) { - SetType("compose"); - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - SetProperties(ComposeProperties(props1, props2), kCopyProperties); - - if (!CompatSymbols(fst2.InputSymbols(), fst1.OutputSymbols())) - LOG(FATAL) << "ComposeFst: output symbol table of 1st argument " - << "does not match input symbol table of 2nd argument"; - - SetInputSymbols(fst1.InputSymbols()); - SetOutputSymbols(fst2.OutputSymbols()); - } - - virtual ~ComposeFstImplBase() { - delete fst1_; - delete fst2_; - } - - StateId Start() { - if (!HasStart()) { - StateId start = ComputeStart(); - if (start != kNoStateId) { - SetStart(start); - } - } - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - Weight final = ComputeFinal(s); - SetFinal(s, final); - } - return CacheImpl<A>::Final(s); - } - - virtual void Expand(StateId s) = 0; - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - // Access to flags encoding compose options/optimizations etc. (for - // debugging). - virtual uint64 ComposeFlags() const = 0; - - protected: - virtual StateId ComputeStart() = 0; - virtual Weight ComputeFinal(StateId s) = 0; - - const Fst<A> *fst1_; // first input Fst - const Fst<A> *fst2_; // second input Fst -}; - - -// The following class encapsulates implementation-dependent details -// of state tuple lookup, i.e. a bijective mapping from triples of two -// FST states and an epsilon filter state to the corresponding state -// IDs of the fst resulting from composition. The mapping must -// implement the [] operator in the style of STL associative -// containers (map, hash_map), i.e. table[x] must return a reference -// to the value associated with x. If x is an unassigned tuple, the -// operator must automatically associate x with value 0. -// -// NB: "table[x] == 0" for unassigned tuples x is required by the -// following off-by-one device used in the implementation of -// ComposeFstImpl. The value stored in the table is equal to tuple ID -// plus one, i.e. it is always a strictly positive number. Therefore, -// table[x] is equal to 0 if and only if x is an unassigned tuple (in -// which the algorithm assigns a new ID to x, and sets table[x] - -// stored in a reference - to "new ID + 1"). This form of lookup is -// more efficient than calling "find(x)" and "insert(make_pair(x, new -// ID))" if x is an unassigned tuple. -// -// The generic implementation is a wrapper around a hash_map. -template <class A, uint64 T> -class ComposeStateTable { - public: - typedef typename A::StateId StateId; - - struct StateTuple { - StateTuple() {} - StateTuple(StateId s1, StateId s2, int f) - : state_id1(s1), state_id2(s2), filt(f) {} - StateId state_id1; // state Id on fst1 - StateId state_id2; // state Id on fst2 - int filt; // epsilon filter state - }; - - ComposeStateTable() { - StateTuple empty_tuple(kNoStateId, kNoStateId, 0); - } - - // NB: if 'tuple' is not in 'table_', the pair (tuple, StateId()) is - // inserted into 'table_' (standard STL container semantics). Since - // StateId is a built-in type, the explicit default constructor call - // StateId() returns 0. - StateId &operator[](const StateTuple &tuple) { - return table_[tuple]; - } - - private: - // Comparison object for hashing StateTuple(s). - class StateTupleEqual { - public: - bool operator()(const StateTuple& x, const StateTuple& y) const { - return x.state_id1 == y.state_id1 && - x.state_id2 == y.state_id2 && - x.filt == y.filt; - } - }; - - static const int kPrime0 = 7853; - static const int kPrime1 = 7867; - - // Hash function for StateTuple to Fst states. - class StateTupleKey { - public: - size_t operator()(const StateTuple& x) const { - return static_cast<size_t>(x.state_id1 + - x.state_id2 * kPrime0 + - x.filt * kPrime1); - } - }; - - // Lookup table mapping state tuples to state IDs. - typedef hash_map<StateTuple, - StateId, - StateTupleKey, - StateTupleEqual> StateTable; - // Actual table data. - StateTable table_; - - DISALLOW_EVIL_CONSTRUCTORS(ComposeStateTable); -}; - - -// State tuple lookup table for the composition of a string FST with a -// deterministic FST. The class maps state tuples to their unique IDs -// (i.e. states of the ComposeFst). Main optimization: due to the -// 1-to-1 correspondence between the states of the input string FST -// and those of the resulting (string) FST, a state tuple (s1, s2) is -// simply mapped to StateId s1. Hence, we use an STL vector as a -// lookup table. Template argument Fst1IsString specifies which FST is -// a string (this determines whether or not we index the lookup table -// by the first or by the second state). -template <class A, bool Fst1IsString> -class StringDetComposeStateTable { - public: - typedef typename A::StateId StateId; - - struct StateTuple { - typedef typename A::StateId StateId; - StateTuple() {} - StateTuple(StateId s1, StateId s2, int /* f */) - : state_id1(s1), state_id2(s2) {} - StateId state_id1; // state Id on fst1 - StateId state_id2; // state Id on fst2 - static const int filt = 0; // 'fake' epsilon filter - only needed - // for API compatibility - }; - - StringDetComposeStateTable() {} - - // Subscript operator. Behaves in a way similar to its map/hash_map - // counterpart, i.e. returns a reference to the value associated - // with 'tuple', inserting a 0 value if 'tuple' is unassigned. - StateId &operator[](const StateTuple &tuple) { - StateId index = Fst1IsString ? tuple.state_id1 : tuple.state_id2; - if (index >= (StateId)data_.size()) { - // NB: all values in [old_size; index] are initialized to 0. - data_.resize(index + 1); - } - return data_[index]; - } - - private: - vector<StateId> data_; - - DISALLOW_EVIL_CONSTRUCTORS(StringDetComposeStateTable); -}; - - -// Specializations of ComposeStateTable for the string/det case. -// Both inherit from StringDetComposeStateTable. -template <class A> -class ComposeStateTable<A, COMPOSE_FST1_STRING | COMPOSE_FST2_DET> - : public StringDetComposeStateTable<A, true> { }; - -template <class A> -class ComposeStateTable<A, COMPOSE_FST2_STRING | COMPOSE_FST1_DET> - : public StringDetComposeStateTable<A, false> { }; - - -// Parameterized implementation of FST composition for a pair of FSTs -// matching the property bit vector T. If possible, -// instantiation-specific switches in the code are based on the values -// of the bits in T, which are known at compile time, so unused code -// should be optimized away by the compiler. -template <class A, uint64 T> -class ComposeFstImpl : public ComposeFstImplBase<A> { - typedef typename A::StateId StateId; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - - enum FindType { FIND_INPUT = 1, // find input label on fst2 - FIND_OUTPUT = 2, // find output label on fst1 - FIND_BOTH = 3 }; // find choice state dependent - - typedef ComposeStateTable<A, T & COMPOSE_INTERNAL_MASK> StateTupleTable; - typedef typename StateTupleTable::StateTuple StateTuple; - - public: - ComposeFstImpl(const Fst<A> &fst1, - const Fst<A> &fst2, - const CacheOptions &opts) - :ComposeFstImplBase<A>(fst1, fst2, opts) { - - bool osorted = fst1.Properties(kOLabelSorted, false); - bool isorted = fst2.Properties(kILabelSorted, false); - - switch (T & COMPOSE_SPECIAL_SYMBOLS) { - case COMPOSE_FST1_RHO: - case COMPOSE_FST1_PHI: - case COMPOSE_FST1_SIGMA: - if (!osorted || FLAGS_fst_verify_properties) - osorted = fst1.Properties(kOLabelSorted, true); - if (!osorted) - LOG(FATAL) << "ComposeFst: 1st argument not output label " - << "sorted (special symbols present)"; - break; - case COMPOSE_FST2_RHO: - case COMPOSE_FST2_PHI: - case COMPOSE_FST2_SIGMA: - if (!isorted || FLAGS_fst_verify_properties) - isorted = fst2.Properties(kILabelSorted, true); - if (!isorted) - LOG(FATAL) << "ComposeFst: 2nd argument not input label " - << "sorted (special symbols present)"; - break; - case 0: - if (!isorted && !osorted || FLAGS_fst_verify_properties) { - osorted = fst1.Properties(kOLabelSorted, true); - if (!osorted) - isorted = fst2.Properties(kILabelSorted, true); - } - break; - default: - LOG(FATAL) - << "ComposeFst: More than one special symbol used in composition"; - } - - if (isorted && (T & COMPOSE_FST2_SIGMA)) { - find_type_ = FIND_INPUT; - } else if (osorted && (T & COMPOSE_FST1_SIGMA)) { - find_type_ = FIND_OUTPUT; - } else if (isorted && (T & COMPOSE_FST2_PHI)) { - find_type_ = FIND_INPUT; - } else if (osorted && (T & COMPOSE_FST1_PHI)) { - find_type_ = FIND_OUTPUT; - } else if (isorted && (T & COMPOSE_FST2_RHO)) { - find_type_ = FIND_INPUT; - } else if (osorted && (T & COMPOSE_FST1_RHO)) { - find_type_ = FIND_OUTPUT; - } else if (isorted && (T & COMPOSE_FST1_STRING)) { - find_type_ = FIND_INPUT; - } else if(osorted && (T & COMPOSE_FST2_STRING)) { - find_type_ = FIND_OUTPUT; - } else if (isorted && osorted) { - find_type_ = FIND_BOTH; - } else if (isorted) { - find_type_ = FIND_INPUT; - } else if (osorted) { - find_type_ = FIND_OUTPUT; - } else { - LOG(FATAL) << "ComposeFst: 1st argument not output label sorted " - << "and 2nd argument is not input label sorted"; - } - } - - // Finds/creates an Fst state given a StateTuple. Only creates a new - // state if StateTuple is not found in the state hash. - // - // The method exploits the following device: all pairs stored in the - // associative container state_tuple_table_ are of the form (tuple, - // id(tuple) + 1), i.e. state_tuple_table_[tuple] > 0 if tuple has - // been stored previously. For unassigned tuples, the call to - // state_tuple_table_[tuple] creates a new pair (tuple, 0). As a - // result, state_tuple_table_[tuple] == 0 iff tuple is new. - StateId FindState(const StateTuple& tuple) { - StateId &assoc_value = state_tuple_table_[tuple]; - if (assoc_value == 0) { // tuple wasn't present in lookup table: - // assign it a new ID. - state_tuples_.push_back(tuple); - assoc_value = state_tuples_.size(); - } - return assoc_value - 1; // NB: assoc_value = ID + 1 - } - - // Generates arc for composition state s from matched input Fst arcs. - void AddArc(StateId s, const A &arca, const A &arcb, int f, - bool find_input) { - A arc; - if (find_input) { - arc.ilabel = arcb.ilabel; - arc.olabel = arca.olabel; - arc.weight = Times(arcb.weight, arca.weight); - StateTuple tuple(arcb.nextstate, arca.nextstate, f); - arc.nextstate = FindState(tuple); - } else { - arc.ilabel = arca.ilabel; - arc.olabel = arcb.olabel; - arc.weight = Times(arca.weight, arcb.weight); - StateTuple tuple(arca.nextstate, arcb.nextstate, f); - arc.nextstate = FindState(tuple); - } - CacheImpl<A>::AddArc(s, arc); - } - - // Arranges it so that the first arg to OrderedExpand is the Fst - // that will be passed to FindLabel. - void Expand(StateId s) { - StateTuple &tuple = state_tuples_[s]; - StateId s1 = tuple.state_id1; - StateId s2 = tuple.state_id2; - int f = tuple.filt; - if (find_type_ == FIND_INPUT) - OrderedExpand(s, ComposeFstImplBase<A>::fst2_, s2, - ComposeFstImplBase<A>::fst1_, s1, f, true); - else - OrderedExpand(s, ComposeFstImplBase<A>::fst1_, s1, - ComposeFstImplBase<A>::fst2_, s2, f, false); - } - - // Access to flags encoding compose options/optimizations etc. (for - // debugging). - virtual uint64 ComposeFlags() const { return T; } - - private: - // This does that actual matching of labels in the composition. The - // arguments are ordered so FindLabel is called with state SA of - // FSTA for each arc leaving state SB of FSTB. The FIND_INPUT arg - // determines whether the input or output label of arcs at SB is - // the one to match on. - void OrderedExpand(StateId s, const Fst<A> *fsta, StateId sa, - const Fst<A> *fstb, StateId sb, int f, bool find_input) { - - size_t numarcsa = fsta->NumArcs(sa); - size_t numepsa = find_input ? fsta->NumInputEpsilons(sa) : - fsta->NumOutputEpsilons(sa); - bool finala = fsta->Final(sa) != Weight::Zero(); - ArcIterator< Fst<A> > aitera(*fsta, sa); - // First handle special epsilons and sigmas on FSTA - for (; !aitera.Done(); aitera.Next()) { - const A &arca = aitera.Value(); - Label match_labela = find_input ? arca.ilabel : arca.olabel; - if (match_labela > 0) { - break; - } - if ((T & COMPOSE_SIGMA) != 0 && match_labela == kSigmaLabel) { - // Found a sigma? Match it against all (non-special) symbols - // on side b. - for (ArcIterator< Fst<A> > aiterb(*fstb, sb); - !aiterb.Done(); - aiterb.Next()) { - const A &arcb = aiterb.Value(); - Label labelb = find_input ? arcb.olabel : arcb.ilabel; - if (labelb <= 0) continue; - AddArc(s, arca, arcb, 0, find_input); - } - } else if (f == 0 && match_labela == 0) { - A earcb(0, 0, Weight::One(), sb); - AddArc(s, arca, earcb, 0, find_input); // move forward on epsilon - } - } - // Next handle non-epsilon matches, rho labels, and epsilons on FSTB - for (ArcIterator< Fst<A> > aiterb(*fstb, sb); - !aiterb.Done(); - aiterb.Next()) { - const A &arcb = aiterb.Value(); - Label match_labelb = find_input ? arcb.olabel : arcb.ilabel; - if (match_labelb) { // Consider non-epsilon match - if (FindLabel(&aitera, numarcsa, match_labelb, find_input)) { - for (; !aitera.Done(); aitera.Next()) { - const A &arca = aitera.Value(); - Label match_labela = find_input ? arca.ilabel : arca.olabel; - if (match_labela != match_labelb) - break; - AddArc(s, arca, arcb, 0, find_input); // move forward on match - } - } else if ((T & COMPOSE_SPECIAL_SYMBOLS) != 0) { - // If there is no transition labelled 'match_labelb' in - // fsta, try matching 'match_labelb' against special symbols - // (Phi, Rho,...). - for (aitera.Reset(); !aitera.Done(); aitera.Next()) { - A arca = aitera.Value(); - Label labela = find_input ? arca.ilabel : arca.olabel; - if (labela >= 0) { - break; - } else if (((T & COMPOSE_PHI) != 0) && (labela == kPhiLabel)) { - // Case 1: if a failure transition exists, follow its - // transitive closure until a) a transition labelled - // 'match_labelb' is found, or b) the initial state of - // fsta is reached. - - StateId sf = sa; // Start of current failure transition. - while (labela == kPhiLabel && sf != arca.nextstate) { - sf = arca.nextstate; - - size_t numarcsf = fsta->NumArcs(sf); - ArcIterator< Fst<A> > aiterf(*fsta, sf); - if (FindLabel(&aiterf, numarcsf, match_labelb, find_input)) { - // Sub-case 1a: there exists a transition starting - // in sf and consuming symbol 'match_labelb'. - AddArc(s, aiterf.Value(), arcb, 0, find_input); - break; - } else { - // No transition labelled 'match_labelb' found: try - // next failure transition (starting at 'sf'). - for (aiterf.Reset(); !aiterf.Done(); aiterf.Next()) { - arca = aiterf.Value(); - labela = find_input ? arca.ilabel : arca.olabel; - if (labela >= kPhiLabel) break; - } - } - } - if (labela == kPhiLabel && sf == arca.nextstate) { - // Sub-case 1b: failure transitions lead to start - // state without finding a matching - // transition. Therefore, we generate a loop in start - // state of fsta. - A loop(match_labelb, match_labelb, Weight::One(), sf); - AddArc(s, loop, arcb, 0, find_input); - } - } else if (((T & COMPOSE_RHO) != 0) && (labela == kRhoLabel)) { - // Case 2: 'match_labelb' can be matched against a - // "rest" (rho) label in fsta. - if (find_input) { - arca.ilabel = match_labelb; - if (arca.olabel == kRhoLabel) - arca.olabel = match_labelb; - } else { - arca.olabel = match_labelb; - if (arca.ilabel == kRhoLabel) - arca.ilabel = match_labelb; - } - AddArc(s, arca, arcb, 0, find_input); // move fwd on match - } - } - } - } else if (numepsa != numarcsa || finala) { // Handle FSTB epsilon - A earca(0, 0, Weight::One(), sa); - AddArc(s, earca, arcb, numepsa > 0, find_input); // move on epsilon - } - } - SetArcs(s); - } - - - // Finds matches to MATCH_LABEL in arcs given by AITER - // using FIND_INPUT to determine whether to look on input or output. - bool FindLabel(ArcIterator< Fst<A> > *aiter, size_t numarcs, - Label match_label, bool find_input) { - // binary search for match - size_t low = 0; - size_t high = numarcs; - while (low < high) { - size_t mid = (low + high) / 2; - aiter->Seek(mid); - Label label = find_input ? - aiter->Value().ilabel : aiter->Value().olabel; - if (label > match_label) { - high = mid; - } else if (label < match_label) { - low = mid + 1; - } else { - // find first matching label (when non-determinism) - for (size_t i = mid; i > low; --i) { - aiter->Seek(i - 1); - label = find_input ? aiter->Value().ilabel : aiter->Value().olabel; - if (label != match_label) { - aiter->Seek(i); - return true; - } - } - return true; - } - } - return false; - } - - StateId ComputeStart() { - StateId s1 = ComposeFstImplBase<A>::fst1_->Start(); - StateId s2 = ComposeFstImplBase<A>::fst2_->Start(); - if (s1 == kNoStateId || s2 == kNoStateId) - return kNoStateId; - StateTuple tuple(s1, s2, 0); - return FindState(tuple); - } - - Weight ComputeFinal(StateId s) { - StateTuple &tuple = state_tuples_[s]; - Weight final = Times(ComposeFstImplBase<A>::fst1_->Final(tuple.state_id1), - ComposeFstImplBase<A>::fst2_->Final(tuple.state_id2)); - return final; - } - - - FindType find_type_; // find label on which side? - - // Maps from StateId to StateTuple. - vector<StateTuple> state_tuples_; - - // Maps from StateTuple to StateId. - StateTupleTable state_tuple_table_; - - DISALLOW_EVIL_CONSTRUCTORS(ComposeFstImpl); -}; - - -// Computes the composition of two transducers. This version is a -// delayed Fst. If FST1 transduces string x to y with weight a and FST2 -// transduces y to z with weight b, then their composition transduces -// string x to z with weight Times(x, z). -// -// The output labels of the first transducer or the input labels of -// the second transducer must be sorted. The weights need to form a -// commutative semiring (valid for TropicalWeight and LogWeight). -// -// Complexity: -// Assuming the first FST is unsorted and the second is sorted: -// - Time: O(v1 v2 d1 (log d2 + m2)), -// - Space: O(v1 v2) -// where vi = # of states visited, di = maximum out-degree, and mi the -// maximum multiplicity of the states visited for the ith -// FST. Constant time and space to visit an input state or arc is -// assumed and exclusive of caching. -// -// Caveats: -// - ComposeFst does not trim its output (since it is a delayed operation). -// - The efficiency of composition can be strongly affected by several factors: -// - the choice of which tnansducer is sorted - prefer sorting the FST -// that has the greater average out-degree. -// - the amount of non-determinism -// - the presence and location of epsilon transitions - avoid epsilon -// transitions on the output side of the first transducer or -// the input side of the second transducer or prefer placing -// them later in a path since they delay matching and can -// introduce non-coaccessible states and transitions. -template <class A> -class ComposeFst : public Fst<A> { - public: - friend class ArcIterator< ComposeFst<A> >; - friend class CacheStateIterator< ComposeFst<A> >; - friend class CacheArcIterator< ComposeFst<A> >; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - ComposeFst(const Fst<A> &fst1, const Fst<A> &fst2) - : impl_(Init(fst1, fst2, ComposeFstOptions<>())) { } - - template <uint64 T> - ComposeFst(const Fst<A> &fst1, - const Fst<A> &fst2, - const ComposeFstOptions<T> &opts) - : impl_(Init(fst1, fst2, opts)) { } - - ComposeFst(const ComposeFst<A> &fst) : Fst<A>(fst), impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~ComposeFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual ComposeFst<A> *Copy() const { - return new ComposeFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - // Access to flags encoding compose options/optimizations etc. (for - // debugging). - uint64 ComposeFlags() const { return impl_->ComposeFlags(); } - - protected: - ComposeFstImplBase<A> *Impl() { return impl_; } - - private: - ComposeFstImplBase<A> *impl_; - - // Auxiliary method encapsulating the creation of a ComposeFst - // implementation that is appropriate for the properties of fst1 and - // fst2. - template <uint64 T> - static ComposeFstImplBase<A> *Init( - const Fst<A> &fst1, - const Fst<A> &fst2, - const ComposeFstOptions<T> &opts) { - - // Filter for sort properties (forces a property check). - uint64 sort_props_mask = kILabelSorted | kOLabelSorted; - // Filter for optimization-related properties (does not force a - // property-check). - uint64 opt_props_mask = - kString | kIDeterministic | kODeterministic | kNoIEpsilons | - kNoOEpsilons; - - uint64 props1 = fst1.Properties(sort_props_mask, true); - uint64 props2 = fst2.Properties(sort_props_mask, true); - - props1 |= fst1.Properties(opt_props_mask, false); - props2 |= fst2.Properties(opt_props_mask, false); - - if (!(Weight::Properties() & kCommutative)) { - props1 |= fst1.Properties(kUnweighted, true); - props2 |= fst2.Properties(kUnweighted, true); - if (!(props1 & kUnweighted) && !(props2 & kUnweighted)) - LOG(FATAL) << "ComposeFst: Weight needs to be a commutative semiring: " - << Weight::Type(); - } - - // Case 1: flag COMPOSE_GENERIC disables optimizations. - if (T & COMPOSE_GENERIC) { - return new ComposeFstImpl<A, T>(fst1, fst2, opts); - } - - const uint64 kStringDetOptProps = - kIDeterministic | kILabelSorted | kNoIEpsilons; - const uint64 kDetStringOptProps = - kODeterministic | kOLabelSorted | kNoOEpsilons; - - // Case 2: fst1 is a string, fst2 is deterministic and epsilon-free. - if ((props1 & kString) && - !(T & (COMPOSE_FST1_RHO | COMPOSE_FST1_PHI | COMPOSE_FST1_SIGMA)) && - ((props2 & kStringDetOptProps) == kStringDetOptProps)) { - return new ComposeFstImpl<A, T | COMPOSE_FST1_STRING | COMPOSE_FST2_DET>( - fst1, fst2, opts); - } - // Case 3: fst1 is deterministic and epsilon-free, fst2 is string. - if ((props2 & kString) && - !(T & (COMPOSE_FST1_RHO | COMPOSE_FST1_PHI | COMPOSE_FST1_SIGMA)) && - ((props1 & kDetStringOptProps) == kDetStringOptProps)) { - return new ComposeFstImpl<A, T | COMPOSE_FST2_STRING | COMPOSE_FST1_DET>( - fst1, fst2, opts); - } - - // Default case: no optimizations. - return new ComposeFstImpl<A, T>(fst1, fst2, opts); - } - - void operator=(const ComposeFst<A> &fst); // disallow -}; - - -// Specialization for ComposeFst. -template<class A> -class StateIterator< ComposeFst<A> > - : public CacheStateIterator< ComposeFst<A> > { - public: - explicit StateIterator(const ComposeFst<A> &fst) - : CacheStateIterator< ComposeFst<A> >(fst) {} -}; - - -// Specialization for ComposeFst. -template <class A> -class ArcIterator< ComposeFst<A> > - : public CacheArcIterator< ComposeFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const ComposeFst<A> &fst, StateId s) - : CacheArcIterator< ComposeFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -template <class A> inline -void ComposeFst<A>::InitStateIterator(StateIteratorData<A> *data) const { - data->base = new StateIterator< ComposeFst<A> >(*this); -} - -// Useful alias when using StdArc. -typedef ComposeFst<StdArc> StdComposeFst; - - -struct ComposeOptions { - bool connect; // Connect output - - ComposeOptions(bool c) : connect(c) {} - ComposeOptions() : connect(true) { } -}; - - -// Computes the composition of two transducers. This version writes -// the composed FST into a MurableFst. If FST1 transduces string x to -// y with weight a and FST2 transduces y to z with weight b, then -// their composition transduces string x to z with weight -// Times(x, z). -// -// The output labels of the first transducer or the input labels of -// the second transducer must be sorted. The weights need to form a -// commutative semiring (valid for TropicalWeight and LogWeight). -// -// Complexity: -// Assuming the first FST is unsorted and the second is sorted: -// - Time: O(V1 V2 D1 (log D2 + M2)), -// - Space: O(V1 V2 D1 M2) -// where Vi = # of states, Di = maximum out-degree, and Mi is -// the maximum multiplicity for the ith FST. -// -// Caveats: -// - Compose trims its output. -// - The efficiency of composition can be strongly affected by several factors: -// - the choice of which tnansducer is sorted - prefer sorting the FST -// that has the greater average out-degree. -// - the amount of non-determinism -// - the presence and location of epsilon transitions - avoid epsilon -// transitions on the output side of the first transducer or -// the input side of the second transducer or prefer placing -// them later in a path since they delay matching and can -// introduce non-coaccessible states and transitions. -template<class Arc> -void Compose(const Fst<Arc> &ifst1, const Fst<Arc> &ifst2, - MutableFst<Arc> *ofst, - const ComposeOptions &opts = ComposeOptions()) { - ComposeFstOptions<> nopts; - nopts.gc_limit = 0; // Cache only the last state for fastest copy. - *ofst = ComposeFst<Arc>(ifst1, ifst2, nopts); - if (opts.connect) - Connect(ofst); -} - -} // namespace fst - -#endif // FST_LIB_COMPOSE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/concat.h b/tools/thirdparty/OpenFst/fst/lib/concat.h deleted file mode 100644 index 4100ec5..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/concat.h +++ /dev/null @@ -1,153 +0,0 @@ -// concat.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to compute the concat of two FSTs. - -#ifndef FST_LIB_CONCAT_H__ -#define FST_LIB_CONCAT_H__ - -#include <algorithm> - -#include "fst/lib/mutable-fst.h" -#include "fst/lib/rational.h" - -namespace fst { - -// Computes the concatenation (product) of two FSTs; this version -// modifies its MutableFst argument. If FST1 transduces string x to y -// with weight a and FST2 transduces string w to v with weight b, then -// their concatenation transduces string xw to yv with Times(a, b). -// -// Complexity: -// - Time: O(V1 + V2 + E2) -// - Space: O(V1 + V2 + E2) -// where Vi = # of states and Ei = # of arcs of the ith FST. -template<class Arc> -void Concat(MutableFst<Arc> *fst1, const Fst<Arc> &fst2) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Label Label; - typedef typename Arc::Weight Weight; - - StateId start1 = fst1->Start(); - if (start1 == kNoStateId) - return; - - uint64 props1 = fst1->Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - - StateId numstates1= fst1->NumStates(); - - for (StateIterator< Fst<Arc> > siter2(fst2); - !siter2.Done(); - siter2.Next()) { - StateId s1 = fst1->AddState(); - StateId s2 = siter2.Value(); - fst1->SetFinal(s1, fst2.Final(s2)); - for (ArcIterator< Fst<Arc> > aiter(fst2, s2); - !aiter.Done(); - aiter.Next()) { - Arc arc = aiter.Value(); - arc.nextstate += numstates1; - fst1->AddArc(s1, arc); - } - } - - StateId start2 = fst2.Start(); - for (StateId s1 = 0; s1 < numstates1; ++s1) { - Weight final = fst1->Final(s1); - if (final != Weight::Zero()) { - fst1->SetFinal(s1, Weight::Zero()); - if (start2 != kNoStateId) - fst1->AddArc(s1, Arc(0, 0, final, start2 + numstates1)); - } - } - if (start2 != kNoStateId) - fst1->SetProperties(ConcatProperties(props1, props2), kFstProperties); -} - - -// Computes the concatentation of two FSTs. This version modifies its -// RationalFst input. -template<class Arc> -void Concat(RationalFst<Arc> *fst1, const Fst<Arc> &fst2) { - fst1->Impl()->AddConcat(fst2); -} - - -typedef RationalFstOptions ConcatFstOptions; - - -// Computes the concatenation (product) of two FSTs; this version is a -// delayed Fst. If FST1 transduces string x to y with weight a and FST2 -// transduces string w to v with weight b, then their concatenation -// transduces string xw to yv with Times(a, b). -// -// Complexity: -// - Time: O(v1 + e1 + v2 + e2), -// - Space: O(v1 + v2) -// where vi = # of states visited and ei = # of arcs visited of the -// ith FST. Constant time and space to visit an input state or arc is -// assumed and exclusive of caching. -template <class A> -class ConcatFst : public RationalFst<A> { - public: - using RationalFst<A>::Impl; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - ConcatFst(const Fst<A> &fst1, const Fst<A> &fst2) { - Impl()->InitConcat(fst1, fst2); - } - - ConcatFst(const Fst<A> &fst1, const Fst<A> &fst2, - const ConcatFstOptions &opts) : RationalFst<A>(opts) { - Impl()->InitConcat(fst1, fst2); - } - - ConcatFst(const ConcatFst<A> &fst) : RationalFst<A>(fst) {} - - virtual ConcatFst<A> *Copy() const { return new ConcatFst<A>(*this); } -}; - - -// Specialization for ConcatFst. -template <class A> -class StateIterator< ConcatFst<A> > : public StateIterator< RationalFst<A> > { - public: - explicit StateIterator(const ConcatFst<A> &fst) - : StateIterator< RationalFst<A> >(fst) {} -}; - - -// Specialization for ConcatFst. -template <class A> -class ArcIterator< ConcatFst<A> > : public ArcIterator< RationalFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const ConcatFst<A> &fst, StateId s) - : ArcIterator< RationalFst<A> >(fst, s) {} -}; - - -// Useful alias when using StdArc. -typedef ConcatFst<StdArc> StdConcatFst; - -} // namespace fst - -#endif // FST_LIB_CONCAT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/connect.h b/tools/thirdparty/OpenFst/fst/lib/connect.h deleted file mode 100644 index 27f781f..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/connect.h +++ /dev/null @@ -1,220 +0,0 @@ -// connect.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Classes and functions to remove unsuccessful paths from an Fst. - -#ifndef FST_LIB_CONNECT_H__ -#define FST_LIB_CONNECT_H__ - -#include "fst/lib/mutable-fst.h" - -namespace fst { - -// Finds and returns strongly-connected components, accessible and -// coaccessible states and related properties. Uses Tarzan's single -// DFS SCC algorithm (see Aho, et al, "Design and Analysis of Computer -// Algorithms", 189pp). -template <class A> -class SccVisitor { - public: - typedef A Arc; - typedef typename Arc::Weight Weight; - typedef typename A::StateId StateId; - - // scc[i]: strongly-connected component number for state i. - // SCC numbers will be in topological order for acyclic input. - // access[i]: accessibility of state i. - // coaccess[i]: coaccessibility of state i. - // Any of above can be NULL. - // props: related property bits (cyclicity, initial cyclicity, - // accessibiliity, coaccessiblity) set/cleared (o.w. unchanged). - SccVisitor(vector<StateId> *scc, vector<bool> *access, - vector<bool> *coaccess, uint64 *props) - : scc_(scc), access_(access), coaccess_(coaccess), props_(props) {} - SccVisitor(uint64 *props) - : scc_(0), access_(0), coaccess_(0), props_(props) {} - - void InitVisit(const Fst<A> &fst) { - if (scc_) - scc_->clear(); - if (access_) - access_->clear(); - if (coaccess_) { - coaccess_->clear(); - coaccess_internal_ = false; - } else { - coaccess_ = new vector<bool>; - coaccess_internal_ = true; - } - *props_ |= kAcyclic | kInitialAcyclic | kAccessible | kCoAccessible; - *props_ &= ~(kCyclic | kInitialCyclic | kNotAccessible | kNotCoAccessible); - fst_ = &fst; - start_ = fst.Start(); - nstates_ = 0; - nscc_ = 0; - dfnumber_ = new vector<StateId>; - lowlink_ = new vector<StateId>; - onstack_ = new vector<bool>; - scc_stack_ = new vector<StateId>; - } - - bool InitState(StateId s, StateId root) { - scc_stack_->push_back(s); - while ((StateId)dfnumber_->size() <= s) { - if (scc_) - scc_->push_back(-1); - if (access_) - access_->push_back(false); - coaccess_->push_back(false); - dfnumber_->push_back(-1); - lowlink_->push_back(-1); - onstack_->push_back(false); - } - (*dfnumber_)[s] = nstates_; - (*lowlink_)[s] = nstates_; - (*onstack_)[s] = true; - if (root == start_) { - if (access_) - (*access_)[s] = true; - } else { - if (access_) - (*access_)[s] = false; - *props_ |= kNotAccessible; - *props_ &= ~kAccessible; - } - ++nstates_; - return true; - } - - bool TreeArc(StateId s, const A &arc) { return true; } - - bool BackArc(StateId s, const A &arc) { - StateId t = arc.nextstate; - if ((*dfnumber_)[t] < (*lowlink_)[s]) - (*lowlink_)[s] = (*dfnumber_)[t]; - if ((*coaccess_)[t]) - (*coaccess_)[s] = true; - *props_ |= kCyclic; - *props_ &= ~kAcyclic; - if (arc.nextstate == start_) { - *props_ |= kInitialCyclic; - *props_ &= ~kInitialAcyclic; - } - return true; - } - - bool ForwardOrCrossArc(StateId s, const A &arc) { - StateId t = arc.nextstate; - if ((*dfnumber_)[t] < (*dfnumber_)[s] /* cross edge */ && - (*onstack_)[t] && (*dfnumber_)[t] < (*lowlink_)[s]) - (*lowlink_)[s] = (*dfnumber_)[t]; - if ((*coaccess_)[t]) - (*coaccess_)[s] = true; - return true; - } - - void FinishState(StateId s, StateId p, const A *) { - if (fst_->Final(s) != Weight::Zero()) - (*coaccess_)[s] = true; - if ((*dfnumber_)[s] == (*lowlink_)[s]) { // root of new SCC - bool scc_coaccess = false; - size_t i = scc_stack_->size(); - StateId t; - do { - t = (*scc_stack_)[--i]; - if ((*coaccess_)[t]) - scc_coaccess = true; - } while (s != t); - do { - t = scc_stack_->back(); - if (scc_) - (*scc_)[t] = nscc_; - if (scc_coaccess) - (*coaccess_)[t] = true; - (*onstack_)[t] = false; - scc_stack_->pop_back(); - } while (s != t); - if (!scc_coaccess) { - *props_ |= kNotCoAccessible; - *props_ &= ~kCoAccessible; - } - ++nscc_; - } - if (p != kNoStateId) { - if ((*coaccess_)[s]) - (*coaccess_)[p] = true; - if ((*lowlink_)[s] < (*lowlink_)[p]) - (*lowlink_)[p] = (*lowlink_)[s]; - } - } - - void FinishVisit() { - // Numbers SCC's in topological order when acyclic. - if (scc_) - for (StateId i = 0; i < (StateId)scc_->size(); ++i) - (*scc_)[i] = nscc_ - 1 - (*scc_)[i]; - if (coaccess_internal_) - delete coaccess_; - delete dfnumber_; - delete lowlink_; - delete onstack_; - delete scc_stack_; - } - - private: - vector<StateId> *scc_; // State's scc number - vector<bool> *access_; // State's accessibility - vector<bool> *coaccess_; // State's coaccessibility - uint64 *props_; - const Fst<A> *fst_; - StateId start_; - StateId nstates_; // State count - StateId nscc_; // SCC count - bool coaccess_internal_; - vector<StateId> *dfnumber_; // state discovery times - vector<StateId> *lowlink_; // lowlink[s] == dfnumber[s] => SCC root - vector<bool> *onstack_; // is a state on the SCC stack - vector<StateId> *scc_stack_; // SCC stack (w/ random access) -}; - - -// Trims an FST, removing states and arcs that are not on successful -// paths. This version modifies its input. -// -// Complexity: -// - Time: O(V + E) -// - Space: O(V + E) -// where V = # of states and E = # of arcs. -template<class Arc> -void Connect(MutableFst<Arc> *fst) { - typedef typename Arc::StateId StateId; - - vector<bool> access; - vector<bool> coaccess; - uint64 props = 0; - SccVisitor<Arc> scc_visitor(0, &access, &coaccess, &props); - DfsVisit(*fst, &scc_visitor); - vector<StateId> dstates; - for (StateId s = 0; s < (StateId)access.size(); ++s) - if (!access[s] || !coaccess[s]) - dstates.push_back(s); - fst->DeleteStates(dstates); - fst->SetProperties(kAccessible | kCoAccessible, kAccessible | kCoAccessible); -} - -} // namespace fst - -#endif // FST_LIB_CONNECT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/const-fst.h b/tools/thirdparty/OpenFst/fst/lib/const-fst.h deleted file mode 100644 index 74f0c91..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/const-fst.h +++ /dev/null @@ -1,394 +0,0 @@ -// const-fst.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Simple concrete immutable FST whose states and arcs are each stored -// in single arrays. - -#ifndef FST_LIB_CONST_FST_H__ -#define FST_LIB_CONST_FST_H__ - -#include "fst/lib/expanded-fst.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -template <class A> class ConstFst; - -// States and arcs each implemented by single arrays, templated on the -// Arc definition. -template <class A> -class ConstFstImpl : public FstImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::WriteHeaderAndSymbols; - - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - ConstFstImpl() - : states_(0), arcs_(0), nstates_(0), narcs_(0), start_(kNoStateId) { - SetType("const"); - SetProperties(kNullProperties | kStaticProperties); - } - - explicit ConstFstImpl(const Fst<A> &fst); - - ~ConstFstImpl() { - delete[] states_; - delete[] arcs_; - } - - StateId Start() const { return start_; } - - Weight Final(StateId s) const { return states_[s].final; } - - StateId NumStates() const { return nstates_; } - - size_t NumArcs(StateId s) const { return states_[s].narcs; } - - size_t NumInputEpsilons(StateId s) const { return states_[s].niepsilons; } - - size_t NumOutputEpsilons(StateId s) const { return states_[s].noepsilons; } - - static ConstFstImpl<A> *Read(istream &strm, const FstReadOptions &opts); - - bool Write(ostream &strm, const FstWriteOptions &opts) const; - - A *Arcs(StateId s) { return arcs_ + states_[s].pos; } - - // Provide information needed for generic state iterator - void InitStateIterator(StateIteratorData<A> *data) const { - data->base = 0; - data->nstates = nstates_; - } - - // Provide information needed for the generic arc iterator - void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - data->base = 0; - data->arcs = arcs_ + states_[s].pos; - data->narcs = states_[s].narcs; - data->ref_count = 0; - } - - private: - // States implemented by array *states_ below, arcs by (single) *arcs_. - struct State { - Weight final; // Final weight - uint32 pos; // Start of state's arcs in *arcs_ - uint32 narcs; // Number of arcs (per state) - uint32 niepsilons; // # of input epsilons - uint32 noepsilons; // # of output epsilons - State() : final(Weight::Zero()), niepsilons(0), noepsilons(0) {} - }; - - // Properties always true of this Fst class - static const uint64 kStaticProperties = kExpanded; - // Current file format version - static const int kFileVersion = 1; - // Minimum file format version supported - static const int kMinFileVersion = 1; - // Byte alignment for states and arcs in file format - static const int kFileAlign = 16; - - State *states_; // States represenation - A *arcs_; // Arcs representation - StateId nstates_; // Number of states - size_t narcs_; // Number of arcs (per FST) - StateId start_; // Initial state - - DISALLOW_EVIL_CONSTRUCTORS(ConstFstImpl); -}; - -template<class A> -ConstFstImpl<A>::ConstFstImpl(const Fst<A> &fst) : nstates_(0), narcs_(0) { - SetType("const"); - uint64 copy_properties = fst.Properties(kCopyProperties, true); - SetProperties(copy_properties | kStaticProperties); - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - start_ = fst.Start(); - - // count # of states and arcs - for (StateIterator< Fst<A> > siter(fst); - !siter.Done(); - siter.Next()) { - ++nstates_; - StateId s = siter.Value(); - for (ArcIterator< Fst<A> > aiter(fst, s); - !aiter.Done(); - aiter.Next()) - ++narcs_; - } - states_ = new State[nstates_]; - arcs_ = new A[narcs_]; - size_t pos = 0; - for (StateId s = 0; s < nstates_; ++s) { - states_[s].final = fst.Final(s); - states_[s].pos = pos; - states_[s].narcs = 0; - states_[s].niepsilons = 0; - states_[s].noepsilons = 0; - for (ArcIterator< Fst<A> > aiter(fst, s); - !aiter.Done(); - aiter.Next()) { - const A &arc = aiter.Value(); - ++states_[s].narcs; - if (arc.ilabel == 0) - ++states_[s].niepsilons; - if (arc.olabel == 0) - ++states_[s].noepsilons; - arcs_[pos++] = arc; - } - } -} - -template<class A> -ConstFstImpl<A> *ConstFstImpl<A>::Read(istream &strm, - const FstReadOptions &opts) { - ConstFstImpl<A> *impl = new ConstFstImpl<A>; - FstHeader hdr; - if (!impl->ReadHeaderAndSymbols(strm, opts, kMinFileVersion, &hdr)) - return 0; - impl->start_ = hdr.Start(); - impl->nstates_ = hdr.NumStates(); - impl->narcs_ = hdr.NumArcs(); - impl->states_ = new State[impl->nstates_]; - impl->arcs_ = new A[impl->narcs_]; - - char c; - for (int i = 0; i < kFileAlign && strm.tellg() % kFileAlign; ++i) - strm.read(&c, 1); - // TODO: memory map this - size_t b = impl->nstates_ * sizeof(typename ConstFstImpl<A>::State); - strm.read(reinterpret_cast<char *>(impl->states_), b); - if (!strm) { - LOG(ERROR) << "ConstFst::Read: Read failed: " << opts.source; - return 0; - } - // TODO: memory map this - b = impl->narcs_ * sizeof(A); - for (int i = 0; i < kFileAlign && strm.tellg() % kFileAlign; ++i) - strm.read(&c, 1); - strm.read(reinterpret_cast<char *>(impl->arcs_), b); - if (!strm) { - LOG(ERROR) << "ConstFst::Read: Read failed: " << opts.source; - return 0; - } - return impl; -} - -template<class A> -bool ConstFstImpl<A>::Write(ostream &strm, - const FstWriteOptions &opts) const { - FstHeader hdr; - hdr.SetStart(start_); - hdr.SetNumStates(nstates_); - hdr.SetNumArcs(narcs_); - WriteHeaderAndSymbols(strm, opts, kFileVersion, &hdr); - if (!strm) - return false; - - for (int i = 0; i < kFileAlign && strm.tellp() % kFileAlign; ++i) - strm.write("", 1); - strm.write(reinterpret_cast<char *>(states_), - nstates_ * sizeof(State)); - for (int i = 0; i < kFileAlign && strm.tellp() % kFileAlign; ++i) - strm.write("", 1); - strm.write(reinterpret_cast<char *>(arcs_), narcs_ * sizeof(A)); - strm.flush(); - if (!strm) - LOG(ERROR) << "ConstFst::Write: Write failed: " << opts.source; - return strm; -} - -// Simple concrete immutable FST. This class attaches interface to -// implementation and handles reference counting. -template <class A> -class ConstFst : public ExpandedFst<A> { - public: - friend class StateIterator< ConstFst<A> >; - friend class ArcIterator< ConstFst<A> >; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - ConstFst() : impl_(new ConstFstImpl<A>()) {} - - ConstFst(const ConstFst<A> &fst) : impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - explicit ConstFst(const Fst<A> &fst) : impl_(new ConstFstImpl<A>(fst)) {} - - virtual ~ConstFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - StateId NumStates() const { return impl_->NumStates(); } - - size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - // Get a copy of this ConstFst - virtual ConstFst<A> *Copy() const { - impl_->IncrRefCount(); - return new ConstFst<A>(impl_); - } - - // Read a ConstFst from an input stream; return NULL on error - static ConstFst<A> *Read(istream &strm, const FstReadOptions &opts) { - ConstFstImpl<A>* impl = ConstFstImpl<A>::Read(strm, opts); - return impl ? new ConstFst<A>(impl) : 0; - } - - // Read a ConstFst from a file; returno NULL on error - static ConstFst<A> *Read(const string &filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "ConstFst::Write: Can't open file: " << filename; - return 0; - } - return Read(strm, FstReadOptions(filename)); - } - - // Write a ConstFst to an output stream; return false on error - virtual bool Write(ostream &strm, const FstWriteOptions &opts) const { - return impl_->Write(strm, opts); - } - - // Write a ConstFst to a file; return false on error - virtual bool Write(const string &filename) const { - if (!filename.empty()) { - ofstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "ConstrFst::Write: Can't open file: " << filename; - return false; - } - return Write(strm, FstWriteOptions(filename)); - } else { - return Write(std::cout, FstWriteOptions("standard output")); - } - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual void InitStateIterator(StateIteratorData<A> *data) const { - impl_->InitStateIterator(data); - } - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - private: - ConstFst(ConstFstImpl<A> *impl) : impl_(impl) {} - - ConstFstImpl<A> *impl_; // FST's impl - - void operator=(const ConstFst<A> &fst); // disallow -}; - -// Specialization for ConstFst; see generic version in fst.h -// for sample usage (but use the ConstFst type!). This version -// should inline. -template <class A> -class StateIterator< ConstFst<A> > { - public: - typedef typename A::StateId StateId; - - explicit StateIterator(const ConstFst<A> &fst) - : nstates_(fst.impl_->NumStates()), s_(0) {} - - bool Done() const { return s_ >= nstates_; } - - StateId Value() const { return s_; } - - void Next() { ++s_; } - - void Reset() { s_ = 0; } - - private: - StateId nstates_; - StateId s_; - - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - -// Specialization for ConstFst; see generic version in fst.h -// for sample usage (but use the ConstFst type!). This version -// should inline. -template <class A> -class ArcIterator< ConstFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const ConstFst<A> &fst, StateId s) - : arcs_(fst.impl_->Arcs(s)), narcs_(fst.impl_->NumArcs(s)), i_(0) {} - - bool Done() const { return i_ >= narcs_; } - - const A& Value() const { return arcs_[i_]; } - - void Next() { ++i_; } - - void Reset() { i_ = 0; } - - void Seek(size_t a) { i_ = a; } - - private: - const A *arcs_; - size_t narcs_; - size_t i_; - - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -// A useful alias when using StdArc. -typedef ConstFst<StdArc> StdConstFst; - -} // namespace fst; - -#endif // FST_LIB_CONST_FST_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/determinize.h b/tools/thirdparty/OpenFst/fst/lib/determinize.h deleted file mode 100644 index 63b96ec..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/determinize.h +++ /dev/null @@ -1,721 +0,0 @@ -// determinize.h - -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to determinize an FST. - -#ifndef FST_LIB_DETERMINIZE_H__ -#define FST_LIB_DETERMINIZE_H__ - -#include <algorithm> -#include <map> - -#include <ext/hash_map> -using __gnu_cxx::hash_map; -#include <ext/slist> -using __gnu_cxx::slist; - -#include "fst/lib/cache.h" -#include "fst/lib/factor-weight.h" -#include "fst/lib/map.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -// -// COMMON DIVISORS - these are used in determinization to compute -// the transition weights. In the simplest case, it is just the same -// as the semiring Plus(). However, other choices permit more efficient -// determinization when the output contains strings. -// - -// The default common divisor uses the semiring Plus. -template <class W> -class DefaultCommonDivisor { - public: - typedef W Weight; - - W operator()(const W &w1, const W &w2) const { return Plus(w1, w2); } -}; - - -// The label common divisor for a (left) string semiring selects a -// single letter common prefix or the empty string. This is used in -// the determinization of output strings so that at most a single -// letter will appear in the output of a transtion. -template <typename L, StringType S> -class LabelCommonDivisor { - public: - typedef StringWeight<L, S> Weight; - - Weight operator()(const Weight &w1, const Weight &w2) const { - StringWeightIterator<L, S> iter1(w1); - StringWeightIterator<L, S> iter2(w2); - - if (!(StringWeight<L, S>::Properties() & kLeftSemiring)) - LOG(FATAL) << "LabelCommonDivisor: Weight needs to be left semiring"; - - if (w1.Size() == 0 || w2.Size() == 0) - return Weight::One(); - else if (w1 == Weight::Zero()) - return Weight(iter2.Value()); - else if (w2 == Weight::Zero()) - return Weight(iter1.Value()); - else if (iter1.Value() == iter2.Value()) - return Weight(iter1.Value()); - else - return Weight::One(); - } -}; - - -// The gallic common divisor uses the label common divisor on the -// string component and the template argument D common divisor on the -// weight component, which defaults to the default common divisor. -template <class L, class W, StringType S, class D = DefaultCommonDivisor<W> > -class GallicCommonDivisor { - public: - typedef GallicWeight<L, W, S> Weight; - - Weight operator()(const Weight &w1, const Weight &w2) const { - return Weight(label_common_divisor_(w1.Value1(), w2.Value1()), - weight_common_divisor_(w1.Value2(), w2.Value2())); - } - - private: - LabelCommonDivisor<L, S> label_common_divisor_; - D weight_common_divisor_; -}; - -// Options for finite-state transducer determinization. -struct DeterminizeFstOptions : CacheOptions { - float delta; // Quantization delta for subset weights - - explicit DeterminizeFstOptions(const CacheOptions &opts, float del = kDelta) - : CacheOptions(opts), delta(del) {} - - explicit DeterminizeFstOptions(float del = kDelta) : delta(del) {} -}; - - -// Implementation of delayed DeterminizeFst. This base class is -// common to the variants that implement acceptor and transducer -// determinization. -template <class A> -class DeterminizeFstImplBase : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - using CacheBaseImpl< CacheState<A> >::HasStart; - using CacheBaseImpl< CacheState<A> >::HasFinal; - using CacheBaseImpl< CacheState<A> >::HasArcs; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - DeterminizeFstImplBase(const Fst<A> &fst, const CacheOptions &opts) - : CacheImpl<A>(opts), fst_(fst.Copy()) { - SetType("determinize"); - uint64 props = fst.Properties(kFstProperties, false); - SetProperties(DeterminizeProperties(props), kCopyProperties); - - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - } - - virtual ~DeterminizeFstImplBase() { delete fst_; } - - StateId Start() { - if (!HasStart()) { - StateId start = ComputeStart(); - if (start != kNoStateId) { - SetStart(start); - } - } - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - Weight final = ComputeFinal(s); - SetFinal(s, final); - } - return CacheImpl<A>::Final(s); - } - - virtual void Expand(StateId s) = 0; - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - virtual StateId ComputeStart() = 0; - - virtual Weight ComputeFinal(StateId s) = 0; - - protected: - const Fst<A> *fst_; // Input Fst - - DISALLOW_EVIL_CONSTRUCTORS(DeterminizeFstImplBase); -}; - - -// Implementation of delayed determinization for weighted acceptors. -// It is templated on the arc type A and the common divisor C. -template <class A, class C> -class DeterminizeFsaImpl : public DeterminizeFstImplBase<A> { - public: - using DeterminizeFstImplBase<A>::fst_; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - struct Element { - Element() {} - - Element(StateId s, Weight w) : state_id(s), weight(w) {} - - StateId state_id; // Input state Id - Weight weight; // Residual weight - }; - typedef slist<Element> Subset; - typedef map<Label, Subset*> LabelMap; - - DeterminizeFsaImpl(const Fst<A> &fst, C common_divisor, - const DeterminizeFstOptions &opts) - : DeterminizeFstImplBase<A>(fst, opts), - delta_(opts.delta), common_divisor_(common_divisor), - subset_hash_(0, SubsetKey(), SubsetEqual(&elements_)) { - if (!fst.Properties(kAcceptor, true)) - LOG(FATAL) << "DeterminizeFst: argument not an acceptor"; - if (!(Weight::Properties() & kLeftSemiring)) - LOG(FATAL) << "DeterminizeFst: Weight needs to be left distributive: " - << Weight::Type(); - } - - virtual ~DeterminizeFsaImpl() { - for (unsigned int i = 0; i < subsets_.size(); ++i) - delete subsets_[i]; - } - - virtual StateId ComputeStart() { - StateId s = fst_->Start(); - if (s == kNoStateId) - return kNoStateId; - Element element(s, Weight::One()); - Subset *subset = new Subset; - subset->push_front(element); - return FindState(subset); - } - - virtual Weight ComputeFinal(StateId s) { - Subset *subset = subsets_[s]; - Weight final = Weight::Zero(); - for (typename Subset::iterator siter = subset->begin(); - siter != subset->end(); - ++siter) { - Element &element = *siter; - final = Plus(final, Times(element.weight, - fst_->Final(element.state_id))); - } - return final; - } - - // Finds the state corresponding to a subset. Only creates a new state - // if the subset is not found in the subset hash. FindState takes - // ownership of the subset argument (so that it doesn't have to copy it - // if it creates a new state). - // - // The method exploits the following device: all pairs stored in the - // associative container subset_hash_ are of the form (subset, - // id(subset) + 1), i.e. subset_hash_[subset] > 0 if subset has been - // stored previously. For unassigned subsets, the call to - // subset_hash_[subset] creates a new pair (subset, 0). As a result, - // subset_hash_[subset] == 0 iff subset is new. - StateId FindState(Subset *subset) { - StateId &assoc_value = subset_hash_[subset]; - if (assoc_value == 0) { // subset wasn't present; assign it a new ID - subsets_.push_back(subset); - assoc_value = subsets_.size(); - } else { - delete subset; - } - return assoc_value - 1; // NB: assoc_value = ID + 1 - } - - // Computes the outgoing transitions from a state, creating new destination - // states as needed. - virtual void Expand(StateId s) { - - LabelMap label_map; - LabelSubsets(s, &label_map); - - for (typename LabelMap::iterator liter = label_map.begin(); - liter != label_map.end(); - ++liter) - AddArc(s, liter->first, liter->second); - SetArcs(s); - } - - private: - // Constructs destination subsets per label. At return, subset - // element weights include the input automaton label weights and the - // subsets may contain duplicate states. - void LabelSubsets(StateId s, LabelMap *label_map) { - Subset *src_subset = subsets_[s]; - - for (typename Subset::iterator siter = src_subset->begin(); - siter != src_subset->end(); - ++siter) { - Element &src_element = *siter; - for (ArcIterator< Fst<A> > aiter(*fst_, src_element.state_id); - !aiter.Done(); - aiter.Next()) { - const A &arc = aiter.Value(); - Element dest_element(arc.nextstate, - Times(src_element.weight, arc.weight)); - Subset* &dest_subset = (*label_map)[arc.ilabel]; - if (dest_subset == 0) - dest_subset = new Subset; - dest_subset->push_front(dest_element); - } - } - } - - // Adds an arc from state S to the destination state associated - // with subset DEST_SUBSET (as created by LabelSubsets). - void AddArc(StateId s, Label label, Subset *dest_subset) { - A arc; - arc.ilabel = label; - arc.olabel = label; - arc.weight = Weight::Zero(); - - typename Subset::iterator oiter; - for (typename Subset::iterator diter = dest_subset->begin(); - diter != dest_subset->end();) { - Element &dest_element = *diter; - // Computes label weight. - arc.weight = common_divisor_(arc.weight, dest_element.weight); - - while ((StateId)elements_.size() <= dest_element.state_id) - elements_.push_back(0); - Element *matching_element = elements_[dest_element.state_id]; - if (matching_element) { - // Found duplicate state: sums state weight and deletes dup. - matching_element->weight = Plus(matching_element->weight, - dest_element.weight); - ++diter; - dest_subset->erase_after(oiter); - } else { - // Saves element so we can check for duplicate for this state. - elements_[dest_element.state_id] = &dest_element; - oiter = diter; - ++diter; - } - } - - // Divides out label weight from destination subset elements. - // Quantizes to ensure comparisons are effective. - // Clears element vector. - for (typename Subset::iterator diter = dest_subset->begin(); - diter != dest_subset->end(); - ++diter) { - Element &dest_element = *diter; - dest_element.weight = Divide(dest_element.weight, arc.weight, - DIVIDE_LEFT); - dest_element.weight = dest_element.weight.Quantize(delta_); - elements_[dest_element.state_id] = 0; - } - - arc.nextstate = FindState(dest_subset); - CacheImpl<A>::AddArc(s, arc); - } - - // Comparison object for hashing Subset(s). Subsets are not sorted in this - // implementation, so ordering must not be assumed in the equivalence - // test. - class SubsetEqual { - public: - // Constructor takes vector needed to check equality. See immediately - // below for constraints on it. - explicit SubsetEqual(vector<Element *> *elements) - : elements_(elements) {} - - // At each call to operator(), elements_[state] must be defined and - // NULL for each state in the subset arguments. When this operator - // returns, elements_ will preserve that property. We keep it - // full of NULLs so that it is ready for the next call. - bool operator()(Subset* subset1, Subset* subset2) const { - if (subset1->size() != subset2->size()) - return false; - - // Loads first subset elements in element vector. - for (typename Subset::iterator iter1 = subset1->begin(); - iter1 != subset1->end(); - ++iter1) { - Element &element1 = *iter1; - (*elements_)[element1.state_id] = &element1; - } - - // Checks second subset matches first via element vector. - for (typename Subset::iterator iter2 = subset2->begin(); - iter2 != subset2->end(); - ++iter2) { - Element &element2 = *iter2; - Element *element1 = (*elements_)[element2.state_id]; - if (!element1 || element1->weight != element2.weight) { - // Mismatch found. Resets element vector before returning false. - for (typename Subset::iterator iter1 = subset1->begin(); - iter1 != subset1->end(); - ++iter1) - (*elements_)[iter1->state_id] = 0; - return false; - } else { - (*elements_)[element2.state_id] = 0; // Clears entry - } - } - return true; - } - private: - vector<Element *> *elements_; - }; - - // Hash function for Subset to Fst states. Subset elements are not - // sorted in this implementation, so the hash must be invariant - // under subset reordering. - class SubsetKey { - public: - size_t operator()(const Subset* subset) const { - size_t hash = 0; - for (typename Subset::const_iterator iter = subset->begin(); - iter != subset->end(); - ++iter) { - const Element &element = *iter; - int lshift = element.state_id % kPrime; - int rshift = sizeof(size_t) - lshift; - hash ^= element.state_id << lshift ^ - element.state_id >> rshift ^ - element.weight.Hash(); - } - return hash; - } - - private: - static const int kPrime = sizeof(size_t) == 8 ? 23 : 13; - }; - - float delta_; // Quantization delta for subset weights - C common_divisor_; - - // Used to test equivalence of subsets. - vector<Element *> elements_; - - // Maps from StateId to Subset. - vector<Subset *> subsets_; - - // Hashes from Subset to its StateId in the output automaton. - typedef hash_map<Subset *, StateId, SubsetKey, SubsetEqual> - SubsetHash; - - // Hashes from Label to Subsets corr. to destination states of current state. - SubsetHash subset_hash_; - - DISALLOW_EVIL_CONSTRUCTORS(DeterminizeFsaImpl); -}; - - -// Implementation of delayed determinization for transducers. -// Transducer determinization is implemented by mapping the input to -// the Gallic semiring as an acceptor whose weights contain the output -// strings and using acceptor determinization above to determinize -// that acceptor. -template <class A, StringType S> -class DeterminizeFstImpl : public DeterminizeFstImplBase<A> { - public: - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - typedef ToGallicMapper<A, S> ToMapper; - typedef FromGallicMapper<A, S> FromMapper; - - typedef typename ToMapper::ToArc ToArc; - typedef MapFst<A, ToArc, ToMapper> ToFst; - typedef MapFst<ToArc, A, FromMapper> FromFst; - - typedef GallicCommonDivisor<Label, Weight, S> CommonDivisor; - typedef GallicFactor<Label, Weight, S> FactorIterator; - - // Defined after DeterminizeFst since it calls it. - DeterminizeFstImpl(const Fst<A> &fst, const DeterminizeFstOptions &opts); - - ~DeterminizeFstImpl() { delete from_fst_; } - - virtual StateId ComputeStart() { return from_fst_->Start(); } - - virtual Weight ComputeFinal(StateId s) { return from_fst_->Final(s); } - - virtual void Expand(StateId s) { - for (ArcIterator<FromFst> aiter(*from_fst_, s); - !aiter.Done(); - aiter.Next()) - CacheImpl<A>::AddArc(s, aiter.Value()); - CacheImpl<A>::SetArcs(s); - } - - private: - FromFst *from_fst_; - - DISALLOW_EVIL_CONSTRUCTORS(DeterminizeFstImpl); -}; - - -// Determinizes a weighted transducer. This version is a delayed -// Fst. The result will be an equivalent FST that has the property -// that no state has two transitions with the same input label. -// For this algorithm, epsilon transitions are treated as regular -// symbols (cf. RmEpsilon). -// -// The transducer must be functional. The weights must be (weakly) -// left divisible (valid for TropicalWeight and LogWeight). -// -// Complexity: -// - Determinizable: exponential (polynomial in the size of the output) -// - Non-determinizable) does not terminate -// -// The determinizable automata include all unweighted and all acyclic input. -// -// References: -// - Mehryar Mohri, "Finite-State Transducers in Language and Speech -// Processing". Computational Linguistics, 23:2, 1997. -template <class A> -class DeterminizeFst : public Fst<A> { - public: - friend class ArcIterator< DeterminizeFst<A> >; - friend class CacheStateIterator< DeterminizeFst<A> >; - friend class CacheArcIterator< DeterminizeFst<A> >; - template <class B, StringType S> friend class DeterminizeFstImpl; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef typename A::Label Label; - typedef CacheState<A> State; - - explicit DeterminizeFst(const Fst<A> &fst, - const DeterminizeFstOptions &opts = DeterminizeFstOptions()) { - if (fst.Properties(kAcceptor, true)) { - // Calls implementation for acceptors. - typedef DefaultCommonDivisor<Weight> D; - impl_ = new DeterminizeFsaImpl<A, D>(fst, D(), opts); - } else { - // Calls implementation for transducers. - impl_ = new DeterminizeFstImpl<A, STRING_LEFT_RESTRICT>(fst, opts); - } - } - - DeterminizeFst(const DeterminizeFst<A> &fst) : Fst<A>(fst), impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~DeterminizeFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual DeterminizeFst<A> *Copy() const { - return new DeterminizeFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - protected: - DeterminizeFstImplBase<A> *Impl() { return impl_; } - - private: - // This private version is for passing the common divisor to - // FSA determinization. - template <class D> - DeterminizeFst(const Fst<A> &fst, const D &common_divisor, - const DeterminizeFstOptions &opts) - : impl_(new DeterminizeFsaImpl<A, D>(fst, common_divisor, opts)) {} - - DeterminizeFstImplBase<A> *impl_; - - void operator=(const DeterminizeFst<A> &fst); // Disallow -}; - - -template <class A, StringType S> -DeterminizeFstImpl<A, S>::DeterminizeFstImpl( - const Fst<A> &fst, const DeterminizeFstOptions &opts) - : DeterminizeFstImplBase<A>(fst, opts) { - - // Mapper to an acceptor. - ToFst to_fst(fst, ToMapper()); - - // Determinize acceptor. - // This recursive call terminates since it passes the common divisor - // to a private constructor. - DeterminizeFst<ToArc> det_fsa(to_fst, CommonDivisor(), opts); - - // Mapper back to transducer. - FactorWeightOptions fopts(CacheOptions(true, 0), opts.delta, true); - FactorWeightFst<ToArc, FactorIterator> factored_fst(det_fsa, fopts); - from_fst_ = new FromFst(factored_fst, FromMapper()); -} - - -// Specialization for DeterminizeFst. -template <class A> -class StateIterator< DeterminizeFst<A> > - : public CacheStateIterator< DeterminizeFst<A> > { - public: - explicit StateIterator(const DeterminizeFst<A> &fst) - : CacheStateIterator< DeterminizeFst<A> >(fst) {} -}; - - -// Specialization for DeterminizeFst. -template <class A> -class ArcIterator< DeterminizeFst<A> > - : public CacheArcIterator< DeterminizeFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const DeterminizeFst<A> &fst, StateId s) - : CacheArcIterator< DeterminizeFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - - -template <class A> inline -void DeterminizeFst<A>::InitStateIterator(StateIteratorData<A> *data) const -{ - data->base = new StateIterator< DeterminizeFst<A> >(*this); -} - - -// Useful aliases when using StdArc. -typedef DeterminizeFst<StdArc> StdDeterminizeFst; - - -struct DeterminizeOptions { - float delta; // Quantization delta for subset weights - - explicit DeterminizeOptions(float d) : delta(d) {} - DeterminizeOptions() :delta(kDelta) {} -}; - - -// Determinizes a weighted transducer. This version writes the -// determinized Fst to an output MutableFst. The result will be an -// equivalent FSt that has the property that no state has two -// transitions with the same input label. For this algorithm, epsilon -// transitions are treated as regular symbols (cf. RmEpsilon). -// -// The transducer must be functional. The weights must be (weakly) -// left divisible (valid for TropicalWeight and LogWeight). -// -// Complexity: -// - Determinizable: exponential (polynomial in the size of the output) -// - Non-determinizable: does not terminate -// -// The determinizable automata include all unweighted and all acyclic input. -// -// References: -// - Mehryar Mohri, "Finite-State Transducers in Language and Speech -// Processing". Computational Linguistics, 23:2, 1997. -template <class Arc> -void Determinize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst, - const DeterminizeOptions &opts = DeterminizeOptions()) { - DeterminizeFstOptions nopts; - nopts.delta = opts.delta; - nopts.gc_limit = 0; // Cache only the last state for fastest copy. - *ofst = DeterminizeFst<Arc>(ifst, nopts); -} - - -} // namespace fst - -#endif // FST_LIB_DETERMINIZE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/dfs-visit.h b/tools/thirdparty/OpenFst/fst/lib/dfs-visit.h deleted file mode 100644 index e82c564..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/dfs-visit.h +++ /dev/null @@ -1,164 +0,0 @@ -// dfs-visit.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Depth-first search visitation - -#ifndef FST_LIB_DFS_VISIT_H__ -#define FST_LIB_DFS_VISIT_H__ - -#include <stack> - -#include "fst/lib/arcfilter.h" -#include "fst/lib/expanded-fst.h" - -namespace fst { - -// Visitor Interface - class determines actions taken during a Dfs. -// If any of the boolean member functions return false, the DFS is -// aborted by first calling FinishState() on all currently grey states -// and then calling FinishVisit(). -// -// template <class Arc> -// class Visitor { -// public: -// typedef typename Arc::StateId StateId; -// -// Visitor(T *return_data); -// // Invoked before DFS visit -// void InitVisit(const Fst<Arc> &fst); -// // Invoked when state discovered (2nd arg is DFS tree root) -// bool InitState(StateId s, StateId root); -// // Invoked when tree arc examined (to white/undiscovered state) -// bool TreeArc(StateId s, const Arc &a); -// // Invoked when back arc examined (to grey/unfinished state) -// bool BackArc(StateId s, const Arc &a); -// // Invoked when forward or cross arc examined (to black/finished state) -// bool ForwardOrCrossArc(StateId s, const Arc &a); -// // Invoked when state finished (PARENT is kNoStateID and ARC == NULL -// // when S is tree root) -// void FinishState(StateId s, StateId parent, const Arc *parent_arc); -// // Invoked after DFS visit -// void FinishVisit(); -// }; - -// An Fst state's DFS status -const int kDfsWhite = 0; // Undiscovered -const int kDfsGrey = 1; // Discovered & unfinished -const int kDfsBlack = 2; // Finished - -// An Fst state's DFS stack state -template <class Arc> -struct DfsState { - typedef typename Arc::StateId StateId; - - DfsState(const Fst<Arc> &fst, StateId s): state_id(s), arc_iter(fst, s) {} - - StateId state_id; // Fst state ... - ArcIterator< Fst<Arc> > arc_iter; // and its corresponding arcs -}; - - -// Performs depth-first visitation. Visitor class argument determines actions -// and contains any return data. ArcFilter determines arcs that are considered. -template <class Arc, class V, class ArcFilter> -void DfsVisit(const Fst<Arc> &fst, V *visitor, ArcFilter filter) { - typedef typename Arc::StateId StateId; - - visitor->InitVisit(fst); - - StateId start = fst.Start(); - if (start == kNoStateId) { - visitor->FinishVisit(); - return; - } - - vector<char> state_color; // Fst state DFS status - stack<DfsState<Arc> *> state_stack; // DFS execution stack - - StateId nstates = CountStates(fst); - - while ((StateId)state_color.size() < nstates) - state_color.push_back(kDfsWhite); - - // Continue DFS while true - bool dfs = true; - - // Iterate over trees in DFS forest. - for (StateId root = start; dfs && root < nstates;) { - state_color[root] = kDfsGrey; - state_stack.push(new DfsState<Arc>(fst, root)); - dfs = visitor->InitState(root, root); - while (!state_stack.empty()) { - DfsState<Arc> *dfs_state = state_stack.top(); - StateId s = dfs_state->state_id; - ArcIterator< Fst<Arc> > &aiter = dfs_state->arc_iter; - if (!dfs || aiter.Done()) { - state_color[s] = kDfsBlack; - delete dfs_state; - state_stack.pop(); - if (!state_stack.empty()) { - DfsState<Arc> *parent_state = state_stack.top(); - StateId p = parent_state->state_id; - ArcIterator< Fst<Arc> > &piter = parent_state->arc_iter; - visitor->FinishState(s, p, &piter.Value()); - piter.Next(); - } else { - visitor->FinishState(s, kNoStateId, 0); - } - continue; - } - const Arc &arc = aiter.Value(); - if (!filter(arc)) { - aiter.Next(); - continue; - } - int next_color = state_color[arc.nextstate]; - switch (next_color) { - default: - case kDfsWhite: - dfs = visitor->TreeArc(s, arc); - if (!dfs) break; - state_color[arc.nextstate] = kDfsGrey; - state_stack.push(new DfsState<Arc>(fst, arc.nextstate)); - dfs = visitor->InitState(arc.nextstate, root); - break; - case kDfsGrey: - dfs = visitor->BackArc(s, arc); - aiter.Next(); - break; - case kDfsBlack: - dfs = visitor->ForwardOrCrossArc(s, arc); - aiter.Next(); - break; - } - } - // Find next tree root - for (root = root == start ? 0 : root + 1; - root < nstates && state_color[root] != kDfsWhite; - ++root); - } - visitor->FinishVisit(); -} - - -template <class Arc, class V> -void DfsVisit(const Fst<Arc> &fst, V *visitor) { - DfsVisit(fst, visitor, AnyArcFilter<Arc>()); -} - -} // namespace fst - -#endif // FST_LIB_DFS_VISIT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/difference.h b/tools/thirdparty/OpenFst/fst/lib/difference.h deleted file mode 100644 index fa5a2a2..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/difference.h +++ /dev/null @@ -1,140 +0,0 @@ -// difference.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Class to compute the difference between two FSAs - -#ifndef FST_LIB_DIFFERENCE_H__ -#define FST_LIB_DIFFERENCE_H__ - -#include "fst/lib/compose.h" -#include "fst/lib/complement.h" - -namespace fst { - -template <uint64 T = 0> -struct DifferenceFstOptions - : public ComposeFstOptions<T | COMPOSE_FST2_RHO> { }; - - -// Computes the difference between two FSAs. This version is a delayed -// Fst. Only strings that are in the first automaton but not in second -// are retained in the result. -// -// The first argument must be an acceptor; the second argument must be -// an unweighted, epsilon-free, deterministic acceptor. One of the -// arguments must be label-sorted. -// -// Complexity: same as ComposeFst. -// -// Caveats: same as ComposeFst. -template <class A> -class DifferenceFst : public ComposeFst<A> { - public: - using ComposeFst<A>::Impl; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - // A - B = A ^ B'. - DifferenceFst(const Fst<A> &fst1, const Fst<A> &fst2) - : ComposeFst<A>(fst1, - ComplementFst<A>(fst2), - ComposeFstOptions<COMPOSE_FST2_RHO>()) { - if (!fst1.Properties(kAcceptor, true)) - LOG(FATAL) << "DifferenceFst: 1st argument not an acceptor"; - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - Impl()->SetProperties(DifferenceProperties(props1, props2), - kCopyProperties); - } - - template <uint64 T> - DifferenceFst(const Fst<A> &fst1, const Fst<A> &fst2, - const DifferenceFstOptions<T> &opts) - : ComposeFst<A>(fst1, - ComplementFst<A>(fst2), - ComposeFstOptions<T | COMPOSE_FST2_RHO>(opts)) { - if (!fst1.Properties(kAcceptor, true)) - LOG(FATAL) << "DifferenceFst: 1st argument not an acceptor"; - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - Impl()->SetProperties(DifferenceProperties(props1, props2), - kCopyProperties); - } - - DifferenceFst(const DifferenceFst<A> &fst) - : ComposeFst<A>(fst) {} - - virtual DifferenceFst<A> *Copy() const { - return new DifferenceFst<A>(*this); - } -}; - - -// Specialization for DifferenceFst. -template <class A> -class StateIterator< DifferenceFst<A> > - : public StateIterator< ComposeFst<A> > { - public: - explicit StateIterator(const DifferenceFst<A> &fst) - : StateIterator< ComposeFst<A> >(fst) {} -}; - - -// Specialization for DifferenceFst. -template <class A> -class ArcIterator< DifferenceFst<A> > - : public ArcIterator< ComposeFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const DifferenceFst<A> &fst, StateId s) - : ArcIterator< ComposeFst<A> >(fst, s) {} -}; - -// Useful alias when using StdArc. -typedef DifferenceFst<StdArc> StdDifferenceFst; - - -typedef ComposeOptions DifferenceOptions; - - -// Computes the difference between two FSAs. This version is writes -// the difference to an output MutableFst. Only strings that are in -// the first automaton but not in second are retained in the result. -// -// The first argument must be an acceptor; the second argument must be -// an unweighted, epsilon-free, deterministic acceptor. One of the -// arguments must be label-sorted. -// -// Complexity: same as Compose. -// -// Caveats: same as Compose. -template<class Arc> -void Difference(const Fst<Arc> &ifst1, const Fst<Arc> &ifst2, - MutableFst<Arc> *ofst, - const DifferenceOptions &opts = DifferenceOptions()) { - DifferenceFstOptions<> nopts; - nopts.gc_limit = 0; // Cache only the last state for fastest copy. - *ofst = DifferenceFst<Arc>(ifst1, ifst2, nopts); - if (opts.connect) - Connect(ofst); -} - -} // namespace fst - -#endif // FST_LIB_DIFFERENCE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/encode.h b/tools/thirdparty/OpenFst/fst/lib/encode.h deleted file mode 100644 index 882cdd0..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/encode.h +++ /dev/null @@ -1,428 +0,0 @@ -// encode.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Class to encode and decoder an fst. - -#ifndef FST_LIB_ENCODE_H__ -#define FST_LIB_ENCODE_H__ - -#include "fst/lib/map.h" -#include "fst/lib/rmfinalepsilon.h" - -namespace fst { - -static const uint32 kEncodeLabels = 0x00001; -static const uint32 kEncodeWeights = 0x00002; - -enum EncodeType { ENCODE = 1, DECODE = 2 }; - -// Identifies stream data as an encode table (and its endianity) -static const int32 kEncodeMagicNumber = 2129983209; - - -// The following class encapsulates implementation details for the -// encoding and decoding of label/weight tuples used for encoding -// and decoding of Fsts. The EncodeTable is bidirectional. I.E it -// stores both the Tuple of encode labels and weights to a unique -// label, and the reverse. -template <class A> class EncodeTable { - public: - typedef typename A::Label Label; - typedef typename A::Weight Weight; - - // Encoded data consists of arc input/output labels and arc weight - struct Tuple { - Tuple() {} - Tuple(Label ilabel_, Label olabel_, Weight weight_) - : ilabel(ilabel_), olabel(olabel_), weight(weight_) {} - Tuple(const Tuple& tuple) - : ilabel(tuple.ilabel), olabel(tuple.olabel), weight(tuple.weight) {} - - Label ilabel; - Label olabel; - Weight weight; - }; - - // Comparison object for hashing EncodeTable Tuple(s). - class TupleEqual { - public: - bool operator()(const Tuple* x, const Tuple* y) const { - return (x->ilabel == y->ilabel && - x->olabel == y->olabel && - x->weight == y->weight); - } - }; - - // Hash function for EncodeTabe Tuples. Based on the encode flags - // we either hash the labels, weights or compbination of them. - class TupleKey { - static const int kPrime = 7853; - public: - TupleKey() - : encode_flags_(kEncodeLabels | kEncodeWeights) {} - - TupleKey(const TupleKey& key) - : encode_flags_(key.encode_flags_) {} - - explicit TupleKey(uint32 encode_flags) - : encode_flags_(encode_flags) {} - - size_t operator()(const Tuple* x) const { - int lshift = x->ilabel % kPrime; - int rshift = sizeof(size_t) - lshift; - size_t hash = x->ilabel << lshift; - if (encode_flags_ & kEncodeLabels) hash ^= x->olabel >> rshift; - if (encode_flags_ & kEncodeWeights) hash ^= x->weight.Hash(); - return hash; - } - - private: - int32 encode_flags_; - }; - - typedef hash_map<const Tuple*, - Label, - TupleKey, - TupleEqual> EncodeHash; - - explicit EncodeTable(uint32 encode_flags) - : flags_(encode_flags), - encode_hash_(1024, TupleKey(encode_flags)) {} - - ~EncodeTable() { - for (size_t i = 0; i < encode_tuples_.size(); ++i) { - delete encode_tuples_[i]; - } - } - - // Given an arc encode either input/ouptut labels or input/costs or both - Label Encode(const A &arc) { - const Tuple tuple(arc.ilabel, - flags_ & kEncodeLabels ? arc.olabel : 0, - flags_ & kEncodeWeights ? arc.weight : Weight::One()); - typename EncodeHash::const_iterator it = encode_hash_.find(&tuple); - if (it == encode_hash_.end()) { - encode_tuples_.push_back(new Tuple(tuple)); - encode_hash_[encode_tuples_.back()] = encode_tuples_.size(); - return encode_tuples_.size(); - } else { - return it->second; - } - } - - // Given an encode arc Label decode back to input/output labels and costs - const Tuple* Decode(Label key) { - return key <= (Label)encode_tuples_.size() ? encode_tuples_[key - 1] : 0; - } - - bool Write(ostream &strm, const string &source) const { - WriteType(strm, kEncodeMagicNumber); - WriteType(strm, flags_); - int64 size = encode_tuples_.size(); - WriteType(strm, size); - for (size_t i = 0; i < size; ++i) { - const Tuple* tuple = encode_tuples_[i]; - WriteType(strm, tuple->ilabel); - WriteType(strm, tuple->olabel); - tuple->weight.Write(strm); - } - strm.flush(); - if (!strm) - LOG(ERROR) << "EncodeTable::Write: write failed: " << source; - return strm; - } - - bool Read(istream &strm, const string &source) { - encode_tuples_.clear(); - encode_hash_.clear(); - int32 magic_number = 0; - ReadType(strm, &magic_number); - if (magic_number != kEncodeMagicNumber) { - LOG(ERROR) << "EncodeTable::Read: Bad encode table header: " << source; - return false; - } - ReadType(strm, &flags_); - int64 size; - ReadType(strm, &size); - if (!strm) { - LOG(ERROR) << "EncodeTable::Read: read failed: " << source; - return false; - } - for (size_t i = 0; i < size; ++i) { - Tuple* tuple = new Tuple(); - ReadType(strm, &tuple->ilabel); - ReadType(strm, &tuple->olabel); - tuple->weight.Read(strm); - encode_tuples_.push_back(tuple); - encode_hash_[encode_tuples_.back()] = encode_tuples_.size(); - } - if (!strm) - LOG(ERROR) << "EncodeTable::Read: read failed: " << source; - return strm; - } - - const uint32 flags() const { return flags_; } - private: - uint32 flags_; - vector<Tuple*> encode_tuples_; - EncodeHash encode_hash_; - - DISALLOW_EVIL_CONSTRUCTORS(EncodeTable); -}; - - -// A mapper to encode/decode weighted transducers. Encoding of an -// Fst is useful for performing classical determinization or minimization -// on a weighted transducer by treating it as an unweighted acceptor over -// encoded labels. -// -// The Encode mapper stores the encoding in a local hash table (EncodeTable) -// This table is shared (and reference counted) between the encoder and -// decoder. A decoder has read only access to the EncodeTable. -// -// The EncodeMapper allows on the fly encoding of the machine. As the -// EncodeTable is generated the same table may by used to decode the machine -// on the fly. For example in the following sequence of operations -// -// Encode -> Determinize -> Decode -// -// we will use the encoding table generated during the encode step in the -// decode, even though the encoding is not complete. -// -template <class A> class EncodeMapper { - typedef typename A::Weight Weight; - typedef typename A::Label Label; - public: - EncodeMapper(uint32 flags, EncodeType type) - : ref_count_(1), flags_(flags), type_(type), - table_(new EncodeTable<A>(flags)) {} - - EncodeMapper(const EncodeMapper& mapper) - : ref_count_(mapper.ref_count_ + 1), - flags_(mapper.flags_), - type_(mapper.type_), - table_(mapper.table_) { } - - // Copy constructor but setting the type, typically to DECODE - EncodeMapper(const EncodeMapper& mapper, EncodeType type) - : ref_count_(mapper.ref_count_ + 1), - flags_(mapper.flags_), - type_(type), - table_(mapper.table_) { } - - ~EncodeMapper() { - if (--ref_count_ == 0) delete table_; - } - - A operator()(const A &arc) { - if (type_ == ENCODE) { // labels and/or weights to single label - if ((arc.nextstate == kNoStateId && !(flags_ & kEncodeWeights)) || - (arc.nextstate == kNoStateId && (flags_ & kEncodeWeights) && - arc.weight == Weight::Zero())) { - return arc; - } else { - Label label = table_->Encode(arc); - return A(label, - flags_ & kEncodeLabels ? label : arc.olabel, - flags_ & kEncodeWeights ? Weight::One() : arc.weight, - arc.nextstate); - } - } else { - if (arc.nextstate == kNoStateId) { - return arc; - } else { - const typename EncodeTable<A>::Tuple* tuple = - table_->Decode(arc.ilabel); - return A(tuple->ilabel, - flags_ & kEncodeLabels ? tuple->olabel : arc.olabel, - flags_ & kEncodeWeights ? tuple->weight : arc.weight, - arc.nextstate);; - } - } - } - - uint64 Properties(uint64 props) { - uint64 mask = kFstProperties; - if (flags_ & kEncodeLabels) - mask &= kILabelInvariantProperties & kOLabelInvariantProperties; - if (flags_ & kEncodeWeights) - mask &= kILabelInvariantProperties & kWeightInvariantProperties & - (type_ == ENCODE ? kAddSuperFinalProperties : - kRmSuperFinalProperties); - return props & mask; - } - - - MapFinalAction FinalAction() const { - return (type_ == ENCODE && (flags_ & kEncodeWeights)) ? - MAP_REQUIRE_SUPERFINAL : MAP_NO_SUPERFINAL; - } - - const uint32 flags() const { return flags_; } - const EncodeType type() const { return type_; } - - bool Write(ostream &strm, const string& source) { - return table_->Write(strm, source); - } - - bool Write(const string& filename) { - ofstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "EncodeMap: Can't open file: " << filename; - return false; - } - return Write(strm, filename); - } - - static EncodeMapper<A> *Read(istream &strm, - const string& source, EncodeType type) { - EncodeTable<A> *table = new EncodeTable<A>(0); - bool r = table->Read(strm, source); - return r ? new EncodeMapper(table->flags(), type, table) : 0; - } - - static EncodeMapper<A> *Read(const string& filename, EncodeType type) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "EncodeMap: Can't open file: " << filename; - return false; - } - return Read(strm, filename, type); - } - - private: - uint32 ref_count_; - uint32 flags_; - EncodeType type_; - EncodeTable<A>* table_; - - explicit EncodeMapper(uint32 flags, EncodeType type, EncodeTable<A> *table) - : ref_count_(1), flags_(flags), type_(type), table_(table) {} - void operator=(const EncodeMapper &); // Disallow. -}; - - -// Complexity: O(nstates + narcs) -template<class A> inline -void Encode(MutableFst<A> *fst, EncodeMapper<A>* mapper) { - Map(fst, mapper); -} - - -template<class A> inline -void Decode(MutableFst<A>* fst, const EncodeMapper<A>& mapper) { - Map(fst, EncodeMapper<A>(mapper, DECODE)); - RmFinalEpsilon(fst); -} - - -// On the fly label and/or weight encoding of input Fst -// -// Complexity: -// - Constructor: O(1) -// - Traversal: O(nstates_visited + narcs_visited), assuming constant -// time to visit an input state or arc. -template <class A> -class EncodeFst : public MapFst<A, A, EncodeMapper<A> > { - public: - typedef A Arc; - typedef EncodeMapper<A> C; - - EncodeFst(const Fst<A> &fst, EncodeMapper<A>* encoder) - : MapFst<A, A, C>(fst, encoder, MapFstOptions()) {} - - EncodeFst(const Fst<A> &fst, const EncodeMapper<A>& encoder) - : MapFst<A, A, C>(fst, encoder, MapFstOptions()) {} - - EncodeFst(const EncodeFst<A> &fst) - : MapFst<A, A, C>(fst) {} - - virtual EncodeFst<A> *Copy() const { return new EncodeFst(*this); } -}; - - -// On the fly label and/or weight encoding of input Fst -// -// Complexity: -// - Constructor: O(1) -// - Traversal: O(nstates_visited + narcs_visited), assuming constant -// time to visit an input state or arc. -template <class A> -class DecodeFst : public MapFst<A, A, EncodeMapper<A> > { - public: - typedef A Arc; - typedef EncodeMapper<A> C; - - DecodeFst(const Fst<A> &fst, const EncodeMapper<A>& encoder) - : MapFst<A, A, C>(fst, - EncodeMapper<A>(encoder, DECODE), - MapFstOptions()) {} - - DecodeFst(const EncodeFst<A> &fst) - : MapFst<A, A, C>(fst) {} - - virtual DecodeFst<A> *Copy() const { return new DecodeFst(*this); } -}; - - -// Specialization for EncodeFst. -template <class A> -class StateIterator< EncodeFst<A> > - : public StateIterator< MapFst<A, A, EncodeMapper<A> > > { - public: - explicit StateIterator(const EncodeFst<A> &fst) - : StateIterator< MapFst<A, A, EncodeMapper<A> > >(fst) {} -}; - - -// Specialization for EncodeFst. -template <class A> -class ArcIterator< EncodeFst<A> > - : public ArcIterator< MapFst<A, A, EncodeMapper<A> > > { - public: - ArcIterator(const EncodeFst<A> &fst, typename A::StateId s) - : ArcIterator< MapFst<A, A, EncodeMapper<A> > >(fst, s) {} -}; - - -// Specialization for DecodeFst. -template <class A> -class StateIterator< DecodeFst<A> > - : public StateIterator< MapFst<A, A, EncodeMapper<A> > > { - public: - explicit StateIterator(const DecodeFst<A> &fst) - : StateIterator< MapFst<A, A, EncodeMapper<A> > >(fst) {} -}; - - -// Specialization for DecodeFst. -template <class A> -class ArcIterator< DecodeFst<A> > - : public ArcIterator< MapFst<A, A, EncodeMapper<A> > > { - public: - ArcIterator(const DecodeFst<A> &fst, typename A::StateId s) - : ArcIterator< MapFst<A, A, EncodeMapper<A> > >(fst, s) {} -}; - - -// Useful aliases when using StdArc. -typedef EncodeFst<StdArc> StdEncodeFst; - -typedef DecodeFst<StdArc> StdDecodeFst; - -} - -#endif // FST_LIB_ENCODE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/epsnormalize.h b/tools/thirdparty/OpenFst/fst/lib/epsnormalize.h deleted file mode 100644 index ac4baf2..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/epsnormalize.h +++ /dev/null @@ -1,70 +0,0 @@ -// epsnormalize.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Function that implements epsilon normalization. - -#ifndef FST_LIB_EPSNORMALIZE_H__ -#define FST_LIB_EPSNORMALIZE_H__ - -#include <ext/hash_map> -using __gnu_cxx::hash_map; -#include <ext/slist> -using __gnu_cxx::slist; - -#include "fst/lib/factor-weight.h" -#include "fst/lib/invert.h" -#include "fst/lib/map.h" -#include "fst/lib/rmepsilon.h" - -namespace fst { - -enum EpsNormalizeType {EPS_NORM_INPUT, EPS_NORM_OUTPUT}; - -// Returns an equivalent FST that is epsilon-normalized. An acceptor is -// epsilon-normalized if it is epsilon-removed. A transducer is input -// epsilon-normalized if additionally if on each path any epsilon input -// label follows all non-epsilon input labels. Output epsilon-normalized -// is defined similarly. -// -// The input FST needs to be functional. -// -// References: -// - Mehryar Mohri. "Generic epsilon-removal and input epsilon-normalization -// algorithms for weighted transducers", International Journal of Computer -// Science, 13(1): 129-143, 2002. -template <class Arc> -void EpsNormalize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst, - EpsNormalizeType type = EPS_NORM_INPUT) { - VectorFst< GallicArc<Arc, STRING_RIGHT_RESTRICT> > gfst; - if (type == EPS_NORM_INPUT) - Map(ifst, &gfst, ToGallicMapper<Arc, STRING_RIGHT_RESTRICT>()); - else // type == EPS_NORM_OUTPUT - Map(InvertFst<Arc>(ifst), &gfst, - ToGallicMapper<Arc, STRING_RIGHT_RESTRICT>()); - RmEpsilon(&gfst); - FactorWeightFst< GallicArc<Arc, STRING_RIGHT_RESTRICT>, - GallicFactor<typename Arc::Label, - typename Arc::Weight, STRING_RIGHT_RESTRICT> > - fwfst(gfst); - Map(fwfst, ofst, FromGallicMapper<Arc, STRING_RIGHT_RESTRICT>()); - if(type == EPS_NORM_OUTPUT) - Invert(ofst); -} - -} // namespace fst - -#endif // FST_LIB_EPSNORMALIZE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/equal.h b/tools/thirdparty/OpenFst/fst/lib/equal.h deleted file mode 100644 index 8e9fb11..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/equal.h +++ /dev/null @@ -1,119 +0,0 @@ -// test.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function to test equality of two Fsts. - -#ifndef FST_LIB_EQUAL_H__ -#define FST_LIB_EQUAL_H__ - -#include "fst/lib/fst.h" - -namespace fst { - -// Tests if two Fsts have the same states and arcs in the same order. -template<class Arc> -bool Equal(const Fst<Arc> &fst1, const Fst<Arc> &fst2) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - - if (fst1.Start() != fst2.Start()) { - VLOG(1) << "Equal: mismatched start states"; - return false; - } - - StateIterator< Fst<Arc> > siter1(fst1); - StateIterator< Fst<Arc> > siter2(fst2); - - while (!siter1.Done() || !siter2.Done()) { - if (siter1.Done() || siter2.Done()) { - VLOG(1) << "Equal: mismatched # of states"; - return false; - } - StateId s1 = siter1.Value(); - StateId s2 = siter2.Value(); - if (s1 != s2) { - VLOG(1) << "Equal: mismatched states:" - << ", state1 = " << s1 - << ", state2 = " << s2; - return false; - } - Weight final1 = fst1.Final(s1); - Weight final2 = fst2.Final(s2); - if (!ApproxEqual(final1, final2)) { - VLOG(1) << "Equal: mismatched final weights:" - << " state = " << s1 - << ", final1 = " << final1 - << ", final2 = " << final2; - return false; - } - ArcIterator< Fst<Arc> > aiter1(fst1, s1); - ArcIterator< Fst<Arc> > aiter2(fst2, s2); - for (size_t a = 0; !aiter1.Done() || !aiter2.Done(); ++a) { - if (aiter1.Done() || aiter2.Done()) { - VLOG(1) << "Equal: mismatched # of arcs" - << " state = " << s1; - return false; - } - Arc arc1 = aiter1.Value(); - Arc arc2 = aiter2.Value(); - if (arc1.ilabel != arc2.ilabel) { - VLOG(1) << "Equal: mismatched arc input labels:" - << " state = " << s1 - << ", arc = " << a - << ", ilabel1 = " << arc1.ilabel - << ", ilabel2 = " << arc2.ilabel; - return false; - } else if (arc1.olabel != arc2.olabel) { - VLOG(1) << "Equal: mismatched arc output labels:" - << " state = " << s1 - << ", arc = " << a - << ", olabel1 = " << arc1.olabel - << ", olabel2 = " << arc2.olabel; - return false; - } else if (!ApproxEqual(arc1.weight, arc2.weight)) { - VLOG(1) << "Equal: mismatched arc weights:" - << " state = " << s1 - << ", arc = " << a - << ", weight1 = " << arc1.weight - << ", weight2 = " << arc2.weight; - return false; - } else if (arc1.nextstate != arc2.nextstate) { - VLOG(1) << "Equal: mismatched input label:" - << " state = " << s1 - << ", arc = " << a - << ", nextstate1 = " << arc1.nextstate - << ", nextstate2 = " << arc2.nextstate; - return false; - } - aiter1.Next(); - aiter2.Next(); - - } - // Sanity checks - CHECK_EQ(fst1.NumArcs(s1), fst2.NumArcs(s2)); - CHECK_EQ(fst1.NumInputEpsilons(s1), fst2.NumInputEpsilons(s2)); - CHECK_EQ(fst1.NumOutputEpsilons(s1), fst2.NumOutputEpsilons(s2)); - - siter1.Next(); - siter2.Next(); - } - return true; -} - -} // namespace fst - - -#endif // FST_LIB_EQUAL_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/equivalent.h b/tools/thirdparty/OpenFst/fst/lib/equivalent.h deleted file mode 100644 index e585d9b..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/equivalent.h +++ /dev/null @@ -1,233 +0,0 @@ -// equivalent.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file Functions and classes to determine the equivalence of two -// FSTs. - -#ifndef FST_LIB_EQUIVALENT_H__ -#define FST_LIB_EQUIVALENT_H__ - -#include <algorithm> - -#include <ext/hash_map> -using __gnu_cxx::hash_map; - -#include "fst/lib/encode.h" -#include "fst/lib/push.h" -#include "fst/lib/union-find.h" -#include "fst/lib/vector-fst.h" - -namespace fst { - -// Traits-like struct holding utility functions/typedefs/constants for -// the equivalence algorithm. -// -// Encoding device: in order to make the statesets of the two acceptors -// disjoint, we map Arc::StateId on the type MappedId. The states of -// the first acceptor are mapped on odd numbers (s -> 2s + 1), and -// those of the second one on even numbers (s -> 2s + 2). The number 0 -// is reserved for an implicit (non-final) 'dead state' (required for -// the correct treatment of non-coaccessible states; kNoStateId is -// mapped to kDeadState for both acceptors). The union-find algorithm -// operates on the mapped IDs. -template <class Arc> -struct EquivalenceUtil { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - typedef int32 MappedId; // ID for an equivalence class. - - // MappedId for an implicit dead state. - static const MappedId kDeadState = 0; - - // MappedId for lookup failure. - static const MappedId kInvalidId = -1; - - // Maps state ID to the representative of the corresponding - // equivalence class. The parameter 'which_fst' takes the values 1 - // and 2, identifying the input FST. - static MappedId MapState(StateId s, int32 which_fst) { - return - (kNoStateId == s) - ? - kDeadState - : - (static_cast<MappedId>(s) << 1) + which_fst; - } - // Maps set ID to State ID. - static StateId UnMapState(MappedId id) { - return static_cast<StateId>((--id) >> 1); - } - // Convenience function: checks if state with MappedId 's' is final - // in acceptor 'fa'. - static bool IsFinal(const Fst<Arc> &fa, MappedId s) { - return - (kDeadState == s) ? - false : (fa.Final(UnMapState(s)) != Weight::Zero()); - } - // Convenience function: returns the representative of 'id' in 'sets', - // creating a new set if needed. - static MappedId FindSet(UnionFind<MappedId> *sets, MappedId id) { - MappedId repr = sets->FindSet(id); - if (repr != kInvalidId) { - return repr; - } else { - sets->MakeSet(id); - return id; - } - } -}; - -// Equivalence checking algorithm: determines if the two FSTs -// <code>fst1</code> and <code>fst2</code> are equivalent. The input -// FSTs must be deterministic input-side epsilon-free acceptors, -// unweighted or with weights over a left semiring. Two acceptors are -// considered equivalent if they accept exactly the same set of -// strings (with the same weights). -// -// The algorithm (cf. Aho, Hopcroft and Ullman, "The Design and -// Analysis of Computer Programs") successively constructs sets of -// states that can be reached by the same prefixes, starting with a -// set containing the start states of both acceptors. A disjoint tree -// forest (the union-find algorithm) is used to represent the sets of -// states. The algorithm returns 'false' if one of the constructed -// sets contains both final and non-final states. -// -// Complexity: quasi-linear, i.e. O(n G(n)), where -// n = |S1| + |S2| is the number of states in both acceptors -// G(n) is a very slowly growing function that can be approximated -// by 4 by all practical purposes. -// -template <class Arc> -bool Equivalent(const Fst<Arc> &fst1, const Fst<Arc> &fst2) { - typedef typename Arc::Weight Weight; - // Check properties first: - uint64 props = kNoEpsilons | kIDeterministic | kAcceptor; - if (fst1.Properties(props, true) != props) { - LOG(FATAL) << "Equivalent: first argument not an" - << " epsilon-free deterministic acceptor"; - } - if (fst2.Properties(props, true) != props) { - LOG(FATAL) << "Equivalent: second argument not an" - << " epsilon-free deterministic acceptor"; - } - - if ((fst1.Properties(kUnweighted , true) != kUnweighted) - || (fst2.Properties(kUnweighted , true) != kUnweighted)) { - VectorFst<Arc> efst1(fst1); - VectorFst<Arc> efst2(fst2); - Push(&efst1, REWEIGHT_TO_INITIAL); - Push(&efst2, REWEIGHT_TO_INITIAL); - Map(&efst1, QuantizeMapper<Arc>()); - Map(&efst2, QuantizeMapper<Arc>()); - EncodeMapper<Arc> mapper(kEncodeWeights|kEncodeLabels, ENCODE); - Map(&efst1, &mapper); - Map(&efst2, &mapper); - return Equivalent(efst1, efst2); - } - - // Convenience typedefs: - typedef typename Arc::StateId StateId; - typedef EquivalenceUtil<Arc> Util; - typedef typename Util::MappedId MappedId; - enum { FST1 = 1, FST2 = 2 }; // Required by Util::MapState(...) - - MappedId s1 = Util::MapState(fst1.Start(), FST1); - MappedId s2 = Util::MapState(fst2.Start(), FST2); - - // The union-find structure. - UnionFind<MappedId> eq_classes(1000, Util::kInvalidId); - - // Initialize the union-find structure. - eq_classes.MakeSet(s1); - eq_classes.MakeSet(s2); - - // Early return if the start states differ w.r.t. being final. - if (Util::IsFinal(fst1, s1) != Util::IsFinal(fst2, s2)) { - return false; - } - // Data structure for the (partial) acceptor transition function of - // fst1 and fst2: input labels mapped to pairs of MappedId's - // representing destination states of the corresponding arcs in fst1 - // and fst2, respectively. - typedef - hash_map<typename Arc::Label, pair<MappedId, MappedId> > - Label2StatePairMap; - - Label2StatePairMap arc_pairs; - - // Pairs of MappedId's to be processed, organized in a queue. - deque<pair<MappedId, MappedId> > q; - - // Main loop: explores the two acceptors in a breadth-first manner, - // updating the equivalence relation on the statesets. Loop - // invariant: each block of states contains either final states only - // or non-final states only. - for (q.push_back(make_pair(s1, s2)); !q.empty(); q.pop_front()) { - s1 = q.front().first; - s2 = q.front().second; - - // Representatives of the equivalence classes of s1/s2. - MappedId rep1 = Util::FindSet(&eq_classes, s1); - MappedId rep2 = Util::FindSet(&eq_classes, s2); - - if (rep1 != rep2) { - eq_classes.Union(rep1, rep2); - arc_pairs.clear(); - - // Copy outgoing arcs starting at s1 into the hashtable. - if (Util::kDeadState != s1) { - ArcIterator<Fst<Arc> > arc_iter(fst1, Util::UnMapState(s1)); - for (; !arc_iter.Done(); arc_iter.Next()) { - const Arc &arc = arc_iter.Value(); - if (arc.weight != Weight::Zero()) { // Zero-weight arcs - // are treated as - // non-exisitent. - arc_pairs[arc.ilabel].first = Util::MapState(arc.nextstate, FST1); - } - } - } - // Copy outgoing arcs starting at s2 into the hashtable. - if (Util::kDeadState != s2) { - ArcIterator<Fst<Arc> > arc_iter(fst2, Util::UnMapState(s2)); - for (; !arc_iter.Done(); arc_iter.Next()) { - const Arc &arc = arc_iter.Value(); - if (arc.weight != Weight::Zero()) { // Zero-weight arcs - // are treated as - // non-existent. - arc_pairs[arc.ilabel].second = Util::MapState(arc.nextstate, FST2); - } - } - } - // Iterate through the hashtable and process pairs of target - // states. - for (typename Label2StatePairMap::const_iterator - arc_iter = arc_pairs.begin(); - arc_iter != arc_pairs.end(); - ++arc_iter) { - const pair<MappedId, MappedId> &p = arc_iter->second; - if (Util::IsFinal(fst1, p.first) != Util::IsFinal(fst2, p.second)) { - // Detected inconsistency: return false. - return false; - } - q.push_back(p); - } - } - } - return true; -} - -} // namespace fst - -#endif // FST_LIB_EQUIVALENT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/expanded-fst.h b/tools/thirdparty/OpenFst/fst/lib/expanded-fst.h deleted file mode 100644 index b8beaf8..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/expanded-fst.h +++ /dev/null @@ -1,96 +0,0 @@ -// expanded-fst.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Generic FST augmented with state count - interface class definition. - -#ifndef FST_LIB_EXPANDED_FST_H__ -#define FST_LIB_EXPANDED_FST_H__ - -#include "fst/lib/fst.h" - -namespace fst { - -// A generic FST plus state count. -template <class A> -class ExpandedFst : public Fst<A> { - public: - typedef A Arc; - typedef typename A::StateId StateId; - - virtual StateId NumStates() const = 0; // State count - - // Get a copy of this ExpandedFst. - virtual ExpandedFst<A> *Copy() const = 0; - // Read an ExpandedFst from an input stream; return NULL on error. - static ExpandedFst<A> *Read(istream &strm, const FstReadOptions &opts) { - FstReadOptions ropts(opts); - FstHeader hdr; - if (ropts.header) - hdr = *opts.header; - else { - if (!hdr.Read(strm, opts.source)) - return 0; - ropts.header = &hdr; - } - if (!(hdr.Properties() & kExpanded)) { - LOG(ERROR) << "ExpandedFst::Read: Not an ExpandedFst: " << ropts.source; - return 0; - } - FstRegister<A> *registr = FstRegister<A>::GetRegister(); - const typename FstRegister<A>::Reader reader = - registr->GetReader(hdr.FstType()); - if (!reader) { - LOG(ERROR) << "ExpandedFst::Read: Unknown FST type \"" << hdr.FstType() - << "\" (arc type = \"" << A::Type() - << "\"): " << ropts.source; - return 0; - } - Fst<A> *fst = reader(strm, ropts); - if (!fst) return 0; - return down_cast<ExpandedFst<A> *>(fst); - } - // Read an ExpandedFst from a file; return NULL on error. - static ExpandedFst<A> *Read(const string &filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename; - return 0; - } - return Read(strm, FstReadOptions(filename)); - } -}; - -// A useful alias when using StdArc. -typedef ExpandedFst<StdArc> StdExpandedFst; - -// Function to return the number of states in an FST, counting them -// if necessary. -template <class Arc> -typename Arc::StateId CountStates(const Fst<Arc> &fst) { - if (fst.Properties(kExpanded, false)) { - const ExpandedFst<Arc> *efst = down_cast<const ExpandedFst<Arc> *>(&fst); - return efst->NumStates(); - } else { - typename Arc::StateId nstates = 0; - for (StateIterator< Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) - ++nstates; - return nstates; - } -} - -} // FST_LIB_FST_H__ - -#endif // FST_LIB_EXPANDED_FST_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/factor-weight.h b/tools/thirdparty/OpenFst/fst/lib/factor-weight.h deleted file mode 100644 index 3a2ef85..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/factor-weight.h +++ /dev/null @@ -1,449 +0,0 @@ -// factor-weight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Classes to factor weights in an FST. - -#ifndef FST_LIB_FACTOR_WEIGHT_H__ -#define FST_LIB_FACTOR_WEIGHT_H__ - -#include <algorithm> - -#include <ext/hash_map> -using __gnu_cxx::hash_map; -#include <ext/slist> -using __gnu_cxx::slist; - -#include "fst/lib/cache.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -struct FactorWeightOptions : CacheOptions { - float delta; - bool final_only; // only factor final weights when true - - FactorWeightOptions(const CacheOptions &opts, float d, bool of) - : CacheOptions(opts), delta(d), final_only(of) {} - - explicit FactorWeightOptions(float d, bool of = false) - : delta(d), final_only(of) {} - - FactorWeightOptions(bool of = false) - : delta(kDelta), final_only(of) {} -}; - - -// A factor iterator takes as argument a weight w and returns a -// sequence of pairs of weights (xi,yi) such that the sum of the -// products xi times yi is equal to w. If w is fully factored, -// the iterator should return nothing. -// -// template <class W> -// class FactorIterator { -// public: -// FactorIterator(W w); -// bool Done() const; -// void Next(); -// pair<W, W> Value() const; -// void Reset(); -// } - - -// Factor trivially. -template <class W> -class IdentityFactor { - public: - IdentityFactor(const W &w) {} - bool Done() const { return true; } - void Next() {} - pair<W, W> Value() const { return make_pair(W::One(), W::One()); } // unused - void Reset() {} -}; - - -// Factor a StringWeight w as 'ab' where 'a' is a label. -template <typename L, StringType S = STRING_LEFT> -class StringFactor { - public: - StringFactor(const StringWeight<L, S> &w) - : weight_(w), done_(w.Size() <= 1) {} - - bool Done() const { return done_; } - - void Next() { done_ = true; } - - pair< StringWeight<L, S>, StringWeight<L, S> > Value() const { - StringWeightIterator<L, S> iter(weight_); - StringWeight<L, S> w1(iter.Value()); - StringWeight<L, S> w2; - for (iter.Next(); !iter.Done(); iter.Next()) - w2.PushBack(iter.Value()); - return make_pair(w1, w2); - } - - void Reset() { done_ = weight_.Size() <= 1; } - - private: - StringWeight<L, S> weight_; - bool done_; -}; - - -// Factor a GallicWeight using StringFactor. -template <class L, class W, StringType S = STRING_LEFT> -class GallicFactor { - public: - GallicFactor(const GallicWeight<L, W, S> &w) - : weight_(w), done_(w.Value1().Size() <= 1) {} - - bool Done() const { return done_; } - - void Next() { done_ = true; } - - pair< GallicWeight<L, W, S>, GallicWeight<L, W, S> > Value() const { - StringFactor<L, S> iter(weight_.Value1()); - GallicWeight<L, W, S> w1(iter.Value().first, weight_.Value2()); - GallicWeight<L, W, S> w2(iter.Value().second, W::One()); - return make_pair(w1, w2); - } - - void Reset() { done_ = weight_.Value1().Size() <= 1; } - - private: - GallicWeight<L, W, S> weight_; - bool done_; -}; - - -// Implementation class for FactorWeight -template <class A, class F> -class FactorWeightFstImpl - : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - using CacheBaseImpl< CacheState<A> >::HasStart; - using CacheBaseImpl< CacheState<A> >::HasFinal; - using CacheBaseImpl< CacheState<A> >::HasArcs; - - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef F FactorIterator; - - struct Element { - Element() {} - - Element(StateId s, Weight w) : state(s), weight(w) {} - - StateId state; // Input state Id - Weight weight; // Residual weight - }; - - FactorWeightFstImpl(const Fst<A> &fst, const FactorWeightOptions &opts) - : CacheImpl<A>(opts), fst_(fst.Copy()), delta_(opts.delta), - final_only_(opts.final_only) { - SetType("factor-weight"); - uint64 props = fst.Properties(kFstProperties, false); - SetProperties(FactorWeightProperties(props), kCopyProperties); - - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - } - - ~FactorWeightFstImpl() { - delete fst_; - } - - StateId Start() { - if (!HasStart()) { - StateId s = fst_->Start(); - if (s == kNoStateId) - return kNoStateId; - StateId start = FindState(Element(fst_->Start(), Weight::One())); - SetStart(start); - } - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - const Element &e = elements_[s]; - // TODO: fix so cast is unnecessary - Weight w = e.state == kNoStateId - ? e.weight - : (Weight) Times(e.weight, fst_->Final(e.state)); - FactorIterator f(w); - if (w != Weight::Zero() && f.Done()) - SetFinal(s, w); - else - SetFinal(s, Weight::Zero()); - } - return CacheImpl<A>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - - // Find state corresponding to an element. Create new state - // if element not found. - StateId FindState(const Element &e) { - if (final_only_ && e.weight == Weight::One()) { - while (unfactored_.size() <= (unsigned int)e.state) - unfactored_.push_back(kNoStateId); - if (unfactored_[e.state] == kNoStateId) { - unfactored_[e.state] = elements_.size(); - elements_.push_back(e); - } - return unfactored_[e.state]; - } else { - typename ElementMap::iterator eit = element_map_.find(e); - if (eit != element_map_.end()) { - return (*eit).second; - } else { - StateId s = elements_.size(); - elements_.push_back(e); - element_map_.insert(pair<const Element, StateId>(e, s)); - return s; - } - } - } - - // Computes the outgoing transitions from a state, creating new destination - // states as needed. - void Expand(StateId s) { - Element e = elements_[s]; - if (e.state != kNoStateId) { - for (ArcIterator< Fst<A> > ait(*fst_, e.state); - !ait.Done(); - ait.Next()) { - const A &arc = ait.Value(); - Weight w = Times(e.weight, arc.weight); - FactorIterator fit(w); - if (final_only_ || fit.Done()) { - StateId d = FindState(Element(arc.nextstate, Weight::One())); - AddArc(s, Arc(arc.ilabel, arc.olabel, w, d)); - } else { - for (; !fit.Done(); fit.Next()) { - const pair<Weight, Weight> &p = fit.Value(); - StateId d = FindState(Element(arc.nextstate, - p.second.Quantize(delta_))); - AddArc(s, Arc(arc.ilabel, arc.olabel, p.first, d)); - } - } - } - } - if ((e.state == kNoStateId) || - (fst_->Final(e.state) != Weight::Zero())) { - Weight w = e.state == kNoStateId - ? e.weight - : Times(e.weight, fst_->Final(e.state)); - for (FactorIterator fit(w); - !fit.Done(); - fit.Next()) { - const pair<Weight, Weight> &p = fit.Value(); - StateId d = FindState(Element(kNoStateId, - p.second.Quantize(delta_))); - AddArc(s, Arc(0, 0, p.first, d)); - } - } - SetArcs(s); - } - - private: - // Equality function for Elements, assume weights have been quantized. - class ElementEqual { - public: - bool operator()(const Element &x, const Element &y) const { - return x.state == y.state && x.weight == y.weight; - } - }; - - // Hash function for Elements to Fst states. - class ElementKey { - public: - size_t operator()(const Element &x) const { - return static_cast<size_t>(x.state * kPrime + x.weight.Hash()); - } - private: - static const int kPrime = 7853; - }; - - typedef hash_map<Element, StateId, ElementKey, ElementEqual> ElementMap; - - const Fst<A> *fst_; - float delta_; - bool final_only_; - vector<Element> elements_; // mapping Fst state to Elements - ElementMap element_map_; // mapping Elements to Fst state - // mapping between old/new 'StateId' for states that do not need to - // be factored when 'final_only_' is true - vector<StateId> unfactored_; - - DISALLOW_EVIL_CONSTRUCTORS(FactorWeightFstImpl); -}; - - -// FactorWeightFst takes as template parameter a FactorIterator as -// defined above. The result of weight factoring is a transducer -// equivalent to the input whose path weights have been factored -// according to the FactorIterator. States and transitions will be -// added as necessary. The algorithm is a generalization to arbitrary -// weights of the second step of the input epsilon-normalization -// algorithm due to Mohri, "Generic epsilon-removal and input -// epsilon-normalization algorithms for weighted transducers", -// International Journal of Computer Science 13(1): 129-143 (2002). -template <class A, class F> -class FactorWeightFst : public Fst<A> { - public: - friend class ArcIterator< FactorWeightFst<A, F> >; - friend class CacheStateIterator< FactorWeightFst<A, F> >; - friend class CacheArcIterator< FactorWeightFst<A, F> >; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - FactorWeightFst(const Fst<A> &fst) - : impl_(new FactorWeightFstImpl<A, F>(fst, FactorWeightOptions())) {} - - FactorWeightFst(const Fst<A> &fst, const FactorWeightOptions &opts) - : impl_(new FactorWeightFstImpl<A, F>(fst, opts)) {} - FactorWeightFst(const FactorWeightFst<A, F> &fst) : Fst<A>(fst), impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~FactorWeightFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual FactorWeightFst<A, F> *Copy() const { - return new FactorWeightFst<A, F>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - private: - FactorWeightFstImpl<A, F> *Impl() { return impl_; } - - FactorWeightFstImpl<A, F> *impl_; - - void operator=(const FactorWeightFst<A, F> &fst); // Disallow -}; - - -// Specialization for FactorWeightFst. -template<class A, class F> -class StateIterator< FactorWeightFst<A, F> > - : public CacheStateIterator< FactorWeightFst<A, F> > { - public: - explicit StateIterator(const FactorWeightFst<A, F> &fst) - : CacheStateIterator< FactorWeightFst<A, F> >(fst) {} -}; - - -// Specialization for FactorWeightFst. -template <class A, class F> -class ArcIterator< FactorWeightFst<A, F> > - : public CacheArcIterator< FactorWeightFst<A, F> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const FactorWeightFst<A, F> &fst, StateId s) - : CacheArcIterator< FactorWeightFst<A, F> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -template <class A, class F> inline -void FactorWeightFst<A, F>::InitStateIterator(StateIteratorData<A> *data) const -{ - data->base = new StateIterator< FactorWeightFst<A, F> >(*this); -} - - -} // namespace fst - -#endif // FST_LIB_FACTOR_WEIGHT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/float-weight.h b/tools/thirdparty/OpenFst/fst/lib/float-weight.h deleted file mode 100644 index bec59c4..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/float-weight.h +++ /dev/null @@ -1,255 +0,0 @@ -// float-weight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Float weight set and associated semiring operation definitions. -// - -#ifndef FST_LIB_FLOAT_WEIGHT_H__ -#define FST_LIB_FLOAT_WEIGHT_H__ - -#include <limits> - -#include "fst/lib/weight.h" - -namespace fst { - -static const float kPosInfinity = numeric_limits<float>::infinity(); -static const float kNegInfinity = -kPosInfinity; - -// Single precision floating point weight base class -class FloatWeight { - public: - FloatWeight() {} - - FloatWeight(float f) : value_(f) {} - - FloatWeight(const FloatWeight &w) : value_(w.value_) {} - - FloatWeight &operator=(const FloatWeight &w) { - value_ = w.value_; - return *this; - } - - istream &Read(istream &strm) { - return ReadType(strm, &value_); - } - - ostream &Write(ostream &strm) const { - return WriteType(strm, value_); - } - - ssize_t Hash() const { - union { - float f; - ssize_t s; - } u = { value_ }; - return u.s; - } - - const float &Value() const { return value_; } - - protected: - float value_; -}; - -inline bool operator==(const FloatWeight &w1, const FloatWeight &w2) { - // Volatile qualifier thwarts over-aggressive compiler optimizations - // that lead to problems esp. with NaturalLess(). - volatile float v1 = w1.Value(); - volatile float v2 = w2.Value(); - return v1 == v2; -} - -inline bool operator!=(const FloatWeight &w1, const FloatWeight &w2) { - return !(w1 == w2); -} - -inline bool ApproxEqual(const FloatWeight &w1, const FloatWeight &w2, - float delta = kDelta) { - return w1.Value() <= w2.Value() + delta && w2.Value() <= w1.Value() + delta; -} - -inline ostream &operator<<(ostream &strm, const FloatWeight &w) { - if (w.Value() == kPosInfinity) - return strm << "Infinity"; - else if (w.Value() == kNegInfinity) - return strm << "-Infinity"; - else if (w.Value() != w.Value()) // Fails for NaN - return strm << "BadFloat"; - else - return strm << w.Value(); -} - -inline istream &operator>>(istream &strm, FloatWeight &w) { - string s; - strm >> s; - if (s == "Infinity") { - w = FloatWeight(kPosInfinity); - } else if (s == "-Infinity") { - w = FloatWeight(kNegInfinity); - } else { - char *p; - float f = strtod(s.c_str(), &p); - if (p < s.c_str() + s.size()) - strm.clear(std::ios::badbit); - else - w = FloatWeight(f); - } - return strm; -} - - -// Tropical semiring: (min, +, inf, 0) -class TropicalWeight : public FloatWeight { - public: - typedef TropicalWeight ReverseWeight; - - TropicalWeight() : FloatWeight() {} - - TropicalWeight(float f) : FloatWeight(f) {} - - TropicalWeight(const TropicalWeight &w) : FloatWeight(w) {} - - static const TropicalWeight Zero() { return TropicalWeight(kPosInfinity); } - - static const TropicalWeight One() { return TropicalWeight(0.0F); } - - static const string &Type() { - static const string type = "tropical"; - return type; - } - - bool Member() const { - // First part fails for IEEE NaN - return Value() == Value() && Value() != kNegInfinity; - } - - TropicalWeight Quantize(float delta = kDelta) const { - return TropicalWeight(floor(Value()/delta + 0.5F) * delta); - } - - TropicalWeight Reverse() const { return *this; } - - static uint64 Properties() { - return kLeftSemiring | kRightSemiring | kCommutative | - kPath | kIdempotent; - } -}; - -inline TropicalWeight Plus(const TropicalWeight &w1, - const TropicalWeight &w2) { - return w1.Value() < w2.Value() ? w1 : w2; -} - -inline TropicalWeight Times(const TropicalWeight &w1, - const TropicalWeight &w2) { - float f1 = w1.Value(), f2 = w2.Value(); - if (f1 == kPosInfinity) - return w1; - else if (f2 == kPosInfinity) - return w2; - else - return TropicalWeight(f1 + f2); -} - -inline TropicalWeight Divide(const TropicalWeight &w1, - const TropicalWeight &w2, - DivideType typ = DIVIDE_ANY) { - float f1 = w1.Value(), f2 = w2.Value(); - if (f2 == kPosInfinity) - return kNegInfinity; - else if (f1 == kPosInfinity) - return kPosInfinity; - else - return TropicalWeight(f1 - f2); -} - - -// Log semiring: (log(e^-x + e^y), +, inf, 0) -class LogWeight : public FloatWeight { - public: - typedef LogWeight ReverseWeight; - - LogWeight() : FloatWeight() {} - - LogWeight(float f) : FloatWeight(f) {} - - LogWeight(const LogWeight &w) : FloatWeight(w) {} - - static const LogWeight Zero() { return LogWeight(kPosInfinity); } - - static const LogWeight One() { return LogWeight(0.0F); } - - static const string &Type() { - static const string type = "log"; - return type; - } - - bool Member() const { - // First part fails for IEEE NaN - return Value() == Value() && Value() != kNegInfinity; - } - - LogWeight Quantize(float delta = kDelta) const { - return LogWeight(floor(Value()/delta + 0.5F) * delta); - } - - LogWeight Reverse() const { return *this; } - - static uint64 Properties() { - return kLeftSemiring | kRightSemiring | kCommutative; - } -}; - -inline double LogExp(double x) { return log(1.0F + exp(-x)); } - -inline LogWeight Plus(const LogWeight &w1, const LogWeight &w2) { - float f1 = w1.Value(), f2 = w2.Value(); - if (f1 == kPosInfinity) - return w2; - else if (f2 == kPosInfinity) - return w1; - else if (f1 > f2) - return LogWeight(f2 - LogExp(f1 - f2)); - else - return LogWeight(f1 - LogExp(f2 - f1)); -} - -inline LogWeight Times(const LogWeight &w1, const LogWeight &w2) { - float f1 = w1.Value(), f2 = w2.Value(); - if (f1 == kPosInfinity) - return w1; - else if (f2 == kPosInfinity) - return w2; - else - return LogWeight(f1 + f2); -} - -inline LogWeight Divide(const LogWeight &w1, - const LogWeight &w2, - DivideType typ = DIVIDE_ANY) { - float f1 = w1.Value(), f2 = w2.Value(); - if (f2 == kPosInfinity) - return kNegInfinity; - else if (f1 == kPosInfinity) - return kPosInfinity; - else - return LogWeight(f1 - f2); -} - -} // namespace fst; - -#endif // FST_LIB_FLOAT_WEIGHT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/fst-decl.h b/tools/thirdparty/OpenFst/fst/lib/fst-decl.h deleted file mode 100644 index a5cdc75..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/fst-decl.h +++ /dev/null @@ -1,79 +0,0 @@ -// fst-decl.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// This file contains declarations of classes in the OpenFst library. - -#ifndef FST_LIB_FST_DECL_H__ -#define FST_LIB_FST_DECL_H__ - -namespace fst { - -class SymbolTable; -class SymbolTableIterator; - -class LogWeight; -class TropicalWeight; - -class LogArc; -class StdArc; - -template <class A> class ConstFst; -template <class A> class ExpandedFst; -template <class A> class Fst; -template <class A> class MutableFst; -template <class A> class VectorFst; - -template <class A, class C> class ArcSortFst; -template <class A> class ClosureFst; -template <class A> class ComposeFst; -template <class A> class ConcatFst; -template <class A> class DeterminizeFst; -template <class A> class DeterminizeFst; -template <class A> class DifferenceFst; -template <class A> class IntersectFst; -template <class A> class InvertFst; -template <class A, class B, class C> class MapFst; -template <class A> class ProjectFst; -template <class A> class RelabelFst; -template <class A> class ReplaceFst; -template <class A> class RmEpsilonFst; -template <class A> class UnionFst; - -template <class T, class Compare> class Heap; - -typedef ConstFst<StdArc> StdConstFst; -typedef ExpandedFst<StdArc> StdExpandedFst; -typedef Fst<StdArc> StdFst; -typedef MutableFst<StdArc> StdMutableFst; -typedef VectorFst<StdArc> StdVectorFst; - -template <class C> class StdArcSortFst; -typedef ClosureFst<StdArc> StdClosureFst; -typedef ComposeFst<StdArc> StdComposeFst; -typedef ConcatFst<StdArc> StdConcatFst; -typedef DeterminizeFst<StdArc> StdDeterminizeFst; -typedef DifferenceFst<StdArc> StdDifferenceFst; -typedef IntersectFst<StdArc> StdIntersectFst; -typedef InvertFst<StdArc> StdInvertFst; -typedef ProjectFst<StdArc> StdProjectFst; -typedef RelabelFst<StdArc> StdRelabelFst; -typedef ReplaceFst<StdArc> StdReplaceFst; -typedef RmEpsilonFst<StdArc> StdRmEpsilonFst; -typedef UnionFst<StdArc> StdUnionFst; - -} - -#endif // FST_LIB_FST_DECL_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/fst.cpp b/tools/thirdparty/OpenFst/fst/lib/fst.cpp deleted file mode 100644 index 3b8c9ef..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/fst.cpp +++ /dev/null @@ -1,89 +0,0 @@ -// fst.cc -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// FST definitions. - -#include "fst/lib/fst.h" - -// Include these so they are registered -#include "fst/lib/const-fst.h" -#include "fst/lib/vector-fst.h" - -// FST flag definitions - -DEFINE_bool(fst_verify_properties, false, - "Verify fst properties queried by TestProperties"); - -DEFINE_string(fst_product_separator, ",", - "Character separator between printed weights" - " in a product semiring"); - -DEFINE_bool(fst_default_cache_gc, true, "Enable garbage collection of cache"); - -DEFINE_int64(fst_default_cache_gc_limit, 1<<20LL, - "Cache byte size that triggers garbage collection"); - -namespace fst { - -// Register VectorFst and ConstFst for common arcs types - -REGISTER_FST(VectorFst, StdArc); -REGISTER_FST(VectorFst, LogArc); -REGISTER_FST(ConstFst, StdArc); -REGISTER_FST(ConstFst, LogArc); - -// Identifies stream data as an FST (and its endianity) -static const int32 kFstMagicNumber = 2125659606; - -// Check Fst magic number and read in Fst header. -bool FstHeader::Read(istream &strm, const string &source) { - int32 magic_number = 0; - ReadType(strm, &magic_number); - if (magic_number != kFstMagicNumber) { - LOG(ERROR) << "FstHeader::Read: Bad FST header: " << source; - return false; - } - - ReadType(strm, &fsttype_); - ReadType(strm, &arctype_); - ReadType(strm, &version_); - ReadType(strm, &flags_); - ReadType(strm, &properties_); - ReadType(strm, &start_); - ReadType(strm, &numstates_); - ReadType(strm, &numarcs_); - if (!strm) - LOG(ERROR) << "FstHeader::Read: read failed: " << source; - return strm; -} - -// Write Fst magic number and Fst header. -bool FstHeader::Write(ostream &strm, const string &source) const { - WriteType(strm, kFstMagicNumber); - WriteType(strm, fsttype_); - WriteType(strm, arctype_); - WriteType(strm, version_); - WriteType(strm, flags_); - WriteType(strm, properties_); - WriteType(strm, start_); - WriteType(strm, numstates_); - WriteType(strm, numarcs_); - if (!strm) - LOG(ERROR) << "FstHeader::Write: write failed: " << source; - return strm; -} - -} diff --git a/tools/thirdparty/OpenFst/fst/lib/fst.h b/tools/thirdparty/OpenFst/fst/lib/fst.h deleted file mode 100644 index 5a1a225..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/fst.h +++ /dev/null @@ -1,533 +0,0 @@ -// fst.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Finite-State Transducer (FST) - abstract base class definition, -// state and arc iterator interface, and suggested base implementation. - -#ifndef FST_LIB_FST_H__ -#define FST_LIB_FST_H__ - -#include "fst/lib/arc.h" -#include "fst/lib/compat.h" -#include "fst/lib/properties.h" -#include "fst/lib/register.h" -#include "fst/lib/symbol-table.h" -#include "fst/lib/util.h" - -namespace fst { - -class FstHeader; -template <class A> class StateIteratorData; -template <class A> class ArcIteratorData; - -struct FstReadOptions { - string source; // Where you're reading from - const FstHeader *header; // Pointer to Fst header (if non-zero) - const SymbolTable* isymbols; // Pointer to input symbols (if non-zero) - const SymbolTable* osymbols; // Pointer to output symbols (if non-zero) - - explicit FstReadOptions(const string& src = "<unspecified>", - const FstHeader *hdr = 0, - const SymbolTable* isym = 0, - const SymbolTable* osym = 0) - : source(src), header(hdr), isymbols(isym), osymbols(osym) {} -}; - - -struct FstWriteOptions { - string source; // Where you're writing to - bool write_header; // Write the header? - bool write_isymbols; // Write input symbols? - bool write_osymbols; // Write output symbols? - - explicit FstWriteOptions(const string& src = "<unspecifed>", - bool hdr = true, bool isym = true, - bool osym = true) - : source(src), write_header(hdr), - write_isymbols(isym), write_osymbols(osym) {} -}; - -// -// Fst HEADER CLASS -// -// This is the recommended Fst file header representation. -// - -class FstHeader { - public: - enum { - HAS_ISYMBOLS = 1, // Has input symbol table - HAS_OSYMBOLS = 2 // Has output symbol table - } Flags; - - FstHeader() : version_(0), flags_(0), properties_(0), start_(-1), - numstates_(0), numarcs_(0) {} - const string &FstType() const { return fsttype_; } - const string &ArcType() const { return arctype_; } - int32 Version() const { return version_; } - int32 GetFlags() const { return flags_; } - uint64 Properties() const { return properties_; } - int64 Start() const { return start_; } - int64 NumStates() const { return numstates_; } - int64 NumArcs() const { return numarcs_; } - - void SetFstType(const string& type) { fsttype_ = type; } - void SetArcType(const string& type) { arctype_ = type; } - void SetVersion(int32 version) { version_ = version; } - void SetFlags(int32 flags) { flags_ = flags; } - void SetProperties(uint64 properties) { properties_ = properties; } - void SetStart(int64 start) { start_ = start; } - void SetNumStates(int64 numstates) { numstates_ = numstates; } - void SetNumArcs(int64 numarcs) { numarcs_ = numarcs; } - - bool Read(istream &strm, const string &source); - bool Write(ostream &strm, const string &source) const; - - private: - string fsttype_; // E.g. "vector" - string arctype_; // E.g. "standard" - int32 version_; // Type version # - int32 flags_; // File format bits - uint64 properties_; // FST property bits - int64 start_; // Start state - int64 numstates_; // # of states - int64 numarcs_; // # of arcs -}; - -// -// Fst INTERFACE CLASS DEFINITION -// - -// A generic FST, templated on the arc definition, with -// common-demoninator methods (use StateIterator and ArcIterator to -// iterate over its states and arcs). -template <class A> -class Fst { - public: - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - virtual ~Fst() {} - - virtual StateId Start() const = 0; // Initial state - - virtual Weight Final(StateId) const = 0; // State's final weight - - virtual size_t NumArcs(StateId) const = 0; // State's arc count - - virtual size_t NumInputEpsilons(StateId) - const = 0; // State's input epsilon count - - virtual size_t NumOutputEpsilons(StateId) - const = 0; // State's output epsilon count - - // If test=false, return stored properties bits for mask (some poss. unknown) - // If test=true, return property bits for mask (computing o.w. unknown) - virtual uint64 Properties(uint64 mask, bool test) - const = 0; // Property bits - - virtual const string& Type() const = 0; // Fst type name - - // Get a copy of this Fst. - virtual Fst<A> *Copy() const = 0; - // Read an Fst from an input stream; returns NULL on error - - static Fst<A> *Read(istream &strm, const FstReadOptions &opts) { - FstReadOptions ropts(opts); - FstHeader hdr; - if (ropts.header) - hdr = *opts.header; - else { - if (!hdr.Read(strm, opts.source)) - return 0; - ropts.header = &hdr; - } - FstRegister<A> *registr = FstRegister<A>::GetRegister(); - const typename FstRegister<A>::Reader reader = - registr->GetReader(hdr.FstType()); - if (!reader) { - LOG(ERROR) << "Fst::Read: Unknown FST type \"" << hdr.FstType() - << "\" (arc type = \"" << A::Type() - << "\"): " << ropts.source; - return 0; - } - return reader(strm, ropts); - }; - - // Read an Fst from a file; return NULL on error - static Fst<A> *Read(const string &filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "Fst::Read: Can't open file: " << filename; - return 0; - } - return Read(strm, FstReadOptions(filename)); - } - - // Write an Fst to an output stream; return false on error - virtual bool Write(ostream &strm, const FstWriteOptions &opts) const { - LOG(ERROR) << "Fst::Write: No write method for " << Type() << " Fst type"; - return false; - } - - // Write an Fst to a file; return false on error - virtual bool Write(const string &filename) const { - LOG(ERROR) << "Fst::Write: No write method for " - << Type() << " Fst type: " - << (filename.empty() ? "standard output" : filename); - return false; - } - - // Return input label symbol table; return NULL if not specified - virtual const SymbolTable* InputSymbols() const = 0; - - // Return output label symbol table; return NULL if not specified - virtual const SymbolTable* OutputSymbols() const = 0; - - // For generic state iterator construction; not normally called - // directly by users. - virtual void InitStateIterator(StateIteratorData<A> *) const = 0; - - // For generic arc iterator construction; not normally called - // directly by users. - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *) const = 0; -}; - - -// -// STATE and ARC ITERATOR DEFINITIONS -// - -// State iterator interface templated on the Arc definition; used -// for StateIterator specializations returned by InitStateIterator. -template <class A> -class StateIteratorBase { - public: - typedef A Arc; - typedef typename A::StateId StateId; - - virtual ~StateIteratorBase() {} - virtual bool Done() const = 0; // End of iterator? - virtual StateId Value() const = 0; // Current state (when !Done) - virtual void Next() = 0; // Advance to next state (when !Done) - virtual void Reset() = 0; // Return to initial condition -}; - - -// StateIterator initialization data -template <class A> struct StateIteratorData { - StateIteratorBase<A> *base; // Specialized iterator if non-zero - typename A::StateId nstates; // O.w. total # of states -}; - - -// Generic state iterator, templated on the FST definition -// - a wrapper around pointer to specific one. -// Here is a typical use: \code -// for (StateIterator<StdFst> siter(fst); -// !siter.Done(); -// siter.Next()) { -// StateId s = siter.Value(); -// ... -// } \endcode -template <class F> -class StateIterator { - public: - typedef typename F::Arc Arc; - typedef typename Arc::StateId StateId; - - explicit StateIterator(const F &fst) : s_(0) { - fst.InitStateIterator(&data_); - } - - ~StateIterator() { if (data_.base) delete data_.base; } - - bool Done() const { - return data_.base ? data_.base->Done() : s_ >= data_.nstates; - } - - StateId Value() const { return data_.base ? data_.base->Value() : s_; } - - void Next() { - if (data_.base) - data_.base->Next(); - else - ++s_; - } - - void Reset() { - if (data_.base) - data_.base->Reset(); - else - s_ = 0; - } - - private: - StateIteratorData<Arc> data_; - StateId s_; - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - - -// Arc iterator interface, templated on the Arc definition; used -// for Arc iterator specializations that are returned by InitArcIterator. -template <class A> -class ArcIteratorBase { - public: - typedef A Arc; - typedef typename A::StateId StateId; - - virtual ~ArcIteratorBase() {} - virtual bool Done() const = 0; // End of iterator? - virtual const A& Value() const = 0; // Current state (when !Done) - virtual void Next() = 0; // Advance to next arc (when !Done) - virtual void Reset() = 0; // Return to initial condition - virtual void Seek(size_t a) = 0; // Random arc access by position -}; - - -// ArcIterator initialization data -template <class A> struct ArcIteratorData { - ArcIteratorBase<A> *base; // Specialized iterator if non-zero - const A *arcs; // O.w. arcs pointer - size_t narcs; // ... and arc count - int *ref_count; // ... and reference count if non-zero -}; - - -// Generic arc iterator, templated on the FST definition -// - a wrapper around pointer to specific one. -// Here is a typical use: \code -// for (ArcIterator<StdFst> aiter(fst, s)); -// !aiter.Done(); -// aiter.Next()) { -// StdArc &arc = aiter.Value(); -// ... -// } \endcode -template <class F> -class ArcIterator { - public: - typedef typename F::Arc Arc; - typedef typename Arc::StateId StateId; - - ArcIterator(const F &fst, StateId s) : i_(0) { - fst.InitArcIterator(s, &data_); - } - - ~ArcIterator() { - if (data_.base) - delete data_.base; - else if (data_.ref_count) - --(*data_.ref_count); - } - - bool Done() const { - return data_.base ? data_.base->Done() : i_ >= data_.narcs; - } - - const Arc& Value() const { - return data_.base ? data_.base->Value() : data_.arcs[i_]; - } - - void Next() { - if (data_.base) - data_.base->Next(); - else - ++i_; - } - - void Reset() { - if (data_.base) - data_.base->Reset(); - else - i_ = 0; - } - - void Seek(size_t a) { - if (data_.base) - data_.base->Seek(a); - else - i_ = a; - } - - private: - ArcIteratorData<Arc> data_; - size_t i_; - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - - -// A useful alias when using StdArc. -typedef Fst<StdArc> StdFst; - - -// -// CONSTANT DEFINITIONS -// - -const int kNoStateId = -1; // Not a valid state ID -const int kNoLabel = -1; // Not a valid label -const int kPhiLabel = -2; // Failure transition label -const int kRhoLabel = -3; // Matches o.w. unmatched labels (lib. internal) -const int kSigmaLabel = -4; // Matches all labels in alphabet. - - -// -// Fst IMPLEMENTATION BASE -// -// This is the recommended Fst implementation base class. It will -// handle reference counts, property bits, type information and symbols. -// - -template <class A> class FstImpl { - public: - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - FstImpl() - : properties_(0), type_("null"), isymbols_(0), osymbols_(0), - ref_count_(1) {} - - FstImpl(const FstImpl<A> &impl) - : properties_(impl.properties_), type_(impl.type_), - isymbols_(impl.isymbols_ ? new SymbolTable(impl.isymbols_) : 0), - osymbols_(impl.osymbols_ ? new SymbolTable(impl.osymbols_) : 0), - ref_count_(1) {} - - ~FstImpl() { - delete isymbols_; - delete osymbols_; - } - - const string& Type() const { return type_; } - - void SetType(const string &type) { type_ = type; } - - uint64 Properties() const { return properties_; } - - uint64 Properties(uint64 mask) const { return properties_ & mask; } - - void SetProperties(uint64 props) { properties_ = props; } - - void SetProperties(uint64 props, uint64 mask) { - properties_ &= ~mask; - properties_ |= props & mask; - } - - const SymbolTable* InputSymbols() const { return isymbols_; } - - const SymbolTable* OutputSymbols() const { return osymbols_; } - - SymbolTable* InputSymbols() { return isymbols_; } - - SymbolTable* OutputSymbols() { return osymbols_; } - - void SetInputSymbols(const SymbolTable* isyms) { - if (isymbols_) delete isymbols_; - isymbols_ = isyms ? isyms->Copy() : 0; - } - - void SetOutputSymbols(const SymbolTable* osyms) { - if (osymbols_) delete osymbols_; - osymbols_ = osyms ? osyms->Copy() : 0; - } - - int RefCount() const { return ref_count_; } - - int IncrRefCount() { return ++ref_count_; } - - int DecrRefCount() { return --ref_count_; } - - // Read-in header and symbols, initialize Fst, and return the header. - // If opts.header is non-null, skip read-in and use the option value. - // If opts.[io]symbols is non-null, read-in but use the option value. - bool ReadHeaderAndSymbols(istream &strm, const FstReadOptions& opts, - int min_version, FstHeader *hdr) { - if (opts.header) - *hdr = *opts.header; - else if (!hdr->Read(strm, opts.source)) - return false; - if (hdr->FstType() != type_) { - LOG(ERROR) << "FstImpl::ReadHeaderAndSymbols: Fst not of type \"" - << type_ << "\": " << opts.source; - return false; - } - if (hdr->ArcType() != A::Type()) { - LOG(ERROR) << "FstImpl::ReadHeaderAndSymbols: Arc not of type \"" - << A::Type() - << "\": " << opts.source; - return false; - } - if (hdr->Version() < min_version) { - LOG(ERROR) << "FstImpl::ReadHeaderAndSymbols: Obsolete " - << type_ << " Fst version: " << opts.source; - return false; - } - properties_ = hdr->Properties(); - if (hdr->GetFlags() & FstHeader::HAS_ISYMBOLS) - isymbols_ = SymbolTable::Read(strm, opts.source); - if (hdr->GetFlags() & FstHeader::HAS_OSYMBOLS) - osymbols_ =SymbolTable::Read(strm, opts.source); - - if (opts.isymbols) { - delete isymbols_; - isymbols_ = opts.isymbols->Copy(); - } - if (opts.osymbols) { - delete osymbols_; - osymbols_ = opts.osymbols->Copy(); - } - return true; - } - - // Write-out header and symbols. - // If a opts.header is false, skip writing header. - // If opts.[io]symbols is false, skip writing those symbols. - void WriteHeaderAndSymbols(ostream &strm, const FstWriteOptions& opts, - int version, FstHeader *hdr) const { - if (opts.write_header) { - hdr->SetFstType(type_); - hdr->SetArcType(A::Type()); - hdr->SetVersion(version); - hdr->SetProperties(properties_); - int32 file_flags = 0; - if (isymbols_ && opts.write_isymbols) - file_flags |= FstHeader::HAS_ISYMBOLS; - if (osymbols_ && opts.write_osymbols) - file_flags |= FstHeader::HAS_OSYMBOLS; - hdr->SetFlags(file_flags); - hdr->Write(strm, opts.source); - } - if (isymbols_ && opts.write_isymbols) isymbols_->Write(strm); - if (osymbols_ && opts.write_osymbols) osymbols_->Write(strm); - } - - protected: - uint64 properties_; // Property bits - - private: - string type_; // Unique name of Fst class - SymbolTable *isymbols_; // Ilabel symbol table - SymbolTable *osymbols_; // Olabel symbol table - int ref_count_; // Reference count - - void operator=(const FstImpl<A> &impl); // disallow -}; - -} // namespace fst; - -#endif // FST_LIB_FST_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/fstlib.h b/tools/thirdparty/OpenFst/fst/lib/fstlib.h deleted file mode 100644 index 0f20ce5..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/fstlib.h +++ /dev/null @@ -1,87 +0,0 @@ -// fstlib.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \page OpenFst - Weighted Finite State Transducers -// This is a library for constructing, combining, optimizing, and -// searching "weighted finite-state transducers" (FSTs). Weighted -// finite-state transducers are automata where each transition has an -// input label, an output label, and a weight. The more familiar -// finite-state acceptor is represented as a transducer with each -// transition's input and output the same. Finite-state acceptors -// are used to represent sets of strings (specifically, "regular" or -// "rational sets"); finite-state transducers are used to represent -// binary relations between pairs of strings (specifically, "rational -// transductions"). The weights can be used to represent the cost of -// taking a particular transition. -// -// In this library, the transducers are templated on the Arc -// (transition) definition, which allows changing the label, weight, -// and state ID sets. Labels and state IDs are restricted to signed -// integral types but the weight can be an arbitrary type whose -// members satisfy certain algebraic ("semiring") properties. -// -// For more information, see the OpenFst web site: -// http://www.openfst.org. - -// \file -// This convenience file includes all other OpenFst header files. - -#ifndef FST_LIB_FSTLIB_H__ -#define FST_LIB_FSTLIB_H__ - -// Abstract FST classes -#include "fst/lib/fst.h" -#include "fst/lib/expanded-fst.h" -#include "fst/lib/mutable-fst.h" - -// Concrete FST classes -#include "fst/lib/vector-fst.h" -#include "fst/lib/const-fst.h" - -// FST algorithms and delayed FST classes -#include "fst/lib/arcsort.h" -#include "fst/lib/closure.h" -#include "fst/lib/compose.h" -#include "fst/lib/concat.h" -#include "fst/lib/connect.h" -#include "fst/lib/determinize.h" -#include "fst/lib/difference.h" -#include "fst/lib/encode.h" -#include "fst/lib/epsnormalize.h" -#include "fst/lib/equal.h" -#include "fst/lib/equivalent.h" -#include "fst/lib/factor-weight.h" -#include "fst/lib/intersect.h" -#include "fst/lib/invert.h" -#include "fst/lib/map.h" -#include "fst/lib/minimize.h" -#include "fst/lib/project.h" -#include "fst/lib/prune.h" -#include "fst/lib/push.h" -#include "fst/lib/randgen.h" -#include "fst/lib/relabel.h" -#include "fst/lib/replace.h" -#include "fst/lib/reverse.h" -#include "fst/lib/reweight.h" -#include "fst/lib/rmepsilon.h" -#include "fst/lib/rmfinalepsilon.h" -#include "fst/lib/shortest-distance.h" -#include "fst/lib/shortest-path.h" -#include "fst/lib/synchronize.h" -#include "fst/lib/topsort.h" -#include "fst/lib/union.h" -#include "fst/lib/verify.h" - -#endif // FST_LIB_FSTLIB_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/heap.h b/tools/thirdparty/OpenFst/fst/lib/heap.h deleted file mode 100644 index 5219c19..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/heap.h +++ /dev/null @@ -1,194 +0,0 @@ -// heap.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Implementation of a heap as in STL, but allows tracking positions -// in heap using a key. The key can be used to do an in place update of -// values in the heap. - -#ifndef FST_LIB_HEAP_H__ -#define FST_LIB_HEAP_H__ - -#include <functional> -#include <vector> - -namespace fst { - -// -// \class Heap -// \brief A templated heap implementation that support in place update -// of values. -// -// The templated heap implementation is a little different from the -// STL priority_queue and the *_heap operations in STL. The heap -// supports indexing of values in the heap via an associated key. -// -// Each value is internally associated with a key which is returned -// to the calling functions on heap insert. This key can be used -// to later update the specific value in the heap. -// -// \param T the element type of the hash, can be POD, Data or Ptr to Data -// \param Compare Comparison class for determing min-heapness of max-heapness -// -static const int kNoKey = -1; -template <class T, class Compare> -class Heap { - public: - - // initialize with a specific comparator - Heap(Compare comp) : comp_(comp), size_(0) { } - - // Create a heap with initial size of internal arrays of 1024 - Heap() : size_(0) { } - - ~Heap() { } - - // insert a value into the heap - int Insert(const T& val) { - if (size_ < (int)A_.size()) { - A_[size_] = val; - pos_[key_[size_]] = size_; - } else { - A_.push_back(val); - pos_.push_back(size_); - key_.push_back(size_); - } - - ++size_; - return Insert(val, size_ - 1); - } - - // update a value at position given by the key. The pos array is first - // indexed by the key. The position gives the position in the heap array. - // Once we have the position we can then use the standard heap operations - // to calculate the parent and child positions. - void Update(int key, const T& val) { - int i = pos_[key]; - if (comp_(val, A_[Parent(i)])) { - Insert(val, i); - } else { - A_[i] = val; - Heapify(i); - } - } - - // pop the (best/worst) from the heap - T Pop() { - T max = A_[0]; - - Swap(0, size_-1); - size_--; - Heapify(0); - return(max); - } - - // return value of best in heap - T Top() const { - return A_[0]; - } - - // check if the heap is empty - bool Empty() const { - return(size_ == 0); - } - - void Clear() { - size_ = 0; - } - - - // - // The following protected routines is used in a supportive role - // for managing the heap and keeping the heap properties. - // - private: - // compute left child of parent - int Left(int i) { - return 2*(i+1)-1; // 0 -> 1, 1 -> 3 - } - - // compute right child of parent - int Right(int i) { - return 2*(i+1); // 0 -> 2, 1 -> 4 - } - - // given a child compute parent - int Parent(int i) { - return (i-1)/2; // 1 -> 0, 2 -> 0, 3 -> 1, 4-> 1 - } - - // swap a child, parent. Use to move element up/down tree - // note a little tricky here. When we swap we need to swap - // the value - // the associated keys - // the position of the value in the heap - void Swap(int j, int k) { - int tkey = key_[j]; - pos_[key_[j] = key_[k]] = j; - pos_[key_[k] = tkey] = k; - - T val = A_[j]; - A_[j] = A_[k]; - A_[k] = val; - } - - - // heapify subtree rooted at index i. - void Heapify(int i) { - int l = Left(i); - int r = Right(i); - int largest; - - if (l < size_ && comp_(A_[l], A_[i]) ) - largest = l; - else - largest = i; - - if (r < size_ && comp_(A_[r], A_[largest]) ) - largest = r; - - if (largest != i) { - Swap(i, largest); - Heapify(largest); - } - } - - - // insert(update) element at subtree rooted at index i - int Insert(const T& val, int i) { - int p; - while (i > 0 && !comp_(A_[p = Parent(i)], val)) { - Swap(i, p); - i = p; - } - - return key_[i]; - } - - - private: - Compare comp_; - - vector<int> pos_; - vector<int> key_; - vector<T> A_; - int size_; - - // DISALLOW_EVIL_CONSTRUCTORS(Heap); -}; - -} - -#endif // FST_LIB_HEAP_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/intersect.h b/tools/thirdparty/OpenFst/fst/lib/intersect.h deleted file mode 100644 index 796509b..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/intersect.h +++ /dev/null @@ -1,130 +0,0 @@ -// intersect.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Class to compute the intersection of two FSAs - -#ifndef FST_LIB_INTERSECT_H__ -#define FST_LIB_INTERSECT_H__ - -#include "fst/lib/compose.h" - -namespace fst { - -template <uint64 T = 0> -struct IntersectFstOptions : public ComposeFstOptions<T> { }; - - -// Computes the intersection (Hadamard product) of two FSAs. This -// version is a delayed Fst. Only strings that are in both automata -// are retained in the result. -// -// The two arguments must be acceptors. One of the arguments must be -// label-sorted. -// -// Complexity: same as ComposeFst. -// -// Caveats: same as ComposeFst. -template <class A> -class IntersectFst : public ComposeFst<A> { - public: - using ComposeFst<A>::Impl; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - IntersectFst(const Fst<A> &fst1, const Fst<A> &fst2) - : ComposeFst<A>(fst1, fst2) { - if (!fst1.Properties(kAcceptor, true) || !fst2.Properties(kAcceptor, true)) - LOG(FATAL) << "IntersectFst: arguments not both acceptors"; - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - Impl()->SetProperties(IntersectProperties(props1, props2), - kCopyProperties); - } - - template <uint64 T> - IntersectFst(const Fst<A> &fst1, const Fst<A> &fst2, - const IntersectFstOptions<T> &opts) - : ComposeFst<A>(fst1, fst2, ComposeFstOptions<T>(opts)) { - if (!fst1.Properties(kAcceptor, true) || !fst2.Properties(kAcceptor, true)) - LOG(FATAL) << "IntersectFst: arguments not both acceptors"; - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - Impl()->SetProperties(IntersectProperties(props1, props2), - kCopyProperties); - } - - IntersectFst(const IntersectFst<A> &fst) : ComposeFst<A>(fst) {} - - virtual IntersectFst<A> *Copy() const { - return new IntersectFst<A>(*this); - } -}; - - -// Specialization for IntersectFst. -template <class A> -class StateIterator< IntersectFst<A> > - : public StateIterator< ComposeFst<A> > { - public: - explicit StateIterator(const IntersectFst<A> &fst) - : StateIterator< ComposeFst<A> >(fst) {} -}; - - -// Specialization for IntersectFst. -template <class A> -class ArcIterator< IntersectFst<A> > - : public ArcIterator< ComposeFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const IntersectFst<A> &fst, StateId s) - : ArcIterator< ComposeFst<A> >(fst, s) {} -}; - -// Useful alias when using StdArc. -typedef IntersectFst<StdArc> StdIntersectFst; - - -typedef ComposeOptions IntersectOptions; - - -// Computes the intersection (Hadamard product) of two FSAs. This -// version writes the intersection to an output MurableFst. Only -// strings that are in both automata are retained in the result. -// -// The two arguments must be acceptors. One of the arguments must be -// label-sorted. -// -// Complexity: same as Compose. -// -// Caveats: same as Compose. -template<class Arc> -void Intersect(const Fst<Arc> &ifst1, const Fst<Arc> &ifst2, - MutableFst<Arc> *ofst, - const IntersectOptions &opts = IntersectOptions()) { - IntersectFstOptions<> nopts; - nopts.gc_limit = 0; // Cache only the last state for fastest copy. - *ofst = IntersectFst<Arc>(ifst1, ifst2, nopts); - if (opts.connect) - Connect(ofst); -} - -} // namespace fst - -#endif // FST_LIB_INTERSECT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/invert.h b/tools/thirdparty/OpenFst/fst/lib/invert.h deleted file mode 100644 index 9aefaea..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/invert.h +++ /dev/null @@ -1,100 +0,0 @@ -// invert.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to invert an Fst. - -#ifndef FST_LIB_INVERT_H__ -#define FST_LIB_INVERT_H__ - -#include "fst/lib/map.h" -#include "fst/lib/mutable-fst.h" - -namespace fst { - -// Mapper to implement inversion of an arc. -template <class A> struct InvertMapper { - InvertMapper() {} - - A operator()(const A &arc) { - return A(arc.olabel, arc.ilabel, arc.weight, arc.nextstate); - } - - uint64 Properties(uint64 props) { return InvertProperties(props); } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } -}; - - -// Inverts the transduction corresponding to an FST by exchanging the -// FST's input and output labels. This version modifies its input. -// -// Complexity: -// - Time: O(V + E) -// - Space: O(1) -// where V = # of states and E = # of arcs. -template<class Arc> inline -void Invert(MutableFst<Arc> *fst) { Map(fst, InvertMapper<Arc>()); } - - -// Inverts the transduction corresponding to an FST by exchanging the -// FST's input and output labels. This version is a delayed Fst. -// -// Complexity: -// - Time: O(v + e) -// - Space: O(1) -// where v = # of states visited, e = # of arcs visited. Constant -// time and to visit an input state or arc is assumed and exclusive -// of caching. -template <class A> -class InvertFst : public MapFst<A, A, InvertMapper<A> > { - public: - typedef A Arc; - typedef InvertMapper<A> C; - - explicit InvertFst(const Fst<A> &fst) : MapFst<A, A, C>(fst, C()) {} - - InvertFst(const InvertFst<A> &fst) : MapFst<A, A, C>(fst) {} - - virtual InvertFst<A> *Copy() const { return new InvertFst(*this); } -}; - - -// Specialization for InvertFst. -template <class A> -class StateIterator< InvertFst<A> > - : public StateIterator< MapFst<A, A, InvertMapper<A> > > { - public: - explicit StateIterator(const InvertFst<A> &fst) - : StateIterator< MapFst<A, A, InvertMapper<A> > >(fst) {} -}; - - -// Specialization for InvertFst. -template <class A> -class ArcIterator< InvertFst<A> > - : public ArcIterator< MapFst<A, A, InvertMapper<A> > > { - public: - ArcIterator(const InvertFst<A> &fst, typename A::StateId s) - : ArcIterator< MapFst<A, A, InvertMapper<A> > >(fst, s) {} -}; - - -// Useful alias when using StdArc. -typedef InvertFst<StdArc> StdInvertFst; - -} // namespace fst - -#endif // FST_LIB_INVERT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/map.h b/tools/thirdparty/OpenFst/fst/lib/map.h deleted file mode 100644 index 722aef3..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/map.h +++ /dev/null @@ -1,967 +0,0 @@ -// map.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Class to map over/transform arcs e.g., change semirings or -// implement project/invert. - -#ifndef FST_LIB_MAP_H__ -#define FST_LIB_MAP_H__ - -#include "fst/lib/cache.h" -#include "fst/lib/mutable-fst.h" - -namespace fst { - -// This determines how final weights are mapped. -enum MapFinalAction { - - // A final weight is mapped into a final weight. An error - // is raised if this is not possible. - MAP_NO_SUPERFINAL, - - // A final weight is mapped to an arc to the superfinal state - // when the result cannot be represented as a final weight. - // The superfinal state will be added only if it is needed. - MAP_ALLOW_SUPERFINAL, - - // A final weight is mapped to an arc to the superfinal state - // unless the result can be represented as a final weight of weight - // Zero(). The superfinal state is always added (if the input is - // not the empty Fst). - MAP_REQUIRE_SUPERFINAL -}; - -// Mapper Interface - class determinies how arcs and final weights -// are mapped. -// -// class Mapper { -// public: -// // Maps an arc type A to arc type B. -// B operator()(const A &arc); -// // Specifies final action the mapper requires (see above). -// // The mapper will be passed final weights as arcs of the -// // form A(0, 0, weight, kNoStateId). -// MapFinalAction FinalAction() const; -// // This specifies the known properties of an Fst mapped by this -// // mapper. It takes as argument the input Fst's known properties. -// uint64 Properties(uint64 props) const; -// } -// -// The Map functions and classes below will use the FinalAction() -// method of the mapper to determine how to treat final weights, -// e.g. whether to add a superfinal state. They will use the Properties() -// method to set the result Fst properties. -// -// We include a various map versions below. One dimension of -// variation is whether the mapping mutates its input, writes to a -// new result Fst, or is an on-the-fly Fst. Another dimension is how -// we pass the mapper. We allow passing the mapper by pointer -// for cases that we need to change the state of the user's mapper. -// This is the case with the encode mapper, which is reused during -// decoding. We also include map versions that pass the mapper -// by value or const reference when this suffices. - - -// Maps an arc type A using a mapper function object C, passed -// by pointer. This version modifies its Fst input. -template<class A, class C> -void Map(MutableFst<A> *fst, C* mapper) { - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - if (fst->Start() == kNoStateId) - return; - - uint64 props = fst->Properties(kFstProperties, false); - - MapFinalAction final_action = mapper->FinalAction(); - StateId superfinal = kNoStateId; - if (final_action == MAP_REQUIRE_SUPERFINAL) { - superfinal = fst->AddState(); - fst->SetFinal(superfinal, Weight::One()); - } - - for (StateId s = 0; s < fst->NumStates(); ++s) { - for (MutableArcIterator< MutableFst<A> > aiter(fst, s); - !aiter.Done(); aiter.Next()) { - const A &arc = aiter.Value(); - aiter.SetValue((*mapper)(arc)); - } - - switch (final_action) { - case MAP_NO_SUPERFINAL: - default: { - A final_arc = (*mapper)(A(0, 0, fst->Final(s), kNoStateId)); - CHECK(final_arc.ilabel == 0 && final_arc.olabel == 0); - fst->SetFinal(s, final_arc.weight); - break; - } - case MAP_ALLOW_SUPERFINAL: { - if (s != superfinal) { - A final_arc = (*mapper)(A(0, 0, fst->Final(s), kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0) { - // Add a superfinal state if not already done. - if (superfinal == kNoStateId) { - superfinal = fst->AddState(); - fst->SetFinal(superfinal, Weight::One()); - } - final_arc.nextstate = superfinal; - fst->AddArc(s, final_arc); - fst->SetFinal(s, Weight::Zero()); - } else { - fst->SetFinal(s, final_arc.weight); - } - break; - } - } - case MAP_REQUIRE_SUPERFINAL: { - if (s != superfinal) { - A final_arc = (*mapper)(A(0, 0, fst->Final(s), kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0 || - final_arc.weight != Weight::Zero()) - fst->AddArc(s, A(final_arc.ilabel, final_arc.olabel, - final_arc.weight, superfinal)); - fst->SetFinal(s, Weight::Zero()); - } - break; - } - } - } - fst->SetProperties(mapper->Properties(props), kFstProperties); -} - -// Maps an arc type A using a mapper function object C, passed -// by value. This version modifies its Fst input. -template<class A, class C> -void Map(MutableFst<A> *fst, C mapper) { - Map(fst, &mapper); -} - - -// Maps an arc type A to an arc type B using mapper function -// object C, passed by pointer. This version writes the mapped -// input Fst to an output MutableFst. -template<class A, class B, class C> -void Map(const Fst<A> &ifst, MutableFst<B> *ofst, C* mapper) { - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - ofst->DeleteStates(); - ofst->SetInputSymbols(ifst.InputSymbols()); - ofst->SetOutputSymbols(ifst.OutputSymbols()); - - if (ifst.Start() == kNoStateId) - return; - - // Add all states. - for (StateIterator< Fst<A> > siter(ifst); !siter.Done(); siter.Next()) - ofst->AddState(); - - MapFinalAction final_action = mapper->FinalAction(); - StateId superfinal = kNoStateId; - if (final_action == MAP_REQUIRE_SUPERFINAL) { - superfinal = ofst->AddState(); - ofst->SetFinal(superfinal, B::Weight::One()); - } - for (StateIterator< Fst<A> > siter(ifst); !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - if (s == ifst.Start()) - ofst->SetStart(s); - - for (ArcIterator< Fst<A> > aiter(ifst, s); !aiter.Done(); aiter.Next()) - ofst->AddArc(s, (*mapper)(aiter.Value())); - - switch (final_action) { - case MAP_NO_SUPERFINAL: - default: { - B final_arc = (*mapper)(A(0, 0, ifst.Final(s), kNoStateId)); - CHECK(final_arc.ilabel == 0 && final_arc.olabel == 0); - ofst->SetFinal(s, final_arc.weight); - break; - } - case MAP_ALLOW_SUPERFINAL: { - B final_arc = (*mapper)(A(0, 0, ifst.Final(s), kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0) { - // Add a superfinal state if not already done. - if (superfinal == kNoStateId) { - superfinal = ofst->AddState(); - ofst->SetFinal(superfinal, B::Weight::One()); - } - final_arc.nextstate = superfinal; - ofst->AddArc(s, final_arc); - ofst->SetFinal(s, B::Weight::Zero()); - } else { - ofst->SetFinal(s, final_arc.weight); - } - break; - } - case MAP_REQUIRE_SUPERFINAL: { - B final_arc = (*mapper)(A(0, 0, ifst.Final(s), kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0 || - final_arc.weight != B::Weight::Zero()) - ofst->AddArc(s, B(final_arc.ilabel, final_arc.olabel, - final_arc.weight, superfinal)); - ofst->SetFinal(s, B::Weight::Zero()); - break; - } - } - } - uint64 iprops = ifst.Properties(kCopyProperties, false); - uint64 oprops = ofst->Properties(kFstProperties, false); - ofst->SetProperties(mapper->Properties(iprops) | oprops, kFstProperties); -} - -// Maps an arc type A to an arc type B using mapper function -// object C, passed by value. This version writes the mapped input -// Fst to an output MutableFst. -template<class A, class B, class C> -void Map(const Fst<A> &ifst, MutableFst<B> *ofst, C mapper) { - Map(ifst, ofst, &mapper); -} - - -struct MapFstOptions : public CacheOptions { - // MapFst default caching behaviour is to do no caching. Most - // mappers are cheap and therefore we save memory by not doing - // caching. - MapFstOptions() : CacheOptions(true, 0) {} - MapFstOptions(const CacheOptions& opts) : CacheOptions(opts) {} -}; - - -template <class A, class B, class C> class MapFst; - -// Implementation of delayed MapFst. -template <class A, class B, class C> -class MapFstImpl : public CacheImpl<B> { - public: - using FstImpl<B>::SetType; - using FstImpl<B>::SetProperties; - using FstImpl<B>::Properties; - using FstImpl<B>::SetInputSymbols; - using FstImpl<B>::SetOutputSymbols; - - using VectorFstBaseImpl<typename CacheImpl<B>::State>::NumStates; - - using CacheImpl<B>::HasArcs; - using CacheImpl<B>::HasFinal; - using CacheImpl<B>::HasStart; - - friend class StateIterator< MapFst<A, B, C> >; - - typedef B Arc; - typedef typename B::Weight Weight; - typedef typename B::StateId StateId; - - MapFstImpl(const Fst<A> &fst, const C &mapper, - const MapFstOptions& opts) - : CacheImpl<B>(opts), fst_(fst.Copy()), - mapper_(new C(mapper)), - own_mapper_(true), - superfinal_(kNoStateId), - nstates_(0) { - Init(); - } - - MapFstImpl(const Fst<A> &fst, C *mapper, - const MapFstOptions& opts) - : CacheImpl<B>(opts), fst_(fst.Copy()), - mapper_(mapper), - own_mapper_(false), - superfinal_(kNoStateId), - nstates_(0) { - Init(); - } - - - ~MapFstImpl() { - delete fst_; - if (own_mapper_) delete mapper_; - } - - StateId Start() { - if (!HasStart()) - SetStart(FindOState(fst_->Start())); - return CacheImpl<B>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - switch (final_action_) { - case MAP_NO_SUPERFINAL: - default: { - B final_arc = (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), - kNoStateId)); - CHECK(final_arc.ilabel == 0 && final_arc.olabel == 0); - SetFinal(s, final_arc.weight); - break; - } - case MAP_ALLOW_SUPERFINAL: { - if (s == superfinal_) { - SetFinal(s, Weight::One()); - } else { - B final_arc = (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), - kNoStateId)); - if (final_arc.ilabel == 0 && final_arc.olabel == 0) - SetFinal(s, final_arc.weight); - else - SetFinal(s, Weight::Zero()); - } - break; - } - case MAP_REQUIRE_SUPERFINAL: { - SetFinal(s, s == superfinal_ ? Weight::One() : Weight::Zero()); - break; - } - } - } - return CacheImpl<B>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<B>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<B>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<B>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<B> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<B>::InitArcIterator(s, data); - } - - void Expand(StateId s) { - // Add exiting arcs. - if (s == superfinal_) { SetArcs(s); return; } - - for (ArcIterator< Fst<A> > aiter(*fst_, FindIState(s)); - !aiter.Done(); aiter.Next()) { - A aarc(aiter.Value()); - aarc.nextstate = FindOState(aarc.nextstate); - const B& barc = (*mapper_)(aarc); - AddArc(s, barc); - } - - // Check for superfinal arcs. - if (!HasFinal(s) || Final(s) == Weight::Zero()) - switch (final_action_) { - case MAP_NO_SUPERFINAL: - default: - break; - case MAP_ALLOW_SUPERFINAL: { - B final_arc = (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), - kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0) { - if (superfinal_ == kNoStateId) - superfinal_ = nstates_++; - final_arc.nextstate = superfinal_; - AddArc(s, final_arc); - } - break; - } - case MAP_REQUIRE_SUPERFINAL: { - B final_arc = (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), - kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0 || - final_arc.weight != B::Weight::Zero()) - AddArc(s, B(final_arc.ilabel, final_arc.olabel, - final_arc.weight, superfinal_)); - break; - } - } - SetArcs(s); - } - - private: - void Init() { - SetType("map"); - SetInputSymbols(fst_->InputSymbols()); - SetOutputSymbols(fst_->OutputSymbols()); - if (fst_->Start() == kNoStateId) { - final_action_ = MAP_NO_SUPERFINAL; - SetProperties(kNullProperties); - } else { - final_action_ = mapper_->FinalAction(); - uint64 props = fst_->Properties(kCopyProperties, false); - SetProperties(mapper_->Properties(props)); - if (final_action_ == MAP_REQUIRE_SUPERFINAL) - superfinal_ = 0; - } - } - - // Maps from output state to input state. - StateId FindIState(StateId s) { - if (superfinal_ == kNoStateId || s < superfinal_) - return s; - else - return s - 1; - } - - // Maps from input state to output state. - StateId FindOState(StateId is) { - StateId os; - if (superfinal_ == kNoStateId || is < superfinal_) - os = is; - else - os = is + 1; - - if (os >= nstates_) - nstates_ = os + 1; - - return os; - } - - - const Fst<A> *fst_; - C* mapper_; - bool own_mapper_; - MapFinalAction final_action_; - - StateId superfinal_; - StateId nstates_; -}; - - -// Maps an arc type A to an arc type B using Mapper function object -// C. This version is a delayed Fst. -template <class A, class B, class C> -class MapFst : public Fst<B> { - public: - friend class ArcIterator< MapFst<A, B, C> >; - friend class StateIterator< MapFst<A, B, C> >; - friend class CacheArcIterator< MapFst<A, B, C> >; - - typedef B Arc; - typedef typename B::Weight Weight; - typedef typename B::StateId StateId; - typedef CacheState<B> State; - - MapFst(const Fst<A> &fst, const C &mapper, - const MapFstOptions& opts) - : impl_(new MapFstImpl<A, B, C>(fst, mapper, opts)) {} - - MapFst(const Fst<A> &fst, C* mapper, - const MapFstOptions& opts) - : impl_(new MapFstImpl<A, B, C>(fst, mapper, opts)) {} - - MapFst(const Fst<A> &fst, const C &mapper) - : impl_(new MapFstImpl<A, B, C>(fst, mapper, - MapFstOptions())) {} - - MapFst(const Fst<A> &fst, C* mapper) - : impl_(new MapFstImpl<A, B, C>(fst, mapper, - MapFstOptions())) {} - - MapFst(const MapFst<A, B, C> &fst) : Fst<B>(fst), impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~MapFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - StateId NumStates() const { return impl_->NumStates(); } - - size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual MapFst<A, B, C> *Copy() const { - return new MapFst<A, B, C>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<B> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<B> *data) const { - impl_->InitArcIterator(s, data); - } - - private: - MapFstImpl<A, B, C> *impl_; - - void operator=(const MapFst<A, B, C> &fst); // disallow -}; - - -// Specialization for MapFst. -template<class A, class B, class C> -class StateIterator< MapFst<A, B, C> > : public StateIteratorBase<B> { - public: - typedef typename B::StateId StateId; - - explicit StateIterator(const MapFst<A, B, C> &fst) - : impl_(fst.impl_), siter_(*impl_->fst_), s_(0), - superfinal_(impl_->final_action_ == MAP_REQUIRE_SUPERFINAL) - { CheckSuperfinal(); } - - bool Done() const { return siter_.Done() && !superfinal_; } - - StateId Value() const { return s_; } - - void Next() { - ++s_; - if (!siter_.Done()) { - siter_.Next(); - CheckSuperfinal(); - } - else if (superfinal_) - superfinal_ = false; - } - - void Reset() { - s_ = 0; - siter_.Reset(); - superfinal_ = impl_->final_action_ == MAP_REQUIRE_SUPERFINAL; - CheckSuperfinal(); - } - - private: - void CheckSuperfinal() { - if (impl_->final_action_ != MAP_ALLOW_SUPERFINAL || superfinal_) - return; - if (!siter_.Done()) { - B final_arc = (*impl_->mapper_)(A(0, 0, impl_->fst_->Final(s_), - kNoStateId)); - if (final_arc.ilabel != 0 || final_arc.olabel != 0) - superfinal_ = true; - } - } - - const MapFstImpl<A, B, C> *impl_; - StateIterator< Fst<A> > siter_; - StateId s_; - bool superfinal_; // true if there is a superfinal state and not done - - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - -// Specialization for MapFst. -template <class A, class B, class C> -class ArcIterator< MapFst<A, B, C> > - : public CacheArcIterator< MapFst<A, B, C> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const MapFst<A, B, C> &fst, StateId s) - : CacheArcIterator< MapFst<A, B, C> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -template <class A, class B, class C> inline -void MapFst<A, B, C>::InitStateIterator(StateIteratorData<B> *data) - const { - data->base = new StateIterator< MapFst<A, B, C> >(*this); -} - - -// -// Utility Mappers -// - -// Mapper that returns its input. -template <class A> -struct IdentityMapper { - typedef A FromArc; - typedef A ToArc; - - A operator()(const A &arc) const { return arc; } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { return props; } -}; - - -// Mapper that returns its input with final states redirected to -// a single super-final state. -template <class A> -struct SuperFinalMapper { - typedef A FromArc; - typedef A ToArc; - - A operator()(const A &arc) const { return arc; } - - MapFinalAction FinalAction() const { return MAP_REQUIRE_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kAddSuperFinalProperties; - } -}; - - -// Mapper from StdArc to LogArc. -struct StdToLogMapper { - typedef StdArc FromArc; - typedef LogArc ToArc; - - LogArc operator()(const StdArc &arc) const { - return LogArc(arc.ilabel, arc.olabel, arc.weight.Value(), arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { return props; } -}; - - -// Mapper from LogArc to StdArc. -struct LogToStdMapper { - typedef LogArc FromArc; - typedef StdArc ToArc; - - StdArc operator()(const LogArc &arc) const { - return StdArc(arc.ilabel, arc.olabel, arc.weight.Value(), arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { return props; } -}; - - -// Mapper from A to GallicArc<A>. -template <class A, StringType S = STRING_LEFT> -struct ToGallicMapper { - typedef A FromArc; - typedef GallicArc<A, S> ToArc; - - typedef StringWeight<typename A::Label, S> SW; - typedef typename A::Weight AW; - typedef typename GallicArc<A, S>::Weight GW; - - ToArc operator()(const A &arc) const { - // 'Super-final' arc. - if (arc.nextstate == kNoStateId && arc.weight != AW::Zero()) - return ToArc(0, 0, GW(SW::One(), arc.weight), kNoStateId); - // 'Super-non-final' arc. - else if (arc.nextstate == kNoStateId) - return ToArc(0, 0, GW(SW::Zero(), arc.weight), kNoStateId); - // Epsilon label. - else if (arc.olabel == 0) - return ToArc(arc.ilabel, arc.ilabel, - GW(SW::One(), arc.weight), arc.nextstate); - // Regular label. - else - return ToArc(arc.ilabel, arc.ilabel, - GW(SW(arc.olabel), arc.weight), arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return ProjectProperties(props, true) & kWeightInvariantProperties; - } -}; - - -// Mapper from GallicArc<A> to A. -template <class A, StringType S = STRING_LEFT> -struct FromGallicMapper { - typedef GallicArc<A, S> FromArc; - typedef A ToArc; - - typedef typename A::Label Label; - typedef StringWeight<Label, S> SW; - typedef typename A::Weight AW; - typedef typename GallicArc<A, S>::Weight GW; - - A operator()(const FromArc &arc) const { - // 'Super-non-final' arc. - if (arc.nextstate == kNoStateId && arc.weight == GW::Zero()) - return A(arc.ilabel, 0, AW::Zero(), kNoStateId); - - SW w1 = arc.weight.Value1(); - AW w2 = arc.weight.Value2(); - StringWeightIterator<Label, S> iter1(w1); - - Label l = w1.Size() == 1 ? iter1.Value() : 0; - - CHECK(l != kStringInfinity); - CHECK(l != kStringBad); - CHECK(arc.ilabel == arc.olabel); - CHECK(w1.Size() <= 1); - - return A(arc.ilabel, l, w2, arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_ALLOW_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kOLabelInvariantProperties & - kWeightInvariantProperties & kAddSuperFinalProperties; - } -}; - - -// Mapper from GallicArc<A> to A. -template <class A, StringType S = STRING_LEFT> -struct GallicToNewSymbolsMapper { - typedef GallicArc<A, S> FromArc; - typedef A ToArc; - - typedef typename A::StateId StateId; - typedef typename A::Label Label; - typedef StringWeight<Label, S> SW; - typedef typename A::Weight AW; - typedef typename GallicArc<A, S>::Weight GW; - - GallicToNewSymbolsMapper(MutableFst<ToArc> *fst) - : fst_(fst), lmax_(0), osymbols_(fst->OutputSymbols()), isymbols_(0) { - fst_->DeleteStates(); - state_ = fst_->AddState(); - fst_->SetStart(state_); - fst_->SetFinal(state_, AW::One()); - if (osymbols_) { - string name = osymbols_->Name() + "_from_gallic"; - isymbols_ = new SymbolTable(name); - isymbols_->AddSymbol(osymbols_->Find((int64) 0), 0); - } - fst_->SetInputSymbols(isymbols_); - } - - A operator()(const FromArc &arc) { - // 'Super-non-final' arc. - if (arc.nextstate == kNoStateId && arc.weight == GW::Zero()) - return A(arc.ilabel, 0, AW::Zero(), kNoStateId); - - SW w1 = arc.weight.Value1(); - AW w2 = arc.weight.Value2(); - Label l; - - if (w1.Size() == 0) { - l = 0; - } else { - typename Map::iterator miter = map_.find(w1); - if (miter != map_.end()) { - l = (*miter).second; - } else { - l = ++lmax_; - map_.insert(pair<const SW, Label>(w1, l)); - StringWeightIterator<Label, S> iter1(w1); - StateId n; - string s; - for(ssize_t i = 0, p = state_; - i < w1.Size(); - ++i, iter1.Next(), p = n) { - n = i == w1.Size() - 1 ? state_ : fst_->AddState(); - fst_->AddArc(p, ToArc(i ? 0 : l, iter1.Value(), AW::One(), n)); - if (isymbols_) { - if (i) s = s + "_"; - s = s + osymbols_->Find(iter1.Value()); - } - } - if (isymbols_) - isymbols_->AddSymbol(s, l); - } - } - - CHECK(l != kStringInfinity); - CHECK(l != kStringBad); - CHECK(arc.ilabel == arc.olabel); - - return A(arc.ilabel, l, w2, arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_ALLOW_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kOLabelInvariantProperties & - kWeightInvariantProperties & kAddSuperFinalProperties; - } - - private: - class StringKey { - public: - size_t operator()(const SW &x) const { - return x.Hash(); - } - }; - - typedef hash_map<SW, Label, StringKey> Map; - - MutableFst<ToArc> *fst_; - Map map_; - Label lmax_; - StateId state_; - SymbolTable *osymbols_, *isymbols_; -}; - - -// Mapper to add a constant to all weights. -template <class A> -struct PlusMapper { - typedef typename A::Weight Weight; - - explicit PlusMapper(Weight w) : weight_(w) {} - - A operator()(const A &arc) const { - if (arc.weight == Weight::Zero()) - return arc; - Weight w = Plus(arc.weight, weight_); - return A(arc.ilabel, arc.olabel, w, arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kWeightInvariantProperties; - } - - Weight weight_; -}; - - -// Mapper to (right) multiply a constant to all weights. -template <class A> -struct TimesMapper { - typedef typename A::Weight Weight; - - explicit TimesMapper(Weight w) : weight_(w) {} - - A operator()(const A &arc) const { - if (arc.weight == Weight::Zero()) - return arc; - Weight w = Times(arc.weight, weight_); - return A(arc.ilabel, arc.olabel, w, arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kWeightInvariantProperties; - } - - Weight weight_; -}; - - -// Mapper to map all non-Zero() weights to One(). -template <class A, class B = A> -struct RmWeightMapper { - typedef A FromArc; - typedef B ToArc; - typedef typename FromArc::Weight FromWeight; - typedef typename ToArc::Weight ToWeight; - - B operator()(const A &arc) const { - ToWeight w = arc.weight != FromWeight::Zero() ? - ToWeight::One() : ToWeight::Zero(); - return B(arc.ilabel, arc.olabel, w, arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kWeightInvariantProperties | kUnweighted; - } -}; - - -// Mapper to quantize all weights. -template <class A, class B = A> -struct QuantizeMapper { - typedef A FromArc; - typedef B ToArc; - typedef typename FromArc::Weight FromWeight; - typedef typename ToArc::Weight ToWeight; - - QuantizeMapper() : delta_(kDelta) {} - - explicit QuantizeMapper(float d) : delta_(d) {} - - B operator()(const A &arc) const { - ToWeight w = arc.weight.Quantize(delta_); - return B(arc.ilabel, arc.olabel, w, arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { - return props & kWeightInvariantProperties; - } - - float delta_; -}; - - -// Mapper from A to B under the assumption: -// B::Weight = A::Weight::ReverseWeight -// B::Label == A::Label -// B::StateId == A::StateId -// The weight is reversed, while the label and nextstate preserved -// in the mapping. -template <class A, class B> -struct ReverseWeightMapper { - typedef A FromArc; - typedef B ToArc; - - B operator()(const A &arc) const { - return B(arc.ilabel, arc.olabel, arc.weight.Reverse(), arc.nextstate); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - uint64 Properties(uint64 props) const { return props; } -}; - -} // namespace fst - -#endif // FST_LIB_MAP_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/minimize.h b/tools/thirdparty/OpenFst/fst/lib/minimize.h deleted file mode 100644 index 4a2aa38..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/minimize.h +++ /dev/null @@ -1,558 +0,0 @@ -// minimize.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file Functions and classes to minimize a finite state acceptor - -#ifndef FST_LIB_MINIMIZE_H__ -#define FST_LIB_MINIMIZE_H__ - -#include <algorithm> -#include <map> -#include <queue> - -#include "fst/lib/arcsort.h" -#include "fst/lib/arcsum.h" -#include "fst/lib/connect.h" -#include "fst/lib/dfs-visit.h" -#include "fst/lib/encode.h" -#include "fst/lib/factor-weight.h" -#include "fst/lib/fst.h" -#include "fst/lib/mutable-fst.h" -#include "fst/lib/partition.h" -#include "fst/lib/push.h" -#include "fst/lib/queue.h" -#include "fst/lib/reverse.h" - -namespace fst { - -// comparator for creating partition based on sorting on -// - states -// - final weight -// - out degree, -// - (input label, output label, weight, destination_block) -template <class A> -class StateComparator { - public: - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - static const int32 kCompareFinal = 0x0000001; - static const int32 kCompareOutDegree = 0x0000002; - static const int32 kCompareArcs = 0x0000004; - static const int32 kCompareAll = (kCompareFinal | - kCompareOutDegree | - kCompareArcs); - - StateComparator(const Fst<A>& fst, - const Partition<typename A::StateId>& partition, - int32 flags = kCompareAll) - : fst_(fst), partition_(partition), flags_(flags) {} - - // compare state x with state y based on sort criteria - bool operator()(const StateId x, const StateId y) const { - // check for final state equivalence - if (flags_ & kCompareFinal) { - const ssize_t xfinal = fst_.Final(x).Hash(); - const ssize_t yfinal = fst_.Final(y).Hash(); - if (xfinal < yfinal) return true; - else if (xfinal > yfinal) return false; - } - - if (flags_ & kCompareOutDegree) { - // check for # arcs - if (fst_.NumArcs(x) < fst_.NumArcs(y)) return true; - if (fst_.NumArcs(x) > fst_.NumArcs(y)) return false; - - if (flags_ & kCompareArcs) { - // # arcs are equal, check for arc match - for (ArcIterator<Fst<A> > aiter1(fst_, x), aiter2(fst_, y); - !aiter1.Done() && !aiter2.Done(); aiter1.Next(), aiter2.Next()) { - const A& arc1 = aiter1.Value(); - const A& arc2 = aiter2.Value(); - if (arc1.ilabel < arc2.ilabel) return true; - if (arc1.ilabel > arc2.ilabel) return false; - - if (partition_.class_id(arc1.nextstate) < - partition_.class_id(arc2.nextstate)) return true; - if (partition_.class_id(arc1.nextstate) > - partition_.class_id(arc2.nextstate)) return false; - } - } - } - - return false; - } - - private: - const Fst<A>& fst_; - const Partition<typename A::StateId>& partition_; - const int32 flags_; -}; - -// Computes equivalence classes for cyclic Fsts. For cyclic minimization -// we use the classic HopCroft minimization algorithm, which is of -// -// O(E)log(N), -// -// where E is the number of edges in the machine and N is number of states. -// -// The following paper describes the original algorithm -// An N Log N algorithm for minimizing states in a finite automaton -// by John HopCroft, January 1971 -// -template <class A, class Queue> -class CyclicMinimizer { - public: - typedef typename A::Label Label; - typedef typename A::StateId StateId; - typedef typename A::StateId ClassId; - typedef typename A::Weight Weight; - typedef ReverseArc<A> RevA; - - CyclicMinimizer(const ExpandedFst<A>& fst) { - Initialize(fst); - Compute(fst); - } - - ~CyclicMinimizer() { - delete aiter_queue_; - } - - const Partition<StateId>& partition() const { - return P_; - } - - // helper classes - private: - typedef ArcIterator<Fst<RevA> > ArcIter; - class ArcIterCompare { - public: - ArcIterCompare(const Partition<StateId>& partition) - : partition_(partition) {} - - ArcIterCompare(const ArcIterCompare& comp) - : partition_(comp.partition_) {} - - // compare two iterators based on there input labels, and proto state - // (partition class Ids) - bool operator()(const ArcIter* x, const ArcIter* y) const { - const RevA& xarc = x->Value(); - const RevA& yarc = y->Value(); - return (xarc.ilabel > yarc.ilabel); - } - - private: - const Partition<StateId>& partition_; - }; - - typedef priority_queue<ArcIter*, vector<ArcIter*>, ArcIterCompare> - ArcIterQueue; - - // helper methods - private: - // prepartitions the space into equivalence classes with - // same final weight - // same # arcs per state - // same outgoing arcs - void PrePartition(const Fst<A>& fst) { - VLOG(5) << "PrePartition"; - - typedef map<StateId, StateId, StateComparator<A> > EquivalenceMap; - StateComparator<A> comp(fst, P_, StateComparator<A>::kCompareFinal); - EquivalenceMap equiv_map(comp); - - StateIterator<Fst<A> > siter(fst); - StateId class_id = P_.AddClass(); - P_.Add(siter.Value(), class_id); - equiv_map[siter.Value()] = class_id; - L_.Enqueue(class_id); - for (siter.Next(); !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - typename EquivalenceMap::const_iterator it = equiv_map.find(s); - if (it == equiv_map.end()) { - class_id = P_.AddClass(); - P_.Add(s, class_id); - equiv_map[s] = class_id; - L_.Enqueue(class_id); - } else { - P_.Add(s, it->second); - equiv_map[s] = it->second; - } - } - - VLOG(5) << "Initial Partition: " << P_.num_classes(); - } - - // - Create inverse transition Tr_ = rev(fst) - // - loop over states in fst and split on final, creating two blocks - // in the partition corresponding to final, non-final - void Initialize(const Fst<A>& fst) { - // construct Tr - Reverse(fst, &Tr_); - ILabelCompare<RevA> ilabel_comp; - ArcSort(&Tr_, ilabel_comp); - - // initial split (F, S - F) - P_.Initialize(Tr_.NumStates() - 1); - - // prep partition - PrePartition(fst); - - // allocate arc iterator queue - ArcIterCompare comp(P_); - aiter_queue_ = new ArcIterQueue(comp); - } - - // partition all classes with destination C - void Split(ClassId C) { - // Prep priority queue. Open arc iterator for each state in C, and - // insert into priority queue. - for (PartitionIterator<StateId> siter(P_, C); - !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - if (Tr_.NumArcs(s + 1)) - aiter_queue_->push(new ArcIterator<Fst<RevA> >(Tr_, s + 1)); - } - - // Now pop arc iterator from queue, split entering equivalence class - // re-insert updated iterator into queue. - Label prev_label = -1; - while (!aiter_queue_->empty()) { - ArcIterator<Fst<RevA> >* aiter = aiter_queue_->top(); - aiter_queue_->pop(); - if (aiter->Done()) { - delete aiter; - continue; - } - - const RevA& arc = aiter->Value(); - StateId from_state = aiter->Value().nextstate - 1; - Label from_label = arc.ilabel; - if (prev_label != from_label) - P_.FinalizeSplit(&L_); - - StateId from_class = P_.class_id(from_state); - if (P_.class_size(from_class) > 1) - P_.SplitOn(from_state); - - prev_label = from_label; - aiter->Next(); - if (aiter->Done()) - delete aiter; - else - aiter_queue_->push(aiter); - } - P_.FinalizeSplit(&L_); - } - - // Main loop for hopcroft minimization. - void Compute(const Fst<A>& fst) { - // process active classes (FIFO, or FILO) - while (!L_.Empty()) { - ClassId C = L_.Head(); - L_.Dequeue(); - - // split on C, all labels in C - Split(C); - } - } - - // helper data - private: - // Partioning of states into equivalence classes - Partition<StateId> P_; - - // L = set of active classes to be processed in partition P - Queue L_; - - // reverse transition function - VectorFst<RevA> Tr_; - - // Priority queue of open arc iterators for all states in the 'splitter' - // equivalence class - ArcIterQueue* aiter_queue_; -}; - - -// Computes equivalence classes for acyclic Fsts. The implementation details -// for this algorithms is documented by the following paper. -// -// Minimization of acyclic deterministic automata in linear time -// Dominque Revuz -// -// Complexity O(|E|) -// -template <class A> -class AcyclicMinimizer { - public: - typedef typename A::Label Label; - typedef typename A::StateId StateId; - typedef typename A::StateId ClassId; - typedef typename A::Weight Weight; - - AcyclicMinimizer(const ExpandedFst<A>& fst) { - Initialize(fst); - Refine(fst); - } - - const Partition<StateId>& partition() { - return partition_; - } - - // helper classes - private: - // DFS visitor to compute the height (distance) to final state. - class HeightVisitor { - public: - HeightVisitor() : max_height_(0), num_states_(0) { } - - // invoked before dfs visit - void InitVisit(const Fst<A>& fst) {} - - // invoked when state is discovered (2nd arg is DFS tree root) - bool InitState(StateId s, StateId root) { - // extend height array and initialize height (distance) to 0 - for (size_t i = height_.size(); i <= (size_t)s; ++i) - height_.push_back(-1); - - if (s >= (StateId)num_states_) num_states_ = s + 1; - return true; - } - - // invoked when tree arc examined (to undiscoverted state) - bool TreeArc(StateId s, const A& arc) { - return true; - } - - // invoked when back arc examined (to unfinished state) - bool BackArc(StateId s, const A& arc) { - return true; - } - - // invoked when forward or cross arc examined (to finished state) - bool ForwardOrCrossArc(StateId s, const A& arc) { - if (height_[arc.nextstate] + 1 > height_[s]) - height_[s] = height_[arc.nextstate] + 1; - return true; - } - - // invoked when state finished (parent is kNoStateId for tree root) - void FinishState(StateId s, StateId parent, const A* parent_arc) { - if (height_[s] == -1) height_[s] = 0; - StateId h = height_[s] + 1; - if (parent >= 0) { - if (h > height_[parent]) height_[parent] = h; - if (h > (StateId)max_height_) max_height_ = h; - } - } - - // invoked after DFS visit - void FinishVisit() {} - - size_t max_height() const { return max_height_; } - - const vector<StateId>& height() const { return height_; } - - const size_t num_states() const { return num_states_; } - - private: - vector<StateId> height_; - size_t max_height_; - size_t num_states_; - }; - - // helper methods - private: - // cluster states according to height (distance to final state) - void Initialize(const Fst<A>& fst) { - // compute height (distance to final state) - HeightVisitor hvisitor; - DfsVisit(fst, &hvisitor); - - // create initial partition based on height - partition_.Initialize(hvisitor.num_states()); - partition_.AllocateClasses(hvisitor.max_height() + 1); - const vector<StateId>& hstates = hvisitor.height(); - for (size_t s = 0; s < hstates.size(); ++s) - partition_.Add(s, hstates[s]); - } - - // refine states based on arc sort (out degree, arc equivalence) - void Refine(const Fst<A>& fst) { - typedef map<StateId, StateId, StateComparator<A> > EquivalenceMap; - StateComparator<A> comp(fst, partition_); - - // start with tail (height = 0) - size_t height = partition_.num_classes(); - for (size_t h = 0; h < height; ++h) { - EquivalenceMap equiv_classes(comp); - - // sort states within equivalence class - PartitionIterator<StateId> siter(partition_, h); - equiv_classes[siter.Value()] = h; - for (siter.Next(); !siter.Done(); siter.Next()) { - const StateId s = siter.Value(); - typename EquivalenceMap::const_iterator it = equiv_classes.find(s); - if (it == equiv_classes.end()) - equiv_classes[s] = partition_.AddClass(); - else - equiv_classes[s] = it->second; - } - - // create refined partition - for (siter.Reset(); !siter.Done();) { - const StateId s = siter.Value(); - const StateId old_class = partition_.class_id(s); - const StateId new_class = equiv_classes[s]; - - // a move operation can invalidate the iterator, so - // we first update the iterator to the next element - // before we move the current element out of the list - siter.Next(); - if (old_class != new_class) - partition_.Move(s, new_class); - } - } - } - - private: - Partition<StateId> partition_; -}; - - -// Given a partition and a mutable fst, merge states of Fst inplace -// (i.e. destructively). Merging works by taking the first state in -// a class of the partition to be the representative state for the class. -// Each arc is then reconnected to this state. All states in the class -// are merged by adding there arcs to the representative state. -template <class A> -void MergeStates( - const Partition<typename A::StateId>& partition, MutableFst<A>* fst) { - typedef typename A::StateId StateId; - - vector<StateId> state_map(partition.num_classes()); - for (size_t i = 0; i < (size_t)partition.num_classes(); ++i) { - PartitionIterator<StateId> siter(partition, i); - state_map[i] = siter.Value(); // first state in partition; - } - - // relabel destination states - for (size_t c = 0; c < (size_t)partition.num_classes(); ++c) { - for (PartitionIterator<StateId> siter(partition, c); - !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - for (MutableArcIterator<MutableFst<A> > aiter(fst, s); - !aiter.Done(); aiter.Next()) { - A arc = aiter.Value(); - arc.nextstate = state_map[partition.class_id(arc.nextstate)]; - - if (s == state_map[c]) // first state just set destination - aiter.SetValue(arc); - else - fst->AddArc(state_map[c], arc); - } - } - } - fst->SetStart(state_map[partition.class_id(fst->Start())]); - - Connect(fst); -} - -template <class A> -void AcceptorMinimize(MutableFst<A>* fst) { - typedef typename A::StateId StateId; - if (!(fst->Properties(kAcceptor | kUnweighted, true))) - LOG(FATAL) << "Input Fst is not an unweighted acceptor"; - - // connect fst before minimization, handles disconnected states - Connect(fst); - if (fst->NumStates() == 0) return; - - if (fst->Properties(kAcyclic, true)) { - // Acyclic minimization (revuz) - VLOG(2) << "Acyclic Minimization"; - AcyclicMinimizer<A> minimizer(*fst); - MergeStates(minimizer.partition(), fst); - - } else { - // Cyclic minimizaton (hopcroft) - VLOG(2) << "Cyclic Minimization"; - CyclicMinimizer<A, LifoQueue<StateId> > minimizer(*fst); - MergeStates(minimizer.partition(), fst); - } - - // sort arcs before summing - ArcSort(fst, ILabelCompare<A>()); - - // sum in appropriate semiring - ArcSum(fst); -} - - -// In place minimization of unweighted, deterministic acceptors -// -// For acyclic automata we use an algorithm from Dominique Revuz that is -// linear in the number of arcs (edges) in the machine. -// Complexity = O(E) -// -// For cyclic automata we use the classical hopcroft minimization. -// Complexity = O(|E|log(|N|) -// -template <class A> -void Minimize(MutableFst<A>* fst, MutableFst<A>* sfst = 0) { - uint64 props = fst->Properties(kAcceptor | kIDeterministic| - kWeighted | kUnweighted, true); - if (!(props & kIDeterministic)) - LOG(FATAL) << "Input Fst is not deterministic"; - - if (!(props & kAcceptor)) { // weighted transducer - VectorFst< GallicArc<A, STRING_LEFT> > gfst; - Map(*fst, &gfst, ToGallicMapper<A, STRING_LEFT>()); - fst->DeleteStates(); - gfst.SetProperties(kAcceptor, kAcceptor); - Push(&gfst, REWEIGHT_TO_INITIAL); - Map(&gfst, QuantizeMapper< GallicArc<A, STRING_LEFT> >()); - EncodeMapper< GallicArc<A, STRING_LEFT> > - encoder(kEncodeLabels | kEncodeWeights, ENCODE); - Encode(&gfst, &encoder); - AcceptorMinimize(&gfst); - Decode(&gfst, encoder); - - if (sfst == 0) { - FactorWeightFst< GallicArc<A, STRING_LEFT>, - GallicFactor<typename A::Label, - typename A::Weight, STRING_LEFT> > fwfst(gfst); - Map(fwfst, fst, FromGallicMapper<A, STRING_LEFT>()); - } else { - sfst->SetOutputSymbols(fst->OutputSymbols()); - GallicToNewSymbolsMapper<A, STRING_LEFT> mapper(sfst); - Map(gfst, fst, mapper); - fst->SetOutputSymbols(sfst->InputSymbols()); - } - } else if (props & kWeighted) { // weighted acceptor - Push(fst, REWEIGHT_TO_INITIAL); - Map(fst, QuantizeMapper<A>()); - EncodeMapper<A> encoder(kEncodeLabels | kEncodeWeights, ENCODE); - Encode(fst, &encoder); - AcceptorMinimize(fst); - Decode(fst, encoder); - } else { // unweighted acceptor - AcceptorMinimize(fst); - } -} - -} // namespace fst - -#endif // FST_LIB_MINIMIZE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/mutable-fst.h b/tools/thirdparty/OpenFst/fst/lib/mutable-fst.h deleted file mode 100644 index df00620..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/mutable-fst.h +++ /dev/null @@ -1,167 +0,0 @@ -// mutable-fst.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Expanded FST augmented with mutators - interface class definition -// and mutable arc iterator interface. - -#ifndef FST_LIB_MUTABLE_FST_H__ -#define FST_LIB_MUTABLE_FST_H__ - -#include <vector> - -#include "fst/lib/expanded-fst.h" - -namespace fst { - -template <class A> class MutableArcIteratorData; - -// An expanded FST plus mutators (use MutableArcIterator to modify arcs). -template <class A> -class MutableFst : public ExpandedFst<A> { - public: - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - virtual MutableFst<A> &operator=(const Fst<A> &fst) = 0; - - virtual void SetStart(StateId) = 0; // Set the initial state - virtual void SetFinal(StateId, Weight) = 0; // Set a state's final weight - virtual void SetProperties(uint64 props, - uint64 mask) = 0; // Set property bits wrt mask - virtual StateId AddState() = 0; // Add a state, return its ID - virtual void AddArc(StateId, const A &arc) = 0; // Add an arc to state - - virtual void DeleteStates(const vector<StateId>&) = 0; // Delete some states - virtual void DeleteStates() = 0; // Delete all states - virtual void DeleteArcs(StateId, size_t n) = 0; // Delete some arcs at state - virtual void DeleteArcs(StateId) = 0; // Delete all arcs at state - - // Return input label symbol table; return NULL if not specified - virtual const SymbolTable* InputSymbols() const = 0; - // Return output label symbol table; return NULL if not specified - virtual const SymbolTable* OutputSymbols() const = 0; - - // Return input label symbol table; return NULL if not specified - virtual SymbolTable* InputSymbols() = 0; - // Return output label symbol table; return NULL if not specified - virtual SymbolTable* OutputSymbols() = 0; - - // Set input label symbol table; NULL signifies not unspecified - virtual void SetInputSymbols(const SymbolTable* isyms) = 0; - // Set output label symbol table; NULL signifies not unspecified - virtual void SetOutputSymbols(const SymbolTable* osyms) = 0; - - // Get a copy of this MutableFst. - virtual MutableFst<A> *Copy() const = 0; - // Read an MutableFst from an input stream; return NULL on error. - static MutableFst<A> *Read(istream &strm, const FstReadOptions &opts) { - FstReadOptions ropts(opts); - FstHeader hdr; - if (ropts.header) - hdr = *opts.header; - else { - if (!hdr.Read(strm, opts.source)) - return 0; - ropts.header = &hdr; - } - if (!(hdr.Properties() & kMutable)) { - LOG(ERROR) << "MutableFst::Read: Not an MutableFst: " << ropts.source; - return 0; - } - FstRegister<A> *registr = FstRegister<A>::GetRegister(); - const typename FstRegister<A>::Reader reader = - registr->GetReader(hdr.FstType()); - if (!reader) { - LOG(ERROR) << "MutableFst::Read: Unknown FST type \"" << hdr.FstType() - << "\" (arc type = \"" << A::Type() - << "\"): " << ropts.source; - return 0; - } - Fst<A> *fst = reader(strm, ropts); - if (!fst) return 0; - return down_cast<MutableFst<A> *>(fst); - } - // Read an MutableFst from a file; returns NULL on error. - static MutableFst<A> *Read(const string &filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "MutableFst::Read: Can't open file: " << filename; - return 0; - } - return Read(strm, FstReadOptions(filename)); - } - - // For generic mutuble arc iterator construction; not normally called - // directly by users. - virtual void InitMutableArcIterator(StateId s, - MutableArcIteratorData<A> *) = 0; -}; - -// Mutable arc iterator interface, templated on the Arc definition. -template <class A> -class MutableArcIteratorBase : public ArcIteratorBase<A> { - public: - typedef A Arc; - virtual void SetValue(const A &arc) = 0; // Set current arc's contents -}; - -template <class A> -struct MutableArcIteratorData { - MutableArcIteratorBase<A> *base; // Specific iterator -}; - -// Generic mutable arc iterator, templated on the FST definition -// - a wrapper around pointer to specific one. -// Here is a typical use: \code -// for (MutableArcIterator<StdFst> aiter(&fst, s)); -// !aiter.Done(); -// aiter.Next()) { -// StdArc arc = aiter.Value(); -// arc.ilabel = 7; -// aiter.SetValue(arc); -// ... -// } \endcode -// This version requires function calls. -template <class F> -class MutableArcIterator : public MutableArcIteratorBase<typename F::Arc> { - public: - typedef typename F::Arc Arc; - typedef typename Arc::StateId StateId; - - MutableArcIterator(F *fst, StateId s) { - fst->InitMutableArcIterator(s, &data_); - } - ~MutableArcIterator() { delete data_.base; } - - bool Done() const { return data_.base->Done(); } - const Arc& Value() const { return data_.base->Value(); } - void Next() { data_.base->Next(); } - void Reset() { data_.base->Reset(); } - void Seek(size_t a) { data_.base->Seek(a); } - void SetValue(const Arc &a) { data_.base->SetValue(a); } - - private: - MutableArcIteratorData<Arc> data_; - DISALLOW_EVIL_CONSTRUCTORS(MutableArcIterator); -}; - -// A useful alias when using StdArc. -typedef MutableFst<StdArc> StdMutableFst; - -} // namespace fst; - -#endif // FST_LIB_MUTABLE_FST_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/partition.h b/tools/thirdparty/OpenFst/fst/lib/partition.h deleted file mode 100644 index 1a3f5cd..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/partition.h +++ /dev/null @@ -1,285 +0,0 @@ -// partition.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file Functions and classes to create a partition of states -// - -#ifndef FST_LIB_PARTITION_H__ -#define FST_LIB_PARTITION_H__ - -#include <algorithm> -#include <vector> - -#include "fst/lib/queue.h" - - -namespace fst { - -template <typename T> class PartitionIterator; - -// \class Partition -// \brief Defines a partitioning of states. Typically used to represent -// equivalence classes for Fst operations like minimization. -// -template <typename T> -class Partition { - friend class PartitionIterator<T>; - - struct Element { - Element() : value(0), next(0), prev(0) {} - Element(T v) : value(v), next(0), prev(0) {} - - T value; - Element* next; - Element* prev; - }; - - public: - Partition() {} - - Partition(T num_states) { - Initialize(num_states); - } - - ~Partition() { - for (size_t i = 0; i < elements_.size(); ++i) - delete elements_[i]; - } - - // Create an empty partition for num_states. At initialization time - // all elements are not assigned to a class (i.e class_index = -1). - // Initialize just creates num_states of elements. All element - // operations are then done by simply disconnecting the element from - // it current class and placing it at the head of the next class. - void Initialize(size_t num_states) { - for (size_t i = 0; i < elements_.size(); ++i) - delete elements_[i]; - elements_.clear(); - classes_.clear(); - class_index_.clear(); - - elements_.resize(num_states); - class_index_.resize(num_states, -1); - class_size_.reserve(num_states); - for (size_t i = 0; i < num_states; ++i) - elements_[i] = new Element(i); - num_states_ = num_states; - } - - // Add a class, resize classes_ and class_size_ resource by 1. - size_t AddClass() { - size_t num_classes = classes_.size(); - classes_.resize(num_classes + 1, 0); - class_size_.resize(num_classes + 1, 0); - class_split_.resize(num_classes + 1, 0); - split_size_.resize(num_classes + 1, 0); - return num_classes; - } - - void AllocateClasses(T num_classes) { - size_t n = classes_.size() + num_classes; - classes_.resize(n, 0); - class_size_.resize(n, 0); - class_split_.resize(n, 0); - split_size_.resize(n, 0); - } - - // Add element_id to class_id. The Add method is used to initialize - // partition. Once elements have been added to a class, you need to - // use the Move() method move an element from once class to another. - void Add(T element_id, T class_id) { - Element* element = elements_[element_id]; - - if (classes_[class_id]) - classes_[class_id]->prev = element; - element->next = classes_[class_id]; - element->prev = 0; - classes_[class_id] = element; - - class_index_[element_id] = class_id; - class_size_[class_id]++; - } - - // Move and element_id to class_id. Disconnects (removes) element - // from it current class and - void Move(T element_id, T class_id) { - T old_class_id = class_index_[element_id]; - - Element* element = elements_[element_id]; - if (element->next) element->next->prev = element->prev; - if (element->prev) element->prev->next = element->next; - else classes_[old_class_id] = element->next; - - Add(element_id, class_id); - class_size_[old_class_id]--; - } - - // split class on the element_id - void SplitOn(T element_id) { - T class_id = class_index_[element_id]; - if (class_size_[class_id] == 1) return; - - // first time class is split - if (split_size_[class_id] == 0) - visited_classes_.push_back(class_id); - - // increment size of split (set of element at head of chain) - split_size_[class_id]++; - - // update split point - if (class_split_[class_id] == 0) - class_split_[class_id] = classes_[class_id]; - if (class_split_[class_id] == elements_[element_id]) - class_split_[class_id] = elements_[element_id]->next; - - // move to head of chain in same class - Move(element_id, class_id); - } - - // Finalize class_id, split if required, and update class_splits, - // class indices of the newly created class. Returns the new_class id - // or -1 if no new class was created. - T SplitRefine(T class_id) { - // only split if necessary - if (class_size_[class_id] == split_size_[class_id]) { - class_split_[class_id] = 0; - split_size_[class_id] = 0; - return -1; - } else { - - T new_class = AddClass(); - size_t remainder = class_size_[class_id] - split_size_[class_id]; - if (remainder < (size_t)split_size_[class_id]) { // add smaller - Element* split_el = class_split_[class_id]; - classes_[new_class] = split_el; - class_size_[class_id] = split_size_[class_id]; - class_size_[new_class] = remainder; - split_el->prev->next = 0; - split_el->prev = 0; - } else { - Element* split_el = class_split_[class_id]; - classes_[new_class] = classes_[class_id]; - class_size_[class_id] = remainder; - class_size_[new_class] = split_size_[class_id]; - split_el->prev->next = 0; - split_el->prev = 0; - classes_[class_id] = split_el; - } - - // update class index for element in new class - for (Element* el = classes_[new_class]; el; el = el->next) - class_index_[el->value] = new_class; - - class_split_[class_id] = 0; - split_size_[class_id] = 0; - - return new_class; - } - } - - // Once all states have been processed for a particular class C, we - // can finalize the split. FinalizeSplit() will update each block in the - // partition, create new once and update the queue of active classes - // that require further refinement. - template <class Queue> - void FinalizeSplit(Queue* L) { - for (size_t i = 0; i < visited_classes_.size(); ++i) { - T new_class = SplitRefine(visited_classes_[i]); - if (new_class != -1 && L) - L->Enqueue(new_class); - } - visited_classes_.clear(); - } - - - const T class_id(T element_id) const { - return class_index_[element_id]; - } - - const vector<T>& class_sizes() const { - return class_size_; - } - - const size_t class_size(T class_id) const { - return class_size_[class_id]; - } - - const T num_classes() const { - return classes_.size(); - } - - - private: - int num_states_; - - // container of all elements (owner of ptrs) - vector<Element*> elements_; - - // linked list of elements belonging to class - vector<Element*> classes_; - - // pointer to split point for each class - vector<Element*> class_split_; - - // class index of element - vector<T> class_index_; - - // class sizes - vector<T> class_size_; - - // size of split for each class - vector<T> split_size_; - - // set of visited classes to be used in split refine - vector<T> visited_classes_; -}; - - -// iterate over members of a class in a partition -template <typename T> -class PartitionIterator { - typedef typename Partition<T>::Element Element; - public: - PartitionIterator(const Partition<T>& partition, T class_id) - : p_(partition), - element_(p_.classes_[class_id]), - class_id_(class_id) {} - - bool Done() { - return (element_ == 0); - } - - const T Value() { - return (element_->value); - } - - void Next() { - element_ = element_->next; - } - - void Reset() { - element_ = p_.classes_[class_id_]; - } - - private: - const Partition<T>& p_; - - const Element* element_; - - T class_id_; -}; -} - -#endif // FST_LIB_PARTITION_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/product-weight.h b/tools/thirdparty/OpenFst/fst/lib/product-weight.h deleted file mode 100644 index 9fa3a96..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/product-weight.h +++ /dev/null @@ -1,194 +0,0 @@ -// product-weight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Product weight set and associated semiring operation definitions. - -#ifndef FST_LIB_PRODUCT_WEIGHT_H__ -#define FST_LIB_PRODUCT_WEIGHT_H__ - -#include "fst/lib/weight.h" - -DECLARE_string(fst_product_separator); - -namespace fst { - -// Product semiring: W1 * W2 -template<class W1, class W2> -class ProductWeight { - public: - typedef ProductWeight<typename W1::ReverseWeight, typename W2::ReverseWeight> - ReverseWeight; - - ProductWeight() {} - - ProductWeight(W1 w1, W2 w2) : value1_(w1), value2_(w2) {} - - static const ProductWeight<W1, W2> &Zero() { - static const ProductWeight<W1, W2> zero(W1::Zero(), W2::Zero()); - return zero; - } - - static const ProductWeight<W1, W2> &One() { - static const ProductWeight<W1, W2> one(W1::One(), W2::One()); - return one; - } - - static const string &Type() { - static const string type = W1::Type() + "_X_" + W2::Type(); - return type; - } - - istream &Read(istream &strm) { - value1_.Read(strm); - return value2_.Read(strm); - } - - ostream &Write(ostream &strm) const { - value1_.Write(strm); - return value2_.Write(strm); - } - - ProductWeight<W1, W2> &operator=(const ProductWeight<W1, W2> &w) { - value1_ = w.Value1(); - value2_ = w.Value2(); - return *this; - } - - bool Member() const { return value1_.Member() && value2_.Member(); } - - ssize_t Hash() const { - ssize_t h1 = value1_.Hash(); - ssize_t h2 = value2_.Hash(); - int lshift = 5; - int rshift = sizeof(ssize_t) - 5; - return h1 << lshift ^ h1 >> rshift ^ h2; - } - - ProductWeight<W1, W2> Quantize(float delta = kDelta) const { - return ProductWeight<W1, W2>(value1_.Quantize(), value2_.Quantize()); - } - - ReverseWeight Reverse() const { - return ReverseWeight(value1_.Reverse(), value2_.Reverse()); - } - - static uint64 Properties() { - uint64 props1 = W1::Properties(); - uint64 props2 = W2::Properties(); - return props1 & props2 & (kLeftSemiring | kRightSemiring | - kCommutative | kIdempotent); - } - - W1 Value1() const { return value1_; } - - W2 Value2() const { return value2_; } - - private: - W1 value1_; - W2 value2_; -}; - -template <class W1, class W2> -inline bool operator==(const ProductWeight<W1, W2> &w, - const ProductWeight<W1, W2> &v) { - return w.Value1() == v.Value1() && w.Value2() == v.Value2(); -} - -template <class W1, class W2> -inline bool operator!=(const ProductWeight<W1, W2> &w1, - const ProductWeight<W1, W2> &w2) { - return w1.Value1() != w2.Value1() || w1.Value2() != w2.Value2(); -} - - -template <class W1, class W2> -inline bool ApproxEqual(const ProductWeight<W1, W2> &w1, - const ProductWeight<W1, W2> &w2, - float delta = kDelta) { - return w1 == w2; -} - -template <class W1, class W2> -inline ostream &operator<<(ostream &strm, const ProductWeight<W1, W2> &w) { - CHECK(FLAGS_fst_product_separator.size() == 1); - char separator = FLAGS_fst_product_separator[0]; - return strm << w.Value1() << separator << w.Value2(); -} - -template <class W1, class W2> -inline istream &operator>>(istream &strm, ProductWeight<W1, W2> &w) { - CHECK(FLAGS_fst_product_separator.size() == 1); - char separator = FLAGS_fst_product_separator[0]; - int c; - - // read any initial whitespapce - while (true) { - c = strm.get(); - if (c == EOF || c == separator) { - strm.clear(std::ios::badbit); - return strm; - } - if (!isspace(c)) - break; - } - - // read first element - string s1; - do { - s1 += c; - c = strm.get(); - if (c == EOF || isspace(c)) { - strm.clear(std::ios::badbit); - return strm; - } - } while (c != separator); - istringstream strm1(s1); - W1 w1 = W1::Zero(); - strm1 >> w1; - - // read second element - W2 w2 = W2::Zero(); - strm >> w2; - - w = ProductWeight<W1, W2>(w1, w2); - return strm; -} - -template <class W1, class W2> -inline ProductWeight<W1, W2> Plus(const ProductWeight<W1, W2> &w, - const ProductWeight<W1, W2> &v) { - return ProductWeight<W1, W2>(Plus(w.Value1(), v.Value1()), - Plus(w.Value2(), v.Value2())); -} - -template <class W1, class W2> -inline ProductWeight<W1, W2> Times(const ProductWeight<W1, W2> &w, - const ProductWeight<W1, W2> &v) { - return ProductWeight<W1, W2>(Times(w.Value1(), v.Value1()), - Times(w.Value2(), v.Value2())); -} - -template <class W1, class W2> -inline ProductWeight<W1, W2> Divide(const ProductWeight<W1, W2> &w, - const ProductWeight<W1, W2> &v, - DivideType typ = DIVIDE_ANY) { - return ProductWeight<W1, W2>(Divide(w.Value1(), v.Value1(), typ), - Divide(w.Value2(), v.Value2(), typ)); -} - -} // namespace fst; - -#endif // FST_LIB_PRODUCT_WEIGHT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/project.h b/tools/thirdparty/OpenFst/fst/lib/project.h deleted file mode 100644 index 8fbc30b..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/project.h +++ /dev/null @@ -1,118 +0,0 @@ -// project.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to project an Fst on to its domain or range. - -#ifndef FST_LIB_PROJECT_H__ -#define FST_LIB_PROJECT_H__ - -#include "fst/lib/map.h" -#include "fst/lib/mutable-fst.h" - -namespace fst { - -// This specifies whether to project on input or output. -enum ProjectType { PROJECT_INPUT = 1, PROJECT_OUTPUT = 2 }; - - -// Mapper to implement projection per arc. -template <class A> class ProjectMapper { - public: - explicit ProjectMapper(ProjectType project_type) - : project_type_(project_type) {} - - A operator()(const A &arc) { - typename A::Label label = project_type_ == PROJECT_INPUT - ? arc.ilabel : arc.olabel; - return A(label, label, arc.weight, arc.nextstate); - } - - uint64 Properties(uint64 props) { - return ProjectProperties(props, project_type_ == PROJECT_INPUT); - } - - MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; } - - private: - ProjectType project_type_; -}; - - -// Projects an FST onto its domain or range by either copying each arcs' -// input label to the output label or vice versa. This version modifies -// its input. -// -// Complexity: -// - Time: O(V + E) -// - Space: O(1) -// where V = # of states and E = # of arcs. -template<class Arc> inline -void Project(MutableFst<Arc> *fst, ProjectType project_type) { - Map(fst, ProjectMapper<Arc>(project_type)); -} - - -// Projects an FST onto its domain or range by either copying each arc's -// input label to the output label or vice versa. This version is a delayed -// Fst. -// -// Complexity: -// - Time: O(v + e) -// - Space: O(1) -// where v = # of states visited, e = # of arcs visited. Constant -// time and to visit an input state or arc is assumed and exclusive -// of caching. -template <class A> -class ProjectFst : public MapFst<A, A, ProjectMapper<A> > { - public: - typedef A Arc; - typedef ProjectMapper<A> C; - - ProjectFst(const Fst<A> &fst, ProjectType project_type) - : MapFst<A, A, C>(fst, C(project_type)) {} - - ProjectFst(const ProjectFst<A> &fst) : MapFst<A, A, C>(fst) {} - - virtual ProjectFst<A> *Copy() const { return new ProjectFst(*this); } -}; - - -// Specialization for ProjectFst. -template <class A> -class StateIterator< ProjectFst<A> > - : public StateIterator< MapFst<A, A, ProjectMapper<A> > > { - public: - explicit StateIterator(const ProjectFst<A> &fst) - : StateIterator< MapFst<A, A, ProjectMapper<A> > >(fst) {} -}; - - -// Specialization for ProjectFst. -template <class A> -class ArcIterator< ProjectFst<A> > - : public ArcIterator< MapFst<A, A, ProjectMapper<A> > > { - public: - ArcIterator(const ProjectFst<A> &fst, typename A::StateId s) - : ArcIterator< MapFst<A, A, ProjectMapper<A> > >(fst, s) {} -}; - - -// Useful alias when using StdArc. -typedef ProjectFst<StdArc> StdProjectFst; - -} // namespace fst - -#endif // FST_LIB_PROJECT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/properties.cpp b/tools/thirdparty/OpenFst/fst/lib/properties.cpp deleted file mode 100644 index 2c66172..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/properties.cpp +++ /dev/null @@ -1,340 +0,0 @@ -// properties.cc -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions for updating property bits for various FST operations and -// string names of the properties. - -#include <vector> - -#include "fst/lib/properties.h" - -namespace fst { - -// These functions determine the properties associated with the FST -// result of various finite-state operations. The property arguments -// correspond to the operation's FST arguments. The properties -// returned assume the operation modifies its first argument. -// Bitwise-and this result with kCopyProperties for the case when a -// new (possibly delayed) FST is instead constructed. - -// Properties for a concatenatively-closed FST. -uint64 ClosureProperties(uint64 inprops, bool star, bool delayed) { - uint64 outprops = (kAcceptor | kUnweighted | kAccessible) & inprops; - if (!delayed) - outprops |= (kExpanded | kMutable | kCoAccessible | - kNotTopSorted | kNotString) & inprops; - if (!delayed || inprops & kAccessible) - outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic | - kNotILabelSorted | kNotOLabelSorted | kWeighted | - kNotAccessible | kNotCoAccessible) & inprops; - return outprops; -} - -// Properties for a complemented FST. -uint64 ComplementProperties(uint64 inprops) { - uint64 outprops = kAcceptor | kUnweighted | kNoEpsilons | - kNoIEpsilons | kNoOEpsilons | - kIDeterministic | kODeterministic | kAccessible; - outprops |= (kILabelSorted | kOLabelSorted | kInitialCyclic) & inprops; - if (inprops & kAccessible) - outprops |= kNotILabelSorted | kNotOLabelSorted | kCyclic; - return outprops; -} - -// Properties for a composed FST. -uint64 ComposeProperties(uint64 inprops1, uint64 inprops2) { - uint64 outprops = kAccessible; - outprops |= (kAcceptor | kNoIEpsilons | kAcyclic | kInitialAcyclic) & - inprops1 & inprops2; - if ((kNoIEpsilons & inprops1 & inprops2)) { - outprops |= kIDeterministic & inprops1 & inprops2; - } - return outprops; -} - -// Properties for a concatenated FST. -uint64 ConcatProperties(uint64 inprops1, uint64 inprops2, bool delayed) { - uint64 outprops = - (kAcceptor | kUnweighted | kAcyclic) & inprops1 & inprops2; - - bool empty1 = delayed; // Can fst1 be the empty machine? - bool empty2 = delayed; // Can fst2 be the empty machine? - - if (!delayed) { - outprops |= (kExpanded | kMutable | kNotTopSorted | kNotString) & inprops1; - outprops |= (kNotTopSorted | kNotString) & inprops2; - } - if (!empty1) - outprops |= (kInitialAcyclic | kInitialCyclic) & inprops1; - if (!delayed || inprops1 & kAccessible) - outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic | - kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted | - kNotOLabelSorted | kWeighted | kCyclic | - kNotAccessible | kNotCoAccessible) & inprops1; - if ((inprops1 & (kAccessible | kCoAccessible)) == - (kAccessible | kCoAccessible) && !empty1) { - outprops |= kAccessible & inprops2; - if (!empty2) - outprops |= kCoAccessible & inprops2; - if (!delayed || inprops2 & kAccessible) - outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic | - kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted | - kNotOLabelSorted | kWeighted | kCyclic | - kNotAccessible | kNotCoAccessible) & inprops2; - } - return outprops; -} - -// Properties for a determinized FST. -uint64 DeterminizeProperties(uint64 inprops) { - uint64 outprops = kIDeterministic | kAccessible; - outprops |= (kAcceptor | kNoEpsilons | kAcyclic | - kInitialAcyclic | kCoAccessible | kString) & inprops; - if (inprops & kAccessible) - outprops |= (kNotAcceptor | kEpsilons | kIEpsilons | kOEpsilons | - kCyclic) & inprops; - if (inprops & kAcceptor) - outprops |= (kNoIEpsilons | kNoOEpsilons | kAccessible) & inprops; - return outprops; -} - -// Properties for a differenced FST. -uint64 DifferenceProperties(uint64 inprops1, uint64 inprops2) { - return IntersectProperties(inprops1, ComplementProperties(inprops2)); -} - -// Properties for factored weight FST. -uint64 FactorWeightProperties(uint64 inprops) { - uint64 outprops = (kExpanded | kMutable | kAcceptor | - kAcyclic | kAccessible | kCoAccessible) & inprops; - if (inprops & kAccessible) - outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic | - kEpsilons | kIEpsilons | kOEpsilons | kCyclic | - kNotILabelSorted | kNotOLabelSorted) - & inprops; - return outprops; -} - -// Properties for an intersected FST. -uint64 IntersectProperties(uint64 inprops1, uint64 inprops2) { - uint64 outprops = kAcceptor | kAccessible; - - outprops |= (kNoEpsilons | kNoIEpsilons | kNoOEpsilons | kAcyclic | - kInitialAcyclic) & inprops1 & inprops2; - - if ((kNoIEpsilons & inprops1 & inprops2)) - outprops |= (kIDeterministic | kODeterministic) & inprops1 & inprops2; - return outprops; -} - -// Properties for an inverted FST. -uint64 InvertProperties(uint64 inprops) { - uint64 outprops = (kExpanded | kMutable | kAcceptor | kNotAcceptor | - kEpsilons | kNoEpsilons | kWeighted | kUnweighted | - kCyclic | kAcyclic | kInitialCyclic | kInitialAcyclic | - kTopSorted | kNotTopSorted | - kAccessible | kNotAccessible | - kCoAccessible | kNotCoAccessible | - kString | kNotString) & inprops; - if (kIDeterministic & inprops) - outprops |= kODeterministic; - if (kNonIDeterministic & inprops) - outprops |= kNonODeterministic; - if (kODeterministic & inprops) - outprops |= kIDeterministic; - if (kNonODeterministic & inprops) - outprops |= kNonIDeterministic; - - if (kIEpsilons & inprops) - outprops |= kOEpsilons; - if (kNoIEpsilons & inprops) - outprops |= kNoOEpsilons; - if (kOEpsilons & inprops) - outprops |= kIEpsilons; - if (kNoOEpsilons & inprops) - outprops |= kNoIEpsilons; - - if (kILabelSorted & inprops) - outprops |= kOLabelSorted; - if (kNotILabelSorted & inprops) - outprops |= kNotOLabelSorted; - if (kOLabelSorted & inprops) - outprops |= kILabelSorted; - if (kNotOLabelSorted & inprops) - outprops |= kNotILabelSorted; - return outprops; -} - -// Properties for a projected FST. -uint64 ProjectProperties(uint64 inprops, bool project_input) { - uint64 outprops = kAcceptor; - outprops |= (kExpanded | kMutable | kWeighted | kUnweighted | - kCyclic | kAcyclic | kInitialCyclic | kInitialAcyclic | - kTopSorted | kNotTopSorted | kAccessible | kNotAccessible | - kCoAccessible | kNotCoAccessible | - kString | kNotString) & inprops; - if (project_input) { - outprops |= (kIDeterministic | kNonIDeterministic | - kIEpsilons | kNoIEpsilons | - kILabelSorted | kNotILabelSorted) & inprops; - - if (kIDeterministic & inprops) - outprops |= kODeterministic; - if (kNonIDeterministic & inprops) - outprops |= kNonODeterministic; - - if (kIEpsilons & inprops) - outprops |= kOEpsilons | kEpsilons; - if (kNoIEpsilons & inprops) - outprops |= kNoOEpsilons | kNoEpsilons; - - if (kILabelSorted & inprops) - outprops |= kOLabelSorted; - if (kNotILabelSorted & inprops) - outprops |= kNotOLabelSorted; - } else { - outprops |= (kODeterministic | kNonODeterministic | - kOEpsilons | kNoOEpsilons | - kOLabelSorted | kNotOLabelSorted) & inprops; - - if (kODeterministic & inprops) - outprops |= kIDeterministic; - if (kNonODeterministic & inprops) - outprops |= kNonIDeterministic; - - if (kOEpsilons & inprops) - outprops |= kIEpsilons | kEpsilons; - if (kNoOEpsilons & inprops) - outprops |= kNoIEpsilons | kNoEpsilons; - - if (kOLabelSorted & inprops) - outprops |= kILabelSorted; - if (kNotOLabelSorted & inprops) - outprops |= kNotILabelSorted; - } - return outprops; -} - -// Properties for a replace FST. -uint64 ReplaceProperties(const vector<uint64>& inprops) { - return 0; -} - -// Properties for a relabeled FST. -uint64 RelabelProperties(uint64 inprops) { - uint64 outprops = (kExpanded | kMutable | - kWeighted | kUnweighted | - kCyclic | kAcyclic | - kInitialCyclic | kInitialAcyclic | - kTopSorted | kNotTopSorted | - kAccessible | kNotAccessible | - kCoAccessible | kNotCoAccessible | - kString | kNotString) & inprops; - return outprops; -} - -// Properties for a reversed FST. (the superinitial state limits this set) -uint64 ReverseProperties(uint64 inprops) { - uint64 outprops = - (kExpanded | kMutable | kAcceptor | kNotAcceptor | kEpsilons | - kIEpsilons | kOEpsilons | kWeighted | kUnweighted | - kCyclic | kAcyclic) & inprops; - return outprops; -} - -// Properties for re-weighted FST. -uint64 ReweightProperties(uint64 inprops) { - uint64 outprops = inprops & kWeightInvariantProperties; - outprops = outprops & ~kCoAccessible; - return outprops; -} - -// Properties for an epsilon-removed FST. -uint64 RmEpsilonProperties(uint64 inprops, bool delayed) { - uint64 outprops = kNoEpsilons; - outprops |= (kAcceptor | kAcyclic | kInitialAcyclic) & inprops; - if (inprops & kAcceptor) - outprops |= kNoIEpsilons | kNoOEpsilons; - if (!delayed) { - outprops |= kExpanded | kMutable; - outprops |= kTopSorted & inprops; - } - if (!delayed || inprops & kAccessible) - outprops |= kNotAcceptor & inprops; - return outprops; -} - -// Properties for a synchronized FST. -uint64 SynchronizeProperties(uint64 inprops) { - uint64 outprops = (kAcceptor | kAcyclic | kAccessible | kCoAccessible | - kUnweighted) & inprops; - if (inprops & kAccessible) - outprops |= (kCyclic | kNotCoAccessible | kWeighted) & inprops; - return outprops; -} - -// Properties for a unioned FST. -uint64 UnionProperties(uint64 inprops1, uint64 inprops2, bool delayed) { - uint64 outprops = (kAcceptor | kUnweighted | kAcyclic | kAccessible) - & inprops1 & inprops2; - bool empty1 = delayed; // Can fst1 be the empty machine? - bool empty2 = delayed; // Can fst2 be the empty machine? - if (!delayed) { - outprops |= (kExpanded | kMutable | kNotTopSorted | kNotString) & inprops1; - outprops |= (kNotTopSorted | kNotString) & inprops2; - } - if (!empty1 && !empty2) { - outprops |= kEpsilons | kIEpsilons | kOEpsilons; - outprops |= kCoAccessible & inprops1 & inprops2; - } - // Note kNotCoAccessible does not hold because of kInitialAcyclic opt. - if (!delayed || inprops1 & kAccessible) - outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic | - kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted | - kNotOLabelSorted | kWeighted | kCyclic | - kNotAccessible) & inprops1; - if (!delayed || inprops2 & kAccessible) - outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic | - kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted | - kNotOLabelSorted | kWeighted | kCyclic | - kNotAccessible | kNotCoAccessible) & inprops2; - return outprops; -} - -// Property string names (indexed by bit position). -const char *PropertyNames[] = { - // binary - "expanded", "mutable", "", "", "", "", "", "", - "", "", "", "", "", "", "", "", - // trinary - "acceptor", "not acceptor", - "input deterministic", "non input deterministic", - "output deterministic", "non output deterministic", - "input/output epsilons", "no input/output epsilons", - "input epsilons", "no input epsilons", - "output epsilons", "no output epsilons", - "input label sorted", "not input label sorted", - "output label sorted", "not output label sorted", - "weighted", "unweighted", - "cyclic", "acyclic", - "cyclic at initial state", "acyclic at initial state", - "top sorted", "not top sorted", - "accessible", "not accessible", - "coaccessible", "not coaccessible", - "string", "not string", -}; - -} diff --git a/tools/thirdparty/OpenFst/fst/lib/properties.h b/tools/thirdparty/OpenFst/fst/lib/properties.h deleted file mode 100644 index caa886b..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/properties.h +++ /dev/null @@ -1,320 +0,0 @@ -// properties.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// \file -// FST property bits. - -#ifndef FST_LIB_PROPERTIES_H__ -#define FST_LIB_PROPERTIES_H__ - -#include "fst/lib/compat.h" - -namespace fst { - -// The property bits here assert facts about an FST. If individual -// bits are added, then the composite properties below, the property -// functions and property names in properties.cc, and -// TestProperties() in test-properties.h should be updated. - -// -// BINARY PROPERTIES -// -// For each property below, there is a single bit. If it is set, -// the property is true. If it is not set, the property is false. -// - -// The Fst is an ExpandedFst -const uint64 kExpanded = 0x0000000000000001ULL; - -// The Fst is a MutableFst -const uint64 kMutable = 0x0000000000000002ULL; - -// -// TRINARY PROPERTIES -// -// For each of these properties below there is a pair of property bits -// - one positive and one negative. If the positive bit is set, the -// property is true. If the negative bit is set, the property is -// false. If neither is set, the property has unknown value. Both -// should never be simultaneously set. The individual positive and -// negative bit pairs should be adjacent with the positive bit -// at an odd and lower position. - -// ilabel == olabel for each arc -const uint64 kAcceptor = 0x0000000000010000ULL; -// ilabel != olabel for some arc -const uint64 kNotAcceptor = 0x0000000000020000ULL; - -// ilabels unique leaving each state -const uint64 kIDeterministic = 0x0000000000040000ULL; -// ilabels not unique leaving some state -const uint64 kNonIDeterministic = 0x0000000000080000ULL; - -// olabels unique leaving each state -const uint64 kODeterministic = 0x0000000000100000ULL; -// olabels not unique leaving some state -const uint64 kNonODeterministic = 0x0000000000200000ULL; - -// FST has input/output epsilons -const uint64 kEpsilons = 0x0000000000400000ULL; -// FST has no input/output epsilons -const uint64 kNoEpsilons = 0x0000000000800000ULL; - -// FST has input epsilons -const uint64 kIEpsilons = 0x0000000001000000ULL; -// FST has no input epsilons -const uint64 kNoIEpsilons = 0x0000000002000000ULL; - -// FST has output epsilons -const uint64 kOEpsilons = 0x0000000004000000ULL; -// FST has no output epsilons -const uint64 kNoOEpsilons = 0x0000000008000000ULL; - -// ilabels sorted wrt < for each state -const uint64 kILabelSorted = 0x0000000010000000ULL; -// ilabels not sorted wrt < for some state -const uint64 kNotILabelSorted = 0x0000000020000000ULL; - -// olabels sorted wrt < for each state -const uint64 kOLabelSorted = 0x0000000040000000ULL; -// olabels not sorted wrt < for some state -const uint64 kNotOLabelSorted = 0x0000000080000000ULL; - -// Non-trivial arc or final weights -const uint64 kWeighted = 0x0000000100000000ULL; -// Only trivial arc and final weights -const uint64 kUnweighted = 0x0000000200000000ULL; - -// FST has cycles -const uint64 kCyclic = 0x0000000400000000ULL; -// FST has no cycles -const uint64 kAcyclic = 0x0000000800000000ULL; - -// FST has cycles containing the initial state -const uint64 kInitialCyclic = 0x0000001000000000ULL; -// FST has no cycles containing the initial state -const uint64 kInitialAcyclic = 0x0000002000000000ULL; - -// FST is topologically sorted -const uint64 kTopSorted = 0x0000004000000000ULL; -// FST is not topologically sorted -const uint64 kNotTopSorted = 0x0000008000000000ULL; - -// All states reachable from the initial state -const uint64 kAccessible = 0x0000010000000000ULL; -// Not all states reachable from the initial state -const uint64 kNotAccessible = 0x0000020000000000ULL; - -// All states can reach a final state -const uint64 kCoAccessible = 0x0000040000000000ULL; -// Not all states can reach a final state -const uint64 kNotCoAccessible = 0x0000080000000000ULL; - -// If NumStates() > 0, then state 0 is initial, state NumStates()-1 is -// final, there is a transition from each non-final state i to -// state i+1, and there are no other transitions. -const uint64 kString = 0x0000100000000000ULL; - -// Not a string FST -const uint64 kNotString = 0x0000200000000000ULL; - -// -// COMPOSITE PROPERTIES -// - -// Properties of an empty machine - -const uint64 kNullProperties - = kAcceptor | kIDeterministic | kODeterministic | kNoEpsilons | - kNoIEpsilons | kNoOEpsilons | kILabelSorted | kOLabelSorted | - kUnweighted | kAcyclic | kInitialAcyclic | kTopSorted | - kAccessible | kCoAccessible | kString; - -// Properties that are preserved when an FST is copied -const uint64 kCopyProperties - = kAcceptor | kNotAcceptor | kIDeterministic | kNonIDeterministic | - kODeterministic | kNonODeterministic | kEpsilons | kNoEpsilons | - kIEpsilons | kNoIEpsilons | kOEpsilons | kNoOEpsilons | - kILabelSorted | kNotILabelSorted | kOLabelSorted | - kNotOLabelSorted | kWeighted | kUnweighted | kCyclic | kAcyclic | - kInitialCyclic | kInitialAcyclic | kTopSorted | kNotTopSorted | - kAccessible | kNotAccessible | kCoAccessible | kNotCoAccessible | - kString | kNotString; - -// Properties that are preserved when an FST start state is set -const uint64 kSetStartProperties - = kExpanded | kMutable | kAcceptor | kNotAcceptor | kIDeterministic | - kNonIDeterministic | kODeterministic | kNonODeterministic | - kEpsilons | kNoEpsilons | kIEpsilons | kNoIEpsilons | kOEpsilons | - kNoOEpsilons | kILabelSorted | kNotILabelSorted | kOLabelSorted | - kNotOLabelSorted | kWeighted | kUnweighted | kCyclic | kAcyclic | - kTopSorted | kNotTopSorted | kCoAccessible | kNotCoAccessible; - -// Properties that are preserved when an FST final weight is set -const uint64 kSetFinalProperties - = kExpanded | kMutable | kAcceptor | kNotAcceptor | kIDeterministic | - kNonIDeterministic | kODeterministic | kNonODeterministic | - kEpsilons | kNoEpsilons | kIEpsilons | kNoIEpsilons | kOEpsilons | - kNoOEpsilons | kILabelSorted | kNotILabelSorted | kOLabelSorted | - kNotOLabelSorted | kCyclic | kAcyclic | kInitialCyclic | - kInitialAcyclic | kTopSorted | kNotTopSorted | kAccessible | - kNotAccessible; - -// Properties that are preserved when an FST state is added -const uint64 kAddStateProperties - = kExpanded | kMutable | kAcceptor | kNotAcceptor | kIDeterministic | - kNonIDeterministic | kODeterministic | kNonODeterministic | - kEpsilons | kNoEpsilons | kIEpsilons | kNoIEpsilons | kOEpsilons | - kNoOEpsilons | kILabelSorted | kNotILabelSorted | kOLabelSorted | - kNotOLabelSorted | kWeighted | kUnweighted | kCyclic | kAcyclic | - kInitialCyclic | kInitialAcyclic | kTopSorted | kNotTopSorted | - kNotAccessible | kNotCoAccessible | kNotString; - -// Properties that are preserved when an FST arc is added -const uint64 kAddArcProperties = kExpanded | kMutable | kNotAcceptor | - kNonIDeterministic | kNonODeterministic | kEpsilons | kIEpsilons | - kOEpsilons | kNotILabelSorted | kNotOLabelSorted | kWeighted | - kCyclic | kInitialCyclic | kNotTopSorted | kAccessible | kCoAccessible; - -// Properties that are preserved when an FST arc is set -const uint64 kSetArcProperties = kExpanded | kMutable; - -// Properties that are preserved when FST states are deleted -const uint64 kDeleteStatesProperties - = kExpanded | kMutable | kAcceptor | kIDeterministic | - kODeterministic | kNoEpsilons | kNoIEpsilons | kNoOEpsilons | - kILabelSorted | kOLabelSorted | kUnweighted | kAcyclic | - kInitialAcyclic | kTopSorted; - -// Properties that are preserved when FST arcs are deleted -const uint64 kDeleteArcsProperties - = kExpanded | kMutable | kAcceptor | kIDeterministic | - kODeterministic | kNoEpsilons | kNoIEpsilons | kNoOEpsilons | - kILabelSorted | kOLabelSorted | kUnweighted | kAcyclic | - kInitialAcyclic | kTopSorted | kNotAccessible | kNotCoAccessible; - -// Properties that are preserved when an FST's states are reordered -const uint64 kStateSortProperties = kExpanded | kMutable | kAcceptor | - kNotAcceptor | kIDeterministic | kNonIDeterministic | - kODeterministic | kNonODeterministic | kEpsilons | kNoEpsilons | - kIEpsilons | kNoIEpsilons | kOEpsilons | kNoOEpsilons | - kILabelSorted | kNotILabelSorted | kOLabelSorted | kNotOLabelSorted - | kWeighted | kUnweighted | kCyclic | kAcyclic | kInitialCyclic | - kInitialAcyclic | kAccessible | kNotAccessible | kCoAccessible | - kNotCoAccessible; - -// Properties that are preserved when an FST's arcs are reordered -const uint64 kArcSortProperties = - kExpanded | kMutable | kAcceptor | kNotAcceptor | kIDeterministic | - kNonIDeterministic | kODeterministic | kNonODeterministic | - kEpsilons | kNoEpsilons | kIEpsilons | kNoIEpsilons | kOEpsilons | - kNoOEpsilons | kWeighted | kUnweighted | kCyclic | kAcyclic | - kInitialCyclic | kInitialAcyclic | kTopSorted | kNotTopSorted | - kAccessible | kNotAccessible | kCoAccessible | kNotCoAccessible | - kString | kNotString; - -// Properties that are preserved when an FST's input labels are changed. -const uint64 kILabelInvariantProperties = - kExpanded | kMutable | kODeterministic | kNonODeterministic | - kOEpsilons | kNoOEpsilons | kOLabelSorted | kNotOLabelSorted | - kWeighted | kUnweighted | kCyclic | kAcyclic | kInitialCyclic | - kInitialAcyclic | kTopSorted | kNotTopSorted | kAccessible | - kNotAccessible | kCoAccessible | kNotCoAccessible | kString | kNotString; - -// Properties that are preserved when an FST's output labels are changed. -const uint64 kOLabelInvariantProperties = - kExpanded | kMutable | kIDeterministic | kNonIDeterministic | - kIEpsilons | kNoIEpsilons | kILabelSorted | kNotILabelSorted | - kWeighted | kUnweighted | kCyclic | kAcyclic | kInitialCyclic | - kInitialAcyclic | kTopSorted | kNotTopSorted | kAccessible | - kNotAccessible | kCoAccessible | kNotCoAccessible | kString | kNotString; - -// Properties that are preserved when an FST's weights are changed. -// This assumes that the set of states that are non-final is not changed. -const uint64 kWeightInvariantProperties = - kExpanded | kMutable | kAcceptor | kNotAcceptor | kIDeterministic | - kNonIDeterministic | kODeterministic | kNonODeterministic | - kEpsilons | kNoEpsilons | kIEpsilons | kNoIEpsilons | kOEpsilons | - kNoOEpsilons | kILabelSorted | kNotILabelSorted | kOLabelSorted | - kNotOLabelSorted | kCyclic | kAcyclic | kInitialCyclic | kInitialAcyclic | - kTopSorted | kNotTopSorted | kAccessible | kNotAccessible | kCoAccessible | - kNotCoAccessible | kString | kNotString; - -// Properties that are preserved when a superfinal state is added -// and an FSTs final weights are directed to it via new transitions. -const uint64 kAddSuperFinalProperties = kExpanded | kMutable | kAcceptor | - kNotAcceptor | kNonIDeterministic | kNonODeterministic | kEpsilons | - kIEpsilons | kOEpsilons | kNotILabelSorted | kNotOLabelSorted | - kWeighted | kUnweighted | kCyclic | kAcyclic | kInitialCyclic | - kInitialAcyclic | kNotTopSorted | kNotAccessible | kCoAccessible | - kNotCoAccessible | kNotString; - -// Properties that are preserved when a superfinal state is removed -// and the epsilon transitions directed to it are made final weights. -const uint64 kRmSuperFinalProperties = kExpanded | kMutable | kAcceptor | - kNotAcceptor | kIDeterministic | kODeterministic | kNoEpsilons | - kNoIEpsilons | kNoOEpsilons | kILabelSorted | kOLabelSorted | - kWeighted | kUnweighted | kCyclic | kAcyclic | kInitialCyclic | - kInitialAcyclic | kTopSorted | kAccessible | kCoAccessible | - kNotCoAccessible | kString; - -// All binary properties -const uint64 kBinaryProperties = 0x0000000000000003ULL; - -// All trinary properties -const uint64 kTrinaryProperties = 0x00003fffffff0000ULL; - -// -// COMPUTED PROPERTIES -// - -// 1st bit of trinary properties -const uint64 kPosTrinaryProperties = - kTrinaryProperties & 0x5555555555555555ULL; - -// 2nd bit of trinary properties -const uint64 kNegTrinaryProperties = - kTrinaryProperties & 0xaaaaaaaaaaaaaaaaULL; - -// All properties -const uint64 kFstProperties = kBinaryProperties | kTrinaryProperties; - -// -// PROPERTY FUNCTIONS and STRING NAMES (defined in properties.cc) -// - -uint64 ClosureProperties(uint64 inprops, bool star, bool delayed = false); -uint64 ComplementProperties(uint64 inprops); -uint64 ComposeProperties(uint64 inprops1, uint64 inprops2); -uint64 ConcatProperties(uint64 inprops1, uint64 inprops2, - bool delayed = false); -uint64 DeterminizeProperties(uint64 inprops); -uint64 DifferenceProperties(uint64 inprops1, uint64 inprops2); -uint64 FactorWeightProperties(uint64 inprops); -uint64 IntersectProperties(uint64 inprops1, uint64 inprops2); -uint64 InvertProperties(uint64 inprops); -uint64 ProjectProperties(uint64 inprops, bool project_input); -uint64 RelabelProperties(uint64 inprops); -uint64 ReplaceProperties(const vector<uint64>& inprops); -uint64 ReverseProperties(uint64 inprops); -uint64 ReweightProperties(uint64 inprops); -uint64 RmEpsilonProperties(uint64 inprops, bool delayed = false); -uint64 SynchronizeProperties(uint64 inprops); -uint64 UnionProperties(uint64 inprops1, uint64 inprops2, bool delayed = false); - -extern const char *PropertyNames[]; - -} // namespace fst - -#endif // FST_LIB_PROPERTIES_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/prune.h b/tools/thirdparty/OpenFst/fst/lib/prune.h deleted file mode 100644 index 42c340e..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/prune.h +++ /dev/null @@ -1,247 +0,0 @@ -// prune.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Functions implementing pruning. - -#ifndef FST_LIB_PRUNE_H__ -#define FST_LIB_PRUNE_H__ - -#include "fst/lib/arcfilter.h" -#include "fst/lib/shortest-distance.h" - -namespace fst { - -template <class A, class ArcFilter> -class PruneOptions { - public: - typedef typename A::Weight Weight; - - // Pruning threshold. - Weight threshold; - // Arc filter. - ArcFilter filter; - // If non-zero, passes in pre-computed shortest distance from initial state - // (possibly resized). - vector<Weight> *idistance; - // If non-zero, passes in pre-computed shortest distance to final states - // (possibly resized). - vector<Weight> *fdistance; - - PruneOptions(const Weight& t, ArcFilter f, vector<Weight> *id = 0, - vector<Weight> *fd = 0) - : threshold(t), filter(f), idistance(id), fdistance(fd) {} -}; - - -// Pruning algorithm: this version modifies its input and it takes an -// options class as an argment. Delete states and arcs in 'fst' that -// do not belong to a successful path whose weight is no more than -// 'opts.threshold' Times() the weight of the shortest path. Weights -// need to be commutative and have the path property. -template <class Arc, class ArcFilter> -void Prune(MutableFst<Arc> *fst, - const PruneOptions<Arc, ArcFilter> &opts) { - typedef typename Arc::Weight Weight; - typedef typename Arc::StateId StateId; - - if ((Weight::Properties() & (kPath | kCommutative)) - != (kPath | kCommutative)) - LOG(FATAL) << "Prune: Weight needs to have the path property and" - << " be commutative: " - << Weight::Type(); - - StateId ns = fst->NumStates(); - if (ns == 0) return; - - vector<Weight> *idistance = opts.idistance; - vector<Weight> *fdistance = opts.fdistance; - - if (!idistance) { - idistance = new vector<Weight>(ns, Weight::Zero()); - ShortestDistance(*fst, idistance, false); - } else { - idistance->resize(ns, Weight::Zero()); - } - - if (!fdistance) { - fdistance = new vector<Weight>(ns, Weight::Zero()); - ShortestDistance(*fst, fdistance, true); - } else { - fdistance->resize(ns, Weight::Zero()); - } - - vector<StateId> dead; - dead.push_back(fst->AddState()); - NaturalLess<Weight> less; - Weight ceiling = Times((*fdistance)[fst->Start()], opts.threshold); - - for (StateId state = 0; state < ns; ++state) { - if (less(ceiling, Times((*idistance)[state], (*fdistance)[state]))) { - dead.push_back(state); - continue; - } - for (MutableArcIterator< MutableFst<Arc> > it(fst, state); - !it.Done(); - it.Next()) { - Arc arc = it.Value(); - if (!opts.filter(arc)) continue; - Weight weight = Times(Times((*idistance)[state], arc.weight), - (*fdistance)[arc.nextstate]); - if(less(ceiling, weight)) { - arc.nextstate = dead[0]; - it.SetValue(arc); - } - } - if (less(ceiling, Times((*idistance)[state], fst->Final(state)))) - fst->SetFinal(state, Weight::Zero()); - } - - fst->DeleteStates(dead); - - if (!opts.idistance) - delete idistance; - if (!opts.fdistance) - delete fdistance; -} - - -// Pruning algorithm: this version modifies its input and simply takes -// the pruning threshold as an argument. Delete states and arcs in -// 'fst' that do not belong to a successful path whose weight is no -// more than 'opts.threshold' Times() the weight of the shortest -// path. Weights need to be commutative and have the path property. -template <class Arc> -void Prune(MutableFst<Arc> *fst, typename Arc::Weight threshold) { - PruneOptions<Arc, AnyArcFilter<Arc> > opts(threshold, AnyArcFilter<Arc>()); - Prune(fst, opts); -} - - -// Pruning algorithm: this version writes the pruned input Fst to an -// output MutableFst and it takes an options class as an argument. -// 'ofst' contains states and arcs that belong to a successful path in -// 'ifst' whose weight is no more than 'opts.threshold' Times() the -// weight of the shortest path. Weights need to be commutative and -// have the path property. -template <class Arc, class ArcFilter> -void Prune(const Fst<Arc> &ifst, - MutableFst<Arc> *ofst, - const PruneOptions<Arc, ArcFilter> &opts) { - typedef typename Arc::Weight Weight; - typedef typename Arc::StateId StateId; - - if ((Weight::Properties() & (kPath | kCommutative)) - != (kPath | kCommutative)) - LOG(FATAL) << "Prune: Weight needs to have the path property and" - << " be commutative: " - << Weight::Type(); - - ofst->DeleteStates(); - - if (ifst.Start() == kNoStateId) - return; - - vector<Weight> *idistance = opts.idistance; - vector<Weight> *fdistance = opts.fdistance; - - if (!idistance) { - idistance = new vector<Weight>; - ShortestDistance(ifst, idistance, false); - } - - if (!fdistance) { - fdistance = new vector<Weight>; - ShortestDistance(ifst, fdistance, true); - } - - vector<StateId> copy; - NaturalLess<Weight> less; - while (fdistance->size() <= ifst.Start()) - fdistance->push_back(Weight::Zero()); - Weight ceiling = Times((*fdistance)[ifst.Start()], opts.threshold); - - for (StateIterator< Fst<Arc> > sit(ifst); - !sit.Done(); - sit.Next()) { - StateId state = sit.Value(); - while (idistance->size() <= state) - idistance->push_back(Weight::Zero()); - while (fdistance->size() <= state) - fdistance->push_back(Weight::Zero()); - while (copy.size() <= state) - copy.push_back(kNoStateId); - - if (less(ceiling, Times((*idistance)[state], (*fdistance)[state]))) - continue; - - if (copy[state] == kNoStateId) - copy[state] = ofst->AddState(); - if (!less(ceiling, Times((*idistance)[state], ifst.Final(state)))) - ofst->SetFinal(copy[state], ifst.Final(state)); - - for (ArcIterator< Fst<Arc> > ait(ifst, state); - !ait.Done(); - ait.Next()) { - Arc arc = ait.Value(); - - if (!opts.filter(arc)) continue; - - while (idistance->size() <= arc.nextstate) - idistance->push_back(Weight::Zero()); - while (fdistance->size() <= arc.nextstate) - fdistance->push_back(Weight::Zero()); - while (copy.size() <= arc.nextstate) - copy.push_back(kNoStateId); - - Weight weight = Times(Times((*idistance)[state], arc.weight), - (*fdistance)[arc.nextstate]); - - if (!less(ceiling, weight)) { - if (copy[arc.nextstate] == kNoStateId) - copy[arc.nextstate] = ofst->AddState(); - arc.nextstate = copy[arc.nextstate]; - ofst->AddArc(copy[state], arc); - } - } - } - - ofst->SetStart(copy[ifst.Start()]); - - if (!opts.idistance) - delete idistance; - if (!opts.fdistance) - delete fdistance; -} - - -// Pruning algorithm: this version writes the pruned input Fst to an -// output MutableFst and simply takes the pruning threshold as an -// argument. 'ofst' contains states and arcs that belong to a -// successful path in 'ifst' whose weight is no more than -// 'opts.threshold' Times() the weight of the shortest path. Weights -// need to be commutative and have the path property. -template <class Arc> -void Prune(const Fst<Arc> &ifst, - MutableFst<Arc> *ofst, - typename Arc::Weight threshold) { - PruneOptions<Arc, AnyArcFilter<Arc> > opts(threshold, AnyArcFilter<Arc>()); - Prune(ifst, ofst, opts); -} - -} // namespace fst - -#endif // FST_LIB_PRUNE_H_ diff --git a/tools/thirdparty/OpenFst/fst/lib/push.h b/tools/thirdparty/OpenFst/fst/lib/push.h deleted file mode 100644 index 9317308..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/push.h +++ /dev/null @@ -1,89 +0,0 @@ -// push.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Class to reweight/push an FST. - -#ifndef FST_LIB_PUSH_H__ -#define FST_LIB_PUSH_H__ - -#include "fst/lib/factor-weight.h" -#include "fst/lib/fst.h" -#include "fst/lib/map.h" -#include "fst/lib/reweight.h" -#include "fst/lib/shortest-distance.h" - -namespace fst { - -// Pushes the weights in FST in the direction defined by TYPE. If -// pushing towards the initial state, the sum of the weight of the -// outgoing transitions and final weight at a non-initial state is -// equal to One() in the resulting machine. If pushing towards the -// final state, the same property holds on the reverse machine. -// -// Weight needs to be left distributive when pushing towards the -// initial state and right distributive when pushing towards the final -// states. -template <class Arc> -void Push(MutableFst<Arc> *fst, ReweightType type) { - vector<typename Arc::Weight> distance; - ShortestDistance(*fst, &distance, type == REWEIGHT_TO_INITIAL); - Reweight(fst, distance, type); -} - - -const uint32 kPushWeights = 0x0001; -const uint32 kPushLabels = 0x0002; - -// OFST obtained from IFST by pushing weights and/or labels according -// to PTYPE in the direction defined by RTYPE. Weight needs to be -// left distributive when pushing weights towards the initial state -// and right distributive when pushing weights towards the final -// states. -template <class Arc, ReweightType rtype> -void Push(const Fst<Arc> &ifst, MutableFst<Arc> *ofst, uint32 ptype) { - - if (ptype == kPushWeights) { - *ofst = ifst; - Push(ofst, rtype); - } else if (ptype & kPushLabels) { - const StringType stype = rtype == REWEIGHT_TO_INITIAL - ? STRING_LEFT - : STRING_RIGHT; - vector<typename GallicArc<Arc, stype>::Weight> gdistance; - VectorFst< GallicArc<Arc, stype> > gfst; - Map(ifst, &gfst, ToGallicMapper<Arc, stype>()); - if (ptype == (kPushWeights | kPushLabels)) { - ShortestDistance(gfst, &gdistance, rtype == REWEIGHT_TO_INITIAL); - } else { - MapFst<Arc, Arc, RmWeightMapper<Arc> > - uwfst(ifst, RmWeightMapper<Arc>()); - MapFst<Arc, GallicArc<Arc, stype>, ToGallicMapper<Arc, stype> > - guwfst(uwfst, ToGallicMapper<Arc, stype>()); - ShortestDistance(guwfst, &gdistance, rtype == REWEIGHT_TO_INITIAL); - } - Reweight(&gfst, gdistance, rtype); - FactorWeightFst< GallicArc<Arc, stype>, GallicFactor<typename Arc::Label, - typename Arc::Weight, stype> > fwfst(gfst); - Map(fwfst, ofst, FromGallicMapper<Arc, stype>()); - } else { - *ofst = ifst; - } -} - -} // namespace fst - -#endif /* FST_LIB_PUSH_H_ */ diff --git a/tools/thirdparty/OpenFst/fst/lib/queue.h b/tools/thirdparty/OpenFst/fst/lib/queue.h deleted file mode 100644 index 478dfa7..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/queue.h +++ /dev/null @@ -1,689 +0,0 @@ -// queue.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Functions and classes for various Fst state queues with -// a unified interface. - -#ifndef FST_LIB_QUEUE_H__ -#define FST_LIB_QUEUE_H__ - -#include <deque> -#include <vector> - -#include "fst/lib/arcfilter.h" -#include "fst/lib/connect.h" -#include "fst/lib/heap.h" -#include "fst/lib/topsort.h" - -namespace fst { - -// template <class S> -// class Queue { -// public: -// typedef typename S StateId; -// -// // Ctr: may need args (e.g., Fst, comparator) for some queues -// Queue(...); -// // Returns the head of the queue -// StateId Head() const; -// // Inserts a state -// void Enqueue(StateId s); -// // Removes the head of the queue -// void Dequeue(); -// // Updates ordering of state s when weight changes, if necessary -// void Update(StateId s); -// // Does the queue contain no elements? -// bool Empty() const; -// // Remove all states from queue -// void Clear(); -// }; - -// State queue types. -enum QueueType { - TRIVIAL_QUEUE = 0, // Single state queue - FIFO_QUEUE = 1, // First-in, first-out queue - LIFO_QUEUE = 2, // Last-in, first-out queue - SHORTEST_FIRST_QUEUE = 3, // Shortest-first queue - TOP_ORDER_QUEUE = 4, // Topologically-ordered queue - STATE_ORDER_QUEUE = 5, // State-ID ordered queue - SCC_QUEUE = 6, // Component graph top-ordered meta-queue - AUTO_QUEUE = 7, // Auto-selected queue - OTHER_QUEUE = 8 - }; - - -// QueueBase, templated on the StateId, is the base class shared by the -// queues considered by AutoQueue. -template <class S> -class QueueBase { - public: - typedef S StateId; - - QueueBase(QueueType type) : queue_type_(type) {} - virtual ~QueueBase() {} - StateId Head() const { return Head_(); } - void Enqueue(StateId s) { Enqueue_(s); } - void Dequeue() { Dequeue_(); } - void Update(StateId s) { Update_(s); } - bool Empty() const { return Empty_(); } - void Clear() { Clear_(); } - QueueType Type() { return queue_type_; } - - private: - virtual StateId Head_() const = 0; - virtual void Enqueue_(StateId s) = 0; - virtual void Dequeue_() = 0; - virtual void Update_(StateId s) = 0; - virtual bool Empty_() const = 0; - virtual void Clear_() = 0; - - QueueType queue_type_; -}; - - -// Trivial queue discipline, templated on the StateId. You may enqueue -// at most one state at a time. It is used for strongly connected components -// with only one state and no self loops. -template <class S> -class TrivialQueue : public QueueBase<S> { -public: - typedef S StateId; - - TrivialQueue() : QueueBase<S>(TRIVIAL_QUEUE), front_(kNoStateId) {} - StateId Head() const { return front_; } - void Enqueue(StateId s) { front_ = s; } - void Dequeue() { front_ = kNoStateId; } - void Update(StateId s) {} - bool Empty() const { return front_ == kNoStateId; } - void Clear() { front_ = kNoStateId; } - - -private: - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } - - StateId front_; -}; - - -// First-in, first-out queue discipline, templated on the StateId. -template <class S> -class FifoQueue : public QueueBase<S>, public deque<S> { - public: - using deque<S>::back; - using deque<S>::push_front; - using deque<S>::pop_back; - using deque<S>::empty; - using deque<S>::clear; - - typedef S StateId; - - FifoQueue() : QueueBase<S>(FIFO_QUEUE) {} - StateId Head() const { return back(); } - void Enqueue(StateId s) { push_front(s); } - void Dequeue() { pop_back(); } - void Update(StateId s) {} - bool Empty() const { return empty(); } - void Clear() { clear(); } - - private: - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } -}; - - -// Last-in, first-out queue discipline, templated on the StateId. -template <class S> -class LifoQueue : public QueueBase<S>, public deque<S> { - public: - using deque<S>::front; - using deque<S>::push_front; - using deque<S>::pop_front; - using deque<S>::empty; - using deque<S>::clear; - - typedef S StateId; - - LifoQueue() : QueueBase<S>(LIFO_QUEUE) {} - StateId Head() const { return front(); } - void Enqueue(StateId s) { push_front(s); } - void Dequeue() { pop_front(); } - void Update(StateId s) {} - bool Empty() const { return empty(); } - void Clear() { clear(); } - - private: - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } -}; - - -// Shortest-first queue discipline, templated on the StateId and -// comparison function object. Comparison function object COMP is -// used to compare two StateIds. If a (single) state's order changes, -// it can be reordered in the queue with a call to Update(). -template <typename S, typename C> -class ShortestFirstQueue : public QueueBase<S> { - public: - typedef S StateId; - typedef C Compare; - - ShortestFirstQueue(C comp) - : QueueBase<S>(SHORTEST_FIRST_QUEUE), heap_(comp) {} - - StateId Head() const { return heap_.Top(); } - - void Enqueue(StateId s) { - for (StateId i = key_.size(); i <= s; ++i) - key_.push_back(kNoKey); - key_[s] = heap_.Insert(s); - } - - void Dequeue() { - key_[heap_.Pop()] = kNoKey; - } - - void Update(StateId s) { - if (s >= (StateId)key_.size() || key_[s] == kNoKey) { - Enqueue(s); - } else { - heap_.Update(key_[s], s); - } - } - - bool Empty() const { return heap_.Empty(); } - - void Clear() { - heap_.Clear(); - key_.clear(); - } - - private: - Heap<S, C> heap_; - vector<ssize_t> key_; - - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } -}; - - -// Given a vector that maps from states to weights and a Less -// comparison function object between weights, this class defines a -// comparison function object between states. -template <typename S, typename L> -class StateWeightCompare { - public: - typedef L Less; - typedef typename L::Weight Weight; - typedef S StateId; - - StateWeightCompare(const vector<Weight>* weights, const L &less) - : weights_(weights), less_(less) {} - - bool operator()(const S x, const S y) const { - return less_((*weights_)[x], (*weights_)[y]); - } - - private: - const vector<Weight>* weights_; - L less_; -}; - - -// Shortest-first queue discipline, templated on the StateId and Weight is -// specialized to use the weight's natural order for the comparion function. -template <typename S, typename W> -class NaturalShortestFirstQueue : - public ShortestFirstQueue<S, StateWeightCompare<S, NaturalLess<W> > > { - public: - typedef StateWeightCompare<S, NaturalLess<W> > C; - - NaturalShortestFirstQueue(vector<W> *distance) : - ShortestFirstQueue<S, C>(C(distance, less_)) {} - - private: - NaturalLess<W> less_; -}; - - -// Topological-order queue discipline, templated on the StateId. -// States are ordered in the queue topologically. The FST must be acyclic. -template <class S> -class TopOrderQueue : public QueueBase<S> { - public: - typedef S StateId; - - // This constructor computes the top. order. It accepts an arc filter - // to limit the transitions considered in that computation (e.g., only - // the epsilon graph). - template <class Arc, class ArcFilter> - - TopOrderQueue(const Fst<Arc> &fst, ArcFilter filter) - : QueueBase<S>(TOP_ORDER_QUEUE), front_(0), back_(kNoStateId), - order_(0), state_(0) { - bool acyclic; - TopOrderVisitor<Arc> top_order_visitor(&order_, &acyclic); - DfsVisit(fst, &top_order_visitor, filter); - if (!acyclic) - LOG(FATAL) << "TopOrderQueue: fst is not acyclic."; - state_.resize(order_.size(), kNoStateId); - } - - // This constructor is passed the top. order, useful when we know it - // beforehand. - TopOrderQueue(const vector<StateId> &order) - : QueueBase<S>(TOP_ORDER_QUEUE), front_(0), back_(kNoStateId), - order_(order), state_(order.size(), kNoStateId) {} - - StateId Head() const { return state_[front_]; } - - void Enqueue(StateId s) { - if (front_ > back_) front_ = back_ = order_[s]; - else if (order_[s] > back_) back_ = order_[s]; - else if (order_[s] < front_) front_ = order_[s]; - state_[order_[s]] = s; - } - - void Dequeue() { - state_[front_] = kNoStateId; - while ((front_ <= back_) && (state_[front_] == kNoStateId)) ++front_; - } - - void Update(StateId s) {} - - bool Empty() const { return front_ > back_; } - - void Clear() { - for (StateId i = front_; i <= back_; ++i) state_[i] = kNoStateId; - back_ = kNoStateId; - front_ = 0; - } - - private: - StateId front_; - StateId back_; - vector<StateId> order_; - vector<StateId> state_; - - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } - -}; - - -// State order queue discipline, templated on the StateId. -// States are ordered in the queue by state Id. -template <class S> -class StateOrderQueue : public QueueBase<S> { -public: - typedef S StateId; - - StateOrderQueue() - : QueueBase<S>(STATE_ORDER_QUEUE), front_(0), back_(kNoStateId) {} - - StateId Head() const { return front_; } - - void Enqueue(StateId s) { - if (front_ > back_) front_ = back_ = s; - else if (s > back_) back_ = s; - else if (s < front_) front_ = s; - while ((StateId)enqueued_.size() <= s) enqueued_.push_back(false); - enqueued_[s] = true; - } - - void Dequeue() { - enqueued_[front_] = false; - while ((front_ <= back_) && (enqueued_[front_] == false)) ++front_; - } - - void Update(StateId s) {} - - bool Empty() const { return front_ > back_; } - - void Clear() { - for (StateId i = front_; i <= back_; ++i) enqueued_[i] = false; - front_ = 0; - back_ = kNoStateId; - } - -private: - StateId front_; - StateId back_; - vector<bool> enqueued_; - - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } - -}; - - -// SCC topological-order meta-queue discipline, templated on the StateId S -// and a queue Q, which is used inside each SCC. It visits the SCC's -// of an FST in topological order. Its constructor is passed the queues to -// to use within an SCC. -template <class S, class Q> -class SccQueue : public QueueBase<S> { - public: - typedef S StateId; - typedef Q Queue; - - // Constructor takes a vector specifying the SCC number per state - // and a vector giving the queue to use per SCC number. - SccQueue(const vector<StateId> &scc, vector<Queue*> *queue) - : QueueBase<S>(SCC_QUEUE), queue_(queue), scc_(scc), front_(0), - back_(kNoStateId) {} - - StateId Head() const { - while ((front_ <= back_) && - (((*queue_)[front_] && (*queue_)[front_]->Empty()) - || (((*queue_)[front_] == 0) && - ((front_ > (StateId)trivial_queue_.size()) - || (trivial_queue_[front_] == kNoStateId))))) - ++front_; - if (front_ > back_) - LOG(FATAL) << "SccQueue: head of empty queue"; - if ((*queue_)[front_]) - return (*queue_)[front_]->Head(); - else - return trivial_queue_[front_]; - } - - void Enqueue(StateId s) { - if (front_ > back_) front_ = back_ = scc_[s]; - else if (scc_[s] > back_) back_ = scc_[s]; - else if (scc_[s] < front_) front_ = scc_[s]; - if ((*queue_)[scc_[s]]) { - (*queue_)[scc_[s]]->Enqueue(s); - } else { - while ( (StateId)trivial_queue_.size() <= scc_[s]) - trivial_queue_.push_back(kNoStateId); - trivial_queue_[scc_[s]] = s; - } - } - - void Dequeue() { - if (front_ > back_) - LOG(FATAL) << "SccQueue: dequeue of empty queue"; - if ((*queue_)[front_]) - (*queue_)[front_]->Dequeue(); - else if (front_ < (StateId)trivial_queue_.size()) - trivial_queue_[front_] = kNoStateId; - } - - void Update(StateId s) { - if ((*queue_)[scc_[s]]) - (*queue_)[scc_[s]]->Update(s); - } - - bool Empty() const { - if (front_ < back_) // Queue scc # back_ not empty unless back_==front_ - return false; - else if (front_ > back_) - return true; - else if ((*queue_)[front_]) - return (*queue_)[front_]->Empty(); - else - return (front_ > (StateId)trivial_queue_.size()) - || (trivial_queue_[front_] == kNoStateId); - } - - void Clear() { - for (StateId i = front_; i <= back_; ++i) - if ((*queue_)[i]) - (*queue_)[i]->Clear(); - else if (i < (StateId)trivial_queue_.size()) - trivial_queue_[i] = kNoStateId; - front_ = 0; - back_ = kNoStateId; - } - -private: - vector<Queue*> *queue_; - const vector<StateId> &scc_; - mutable StateId front_; - StateId back_; - vector<StateId> trivial_queue_; - - virtual StateId Head_() const { return Head(); } - virtual void Enqueue_(StateId s) { Enqueue(s); } - virtual void Dequeue_() { Dequeue(); } - virtual void Update_(StateId s) { Update(s); } - virtual bool Empty_() const { return Empty(); } - virtual void Clear_() { return Clear(); } -}; - - -// Automatic queue discipline, templated on the StateId. It selects a -// queue discipline for a given FST based on its properties. -template <class S> -class AutoQueue : public QueueBase<S> { -public: - typedef S StateId; - - // This constructor takes a state distance vector that, if non-null and if - // the Weight type has the path property, will entertain the - // shortest-first queue using the natural order w.r.t to the distance. - template <class Arc, class ArcFilter> - AutoQueue(const Fst<Arc> &fst, const vector<typename Arc::Weight> *distance, - ArcFilter filter) : QueueBase<S>(AUTO_QUEUE) { - typedef typename Arc::Weight Weight; - typedef StateWeightCompare< StateId, NaturalLess<Weight> > Compare; - - // First check if the FST is known to have these properties. - uint64 props = fst.Properties(kAcyclic | kCyclic | - kTopSorted | kUnweighted, false); - if ((props & kTopSorted) || fst.Start() == kNoStateId) { - queue_ = new StateOrderQueue<StateId>(); - VLOG(2) << "AutoQueue: using state-order discipline"; - } else if (props & kAcyclic) { - queue_ = new TopOrderQueue<StateId>(fst, filter); - VLOG(2) << "AutoQueue: using top-order discipline"; - } else if ((props & kUnweighted) && (Weight::Properties() & kIdempotent)) { - queue_ = new LifoQueue<StateId>(); - VLOG(2) << "AutoQueue: using LIFO discipline"; - } else { - uint64 props; - // Decompose into strongly-connected components. - SccVisitor<Arc> scc_visitor(&scc_, 0, 0, &props); - DfsVisit(fst, &scc_visitor, filter); - StateId nscc = *max_element(scc_.begin(), scc_.end()) + 1; - vector<QueueType> queue_types(nscc); - NaturalLess<Weight> *less = 0; - Compare *comp = 0; - if (distance && (Weight::Properties() & kPath)) { - less = new NaturalLess<Weight>; - comp = new Compare(distance, *less); - } - // Find the queue type to use per SCC. - bool unweighted; - bool all_trivial; - SccQueueType(fst, scc_, &queue_types, filter, less, &all_trivial, - &unweighted); - // If unweighted and semiring is idempotent, use lifo queue. - if (unweighted) { - queue_ = new LifoQueue<StateId>(); - VLOG(2) << "AutoQueue: using LIFO discipline"; - delete comp; - delete less; - return; - } - // If all the scc are trivial, FST is acyclic and the scc# gives - // the topological order. - if (all_trivial) { - queue_ = new TopOrderQueue<StateId>(scc_); - VLOG(2) << "AutoQueue: using top-order discipline"; - delete comp; - delete less; - return; - } - VLOG(2) << "AutoQueue: using SCC meta-discipline"; - queues_.resize(nscc); - for (StateId i = 0; i < nscc; ++i) { - switch(queue_types[i]) { - case TRIVIAL_QUEUE: - queues_[i] = 0; - VLOG(3) << "AutoQueue: SCC #" << i - << ": using trivial discipline"; - break; - case SHORTEST_FIRST_QUEUE: - CHECK(comp); - queues_[i] = new ShortestFirstQueue<StateId, Compare>(*comp); - VLOG(3) << "AutoQueue: SCC #" << i << - ": using shortest-first discipline"; - break; - case LIFO_QUEUE: - queues_[i] = new LifoQueue<StateId>(); - VLOG(3) << "AutoQueue: SCC #" << i - << ": using LIFO disciplle"; - break; - case FIFO_QUEUE: - default: - queues_[i] = new FifoQueue<StateId>(); - VLOG(3) << "AutoQueue: SCC #" << i - << ": using FIFO disciplle"; - break; - } - } - queue_ = new SccQueue< StateId, QueueBase<StateId> >(scc_, &queues_); - delete comp; - delete less; - } - } - - ~AutoQueue() { - for (StateId i = 0; i < (StateId)queues_.size(); ++i) /*naucen-edit*/ - delete queues_[i]; - delete queue_; - } - - StateId Head() const { return queue_->Head(); } - - void Enqueue(StateId s) { queue_->Enqueue(s); } - - void Dequeue() { queue_->Dequeue(); } - - void Update(StateId s) { queue_->Update(s); } - - bool Empty() const { return queue_->Empty(); } - - void Clear() { queue_->Clear(); } - - - private: - QueueBase<StateId> *queue_; - vector< QueueBase<StateId>* > queues_; - vector<StateId> scc_; - - template <class Arc, class ArcFilter, class Less> - static void SccQueueType(const Fst<Arc> &fst, - const vector<StateId> &scc, - vector<QueueType> *queue_types, - ArcFilter filter, Less *less, - bool *all_trivial, bool *unweighted); - - virtual StateId Head_() const { return Head(); } - - virtual void Enqueue_(StateId s) { Enqueue(s); } - - virtual void Dequeue_() { Dequeue(); } - - virtual void Update_(StateId s) { Update(s); } - - virtual bool Empty_() const { return Empty(); } - - virtual void Clear_() { return Clear(); } -}; - - -// Examines the states in an Fst's strongly connected components and -// determines which type of queue to use per SCC. Stores result in -// vector QUEUE_TYPES, which is assumed to have length equal to the -// number of SCCs. An arc filter is used to limit the transitions -// considered (e.g., only the epsilon graph). ALL_TRIVIAL is set -// to true if every queue is the trivial queue. UNWEIGHTED is set to -// true if the semiring is idempotent and all the arc weights are equal to -// Zero() or One(). -template <class StateId> -template <class A, class ArcFilter, class Less> -void AutoQueue<StateId>::SccQueueType(const Fst<A> &fst, - const vector<StateId> &scc, - vector<QueueType> *queue_type, - ArcFilter filter, Less *less, - bool *all_trivial, bool *unweighted) { - typedef A Arc; - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - *all_trivial = true; - *unweighted = true; - - for (StateId i = 0; i < (StateId)queue_type->size(); ++i) - (*queue_type)[i] = TRIVIAL_QUEUE; - - for (StateIterator< Fst<Arc> > sit(fst); !sit.Done(); sit.Next()) { - StateId state = sit.Value(); - for (ArcIterator< Fst<Arc> > ait(fst, state); - !ait.Done(); - ait.Next()) { - const Arc &arc = ait.Value(); - if (!filter(arc)) continue; - if (scc[state] == scc[arc.nextstate]) { - QueueType &type = (*queue_type)[scc[state]]; - if (!less || ((*less)(arc.weight, Weight::One()))) - type = FIFO_QUEUE; - else if ((type == TRIVIAL_QUEUE) || (type == LIFO_QUEUE)) - if (!(Weight::Properties() & kIdempotent) || - (arc.weight != Weight::Zero() && arc.weight != Weight::One())) - type = SHORTEST_FIRST_QUEUE; - else - type = LIFO_QUEUE; - if (type != TRIVIAL_QUEUE) *all_trivial = false; - } - if (!(Weight::Properties() & kIdempotent) || - (arc.weight != Weight::Zero() && arc.weight != Weight::One())) - *unweighted = false; - } - } -} - -} // namespace fst - -#endif diff --git a/tools/thirdparty/OpenFst/fst/lib/randgen.h b/tools/thirdparty/OpenFst/fst/lib/randgen.h deleted file mode 100644 index 7ccbed8..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/randgen.h +++ /dev/null @@ -1,169 +0,0 @@ -// randgen.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function to generate random paths through an FST. - -#ifndef FST_LIB_RANDGEN_H__ -#define FST_LIB_RANDGEN_H__ - -#include <cmath> -#include <cstdlib> -#include <ctime> - -#include "fst/lib/mutable-fst.h" - -namespace fst { - -// -// ARC SELECTORS - these function objects are used to select a random -// transition to take from an FST's state. They should return a number -// N s.t. 0 <= N <= NumArcs(). If N < NumArcs(), then the N-th -// transition is selected. If N == NumArcs(), then the final weight at -// that state is selected (i.e., the 'super-final' transition is selected). -// It can be assumed these will not be called unless either there -// are transitions leaving the state and/or the state is final. -// - -// Randomly selects a transition using the uniform distribution. -template <class A> -struct UniformArcSelector { - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - UniformArcSelector(int seed = time(0)) { srand(seed); } - - size_t operator()(const Fst<A> &fst, StateId s) const { - double r = rand()/(RAND_MAX + 1.0); - size_t n = fst.NumArcs(s); - if (fst.Final(s) != Weight::Zero()) - ++n; - return static_cast<size_t>(r * n); - } -}; - -// Randomly selects a transition w.r.t. the weights treated as negative -// log probabilities after normalizing for the total weight leaving -// the state). Weight::zero transitions are disregarded. -// Assumes Weight::Value() accesses the floating point -// representation of the weight. -template <class A> -struct LogProbArcSelector { - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - LogProbArcSelector(int seed = time(0)) { srand(seed); } - - size_t operator()(const Fst<A> &fst, StateId s) const { - // Find total weight leaving state - double sum = 0.0; - for (ArcIterator< Fst<A> > aiter(fst, s); !aiter.Done(); - aiter.Next()) { - const A &arc = aiter.Value(); - sum += exp(-arc.weight.Value()); - } - sum += exp(-fst.Final(s).Value()); - - double r = rand()/(RAND_MAX + 1.0); - double p = 0.0; - int n = 0; - for (ArcIterator< Fst<A> > aiter(fst, s); !aiter.Done(); - aiter.Next(), ++n) { - const A &arc = aiter.Value(); - p += exp(-arc.weight.Value()); - if (p > r * sum) return n; - } - return n; - } -}; - -// Convenience definitions -typedef LogProbArcSelector<StdArc> StdArcSelector; -typedef LogProbArcSelector<LogArc> LogArcSelector; - - -// Options for random path generation. -template <class S> -struct RandGenOptions { - const S &arc_selector; // How an arc is selected at a state - int max_length; // Maximum path length - size_t npath; // # of paths to generate - - // These are used internally by RandGen - int64 source; // 'ifst' state to expand - int64 dest; // 'ofst' state to append - - RandGenOptions(const S &sel, int len = INT_MAX, size_t n = 1) - : arc_selector(sel), max_length(len), npath(n), - source(kNoStateId), dest(kNoStateId) {} -}; - - -// Randomly generate paths through an FST; details controlled by -// RandGenOptions. -template<class Arc, class ArcSelector> -void RandGen(const Fst<Arc> &ifst, MutableFst<Arc> *ofst, - const RandGenOptions<ArcSelector> &opts) { - typedef typename Arc::Weight Weight; - - if (opts.npath == 0 || opts.max_length == 0 || ifst.Start() == kNoStateId) - return; - - if (opts.source == kNoStateId) { // first call - ofst->DeleteStates(); - ofst->SetInputSymbols(ifst.InputSymbols()); - ofst->SetOutputSymbols(ifst.OutputSymbols()); - ofst->SetStart(ofst->AddState()); - RandGenOptions<ArcSelector> nopts(opts); - nopts.source = ifst.Start(); - nopts.dest = ofst->Start(); - for (; nopts.npath > 0; --nopts.npath) - RandGen(ifst, ofst, nopts); - } else { - if (ifst.NumArcs(opts.source) == 0 && - ifst.Final(opts.source) == Weight::Zero()) // Non-coaccessible - return; - // Pick a random transition from the source state - size_t n = opts.arc_selector(ifst, opts.source); - if (n == ifst.NumArcs(opts.source)) { // Take 'super-final' transition - ofst->SetFinal(opts.dest, Weight::One()); - } else { - ArcIterator< Fst<Arc> > aiter(ifst, opts.source); - aiter.Seek(n); - const Arc &iarc = aiter.Value(); - Arc oarc(iarc.ilabel, iarc.olabel, Weight::One(), ofst->AddState()); - ofst->AddArc(opts.dest, oarc); - - RandGenOptions<ArcSelector> nopts(opts); - nopts.source = iarc.nextstate; - nopts.dest = oarc.nextstate; - --nopts.max_length; - RandGen(ifst, ofst, nopts); - } - } -} - -// Randomly generate a path through an FST with the uniform distribution -// over the transitions. -template<class Arc> -void RandGen(const Fst<Arc> &ifst, MutableFst<Arc> *ofst) { - UniformArcSelector<Arc> uniform_selector; - RandGenOptions< UniformArcSelector<Arc> > opts(uniform_selector); - RandGen(ifst, ofst, opts); -} - -} // namespace fst - -#endif // FST_LIB_RANDGEN_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/random-weight.h b/tools/thirdparty/OpenFst/fst/lib/random-weight.h deleted file mode 100644 index f7834e9..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/random-weight.h +++ /dev/null @@ -1,164 +0,0 @@ -// random-weight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function objects to generate random weights in various semirings -// for testing purposes. - -#ifndef FST_LIB_RANDOM_WEIGHT_H__ -#define FST_LIB_RANDOM_WEIGHT_H__ - -#include <cstdlib> -#include <ctime> - -#include "fst/lib/float-weight.h" -#include "fst/lib/product-weight.h" -#include "fst/lib/string-weight.h" - -namespace fst { - -// The boolean 'allow_zero' below determines whether Zero() and zero -// divisors should be returned in the random weight generation. - -// This function object returns TropicalWeights that are random integers -// chosen from [0, kNumRandomWeights). -class TropicalWeightGenerator { - public: - typedef TropicalWeight Weight; - - TropicalWeightGenerator(int seed = time(0), bool allow_zero = true) - : allow_zero_(allow_zero) { - srand(seed); - } - - Weight operator() () const { - int n = rand() % (kNumRandomWeights + allow_zero_); - if (allow_zero_ && n == kNumRandomWeights) - return Weight::Zero(); - - return Weight(static_cast<float>(n)); - } - - private: - // The number of alternative random weights. - static const int kNumRandomWeights = 5; - - bool allow_zero_; // permit Zero() and zero divisors -}; - - -// This function object returns LogWeights that are random integers -// chosen from [0, kNumRandomWeights). -class LogWeightGenerator { - public: - typedef LogWeight Weight; - - LogWeightGenerator(int seed = time(0), bool allow_zero = true) - : allow_zero_(allow_zero) { - srand(seed); - } - - Weight operator() () const { - int n = rand() % (kNumRandomWeights + allow_zero_); - if (allow_zero_ && n == kNumRandomWeights) - return Weight::Zero(); - - return Weight(static_cast<float>(n)); - } - - private: - // Number of alternative random weights. - static const int kNumRandomWeights = 5; - - bool allow_zero_; // permit Zero() and zero divisors -}; - - -// This function object returns StringWeights that are random integer -// strings chosen from {1,...,kAlphabetSize}^{0,kMaxStringLength} U { Zero } -template <typename L, StringType S = STRING_LEFT> -class StringWeightGenerator { - public: - typedef StringWeight<L, S> Weight; - - StringWeightGenerator(int seed = time(0), bool allow_zero = true) - : allow_zero_(allow_zero) { - srand(seed); - } - - Weight operator() () const { - int n = rand() % (kMaxStringLength + allow_zero_); - if (allow_zero_ && n == kMaxStringLength) - return Weight::Zero(); - - vector<L> v; - for (int i = 0; i < n; ++i) - v.push_back(rand() % kAlphabetSize + 1); - return Weight(v.begin(), v.end()); - } - - private: - // Alphabet size for random weights. - static const int kAlphabetSize = 5; - // Number of alternative random weights. - static const int kMaxStringLength = 5; - - bool allow_zero_; // permit Zero() and zero divisors -}; - - -// This function object returns a weight generator over the product of the -// weights for the generators G1 and G2. -template <class G1, class G2> -class ProductWeightGenerator { - public: - typedef typename G1::Weight W1; - typedef typename G2::Weight W2; - typedef ProductWeight<W1, W2> Weight; - - ProductWeightGenerator(int seed = time(0), bool allow_zero = true) - : generator1_(seed, allow_zero), generator2_(seed, allow_zero) {} - - Weight operator() () const { - W1 w1 = generator1_(); - W2 w2 = generator2_(); - return Weight(w1, w2); - } - - private: - G1 generator1_; - G2 generator2_; -}; - -// Product generator of a string weight generator and an -// arbitrary weight generator. -template <class L, class G, StringType S = STRING_LEFT> -class GallicWeightGenerator - : public ProductWeightGenerator<StringWeightGenerator<L, S>, G> { - - public: - typedef ProductWeightGenerator<StringWeightGenerator<L, S>, G> PG; - typedef typename G::Weight W; - typedef GallicWeight<L, W, S> Weight; - - GallicWeightGenerator(int seed = time(0), bool allow_zero = true) - : PG(seed, allow_zero) {} - - GallicWeightGenerator(const PG &pg) : PG(pg) {} -}; - -} // namespace fst; - -#endif // FST_LIB_RANDOM_WEIGHT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/rational.h b/tools/thirdparty/OpenFst/fst/lib/rational.h deleted file mode 100644 index 7a2f765..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/rational.h +++ /dev/null @@ -1,286 +0,0 @@ -// rational.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// An Fst implementation and base interface for delayed unions, -// concatenations and closures. - -#ifndef FST_LIB_RATIONAL_H__ -#define FST_LIB_RATIONAL_H__ - -#include "fst/lib/map.h" -#include "fst/lib/mutable-fst.h" -#include "fst/lib/replace.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -typedef CacheOptions RationalFstOptions; - -// This specifies whether to add the empty string. -enum ClosureType { CLOSURE_STAR = 0, // T* -> add the empty string - CLOSURE_PLUS = 1 }; // T+ -> don't add the empty string - -template <class A> class RationalFst; -template <class A> void Union(RationalFst<A> *fst1, const Fst<A> &fst2); -template <class A> void Concat(RationalFst<A> *fst1, const Fst<A> &fst2); -template <class A> void Closure(RationalFst<A> *fst, ClosureType closure_type); - - -// Implementation class for delayed unions, concatenations and closures. -template<class A> -class RationalFstImpl : public ReplaceFstImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - using ReplaceFstImpl<A>::SetRoot; - - typedef typename A::Weight Weight; - typedef typename A::Label Label; - - explicit RationalFstImpl(const RationalFstOptions &opts) - : ReplaceFstImpl<A>(ReplaceFstOptions(opts, kNoLabel)), - nonterminals_(0) { - SetType("rational"); - } - - // Implementation of UnionFst(fst1,fst2) - void InitUnion(const Fst<A> &fst1, const Fst<A> &fst2) { - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - SetInputSymbols(fst1.InputSymbols()); - SetOutputSymbols(fst1.OutputSymbols()); - rfst_.AddState(); - rfst_.AddState(); - rfst_.SetStart(0); - rfst_.SetFinal(1, Weight::One()); - rfst_.SetInputSymbols(fst1.InputSymbols()); - rfst_.SetOutputSymbols(fst1.OutputSymbols()); - nonterminals_ = 2; - rfst_.AddArc(0, A(0, -1, Weight::One(), 1)); - rfst_.AddArc(0, A(0, -2, Weight::One(), 1)); - AddFst(0, &rfst_); - AddFst(-1, &fst1); - AddFst(-2, &fst2); - SetRoot(0); - SetProperties(UnionProperties(props1, props2, true), kCopyProperties); - } - - // Implementation of ConcatFst(fst1,fst2) - void InitConcat(const Fst<A> &fst1, const Fst<A> &fst2) { - uint64 props1 = fst1.Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - SetInputSymbols(fst1.InputSymbols()); - SetOutputSymbols(fst1.OutputSymbols()); - rfst_.AddState(); - rfst_.AddState(); - rfst_.AddState(); - rfst_.SetStart(0); - rfst_.SetFinal(2, Weight::One()); - rfst_.SetInputSymbols(fst1.InputSymbols()); - rfst_.SetOutputSymbols(fst1.OutputSymbols()); - nonterminals_ = 2; - rfst_.AddArc(0, A(0, -1, Weight::One(), 1)); - rfst_.AddArc(1, A(0, -2, Weight::One(), 2)); - AddFst(0, &rfst_); - AddFst(-1, &fst1); - AddFst(-2, &fst2); - SetRoot(0); - SetProperties(ConcatProperties(props1, props2, true), kCopyProperties); - } - - // Implementation of ClosureFst(fst, closure_type) - void InitClosure(const Fst<A> &fst, ClosureType closure_type) { - uint64 props = fst.Properties(kFstProperties, false); - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - if (closure_type == CLOSURE_STAR) { - rfst_.AddState(); - rfst_.SetStart(0); - rfst_.SetFinal(0, Weight::One()); - rfst_.AddArc(0, A(0, -1, Weight::One(), 0)); - } else { - rfst_.AddState(); - rfst_.AddState(); - rfst_.SetStart(0); - rfst_.SetFinal(1, Weight::One()); - rfst_.AddArc(0, A(0, -1, Weight::One(), 1)); - rfst_.AddArc(1, A(0, 0, Weight::One(), 0)); - } - rfst_.SetInputSymbols(fst.InputSymbols()); - rfst_.SetOutputSymbols(fst.OutputSymbols()); - AddFst(0, &rfst_); - AddFst(-1, &fst); - SetRoot(0); - nonterminals_ = 1; - SetProperties(ClosureProperties(props, closure_type == CLOSURE_STAR, true), - kCopyProperties); - } - - // Implementation of Union(Fst &, RationalFst *) - void AddUnion(const Fst<A> &fst) { - uint64 props1 = Properties(); - uint64 props2 = fst.Properties(kFstProperties, false); - VectorFst<A> afst; - afst.AddState(); - afst.AddState(); - afst.SetStart(0); - afst.SetFinal(1, Weight::One()); - afst.AddArc(0, A(0, -nonterminals_, Weight::One(), 1)); - Union(&rfst_, afst); - SetFst(0, &rfst_); - ++nonterminals_; - SetProperties(UnionProperties(props1, props2, true), kCopyProperties); - } - - // Implementation of Concat(Fst &, RationalFst *) - void AddConcat(const Fst<A> &fst) { - uint64 props1 = Properties(); - uint64 props2 = fst.Properties(kFstProperties, false); - VectorFst<A> afst; - afst.AddState(); - afst.AddState(); - afst.SetStart(0); - afst.SetFinal(1, Weight::One()); - afst.AddArc(0, A(0, -nonterminals_, Weight::One(), 1)); - Concat(&rfst_, afst); - SetFst(0, &rfst_); - ++nonterminals_; - SetProperties(ConcatProperties(props1, props2, true), kCopyProperties); - } - - // Implementation of Closure(RationalFst *, closure_type) - void AddClosure(ClosureType closure_type) { - uint64 props = Properties(); - Closure(&rfst_, closure_type); - SetFst(0, &rfst_); - SetProperties(ClosureProperties(props, closure_type == CLOSURE_STAR, true), - kCopyProperties); - } - - private: - VectorFst<A> rfst_; // rational topology machine; uses neg. nonterminals - Label nonterminals_; // # of nonterminals used - - DISALLOW_EVIL_CONSTRUCTORS(RationalFstImpl); -}; - -// Parent class for the delayed rational operations - delayed union, -// concatenation, and closure. This class attaches interface to -// implementation and handles reference counting. -template <class A> -class RationalFst : public Fst<A> { - public: - friend class CacheStateIterator< RationalFst<A> >; - friend class ArcIterator< RationalFst<A> >; - friend class CacheArcIterator< RationalFst<A> >; - friend void Union<>(RationalFst<A> *fst1, const Fst<A> &fst2); - friend void Concat<>(RationalFst<A> *fst1, const Fst<A> &fst2); - friend void Closure<>(RationalFst<A> *fst, ClosureType closure_type); - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - virtual StateId Start() const { return impl_->Start(); } - virtual Weight Final(StateId s) const { return impl_->Final(s); } - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - virtual const string& Type() const { return impl_->Type(); } - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - protected: - RationalFst() : impl_(new RationalFstImpl<A>(RationalFstOptions())) {} - explicit RationalFst(const RationalFstOptions &opts) - : impl_(new RationalFstImpl<A>(opts)) {} - - - RationalFst(const RationalFst<A> &fst) : impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~RationalFst() { if (!impl_->DecrRefCount()) delete impl_; } - - RationalFstImpl<A> *Impl() { return impl_; } - - private: - RationalFstImpl<A> *impl_; - - void operator=(const RationalFst<A> &fst); // disallow -}; - -// Specialization for RationalFst. -template <class A> -class StateIterator< RationalFst<A> > - : public CacheStateIterator< RationalFst<A> > { - public: - explicit StateIterator(const RationalFst<A> &fst) - : CacheStateIterator< RationalFst<A> >(fst) {} -}; - -// Specialization for RationalFst. -template <class A> -class ArcIterator< RationalFst<A> > - : public CacheArcIterator< RationalFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const RationalFst<A> &fst, StateId s) - : CacheArcIterator< RationalFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -template <class A> inline -void RationalFst<A>::InitStateIterator(StateIteratorData<A> *data) const { - data->base = new StateIterator< RationalFst<A> >(*this); -} - -} // namespace fst - -#endif // FST_LIB_RATIONAL_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/register.h b/tools/thirdparty/OpenFst/fst/lib/register.h deleted file mode 100644 index d15f28f..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/register.h +++ /dev/null @@ -1,173 +0,0 @@ -// fst-register.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Classes for registering derived Fsts for generic reading -// - -#ifndef FST_LIB_REGISTER_H__ -#define FST_LIB_REGISTER_H__ - -#include <map> - -#include <dlfcn.h> -#include <pthread.h> - -#include "fst/lib/compat.h" - -extern "C" { - typedef void (*FstInitFunc)(); -} - -namespace fst { - -template <class A> class Fst; -struct FstReadOptions; - -// This class holds the mapping from Fst name string to its reader -// and converter. -template <class A> -class FstRegister { - public: - typedef Fst<A> *(*Reader)(istream &strm, const FstReadOptions &opts); - typedef Fst<A> *(*Converter)(const Fst<A> &fst); - - struct Entry { - Reader reader; - Converter converter; - Entry() : reader(0), converter(0) {} - }; - - static FstRegister<A> *GetRegister() { - pthread_once(®ister_init_, &FstRegister<A>::Init); - return register_; - } - - const Reader GetReader(const string &type) const { - return GetEntry(type).reader; - } - - const Converter GetConverter(const string &type) const { - return GetEntry(type).converter; - } - - void SetEntry(const string &type, const Entry &entry) { - MutexLock l(register_lock_); - fst_table_.insert(make_pair(type, entry)); - } - - private: - static void Init() { - register_lock_ = new Mutex; - register_ = new FstRegister<A>; - } - - Entry LookupEntry(const string &type) const { - MutexLock l(register_lock_); - typename map<string, Entry>::const_iterator it = fst_table_.find(type); - if (it != fst_table_.end()) - return it->second; - else - return Entry(); - } - - Entry GetEntry(const string &type) const { -#ifdef FST_DL - Entry entry = LookupEntry(type); - if (entry.reader) - return entry; - string so_file = type + "-fst.so"; - void *handle = dlopen(so_file.c_str(), RTLD_LAZY); - if (handle == 0) { - LOG(ERROR) << "FstRegister::GetEntry: " << dlerror(); - return entry; - } - string init_name = type + "_fst_init"; - FstInitFunc init_func = - bit_cast<FstInitFunc>(dlsym(handle, init_name.c_str())); - if (init_func == 0) { - LOG(ERROR) << "FstRegister::GetEntry: " << dlerror(); - return entry; - } - (*init_func)(); -#endif // FST_DL - return LookupEntry(type); - } - - static pthread_once_t register_init_; // ensures only called once - static Mutex* register_lock_; // multithreading lock - static FstRegister<A> *register_; - - map<string, Entry> fst_table_; -}; - -template <class A> -pthread_once_t FstRegister<A>::register_init_ = PTHREAD_ONCE_INIT; - -template <class A> -Mutex *FstRegister<A>::register_lock_ = 0; - -template <class A> -FstRegister<A> *FstRegister<A>::register_ = 0; - -// This class registers an Fst type for generic reading and creating. -// The Fst type must have a default constructor and a copy constructor -// from 'Fst<Arc>' for this to work. -template <class F> -class FstRegisterer { - public: - typedef typename F::Arc Arc; - typedef typename FstRegister<Arc>::Entry Entry; - typedef typename FstRegister<Arc>::Reader Reader; - - FstRegisterer() { - F fst; - F *(*reader)(istream &strm, - const FstReadOptions &opts) = &F::Read; - Entry entry; - entry.reader = reinterpret_cast<Reader>(reader); - entry.converter = &FstRegisterer<F>::Convert; - FstRegister<Arc> *registr = FstRegister<Arc>::GetRegister(); - registr->SetEntry(fst.Type(), entry); - } - - private: - static Fst<Arc> *Convert(const Fst<Arc> &fst) { return new F(fst); } -}; - - -// Convenience macro to generate static FstRegisterer instance. -#define REGISTER_FST(F, A) \ -static fst::FstRegisterer< F<A> > F ## _ ## A ## _registerer - - -// Converts an fst to type 'type'. -template <class A> -Fst<A> *Convert(const Fst<A> &fst, const string &ftype) { - FstRegister<A> *registr = FstRegister<A>::GetRegister(); - const typename FstRegister<A>::Converter - converter = registr->GetConverter(ftype); - if (!converter) { - string atype = A::Type(); - LOG(ERROR) << "Fst::Convert: Unknown FST type \"" << ftype - << "\" (arc type = \"" << atype << "\")"; - return 0; - } - return converter(fst); -} - -} // namespace fst; - -#endif // FST_LIB_REGISTER_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/relabel.h b/tools/thirdparty/OpenFst/fst/lib/relabel.h deleted file mode 100644 index d7f5185..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/relabel.h +++ /dev/null @@ -1,469 +0,0 @@ -// relabel.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to relabel an Fst (either on input or output) -// -#ifndef FST_LIB_RELABEL_H__ -#define FST_LIB_RELABEL_H__ - -#include <ext/hash_map> -using __gnu_cxx::hash_map; - -#include "fst/lib/cache.h" -#include "fst/lib/test-properties.h" - - -namespace fst { - -// -// Relabels either the input labels or output labels. The old to -// new labels are specified using a vector of pair<Label,Label>. -// Any label associations not specified are assumed to be identity -// mapping. -// -// \param fst input fst, must be mutable -// \param relabel_pairs vector of pairs indicating old to new mapping -// \param relabel_flags whether to relabel input or output -// -template <class A> -void Relabel( - MutableFst<A> *fst, - const vector<pair<typename A::Label, typename A::Label> >& ipairs, - const vector<pair<typename A::Label, typename A::Label> >& opairs) { - typedef typename A::StateId StateId; - typedef typename A::Label Label; - - uint64 props = fst->Properties(kFstProperties, false); - - // construct label to label hash. Could - hash_map<Label, Label> input_map; - for (size_t i = 0; i < ipairs.size(); ++i) { - input_map[ipairs[i].first] = ipairs[i].second; - } - - hash_map<Label, Label> output_map; - for (size_t i = 0; i < opairs.size(); ++i) { - output_map[opairs[i].first] = opairs[i].second; - } - - for (StateIterator<MutableFst<A> > siter(*fst); - !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - for (MutableArcIterator<MutableFst<A> > aiter(fst, s); - !aiter.Done(); aiter.Next()) { - A arc = aiter.Value(); - - // relabel input - // only relabel if relabel pair defined - typename hash_map<Label, Label>::iterator it = - input_map.find(arc.ilabel); - if (it != input_map.end()) {arc.ilabel = it->second; } - - // relabel output - it = output_map.find(arc.olabel); - if (it != output_map.end()) { arc.olabel = it->second; } - - aiter.SetValue(arc); - } - } - - fst->SetProperties(RelabelProperties(props), kFstProperties); -} - - - -// -// Relabels either the input labels or output labels. The old to -// new labels mappings are specified using an input Symbol set. -// Any label associations not specified are assumed to be identity -// mapping. -// -// \param fst input fst, must be mutable -// \param new_symbols symbol set indicating new mapping -// \param relabel_flags whether to relabel input or output -// -template<class A> -void Relabel(MutableFst<A> *fst, - const SymbolTable* new_isymbols, - const SymbolTable* new_osymbols) { - typedef typename A::StateId StateId; - typedef typename A::Label Label; - - const SymbolTable* old_isymbols = fst->InputSymbols(); - const SymbolTable* old_osymbols = fst->OutputSymbols(); - - vector<pair<Label, Label> > ipairs; - if (old_isymbols && new_isymbols) { - for (SymbolTableIterator syms_iter(*old_isymbols); !syms_iter.Done(); - syms_iter.Next()) { - ipairs.push_back(make_pair(syms_iter.Value(), - new_isymbols->Find(syms_iter.Symbol()))); - } - fst->SetInputSymbols(new_isymbols); - } - - vector<pair<Label, Label> > opairs; - if (old_osymbols && new_osymbols) { - for (SymbolTableIterator syms_iter(*old_osymbols); !syms_iter.Done(); - syms_iter.Next()) { - opairs.push_back(make_pair(syms_iter.Value(), - new_osymbols->Find(syms_iter.Symbol()))); - } - fst->SetOutputSymbols(new_osymbols); - } - - // call relabel using vector of relabel pairs. - Relabel(fst, ipairs, opairs); -} - - -typedef CacheOptions RelabelFstOptions; - -template <class A> class RelabelFst; - -// -// \class RelabelFstImpl -// \brief Implementation for delayed relabeling -// -// Relabels an FST from one symbol set to another. Relabeling -// can either be on input or output space. RelabelFst implements -// a delayed version of the relabel. Arcs are relabeled on the fly -// and not cached. I.e each request is recomputed. -// -template<class A> -class RelabelFstImpl : public CacheImpl<A> { - friend class StateIterator< RelabelFst<A> >; - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - using CacheImpl<A>::HasStart; - using CacheImpl<A>::HasArcs; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - RelabelFstImpl(const Fst<A>& fst, - const vector<pair<Label, Label> >& ipairs, - const vector<pair<Label, Label> >& opairs, - const RelabelFstOptions &opts) - : CacheImpl<A>(opts), fst_(fst.Copy()), - relabel_input_(false), relabel_output_(false) { - uint64 props = fst.Properties(kCopyProperties, false); - SetProperties(RelabelProperties(props)); - SetType("relabel"); - - // create input label map - if (ipairs.size() > 0) { - for (size_t i = 0; i < ipairs.size(); ++i) { - input_map_[ipairs[i].first] = ipairs[i].second; - } - relabel_input_ = true; - } - - // create output label map - if (opairs.size() > 0) { - for (size_t i = 0; i < opairs.size(); ++i) { - output_map_[opairs[i].first] = opairs[i].second; - } - relabel_output_ = true; - } - } - - RelabelFstImpl(const Fst<A>& fst, - const SymbolTable* new_isymbols, - const SymbolTable* new_osymbols, - const RelabelFstOptions &opts) - : CacheImpl<A>(opts), fst_(fst.Copy()), - relabel_input_(false), relabel_output_(false) { - SetType("relabel"); - - uint64 props = fst.Properties(kCopyProperties, false); - SetProperties(RelabelProperties(props)); - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - - const SymbolTable* old_isymbols = fst.InputSymbols(); - const SymbolTable* old_osymbols = fst.OutputSymbols(); - - if (old_isymbols && new_isymbols && - old_isymbols->CheckSum() != new_isymbols->CheckSum()) { - for (SymbolTableIterator syms_iter(*old_isymbols); !syms_iter.Done(); - syms_iter.Next()) { - input_map_[syms_iter.Value()] = new_isymbols->Find(syms_iter.Symbol()); - } - SetInputSymbols(new_isymbols); - relabel_input_ = true; - } - - if (old_osymbols && new_osymbols && - old_osymbols->CheckSum() != new_osymbols->CheckSum()) { - for (SymbolTableIterator syms_iter(*old_osymbols); !syms_iter.Done(); - syms_iter.Next()) { - output_map_[syms_iter.Value()] = - new_osymbols->Find(syms_iter.Symbol()); - } - SetOutputSymbols(new_osymbols); - relabel_output_ = true; - } - } - - ~RelabelFstImpl() { delete fst_; } - - StateId Start() { - if (!HasStart()) { - StateId s = fst_->Start(); - SetStart(s); - } - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - SetFinal(s, fst_->Final(s)); - } - return CacheImpl<A>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) { - Expand(s); - } - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) { - Expand(s); - } - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) { - Expand(s); - } - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<A>* data) { - if (!HasArcs(s)) { - Expand(s); - } - CacheImpl<A>::InitArcIterator(s, data); - } - - void Expand(StateId s) { - for (ArcIterator<Fst<A> > aiter(*fst_, s); !aiter.Done(); aiter.Next()) { - A arc = aiter.Value(); - - // relabel input - if (relabel_input_) { - typename hash_map<Label, Label>::iterator it = - input_map_.find(arc.ilabel); - if (it != input_map_.end()) { arc.ilabel = it->second; } - } - - // relabel output - if (relabel_output_) { - typename hash_map<Label, Label>::iterator it = - output_map_.find(arc.olabel); - if (it != output_map_.end()) { arc.olabel = it->second; } - } - - AddArc(s, arc); - } - SetArcs(s); - } - - - private: - const Fst<A> *fst_; - - hash_map<Label, Label> input_map_; - hash_map<Label, Label> output_map_; - bool relabel_input_; - bool relabel_output_; - - DISALLOW_EVIL_CONSTRUCTORS(RelabelFstImpl); -}; - - -// -// \class RelabelFst -// \brief Delayed implementation of arc relabeling -// -// This class attaches interface to implementation and handles -// reference counting. -template <class A> -class RelabelFst : public Fst<A> { - public: - friend class ArcIterator< RelabelFst<A> >; - friend class StateIterator< RelabelFst<A> >; - friend class CacheArcIterator< RelabelFst<A> >; - - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - RelabelFst(const Fst<A>& fst, - const vector<pair<Label, Label> >& ipairs, - const vector<pair<Label, Label> >& opairs) : - impl_(new RelabelFstImpl<A>(fst, ipairs, opairs, RelabelFstOptions())) {} - - RelabelFst(const Fst<A>& fst, - const vector<pair<Label, Label> >& ipairs, - const vector<pair<Label, Label> >& opairs, - const RelabelFstOptions &opts) - : impl_(new RelabelFstImpl<A>(fst, ipairs, opairs, opts)) {} - - RelabelFst(const Fst<A>& fst, - const SymbolTable* new_isymbols, - const SymbolTable* new_osymbols) : - impl_(new RelabelFstImpl<A>(fst, new_isymbols, new_osymbols, - RelabelFstOptions())) {} - - RelabelFst(const Fst<A>& fst, - const SymbolTable* new_isymbols, - const SymbolTable* new_osymbols, - const RelabelFstOptions &opts) - : impl_(new RelabelFstImpl<A>(fst, new_isymbols, new_osymbols, opts)) {} - - RelabelFst(const RelabelFst<A> &fst) : impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~RelabelFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual RelabelFst<A> *Copy() const { - return new RelabelFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - return impl_->InitArcIterator(s, data); - } - - private: - RelabelFstImpl<A> *impl_; - - void operator=(const RelabelFst<A> &fst); // disallow -}; - -// Specialization for RelabelFst. -template<class A> -class StateIterator< RelabelFst<A> > : public StateIteratorBase<A> { - public: - typedef typename A::StateId StateId; - - explicit StateIterator(const RelabelFst<A> &fst) - : impl_(fst.impl_), siter_(*impl_->fst_), s_(0) {} - - bool Done() const { return siter_.Done(); } - - StateId Value() const { return s_; } - - void Next() { - if (!siter_.Done()) { - ++s_; - siter_.Next(); - } - } - - void Reset() { - s_ = 0; - siter_.Reset(); - } - - private: - const RelabelFstImpl<A> *impl_; - StateIterator< Fst<A> > siter_; - StateId s_; - - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - - -// Specialization for RelabelFst. -template <class A> -class ArcIterator< RelabelFst<A> > - : public CacheArcIterator< RelabelFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const RelabelFst<A> &fst, StateId s) - : CacheArcIterator< RelabelFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -template <class A> inline -void RelabelFst<A>::InitStateIterator(StateIteratorData<A> *data) const { - data->base = new StateIterator< RelabelFst<A> >(*this); -} - -// Useful alias when using StdArc. -typedef RelabelFst<StdArc> StdRelabelFst; - -} // namespace fst - -#endif // FST_LIB_RELABEL_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/replace.h b/tools/thirdparty/OpenFst/fst/lib/replace.h deleted file mode 100644 index 4b8622e..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/replace.h +++ /dev/null @@ -1,683 +0,0 @@ -// replace.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes for the recursive replacement of Fsts. -// - -#ifndef FST_LIB_REPLACE_H__ -#define FST_LIB_REPLACE_H__ - -#include <ext/hash_map> -using __gnu_cxx::hash_map; - -#include "fst/lib/fst.h" -#include "fst/lib/cache.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -// By default ReplaceFst will copy the input label of the 'replace arc'. -// For acceptors we do not want this behaviour. Instead we need to -// create an epsilon arc when recursing into the appropriate Fst. -// The epsilon_on_replace option can be used to toggle this behaviour. -struct ReplaceFstOptions : CacheOptions { - int64 root; // root rule for expansion - bool epsilon_on_replace; - - ReplaceFstOptions(const CacheOptions &opts, int64 r) - : CacheOptions(opts), root(r), epsilon_on_replace(false) {} - explicit ReplaceFstOptions(int64 r) - : root(r), epsilon_on_replace(false) {} - ReplaceFstOptions(int64 r, bool epsilon_replace_arc) - : root(r), epsilon_on_replace(epsilon_replace_arc) {} - ReplaceFstOptions() - : root(kNoLabel), epsilon_on_replace(false) {} -}; - -// -// \class ReplaceFstImpl -// \brief Implementation class for replace class Fst -// -// The replace implementation class supports a dynamic -// expansion of a recursive transition network represented as Fst -// with dynamic replacable arcs. -// -template <class A> -class ReplaceFstImpl : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - using FstImpl<A>::InputSymbols; - using FstImpl<A>::OutputSymbols; - - using CacheImpl<A>::HasStart; - using CacheImpl<A>::HasArcs; - using CacheImpl<A>::SetStart; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - typedef A Arc; - typedef hash_map<Label, Label> NonTerminalHash; - - - // \struct StateTuple - // \brief Tuple of information that uniquely defines a state - struct StateTuple { - typedef int PrefixId; - - StateTuple() {} - StateTuple(PrefixId p, StateId f, StateId s) : - prefix_id(p), fst_id(f), fst_state(s) {} - - PrefixId prefix_id; // index in prefix table - StateId fst_id; // current fst being walked - StateId fst_state; // current state in fst being walked, not to be - // confused with the state_id of the combined fst - }; - - // constructor for replace class implementation. - // \param fst_tuples array of label/fst tuples, one for each non-terminal - ReplaceFstImpl(const vector< pair<Label, const Fst<A>* > >& fst_tuples, - const ReplaceFstOptions &opts) - : CacheImpl<A>(opts), opts_(opts) { - SetType("replace"); - if (fst_tuples.size() > 0) { - SetInputSymbols(fst_tuples[0].second->InputSymbols()); - SetOutputSymbols(fst_tuples[0].second->OutputSymbols()); - } - - fst_array_.push_back(0); - for (size_t i = 0; i < fst_tuples.size(); ++i) - AddFst(fst_tuples[i].first, fst_tuples[i].second); - - SetRoot(opts.root); - } - - explicit ReplaceFstImpl(const ReplaceFstOptions &opts) - : CacheImpl<A>(opts), opts_(opts), root_(kNoLabel) { - fst_array_.push_back(0); - } - - ReplaceFstImpl(const ReplaceFstImpl& impl) - : opts_(impl.opts_), state_tuples_(impl.state_tuples_), - state_hash_(impl.state_hash_), - prefix_hash_(impl.prefix_hash_), - stackprefix_array_(impl.stackprefix_array_), - nonterminal_hash_(impl.nonterminal_hash_), - root_(impl.root_) { - SetType("replace"); - SetProperties(impl.Properties(), kCopyProperties); - SetInputSymbols(InputSymbols()); - SetOutputSymbols(OutputSymbols()); - fst_array_.reserve(impl.fst_array_.size()); - fst_array_.push_back(0); - for (size_t i = 1; i < impl.fst_array_.size(); ++i) - fst_array_.push_back(impl.fst_array_[i]->Copy()); - } - - ~ReplaceFstImpl() { - for (size_t i = 1; i < fst_array_.size(); ++i) { - delete fst_array_[i]; - } - } - - // Add to Fst array - void AddFst(Label label, const Fst<A>* fst) { - nonterminal_hash_[label] = fst_array_.size(); - fst_array_.push_back(fst->Copy()); - if (fst_array_.size() > 1) { - vector<uint64> inprops(fst_array_.size()); - - for (size_t i = 1; i < fst_array_.size(); ++i) { - inprops[i] = fst_array_[i]->Properties(kCopyProperties, false); - } - SetProperties(ReplaceProperties(inprops)); - - const SymbolTable* isymbols = fst_array_[1]->InputSymbols(); - const SymbolTable* osymbols = fst_array_[1]->OutputSymbols(); - for (size_t i = 2; i < fst_array_.size(); ++i) { - if (!CompatSymbols(isymbols, fst_array_[i]->InputSymbols())) { - LOG(FATAL) << "ReplaceFst::AddFst input symbols of Fst " << i-1 - << " does not match input symbols of base Fst (0'th fst)"; - } - if (!CompatSymbols(osymbols, fst_array_[i]->OutputSymbols())) { - LOG(FATAL) << "ReplaceFst::AddFst output symbols of Fst " << i-1 - << " does not match output symbols of base Fst " - << "(0'th fst)"; - } - } - } - } - - // Computes the dependency graph of the replace class and returns - // true if the dependencies are cyclic. Cyclic dependencies will result - // in an un-expandable replace fst. - bool CyclicDependencies() const { - StdVectorFst depfst; - - // one state for each fst - for (size_t i = 1; i < fst_array_.size(); ++i) - depfst.AddState(); - - // an arc from each state (representing the fst) to the - // state representing the fst being replaced - for (size_t i = 1; i < fst_array_.size(); ++i) { - for (StateIterator<Fst<A> > siter(*(fst_array_[i])); - !siter.Done(); siter.Next()) { - for (ArcIterator<Fst<A> > aiter(*(fst_array_[i]), siter.Value()); - !aiter.Done(); aiter.Next()) { - const A& arc = aiter.Value(); - - typename NonTerminalHash::const_iterator it = - nonterminal_hash_.find(arc.olabel); - if (it != nonterminal_hash_.end()) { - Label j = it->second - 1; - depfst.AddArc(i - 1, A(arc.olabel, arc.olabel, Weight::One(), j)); - } - } - } - } - - depfst.SetStart(root_ - 1); - depfst.SetFinal(root_ - 1, Weight::One()); - return depfst.Properties(kCyclic, true); - } - - // set root rule for expansion - void SetRoot(Label root) { - Label nonterminal = nonterminal_hash_[root]; - root_ = (nonterminal > 0) ? nonterminal : 1; - } - - // Change Fst array - void SetFst(Label label, const Fst<A>* fst) { - Label nonterminal = nonterminal_hash_[label]; - delete fst_array_[nonterminal]; - fst_array_[nonterminal] = fst->Copy(); - } - - // Return or compute start state of replace fst - StateId Start() { - if (!HasStart()) { - if (fst_array_.size() == 1) { // no fsts defined for replace - SetStart(kNoStateId); - return kNoStateId; - } else { - const Fst<A>* fst = fst_array_[root_]; - StateId fst_start = fst->Start(); - if (fst_start == kNoStateId) // root Fst is empty - return kNoStateId; - - int prefix = PrefixId(StackPrefix()); - StateId start = FindState(StateTuple(prefix, root_, fst_start)); - SetStart(start); - return start; - } - } else { - return CacheImpl<A>::Start(); - } - } - - // return final weight of state (kInfWeight means state is not final) - Weight Final(StateId s) { - if (!HasFinal(s)) { - const StateTuple& tuple = state_tuples_[s]; - const StackPrefix& stack = stackprefix_array_[tuple.prefix_id]; - const Fst<A>* fst = fst_array_[tuple.fst_id]; - StateId fst_state = tuple.fst_state; - - if (fst->Final(fst_state) != Weight::Zero() && stack.Depth() == 0) - SetFinal(s, fst->Final(fst_state)); - else - SetFinal(s, Weight::Zero()); - } - return CacheImpl<A>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - // return the base arc iterator, if arcs have not been computed yet, - // extend/recurse for new arcs. - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - // Find/create an Fst state given a StateTuple. Only create a new - // state if StateTuple is not found in the state hash. - StateId FindState(const StateTuple& tuple) { - typename StateTupleHash::iterator it = state_hash_.find(tuple); - if (it == state_hash_.end()) { - StateId new_state_id = state_tuples_.size(); - state_tuples_.push_back(tuple); - state_hash_[tuple] = new_state_id; - return new_state_id; - } else { - return it->second; - } - } - - // extend current state (walk arcs one level deep) - void Expand(StateId s) { - StateTuple tuple = state_tuples_[s]; - const Fst<A>* fst = fst_array_[tuple.fst_id]; - StateId fst_state = tuple.fst_state; - if (fst_state == kNoStateId) { - SetArcs(s); - return; - } - - // if state is final, pop up stack - const StackPrefix& stack = stackprefix_array_[tuple.prefix_id]; - if (fst->Final(fst_state) != Weight::Zero() && stack.Depth()) { - int prefix_id = PopPrefix(stack); - const PrefixTuple& top = stack.Top(); - - StateId nextstate = - FindState(StateTuple(prefix_id, top.fst_id, top.nextstate)); - AddArc(s, A(0, 0, fst->Final(fst_state), nextstate)); - } - - // extend arcs leaving the state - for (ArcIterator< Fst<A> > aiter(*fst, fst_state); - !aiter.Done(); aiter.Next()) { - const Arc& arc = aiter.Value(); - if (arc.olabel == 0) { // expand local fst - StateId nextstate = - FindState(StateTuple(tuple.prefix_id, tuple.fst_id, arc.nextstate)); - AddArc(s, A(arc.ilabel, arc.olabel, arc.weight, nextstate)); - } else { - // check for non terminal - typename NonTerminalHash::const_iterator it = - nonterminal_hash_.find(arc.olabel); - if (it != nonterminal_hash_.end()) { // recurse into non terminal - Label nonterminal = it->second; - const Fst<A>* nt_fst = fst_array_[nonterminal]; - int nt_prefix = PushPrefix(stackprefix_array_[tuple.prefix_id], - tuple.fst_id, arc.nextstate); - - // if start state is valid replace, else arc is implicitly - // deleted - StateId nt_start = nt_fst->Start(); - if (nt_start != kNoStateId) { - StateId nt_nextstate = FindState( - StateTuple(nt_prefix, nonterminal, nt_start)); - Label ilabel = (opts_.epsilon_on_replace) ? 0 : arc.ilabel; - AddArc(s, A(ilabel, 0, arc.weight, nt_nextstate)); - } - } else { - StateId nextstate = - FindState( - StateTuple(tuple.prefix_id, tuple.fst_id, arc.nextstate)); - AddArc(s, A(arc.ilabel, arc.olabel, arc.weight, nextstate)); - } - } - } - - SetArcs(s); - } - - - // private helper classes - private: - static const int kPrime0 = 7853; - static const int kPrime1 = 7867; - - // \class StateTupleEqual - // \brief Compare two StateTuples for equality - class StateTupleEqual { - public: - bool operator()(const StateTuple& x, const StateTuple& y) const { - return ((x.prefix_id == y.prefix_id) && (x.fst_id == y.fst_id) && - (x.fst_state == y.fst_state)); - } - }; - - // \class StateTupleKey - // \brief Hash function for StateTuple to Fst states - class StateTupleKey { - public: - size_t operator()(const StateTuple& x) const { - return static_cast<size_t>(x.prefix_id + - x.fst_id * kPrime0 + - x.fst_state * kPrime1); - } - }; - - typedef hash_map<StateTuple, StateId, StateTupleKey, StateTupleEqual> - StateTupleHash; - - // \class PrefixTuple - // \brief Tuple of fst_id and destination state (entry in stack prefix) - struct PrefixTuple { - PrefixTuple(Label f, StateId s) : fst_id(f), nextstate(s) {} - - Label fst_id; - StateId nextstate; - }; - - // \class StackPrefix - // \brief Container for stack prefix. - class StackPrefix { - public: - StackPrefix() {} - - // copy constructor - StackPrefix(const StackPrefix& x) : - prefix_(x.prefix_) { - } - - void Push(int fst_id, StateId nextstate) { - prefix_.push_back(PrefixTuple(fst_id, nextstate)); - } - - void Pop() { - prefix_.pop_back(); - } - - const PrefixTuple& Top() const { - return prefix_[prefix_.size()-1]; - } - - size_t Depth() const { - return prefix_.size(); - } - - public: - vector<PrefixTuple> prefix_; - }; - - - // \class StackPrefixEqual - // \brief Compare two stack prefix classes for equality - class StackPrefixEqual { - public: - bool operator()(const StackPrefix& x, const StackPrefix& y) const { - if (x.prefix_.size() != y.prefix_.size()) return false; - for (size_t i = 0; i < x.prefix_.size(); ++i) { - if (x.prefix_[i].fst_id != y.prefix_[i].fst_id || - x.prefix_[i].nextstate != y.prefix_[i].nextstate) return false; - } - return true; - } - }; - - // - // \class StackPrefixKey - // \brief Hash function for stack prefix to prefix id - class StackPrefixKey { - public: - size_t operator()(const StackPrefix& x) const { - int sum = 0; - for (size_t i = 0; i < x.prefix_.size(); ++i) { - sum += x.prefix_[i].fst_id + x.prefix_[i].nextstate*kPrime0; - } - return (size_t) sum; - } - }; - - typedef hash_map<StackPrefix, int, StackPrefixKey, StackPrefixEqual> - StackPrefixHash; - - // private methods - private: - // hash stack prefix (return unique index into stackprefix array) - int PrefixId(const StackPrefix& prefix) { - typename StackPrefixHash::iterator it = prefix_hash_.find(prefix); - if (it == prefix_hash_.end()) { - int prefix_id = stackprefix_array_.size(); - stackprefix_array_.push_back(prefix); - prefix_hash_[prefix] = prefix_id; - return prefix_id; - } else { - return it->second; - } - } - - // prefix id after a stack pop - int PopPrefix(StackPrefix prefix) { - prefix.Pop(); - return PrefixId(prefix); - } - - // prefix id after a stack push - int PushPrefix(StackPrefix prefix, Label fst_id, StateId nextstate) { - prefix.Push(fst_id, nextstate); - return PrefixId(prefix); - } - - - // private data - private: - // runtime options - ReplaceFstOptions opts_; - - // maps from StateId to StateTuple - vector<StateTuple> state_tuples_; - - // hashes from StateTuple to StateId - StateTupleHash state_hash_; - - // cross index of unique stack prefix - // could potentially have one copy of prefix array - StackPrefixHash prefix_hash_; - vector<StackPrefix> stackprefix_array_; - - NonTerminalHash nonterminal_hash_; - vector<const Fst<A>*> fst_array_; - - Label root_; - - void operator=(const ReplaceFstImpl<A> &); // disallow -}; - - -// -// \class ReplaceFst -// \brief Recursivively replaces arcs in the root Fst with other Fsts. -// This version is a delayed Fst. -// -// ReplaceFst supports dynamic replacement of arcs in one Fst with -// another Fst. This replacement is recursive. ReplaceFst can be used -// to support a variety of delayed constructions such as recursive -// transition networks, union, or closure. It is constructed with an -// array of Fst(s). One Fst represents the root (or topology) -// machine. The root Fst refers to other Fsts by recursively replacing -// arcs labeled as non-terminals with the matching non-terminal -// Fst. Currently the ReplaceFst uses the output symbols of the arcs -// to determine whether the arc is a non-terminal arc or not. A -// non-terminal can be any label that is not a non-zero terminal label -// in the output alphabet. -// -// Note that the constructor uses a vector of pair<>. These correspond -// to the tuple of non-terminal Label and corresponding Fst. For example -// to implement the closure operation we need 2 Fsts. The first root -// Fst is a single Arc on the start State that self loops, it references -// the particular machine for which we are performing the closure operation. -// -template <class A> -class ReplaceFst : public Fst<A> { - public: - friend class ArcIterator< ReplaceFst<A> >; - friend class CacheStateIterator< ReplaceFst<A> >; - friend class CacheArcIterator< ReplaceFst<A> >; - - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - ReplaceFst(const vector<pair<Label, const Fst<A>* > >& fst_array, - Label root) - : impl_(new ReplaceFstImpl<A>(fst_array, ReplaceFstOptions(root))) {} - - ReplaceFst(const vector<pair<Label, const Fst<A>* > >& fst_array, - const ReplaceFstOptions &opts) - : impl_(new ReplaceFstImpl<A>(fst_array, opts)) {} - - ReplaceFst(const ReplaceFst<A>& fst) : - impl_(new ReplaceFstImpl<A>(*(fst.impl_))) {} - - virtual ~ReplaceFst() { - delete impl_; - } - - virtual StateId Start() const { - return impl_->Start(); - } - - virtual Weight Final(StateId s) const { - return impl_->Final(s); - } - - virtual size_t NumArcs(StateId s) const { - return impl_->NumArcs(s); - } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { - return impl_->Type(); - } - - virtual ReplaceFst<A>* Copy() const { - return new ReplaceFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - bool CyclicDependencies() const { - return impl_->CyclicDependencies(); - } - - private: - ReplaceFstImpl<A>* impl_; -}; - - -// Specialization for ReplaceFst. -template<class A> -class StateIterator< ReplaceFst<A> > - : public CacheStateIterator< ReplaceFst<A> > { - public: - explicit StateIterator(const ReplaceFst<A> &fst) - : CacheStateIterator< ReplaceFst<A> >(fst) {} - - private: - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - -// Specialization for ReplaceFst. -template <class A> -class ArcIterator< ReplaceFst<A> > - : public CacheArcIterator< ReplaceFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const ReplaceFst<A> &fst, StateId s) - : CacheArcIterator< ReplaceFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -template <class A> inline -void ReplaceFst<A>::InitStateIterator(StateIteratorData<A> *data) const { - data->base = new StateIterator< ReplaceFst<A> >(*this); -} - -typedef ReplaceFst<StdArc> StdReplaceFst; - - -// // Recursivively replaces arcs in the root Fst with other Fsts. -// This version writes the result of replacement to an output MutableFst. -// -// Replace supports replacement of arcs in one Fst with another -// Fst. This replacement is recursive. Replace takes an array of -// Fst(s). One Fst represents the root (or topology) machine. The root -// Fst refers to other Fsts by recursively replacing arcs labeled as -// non-terminals with the matching non-terminal Fst. Currently Replace -// uses the output symbols of the arcs to determine whether the arc is -// a non-terminal arc or not. A non-terminal can be any label that is -// not a non-zero terminal label in the output alphabet. Note that -// input argument is a vector of pair<>. These correspond to the tuple -// of non-terminal Label and corresponding Fst. -template<class Arc> -void Replace(const vector<pair<typename Arc::Label, - const Fst<Arc>* > >& ifst_array, - MutableFst<Arc> *ofst, typename Arc::Label root) { - ReplaceFstOptions opts(root); - opts.gc_limit = 0; // Cache only the last state for fastest copy. - *ofst = ReplaceFst<Arc>(ifst_array, opts); -} - -} - -#endif // FST_LIB_REPLACE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/reverse.h b/tools/thirdparty/OpenFst/fst/lib/reverse.h deleted file mode 100644 index 9819788..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/reverse.h +++ /dev/null @@ -1,82 +0,0 @@ -// reverse.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to sort arcs in an FST. - -#ifndef FST_LIB_REVERSE_H__ -#define FST_LIB_REVERSE_H__ - -#include "fst/lib/cache.h" - -namespace fst { - -// Reverses an FST. The reversed result is written to an output -// MutableFst. If A transduces string x to y with weight a, then the -// reverse of A transduces the reverse of x to the reverse of y with -// weight a.Reverse(). -// -// Typically, a = a.Reverse() and Arc = RevArc (e.g. for -// TropicalWeight or LogWeight). In general, e.g. when the weights -// only form a left or right semiring, the output arc type must match -// the input arc type except having the reversed Weight type. -template<class Arc, class RevArc> -void Reverse(const Fst<Arc> &ifst, MutableFst<RevArc> *ofst) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - typedef typename RevArc::Weight RevWeight; - - ofst->DeleteStates(); - ofst->SetInputSymbols(ifst.InputSymbols()); - ofst->SetOutputSymbols(ifst.OutputSymbols()); - StateId istart = ifst.Start(); - StateId ostart = ofst->AddState(); - ofst->SetStart(ostart); - - for (StateIterator< Fst<Arc> > siter(ifst); - !siter.Done(); - siter.Next()) { - StateId is = siter.Value(); - StateId os = is + 1; - while (ofst->NumStates() <= os) - ofst->AddState(); - if (is == istart) - ofst->SetFinal(os, RevWeight::One()); - - Weight final = ifst.Final(is); - if (final != Weight::Zero()) { - RevArc oarc(0, 0, final.Reverse(), os); - ofst->AddArc(0, oarc); - } - - for (ArcIterator< Fst<Arc> > aiter(ifst, is); - !aiter.Done(); - aiter.Next()) { - const Arc &iarc = aiter.Value(); - RevArc oarc(iarc.ilabel, iarc.olabel, iarc.weight.Reverse(), os); - StateId nos = iarc.nextstate + 1; - while (ofst->NumStates() <= nos) - ofst->AddState(); - ofst->AddArc(nos, oarc); - } - } - uint64 iprops = ifst.Properties(kCopyProperties, false); - uint64 oprops = ofst->Properties(kFstProperties, false); - ofst->SetProperties(ReverseProperties(iprops) | oprops, kFstProperties); -} - -} // namespace fst - -#endif // FST_LIB_REVERSE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/reweight.h b/tools/thirdparty/OpenFst/fst/lib/reweight.h deleted file mode 100644 index 194e377..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/reweight.h +++ /dev/null @@ -1,128 +0,0 @@ -// reweight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Function to reweight an FST. - -#ifndef FST_LIB_REWEIGHT_H__ -#define FST_LIB_REWEIGHT_H__ - -#include "fst/lib/mutable-fst.h" - -namespace fst { - -enum ReweightType { REWEIGHT_TO_INITIAL, REWEIGHT_TO_FINAL }; - -// Reweight FST according to the potentials defined by the POTENTIAL -// vector in the direction defined by TYPE. Weight needs to be left -// distributive when reweighting towards the initial state and right -// distributive when reweighting towards the final states. -// -// An arc of weight w, with an origin state of potential p and -// destination state of potential q, is reweighted by p\wq when -// reweighting towards the initial state and by pw/q when reweighting -// towards the final states. -template <class Arc> -void Reweight(MutableFst<Arc> *fst, vector<typename Arc::Weight> potential, - ReweightType type) { - typedef typename Arc::Weight Weight; - - if (!fst->NumStates()) - return; - while ( (int64)potential.size() < (int64)fst->NumStates()) - potential.push_back(Weight::Zero()); - - if (type == REWEIGHT_TO_FINAL && !(Weight::Properties() & kRightSemiring)) - LOG(FATAL) << "Reweight: Reweighting to the final states requires " - << "Weight to be right distributive: " - << Weight::Type(); - - if (type == REWEIGHT_TO_INITIAL && !(Weight::Properties() & kLeftSemiring)) - LOG(FATAL) << "Reweight: Reweighting to the initial state requires " - << "Weight to be left distributive: " - << Weight::Type(); - - for (StateIterator< MutableFst<Arc> > sit(*fst); - !sit.Done(); - sit.Next()) { - typename Arc::StateId state = sit.Value(); - for (MutableArcIterator< MutableFst<Arc> > ait(fst, state); - !ait.Done(); - ait.Next()) { - Arc arc = ait.Value(); - if ((potential[state] == Weight::Zero()) || - (potential[arc.nextstate] == Weight::Zero())) - continue; //temp fix: needs to find best solution for zeros - if ((type == REWEIGHT_TO_INITIAL) - && (potential[state] != Weight::Zero())) - arc.weight = Divide(Times(arc.weight, potential[arc.nextstate]), - potential[state], DIVIDE_LEFT); - else if ((type == REWEIGHT_TO_FINAL) - && (potential[arc.nextstate] != Weight::Zero())) - arc.weight = Divide(Times(potential[state], arc.weight), - potential[arc.nextstate], DIVIDE_RIGHT); - ait.SetValue(arc); - } - if ((type == REWEIGHT_TO_INITIAL) - && (potential[state] != Weight::Zero())) - fst->SetFinal(state, - Divide(fst->Final(state), potential[state], DIVIDE_LEFT)); - else if (type == REWEIGHT_TO_FINAL) - fst->SetFinal(state, Times(potential[state], fst->Final(state))); - } - - if ((potential[fst->Start()] != Weight::One()) && - (potential[fst->Start()] != Weight::Zero())) { - if (fst->Properties(kInitialAcyclic, true) & kInitialAcyclic) { - typename Arc::StateId state = fst->Start(); - for (MutableArcIterator< MutableFst<Arc> > ait(fst, state); - !ait.Done(); - ait.Next()) { - Arc arc = ait.Value(); - if (type == REWEIGHT_TO_INITIAL) - arc.weight = Times(potential[state], arc.weight); - else - arc.weight = Times( - Divide(Weight::One(), potential[state], DIVIDE_RIGHT), - arc.weight); - ait.SetValue(arc); - } - if (type == REWEIGHT_TO_INITIAL) - fst->SetFinal(state, Times(potential[state], fst->Final(state))); - else - fst->SetFinal(state, Times(Divide(Weight::One(), potential[state], - DIVIDE_RIGHT), - fst->Final(state))); - } - else { - typename Arc::StateId state = fst->AddState(); - Weight w = type == REWEIGHT_TO_INITIAL ? - potential[fst->Start()] : - Divide(Weight::One(), potential[fst->Start()], DIVIDE_RIGHT); - Arc arc (0, 0, w, fst->Start()); - fst->AddArc(state, arc); - fst->SetStart(state); - } - } - - fst->SetProperties(ReweightProperties( - fst->Properties(kFstProperties, false)), - kFstProperties); -} - -} // namespace fst - -#endif /* FST_LIB_REWEIGHT_H_ */ diff --git a/tools/thirdparty/OpenFst/fst/lib/rmepsilon.h b/tools/thirdparty/OpenFst/fst/lib/rmepsilon.h deleted file mode 100644 index 69a641b..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/rmepsilon.h +++ /dev/null @@ -1,542 +0,0 @@ -// rmepsilon.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Functions and classes that implemement epsilon-removal. - -#ifndef FST_LIB_RMEPSILON_H__ -#define FST_LIB_RMEPSILON_H__ - -#include <ext/hash_map> -using __gnu_cxx::hash_map; -#include <ext/slist> -using __gnu_cxx::slist; - -#include "fst/lib/arcfilter.h" -#include "fst/lib/cache.h" -#include "fst/lib/connect.h" -#include "fst/lib/factor-weight.h" -#include "fst/lib/invert.h" -#include "fst/lib/map.h" -#include "fst/lib/queue.h" -#include "fst/lib/shortest-distance.h" -#include "fst/lib/topsort.h" - -namespace fst { - -template <class Arc, class Queue> -struct RmEpsilonOptions - : public ShortestDistanceOptions<Arc, Queue, EpsilonArcFilter<Arc> > { - typedef typename Arc::StateId StateId; - - bool connect; // Connect output - - RmEpsilonOptions(Queue *q, float d = kDelta, bool c = true) - : ShortestDistanceOptions<Arc, Queue, EpsilonArcFilter<Arc> >( - q, EpsilonArcFilter<Arc>(), kNoStateId, d), connect(c) {} - -}; - - -// Computation state of the epsilon-removal algorithm. -template <class Arc, class Queue> -class RmEpsilonState { - public: - typedef typename Arc::Label Label; - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - - RmEpsilonState(const Fst<Arc> &fst, - vector<Weight> *distance, - const RmEpsilonOptions<Arc, Queue> &opts) - : fst_(fst), distance_(distance), sd_state_(fst_, distance, opts, true) { - } - - // Compute arcs and final weight for state 's' - void Expand(StateId s); - - // Returns arcs of expanded state. - vector<Arc> &Arcs() { return arcs_; } - - // Returns final weight of expanded state. - const Weight &Final() const { return final_; } - - private: - struct Element { - Label ilabel; - Label olabel; - StateId nextstate; - - Element() {} - - Element(Label i, Label o, StateId s) - : ilabel(i), olabel(o), nextstate(s) {} - }; - - class ElementKey { - public: - size_t operator()(const Element& e) const { - return static_cast<size_t>(e.nextstate); - return static_cast<size_t>(e.nextstate + - e.ilabel * kPrime0 + - e.olabel * kPrime1); - } - - private: - static const int kPrime0 = 7853; - static const int kPrime1 = 7867; - }; - - class ElementEqual { - public: - bool operator()(const Element &e1, const Element &e2) const { - return (e1.ilabel == e2.ilabel) && (e1.olabel == e2.olabel) - && (e1.nextstate == e2.nextstate); - } - }; - - private: - typedef hash_map<Element, pair<StateId, ssize_t>, - ElementKey, ElementEqual> ElementMap; - - const Fst<Arc> &fst_; - // Distance from state being expanded in epsilon-closure. - vector<Weight> *distance_; - // Shortest distance algorithm computation state. - ShortestDistanceState<Arc, Queue, EpsilonArcFilter<Arc> > sd_state_; - // Maps an element 'e' to a pair 'p' corresponding to a position - // in the arcs vector of the state being expanded. 'e' corresponds - // to the position 'p.second' in the 'arcs_' vector if 'p.first' is - // equal to the state being expanded. - ElementMap element_map_; - EpsilonArcFilter<Arc> eps_filter_; - stack<StateId> eps_queue_; // Queue used to visit the epsilon-closure - vector<bool> visited_; // '[i] = true' if state 'i' has been visited - slist<StateId> visited_states_; // List of visited states - vector<Arc> arcs_; // Arcs of state being expanded - Weight final_; // Final weight of state being expanded - - void operator=(const RmEpsilonState); // Disallow -}; - - -template <class Arc, class Queue> -void RmEpsilonState<Arc,Queue>::Expand(typename Arc::StateId source) { - sd_state_.ShortestDistance(source); - eps_queue_.push(source); - final_ = Weight::Zero(); - arcs_.clear(); - - while (!eps_queue_.empty()) { - StateId state = eps_queue_.top(); - eps_queue_.pop(); - - while ((StateId)visited_.size() <= state) visited_.push_back(false); - visited_[state] = true; - visited_states_.push_front(state); - - for (ArcIterator< Fst<Arc> > ait(fst_, state); - !ait.Done(); - ait.Next()) { - Arc arc = ait.Value(); - arc.weight = Times((*distance_)[state], arc.weight); - - if (eps_filter_(arc)) { - while ((StateId)visited_.size() <= arc.nextstate) - visited_.push_back(false); - if (!visited_[arc.nextstate]) - eps_queue_.push(arc.nextstate); - } else { - Element element(arc.ilabel, arc.olabel, arc.nextstate); - typename ElementMap::iterator it = element_map_.find(element); - if (it == element_map_.end()) { - element_map_.insert( - pair<Element, pair<StateId, ssize_t> > - (element, pair<StateId, ssize_t>(source, arcs_.size()))); - arcs_.push_back(arc); - } else { - if (((*it).second).first == source) { - Weight &w = arcs_[((*it).second).second].weight; - w = Plus(w, arc.weight); - } else { - ((*it).second).first = source; - ((*it).second).second = arcs_.size(); - arcs_.push_back(arc); - } - } - } - } - final_ = Plus(final_, Times((*distance_)[state], fst_.Final(state))); - } - - while (!visited_states_.empty()) { - visited_[visited_states_.front()] = false; - visited_states_.pop_front(); - } -} - - -// Removes epsilon-transitions (when both the input and output label -// are an epsilon) from a transducer. The result will be an equivalent -// FST that has no such epsilon transitions. This version modifies -// its input. It allows fine control via the options argument; see -// below for a simpler interface. -// -// The vector 'distance' will be used to hold the shortest distances -// during the epsilon-closure computation. The state queue discipline -// and convergence delta are taken in the options argument. -template <class Arc, class Queue> -void RmEpsilon(MutableFst<Arc> *fst, - vector<typename Arc::Weight> *distance, - const RmEpsilonOptions<Arc, Queue> &opts) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - typedef typename Arc::Label Label; - - // States sorted in topological order when (acyclic) or generic - // topological order (cyclic). - vector<StateId> states; - - if (fst->Properties(kTopSorted, false) & kTopSorted) { - for (StateId i = 0; i < (StateId)fst->NumStates(); i++) - states.push_back(i); - } else if (fst->Properties(kAcyclic, false) & kAcyclic) { - vector<StateId> order; - bool acyclic; - TopOrderVisitor<Arc> top_order_visitor(&order, &acyclic); - DfsVisit(*fst, &top_order_visitor, EpsilonArcFilter<Arc>()); - if (!acyclic) - LOG(FATAL) << "RmEpsilon: not acyclic though property bit is set"; - states.resize(order.size()); - for (StateId i = 0; i < (StateId)order.size(); i++) - states[order[i]] = i; - } else { - uint64 props; - vector<StateId> scc; - SccVisitor<Arc> scc_visitor(&scc, 0, 0, &props); - DfsVisit(*fst, &scc_visitor, EpsilonArcFilter<Arc>()); - vector<StateId> first(scc.size(), kNoStateId); - vector<StateId> next(scc.size(), kNoStateId); - for (StateId i = 0; i < (StateId)scc.size(); i++) { - if (first[scc[i]] != kNoStateId) - next[i] = first[scc[i]]; - first[scc[i]] = i; - } - for (StateId i = 0; i < (StateId)first.size(); i++) - for (StateId j = first[i]; j != kNoStateId; j = next[j]) - states.push_back(j); - } - - RmEpsilonState<Arc, Queue> - rmeps_state(*fst, distance, opts); - - while (!states.empty()) { - StateId state = states.back(); - states.pop_back(); - rmeps_state.Expand(state); - fst->SetFinal(state, rmeps_state.Final()); - fst->DeleteArcs(state); - vector<Arc> &arcs = rmeps_state.Arcs(); - while (!arcs.empty()) { - fst->AddArc(state, arcs.back()); - arcs.pop_back(); - } - } - - fst->SetProperties(RmEpsilonProperties( - fst->Properties(kFstProperties, false)), - kFstProperties); - - if (opts.connect) - Connect(fst); -} - - -// Removes epsilon-transitions (when both the input and output label -// are an epsilon) from a transducer. The result will be an equivalent -// FST that has no such epsilon transitions. This version modifies its -// input. It has a simplified interface; see above for a version that -// allows finer control. -// -// Complexity: -// - Time: -// - Unweighted: O(V2 + V E) -// - Acyclic: O(V2 + V E) -// - Tropical semiring: O(V2 log V + V E) -// - General: exponential -// - Space: O(V E) -// where V = # of states visited, E = # of arcs. -// -// References: -// - Mehryar Mohri. Generic Epsilon-Removal and Input -// Epsilon-Normalization Algorithms for Weighted Transducers, -// "International Journal of Computer Science", 13(1):129-143 (2002). -template <class Arc> -void RmEpsilon(MutableFst<Arc> *fst, bool connect = true) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - typedef typename Arc::Label Label; - - vector<Weight> distance; - AutoQueue<StateId> state_queue(*fst, &distance, EpsilonArcFilter<Arc>()); - RmEpsilonOptions<Arc, AutoQueue<StateId> > - opts(&state_queue, kDelta, connect); - - RmEpsilon(fst, &distance, opts); -} - - -struct RmEpsilonFstOptions : CacheOptions { - float delta; - - RmEpsilonFstOptions(const CacheOptions &opts, float delta = kDelta) - : CacheOptions(opts), delta(delta) {} - - explicit RmEpsilonFstOptions(float delta = kDelta) : delta(delta) {} -}; - - -// Implementation of delayed RmEpsilonFst. -template <class A> -class RmEpsilonFstImpl : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - using CacheBaseImpl< CacheState<A> >::HasStart; - using CacheBaseImpl< CacheState<A> >::HasFinal; - using CacheBaseImpl< CacheState<A> >::HasArcs; - - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - RmEpsilonFstImpl(const Fst<A>& fst, const RmEpsilonFstOptions &opts) - : CacheImpl<A>(opts), - fst_(fst.Copy()), - rmeps_state_( - *fst_, - &distance_, - RmEpsilonOptions<A, FifoQueue<StateId> >(&queue_, opts.delta, - false) - ) { - SetType("rmepsilon"); - uint64 props = fst.Properties(kFstProperties, false); - SetProperties(RmEpsilonProperties(props, true), kCopyProperties); - } - - ~RmEpsilonFstImpl() { - delete fst_; - } - - StateId Start() { - if (!HasStart()) { - SetStart(fst_->Start()); - } - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - Expand(s); - } - return CacheImpl<A>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - void Expand(StateId s) { - rmeps_state_.Expand(s); - SetFinal(s, rmeps_state_.Final()); - vector<A> &arcs = rmeps_state_.Arcs(); - while (!arcs.empty()) { - AddArc(s, arcs.back()); - arcs.pop_back(); - } - SetArcs(s); - } - - private: - const Fst<A> *fst_; - vector<Weight> distance_; - FifoQueue<StateId> queue_; - RmEpsilonState<A, FifoQueue<StateId> > rmeps_state_; - - DISALLOW_EVIL_CONSTRUCTORS(RmEpsilonFstImpl); -}; - - -// Removes epsilon-transitions (when both the input and output label -// are an epsilon) from a transducer. The result will be an equivalent -// FST that has no such epsilon transitions. This version is a -// delayed Fst. -// -// Complexity: -// - Time: -// - Unweighted: O(v^2 + v e) -// - General: exponential -// - Space: O(v e) -// where v = # of states visited, e = # of arcs visited. Constant time -// to visit an input state or arc is assumed and exclusive of caching. -// -// References: -// - Mehryar Mohri. Generic Epsilon-Removal and Input -// Epsilon-Normalization Algorithms for Weighted Transducers, -// "International Journal of Computer Science", 13(1):129-143 (2002). -template <class A> -class RmEpsilonFst : public Fst<A> { - public: - friend class ArcIterator< RmEpsilonFst<A> >; - friend class CacheStateIterator< RmEpsilonFst<A> >; - friend class CacheArcIterator< RmEpsilonFst<A> >; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - RmEpsilonFst(const Fst<A> &fst) - : impl_(new RmEpsilonFstImpl<A>(fst, RmEpsilonFstOptions())) {} - - RmEpsilonFst(const Fst<A> &fst, const RmEpsilonFstOptions &opts) - : impl_(new RmEpsilonFstImpl<A>(fst, opts)) {} - - explicit RmEpsilonFst(const RmEpsilonFst<A> &fst) : impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~RmEpsilonFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual RmEpsilonFst<A> *Copy() const { - return new RmEpsilonFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - protected: - RmEpsilonFstImpl<A> *Impl() { return impl_; } - - private: - RmEpsilonFstImpl<A> *impl_; - - void operator=(const RmEpsilonFst<A> &fst); // disallow -}; - - -// Specialization for RmEpsilonFst. -template<class A> -class StateIterator< RmEpsilonFst<A> > - : public CacheStateIterator< RmEpsilonFst<A> > { - public: - explicit StateIterator(const RmEpsilonFst<A> &fst) - : CacheStateIterator< RmEpsilonFst<A> >(fst) {} -}; - - -// Specialization for RmEpsilonFst. -template <class A> -class ArcIterator< RmEpsilonFst<A> > - : public CacheArcIterator< RmEpsilonFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const RmEpsilonFst<A> &fst, StateId s) - : CacheArcIterator< RmEpsilonFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - - -template <class A> inline -void RmEpsilonFst<A>::InitStateIterator(StateIteratorData<A> *data) const { - data->base = new StateIterator< RmEpsilonFst<A> >(*this); -} - - -// Useful alias when using StdArc. -typedef RmEpsilonFst<StdArc> StdRmEpsilonFst; - -} // namespace fst - -#endif // FST_LIB_RMEPSILON_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/rmfinalepsilon.h b/tools/thirdparty/OpenFst/fst/lib/rmfinalepsilon.h deleted file mode 100644 index 900f0bb..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/rmfinalepsilon.h +++ /dev/null @@ -1,101 +0,0 @@ -// rmfinalepsilon.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function to remove of final states that have epsilon only input arcs. - -#ifndef FST_LIB_RMFINALEPSILON_H__ -#define FST_LIB_RMFINALEPSILON_H__ - -#include <ext/hash_set> -using __gnu_cxx::hash_set; - -#include "fst/lib/connect.h" -#include "fst/lib/mutable-fst.h" - -namespace fst { - -template<class A> -void RmFinalEpsilon(MutableFst<A>* fst) { - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - // Determine the coaccesibility of states. - vector<bool> access; - vector<bool> coaccess; - uint64 props = 0; - SccVisitor<A> scc_visitor(0, &access, &coaccess, &props); - DfsVisit(*fst, &scc_visitor); - - // Find potential list of removable final states. These are final states - // that have no outgoing transitions or final states that have a - // non-coaccessible future. Complexity O(S) - hash_set<StateId> finals; - for (StateIterator<Fst<A> > siter(*fst); !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - if (fst->Final(s) != Weight::Zero()) { - bool future_coaccess = false; - for (ArcIterator<Fst<A> > aiter(*fst, s); !aiter.Done(); aiter.Next()) { - const A& arc = aiter.Value(); - if (coaccess[arc.nextstate]) { - future_coaccess = true; - break; - } - } - if (!future_coaccess) { - finals.insert(s); - } - } - } - - // Move the final weight. Complexity O(E) - vector<A> arcs; - for (StateIterator<Fst<A> > siter(*fst); !siter.Done(); siter.Next()) { - StateId s = siter.Value(); - Weight w(fst->Final(s)); - - arcs.clear(); - for (ArcIterator<Fst<A> > aiter(*fst, s); !aiter.Done(); aiter.Next()) { - const A& arc = aiter.Value(); - // is next state in the list of finals - if (finals.find(arc.nextstate) != finals.end()) { - // sum up all epsilon arcs - if (arc.ilabel == 0 && arc.olabel == 0) { - w = Plus(Times(fst->Final(arc.nextstate), arc.weight), w); - } else { - arcs.push_back(arc); - } - } else { - arcs.push_back(arc); - } - } - - // If some arcs (epsilon arcs) were deleted, delete all - // arcs and add back only the non epsilon arcs - if (arcs.size() < fst->NumArcs(s)) { - fst->DeleteArcs(s); - fst->SetFinal(s, w); - for (size_t i = 0; i < arcs.size(); ++i) { - fst->AddArc(s, arcs[i]); - } - } - } - - Connect(fst); -} - -} - -#endif // FST_LIB_RMFINALEPSILON_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/shortest-distance.h b/tools/thirdparty/OpenFst/fst/lib/shortest-distance.h deleted file mode 100644 index 44c37c9..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/shortest-distance.h +++ /dev/null @@ -1,262 +0,0 @@ -// shortest-distance.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Functions and classes to find shortest distance in an FST. - -#ifndef FST_LIB_SHORTEST_DISTANCE_H__ -#define FST_LIB_SHORTEST_DISTANCE_H__ - -#include <deque> - -#include "fst/lib/arcfilter.h" -#include "fst/lib/cache.h" -#include "fst/lib/queue.h" -#include "fst/lib/reverse.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -template <class Arc, class Queue, class ArcFilter> -struct ShortestDistanceOptions { - typedef typename Arc::StateId StateId; - - Queue *state_queue; // Queue discipline used; owned by caller - ArcFilter arc_filter; // Arc filter (e.g., limit to only epsilon graph) - StateId source; // If kNoStateId, use the Fst's initial state - float delta; // Determines the degree of convergence required - - ShortestDistanceOptions(Queue *q, ArcFilter filt, StateId src = kNoStateId, - float d = kDelta) - : state_queue(q), arc_filter(filt), source(src), delta(d) {} -}; - - -// Computation state of the shortest-distance algorithm. Reusable -// information is maintained across calls to member function -// ShortestDistance(source) when 'retain' is true for improved -// efficiency when calling multiple times from different source states -// (e.g., in epsilon removal). Vector 'distance' should not be -// modified by the user between these calls. -template<class Arc, class Queue, class ArcFilter> -class ShortestDistanceState { - public: - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - - ShortestDistanceState( - const Fst<Arc> &fst, - vector<Weight> *distance, - const ShortestDistanceOptions<Arc, Queue, ArcFilter> &opts, - bool retain) - : fst_(fst.Copy()), distance_(distance), state_queue_(opts.state_queue), - arc_filter_(opts.arc_filter), - delta_(opts.delta), retain_(retain) { - distance_->clear(); - } - - ~ShortestDistanceState() { - delete fst_; - } - - void ShortestDistance(StateId source); - - private: - const Fst<Arc> *fst_; - vector<Weight> *distance_; - Queue *state_queue_; - ArcFilter arc_filter_; - float delta_; - bool retain_; // Retain and reuse information across calls - - vector<Weight> rdistance_; // Relaxation distance. - vector<bool> enqueued_; // Is state enqueued? - vector<StateId> sources_; // Source state for ith state in 'distance_', - // 'rdistance_', and 'enqueued_' if retained. -}; - -// Compute the shortest distance. If 'source' is kNoStateId, use -// the initial state of the Fst. -template <class Arc, class Queue, class ArcFilter> -void ShortestDistanceState<Arc, Queue, ArcFilter>::ShortestDistance( - StateId source) { - if (fst_->Start() == kNoStateId) - return; - - if (!(Weight::Properties() & kRightSemiring)) - LOG(FATAL) << "ShortestDistance: Weight needs to be right distributive: " - << Weight::Type(); - - state_queue_->Clear(); - - if (!retain_) { - distance_->clear(); - rdistance_.clear(); - enqueued_.clear(); - } - - if (source == kNoStateId) - source = fst_->Start(); - - while ((StateId)distance_->size() <= source) { - distance_->push_back(Weight::Zero()); - rdistance_.push_back(Weight::Zero()); - enqueued_.push_back(false); - } - if (retain_) { - while ((StateId)sources_.size() <= source) - sources_.push_back(kNoStateId); - sources_[source] = source; - } - (*distance_)[source] = Weight::One(); - rdistance_[source] = Weight::One(); - enqueued_[source] = true; - - state_queue_->Enqueue(source); - - while (!state_queue_->Empty()) { - StateId s = state_queue_->Head(); - state_queue_->Dequeue(); - while ((StateId)distance_->size() <= s) { - distance_->push_back(Weight::Zero()); - rdistance_.push_back(Weight::Zero()); - enqueued_.push_back(false); - } - enqueued_[s] = false; - Weight r = rdistance_[s]; - rdistance_[s] = Weight::Zero(); - for (ArcIterator< Fst<Arc> > aiter(*fst_, s); - !aiter.Done(); - aiter.Next()) { - const Arc &arc = aiter.Value(); - if (!arc_filter_(arc) || arc.weight == Weight::Zero()) - continue; - while ((StateId)distance_->size() <= arc.nextstate) { - distance_->push_back(Weight::Zero()); - rdistance_.push_back(Weight::Zero()); - enqueued_.push_back(false); - } - if (retain_) { - while ((StateId)sources_.size() <= arc.nextstate) - sources_.push_back(kNoStateId); - if (sources_[arc.nextstate] != source) { - (*distance_)[arc.nextstate] = Weight::Zero(); - rdistance_[arc.nextstate] = Weight::Zero(); - enqueued_[arc.nextstate] = false; - sources_[arc.nextstate] = source; - } - } - Weight &nd = (*distance_)[arc.nextstate]; - Weight &nr = rdistance_[arc.nextstate]; - Weight w = Times(r, arc.weight); - if (!ApproxEqual(nd, Plus(nd, w), delta_)) { - nd = Plus(nd, w); - nr = Plus(nr, w); - if (!enqueued_[arc.nextstate]) { - state_queue_->Enqueue(arc.nextstate); - enqueued_[arc.nextstate] = true; - } else { - state_queue_->Update(arc.nextstate); - } - } - } - } -} - - -// Shortest-distance algorithm: this version allows fine control -// via the options argument. See below for a simpler interface. -// -// This computes the shortest distance from the 'opts.source' state to -// each visited state S and stores the value in the 'distance' vector. -// An unvisited state S has distance Zero(), which will be stored in -// the 'distance' vector if S is less than the maximum visited state. -// The state queue discipline, arc filter, and convergence delta are -// taken in the options argument. - -// The weights must must be right distributive and k-closed (i.e., 1 + -// x + x^2 + ... + x^(k +1) = 1 + x + x^2 + ... + x^k). -// -// The algorithm is from Mohri, "Semiring Framweork and Algorithms for -// Shortest-Distance Problems", Journal of Automata, Languages and -// Combinatorics 7(3):321-350, 2002. The complexity of algorithm -// depends on the properties of the semiring and the queue discipline -// used. Refer to the paper for more details. -template<class Arc, class Queue, class ArcFilter> -void ShortestDistance( - const Fst<Arc> &fst, - vector<typename Arc::Weight> *distance, - const ShortestDistanceOptions<Arc, Queue, ArcFilter> &opts) { - - ShortestDistanceState<Arc, Queue, ArcFilter> - sd_state(fst, distance, opts, false); - sd_state.ShortestDistance(opts.source); -} - -// Shortest-distance algorithm: simplified interface. See above for a -// version that allows finer control. -// -// If 'reverse' is false, this computes the shortest distance from the -// initial state to each state S and stores the value in the -// 'distance' vector. If 'reverse' is true, this computes the shortest -// distance from each state to the final states. An unvisited state S -// has distance Zero(), which will be stored in the 'distance' vector -// if S is less than the maximum visited state. The state queue -// discipline is automatically-selected. -// -// The weights must must be right (left) distributive if reverse is -// false (true) and k-closed (i.e., 1 + x + x^2 + ... + x^(k +1) = 1 + -// x + x^2 + ... + x^k). -// -// The algorithm is from Mohri, "Semiring Framweork and Algorithms for -// Shortest-Distance Problems", Journal of Automata, Languages and -// Combinatorics 7(3):321-350, 2002. The complexity of algorithm -// depends on the properties of the semiring and the queue discipline -// used. Refer to the paper for more details. -template<class Arc> -void ShortestDistance(const Fst<Arc> &fst, - vector<typename Arc::Weight> *distance, - bool reverse = false) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - - if (!reverse) { - AnyArcFilter<Arc> arc_filter; - AutoQueue<StateId> state_queue(fst, distance, arc_filter); - ShortestDistanceOptions< Arc, AutoQueue<StateId>, AnyArcFilter<Arc> > - opts(&state_queue, arc_filter); - ShortestDistance(fst, distance, opts); - } else { - typedef ReverseArc<Arc> ReverseArc; - typedef typename ReverseArc::Weight ReverseWeight; - AnyArcFilter<ReverseArc> rarc_filter; - VectorFst<ReverseArc> rfst; - Reverse(fst, &rfst); - vector<ReverseWeight> rdistance; - AutoQueue<StateId> state_queue(rfst, &rdistance, rarc_filter); - ShortestDistanceOptions< ReverseArc, AutoQueue<StateId>, - AnyArcFilter<ReverseArc> > - ropts(&state_queue, rarc_filter); - ShortestDistance(rfst, &rdistance, ropts); - distance->clear(); - while (distance->size() < rdistance.size() - 1) - distance->push_back(rdistance[distance->size() + 1].Reverse()); - } -} - -} // namespace fst - -#endif // FST_LIB_SHORTEST_DISTANCE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/shortest-path.h b/tools/thirdparty/OpenFst/fst/lib/shortest-path.h deleted file mode 100644 index 412869a..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/shortest-path.h +++ /dev/null @@ -1,363 +0,0 @@ -// shortest-path.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Functions to find shortest paths in an FST. - -#ifndef FST_LIB_SHORTEST_PATH_H__ -#define FST_LIB_SHORTEST_PATH_H__ - -#include <functional> - -#include "fst/lib/cache.h" -#include "fst/lib/queue.h" -#include "fst/lib/shortest-distance.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -template <class Arc, class Queue, class ArcFilter> -struct ShortestPathOptions - : public ShortestDistanceOptions<Arc, Queue, ArcFilter> { - typedef typename Arc::StateId StateId; - - size_t nshortest; // return n-shortest paths - bool unique; // only return paths with distinct input strings - bool has_distance; // distance vector already contains the - // shortest distance from the initial state - - ShortestPathOptions(Queue *q, ArcFilter filt, size_t n = 1, bool u = false, - bool hasdist = false, float d = kDelta) - : ShortestDistanceOptions<Arc, Queue, ArcFilter>(q, filt, kNoStateId, d), - nshortest(n), unique(u), has_distance(hasdist) {} -}; - - -// Shortest-path algorithm: normally not called directly; prefer -// 'ShortestPath' below with n=1. 'ofst' contains the shortest path in -// 'ifst'. 'distance' returns the shortest distances from the source -// state to each state in 'ifst'. 'opts' is used to specify options -// such as the queue discipline, the arc filter and delta. -// -// The shortest path is the lowest weight path w.r.t. the natural -// semiring order. -// -// The weights need to be right distributive and have the path (kPath) -// property. -template<class Arc, class Queue, class ArcFilter> -void SingleShortestPath(const Fst<Arc> &ifst, - MutableFst<Arc> *ofst, - vector<typename Arc::Weight> *distance, - ShortestPathOptions<Arc, Queue, ArcFilter> &opts) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - - ofst->DeleteStates(); - ofst->SetInputSymbols(ifst.InputSymbols()); - ofst->SetOutputSymbols(ifst.OutputSymbols()); - - if (ifst.Start() == kNoStateId) - return; - - vector<Weight> rdistance; - vector<bool> enqueued; - vector<StateId> parent; - vector<Arc> arc_parent; - - Queue *state_queue = opts.state_queue; - StateId source = opts.source == kNoStateId ? ifst.Start() : opts.source; - Weight f_distance = Weight::Zero(); - StateId f_parent = kNoStateId; - - distance->clear(); - state_queue->Clear(); - if (opts.nshortest != 1) - LOG(FATAL) << "SingleShortestPath: for nshortest > 1, use ShortestPath" - << " instead"; - if ((Weight::Properties() & (kPath | kRightSemiring)) - != (kPath | kRightSemiring)) - LOG(FATAL) << "SingleShortestPath: Weight needs to have the path" - << " property and be right distributive: " << Weight::Type(); - - while (distance->size() < source) { - distance->push_back(Weight::Zero()); - enqueued.push_back(false); - parent.push_back(kNoStateId); - arc_parent.push_back(Arc(kNoLabel, kNoLabel, Weight::Zero(), kNoStateId)); - } - distance->push_back(Weight::One()); - parent.push_back(kNoStateId); - arc_parent.push_back(Arc(kNoLabel, kNoLabel, Weight::Zero(), kNoStateId)); - state_queue->Enqueue(source); - enqueued.push_back(true); - - while (!state_queue->Empty()) { - StateId s = state_queue->Head(); - state_queue->Dequeue(); - enqueued[s] = false; - Weight sd = (*distance)[s]; - for (ArcIterator< Fst<Arc> > aiter(ifst, s); - !aiter.Done(); - aiter.Next()) { - const Arc &arc = aiter.Value(); - while (distance->size() <= arc.nextstate) { - distance->push_back(Weight::Zero()); - enqueued.push_back(false); - parent.push_back(kNoStateId); - arc_parent.push_back(Arc(kNoLabel, kNoLabel, Weight::Zero(), - kNoStateId)); - } - Weight &nd = (*distance)[arc.nextstate]; - Weight w = Times(sd, arc.weight); - if (nd != Plus(nd, w)) { - nd = Plus(nd, w); - parent[arc.nextstate] = s; - arc_parent[arc.nextstate] = arc; - if (!enqueued[arc.nextstate]) { - state_queue->Enqueue(arc.nextstate); - enqueued[arc.nextstate] = true; - } else { - state_queue->Update(arc.nextstate); - } - } - } - if (ifst.Final(s) != Weight::Zero()) { - Weight w = Times(sd, ifst.Final(s)); - if (f_distance != Plus(f_distance, w)) { - f_distance = Plus(f_distance, w); - f_parent = s; - } - } - } - (*distance)[source] = Weight::One(); - parent[source] = kNoStateId; - - StateId s_p = kNoStateId, d_p = kNoStateId; - for (StateId s = f_parent, d = kNoStateId; - s != kNoStateId; - d = s, s = parent[s]) { - enqueued[s] = true; - d_p = s_p; - s_p = ofst->AddState(); - if (d == kNoStateId) { - ofst->SetFinal(s_p, ifst.Final(f_parent)); - } else { - arc_parent[d].nextstate = d_p; - ofst->AddArc(s_p, arc_parent[d]); - } - } - ofst->SetStart(s_p); -} - - -template <class S, class W> -class ShortestPathCompare { - public: - typedef S StateId; - typedef W Weight; - typedef pair<StateId, Weight> Pair; - - ShortestPathCompare(const vector<Pair>& pairs, - const vector<Weight>& distance, - StateId sfinal, float d) - : pairs_(pairs), distance_(distance), superfinal_(sfinal), delta_(d) {} - - bool operator()(const StateId x, const StateId y) const { - const Pair &px = pairs_[x]; - const Pair &py = pairs_[y]; - Weight wx = Times(distance_[px.first], px.second); - Weight wy = Times(distance_[py.first], py.second); - // Penalize complete paths to ensure correct results with inexact weights. - // This forms a strict weak order so long as ApproxEqual(a, b) => - // ApproxEqual(a, c) for all c s.t. less_(a, c) && less_(c, b). - if (px.first == superfinal_ && py.first != superfinal_) { - return less_(wy, wx) || ApproxEqual(wx, wy, delta_); - } else if (py.first == superfinal_ && px.first != superfinal_) { - return less_(wy, wx) && !ApproxEqual(wx, wy, delta_); - } else { - return less_(wy, wx); - } - } - - private: - const vector<Pair> &pairs_; - const vector<Weight> &distance_; - StateId superfinal_; - float delta_; - NaturalLess<Weight> less_; -}; - - -// N-Shortest-path algorithm: this version allow fine control -// via the otpions argument. See below for a simpler interface. -// -// 'ofst' contains the n-shortest paths in 'ifst'. 'distance' returns -// the shortest distances from the source state to each state in -// 'ifst'. 'opts' is used to specify options such as the number of -// paths to return, whether they need to have distinct input -// strings, the queue discipline, the arc filter and the convergence -// delta. -// -// The n-shortest paths are the n-lowest weight paths w.r.t. the -// natural semiring order. The single path that can be -// read from the ith of at most n transitions leaving the initial -// state of 'ofst' is the ith shortest path. - -// The weights need to be right distributive and have the path (kPath) -// property. They need to be left distributive as well for nshortest -// > 1. -// -// The algorithm is from Mohri and Riley, "An Efficient Algorithm for -// the n-best-strings problem", ICSLP 2002. The algorithm relies on -// the shortest-distance algorithm. There are some issues with the -// pseudo-code as written in the paper (viz., line 11). -template<class Arc, class Queue, class ArcFilter> -void ShortestPath(const Fst<Arc> &ifst, MutableFst<Arc> *ofst, - vector<typename Arc::Weight> *distance, - ShortestPathOptions<Arc, Queue, ArcFilter> &opts) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - typedef pair<StateId, Weight> Pair; - typedef ReverseArc<Arc> ReverseArc; - typedef typename ReverseArc::Weight ReverseWeight; - - size_t n = opts.nshortest; - - if (n == 1) { - SingleShortestPath(ifst, ofst, distance, opts); - return; - } - ofst->DeleteStates(); - ofst->SetInputSymbols(ifst.InputSymbols()); - ofst->SetOutputSymbols(ifst.OutputSymbols()); - if (n <= 0) return; - if ((Weight::Properties() & (kPath | kSemiring)) != (kPath | kSemiring)) - LOG(FATAL) << "ShortestPath: n-shortest: Weight needs to have the " - << "path property and be distributive: " - << Weight::Type(); - if (opts.unique) - LOG(FATAL) << "ShortestPath: n-shortest-string algorithm not " - << "currently implemented"; - - // Algorithm works on the reverse of 'fst' : 'rfst' 'distance' is - // the distance to the final state in 'rfst' 'ofst' is built as the - // reverse of the tree of n-shortest path in 'rfst'. - - if (!opts.has_distance) - ShortestDistance(ifst, distance, opts); - VectorFst<ReverseArc> rfst; - Reverse(ifst, &rfst); - distance->insert(distance->begin(), Weight::One()); - while (distance->size() < rfst.NumStates()) - distance->push_back(Weight::Zero()); - - - // Each state in 'ofst' corresponds to a path with weight w from the - // initial state of 'rfst' to a state s in 'rfst', that can be - // characterized by a pair (s,w). The vector 'pairs' maps each - // state in 'ofst' to the corresponding pair maps states in OFST to - // the corresponding pair (s,w). - vector<Pair> pairs; - // 'r[s]', 's' state in 'fst', is the number of states in 'ofst' - // which corresponding pair contains 's' ,i.e. , it is number of - // paths computed so far to 's'. - StateId superfinal = distance->size(); // superfinal must be handled - distance->push_back(Weight::One()); // differently when unique=true - ShortestPathCompare<StateId, Weight> - compare(pairs, *distance, superfinal, opts.delta); - vector<StateId> heap; - vector<int> r; - while (r.size() < distance->size()) - r.push_back(0); - ofst->SetStart(ofst->AddState()); - StateId final = ofst->AddState(); - ofst->SetFinal(final, Weight::One()); - while (pairs.size() <= final) - pairs.push_back(Pair(kNoStateId, Weight::Zero())); - pairs[final] = Pair(rfst.Start(), Weight::One()); - heap.push_back(final); - - while (!heap.empty()) { - pop_heap(heap.begin(), heap.end(), compare); - StateId state = heap.back(); - Pair p = pairs[state]; - heap.pop_back(); - - ++r[p.first]; - if (p.first == superfinal) - ofst->AddArc(ofst->Start(), Arc(0, 0, Weight::One(), state)); - if ((p.first == superfinal) && (r[p.first] == n)) break; - if (r[p.first] > n) continue; - if (p.first == superfinal) - continue; - - for (ArcIterator< Fst<ReverseArc> > aiter(rfst, p.first); - !aiter.Done(); - aiter.Next()) { - const ReverseArc &rarc = aiter.Value(); - Arc arc(rarc.ilabel, rarc.olabel, rarc.weight.Reverse(), rarc.nextstate); - Weight w = Times(p.second, arc.weight); - StateId next = ofst->AddState(); - pairs.push_back(Pair(arc.nextstate, w)); - arc.nextstate = state; - ofst->AddArc(next, arc); - heap.push_back(next); - push_heap(heap.begin(), heap.end(), compare); - } - - Weight finalw = rfst.Final(p.first).Reverse(); - if (finalw != Weight::Zero()) { - Weight w = Times(p.second, finalw); - StateId next = ofst->AddState(); - pairs.push_back(Pair(superfinal, w)); - ofst->AddArc(next, Arc(0, 0, finalw, state)); - heap.push_back(next); - push_heap(heap.begin(), heap.end(), compare); - } - } - Connect(ofst); - distance->erase(distance->begin()); - distance->pop_back(); -} - -// Shortest-path algorithm: simplified interface. See above for a -// version that allows finer control. - -// 'ofst' contains the 'n'-shortest paths in 'ifst'. The queue -// discipline is automatically selected. When 'unique' == true, only -// paths with distinct input labels are returned. -// -// The n-shortest paths are the n-lowest weight paths w.r.t. the -// natural semiring order. The single path that can be read from the -// ith of at most n transitions leaving the initial state of 'ofst' is -// the ith best path. -// -// The weights need to be right distributive and have the path -// (kPath) property. -template<class Arc> -void ShortestPath(const Fst<Arc> &ifst, MutableFst<Arc> *ofst, - size_t n = 1, bool unique = false) { - vector<typename Arc::Weight> distance; - AnyArcFilter<Arc> arc_filter; - AutoQueue<typename Arc::StateId> state_queue(ifst, &distance, arc_filter); - ShortestPathOptions< Arc, AutoQueue<typename Arc::StateId>, - AnyArcFilter<Arc> > opts(&state_queue, arc_filter, n, unique); - ShortestPath(ifst, ofst, &distance, opts); -} - -} // namespace fst - -#endif // FST_LIB_SHORTEST_PATH_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/statesort.h b/tools/thirdparty/OpenFst/fst/lib/statesort.h deleted file mode 100644 index 6cf5ed2..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/statesort.h +++ /dev/null @@ -1,88 +0,0 @@ -// statesort.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function to sort states of an Fst. - -#ifndef FST_LIB_STATESORT_H__ -#define FST_LIB_STATESORT_H__ - -#include <algorithm> - -#include "fst/lib/mutable-fst.h" - -namespace fst { - -// Sorts the input states of an FST, modifying it. ORDER[i] gives the -// the state Id after sorting that corresponds to state Id i before -// sorting. ORDER must be a permutation of FST's states ID sequence: -// (1, 2, ..., fst->NumStates() - 1). -template <class Arc> -void StateSort(MutableFst<Arc> *fst, - const vector<typename Arc::StateId> &order) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Weight Weight; - - CHECK_EQ(order.size(), fst->NumStates()); - - if (fst->Start() == kNoStateId) - return; - - uint64 props = fst->Properties(kStateSortProperties, false); - - vector<bool> done(order.size(), false); - vector<Arc> arcsa, arcsb; - vector<Arc> *arcs1 = &arcsa, *arcs2 = &arcsb; - - fst->SetStart(order[fst->Start()]); - - for (StateIterator< MutableFst<Arc> > siter(*fst); - !siter.Done(); - siter.Next()) { - StateId s1 = siter.Value(), s2; - if (done[s1]) - continue; - Weight final1 = fst->Final(s1), final2 = Weight::Zero(); - arcs1->clear(); - for (ArcIterator< MutableFst<Arc> > aiter(*fst, s1); - !aiter.Done(); - aiter.Next()) - arcs1->push_back(aiter.Value()); - for (; !done[s1]; s1 = s2, final1 = final2, swap(arcs1, arcs2)) { - s2 = order[s1]; - if (!done[s2]) { - final2 = fst->Final(s2); - arcs2->clear(); - for (ArcIterator< MutableFst<Arc> > aiter(*fst, s2); - !aiter.Done(); - aiter.Next()) - arcs2->push_back(aiter.Value()); - } - fst->SetFinal(s2, final1); - fst->DeleteArcs(s2); - for (size_t i = 0; i < arcs1->size(); ++i) { - Arc arc = (*arcs1)[i]; - arc.nextstate = order[arc.nextstate]; - fst->AddArc(s2, arc); - } - done[s1] = true; - } - } - fst->SetProperties(props, kFstProperties); -} - -} // namespace fst - -#endif // FST_LIB_STATESORT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/string-weight.h b/tools/thirdparty/OpenFst/fst/lib/string-weight.h deleted file mode 100644 index b649cdc..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/string-weight.h +++ /dev/null @@ -1,525 +0,0 @@ -// string-weight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// String weight set and associated semiring operation definitions. - -#ifndef FST_LIB_STRING_WEIGHT_H__ -#define FST_LIB_STRING_WEIGHT_H__ - -#include <list> - -#include "fst/lib/product-weight.h" -#include "fst/lib/weight.h" - -namespace fst { - -const int kStringInfinity = -1; // Label for the infinite string -const int kStringBad = -2; // Label for a non-string -const char kStringSeparator = '_'; // Label separator in strings - -// Determines whether to use left or right string semiring. Includes -// restricted versions that signal an error if proper prefixes -// (suffixes) would otherwise be returned by Plus, useful with various -// algorithms that require functional transducer input with the -// string semirings. -enum StringType { STRING_LEFT = 0, STRING_RIGHT = 1 , - STRING_LEFT_RESTRICT = 2, STRING_RIGHT_RESTRICT }; - -#define REVERSE_STRING_TYPE(S) \ - ((S) == STRING_LEFT ? STRING_RIGHT : \ - ((S) == STRING_RIGHT ? STRING_LEFT : \ - ((S) == STRING_LEFT_RESTRICT ? STRING_RIGHT_RESTRICT : \ - STRING_LEFT_RESTRICT))) - -template <typename L, StringType S = STRING_LEFT> -class StringWeight; - -template <typename L, StringType S = STRING_LEFT> -class StringWeightIterator; - -template <typename L, StringType S = STRING_LEFT> -class StringWeightReverseIterator; - -template <typename L, StringType S> -bool operator==(const StringWeight<L, S> &, const StringWeight<L, S> &); - - -// String semiring: (longest_common_prefix/suffix, ., Infinity, Epsilon) -template <typename L, StringType S> -class StringWeight { - public: - typedef L Label; - typedef StringWeight<L, REVERSE_STRING_TYPE(S)> ReverseWeight; - - friend class StringWeightIterator<L, S>; - friend class StringWeightReverseIterator<L, S>; - friend bool operator==<>(const StringWeight<L, S> &, - const StringWeight<L, S> &); - - StringWeight() { Init(); } - - template <typename Iter> - StringWeight(const Iter &begin, const Iter &end) { - Init(); - for (Iter iter = begin; iter != end; ++iter) - PushBack(*iter); - } - - explicit StringWeight(L l) { Init(); PushBack(l); } - - static const StringWeight<L, S> &Zero() { - static const StringWeight<L, S> zero(kStringInfinity); - return zero; - } - - static const StringWeight<L, S> &One() { - static const StringWeight<L, S> one; - return one; - } - - static const string &Type() { - static const string type = - S == STRING_LEFT ? "string" : - (S == STRING_RIGHT ? "right_string" : - (S == STRING_LEFT_RESTRICT ? "restricted_string" : - "right_restricted_string")); - return type; - } - - bool Member() const; - - istream &Read(istream &strm); - - ostream &Write(ostream &strm) const; - - ssize_t Hash() const; - - StringWeight<L, S> Quantize(float delta = kDelta) const { - return *this; - } - - ReverseWeight Reverse() const; - - static uint64 Properties() { - return (S == STRING_LEFT || S == STRING_LEFT_RESTRICT ? - kLeftSemiring : kRightSemiring) | kIdempotent; - } - - // NB: This needs to be uncommented only if default fails for this impl. - // StringWeight<L, S> &operator=(const StringWeight<L, S> &w); - - // These operations combined with the StringWeightIterator and - // StringWeightReverseIterator provide the access and mutation of - // the string internal elements. - - // Common initializer among constructors. - void Init() { first_ = 0; } - - // Clear existing StringWeight. - void Clear() { first_ = 0; rest_.clear(); } - - Label Size() const { return first_ ? rest_.size() + 1 : 0; } - - void PushFront(L l) { - if (first_) - rest_.push_front(first_); - first_ = l; - } - - void PushBack(L l) { - if (!first_) - first_ = l; - else - rest_.push_back(l); - } - - private: - L first_; // first label in string (0 if empty) - list<L> rest_; // remaining labels in string -}; - - -// Traverses string in forward direction. -template <typename L, StringType S> -class StringWeightIterator { - public: - explicit StringWeightIterator(const StringWeight<L, S>& w) - : first_(w.first_), rest_(w.rest_), init_(true), - iter_(rest_.begin()) {} - - bool Done() const { - if (init_) return first_ == 0; - else return iter_ == rest_.end(); - } - - const L& Value() const { return init_ ? first_ : *iter_; } - - void Next() { - if (init_) init_ = false; - else ++iter_; - } - - void Reset() { - init_ = true; - iter_ = rest_.begin(); - } - - private: - const L &first_; - const list<L> &rest_; - bool init_; // in the initialized state? - typename list<L>::const_iterator iter_; - - DISALLOW_EVIL_CONSTRUCTORS(StringWeightIterator); -}; - - -// Traverses string in forward direction. -template <typename L, StringType S> -class StringWeightReverseIterator { - public: - explicit StringWeightReverseIterator(const StringWeight<L, S>& w) - : first_(w.first_), rest_(w.rest_), fin_(first_ == 0), - iter_(rest_.rbegin()) {} - - bool Done() const { return fin_; } - - const L& Value() const { return iter_ == rest_.rend() ? first_ : *iter_; } - - void Next() { - if (iter_ == rest_.rend()) fin_ = true; - else ++iter_; - } - - void Reset() { - fin_ = false; - iter_ = rest_.rbegin(); - } - - private: - const L &first_; - const list<L> &rest_; - bool fin_; // in the final state? - typename list<L>::const_reverse_iterator iter_; - - DISALLOW_EVIL_CONSTRUCTORS(StringWeightReverseIterator); -}; - - -// StringWeight member functions follow that require -// StringWeightIterator or StringWeightReverseIterator. - -template <typename L, StringType S> -inline istream &StringWeight<L, S>::Read(istream &strm) { - Clear(); - int32 size = 0; - ReadType(strm, &size); - for (int i = 0; i < size; ++i) { - L label; - ReadType(strm, &label); - PushBack(label); - } - return strm; -} - -template <typename L, StringType S> -inline ostream &StringWeight<L, S>::Write(ostream &strm) const { - int32 size = Size(); - WriteType(strm, size); - for (StringWeightIterator<L, S> iter(*this); !iter.Done(); iter.Next()) { - L label = iter.Value(); - WriteType(strm, label); - } - return strm; -} - -template <typename L, StringType S> -inline bool StringWeight<L, S>::Member() const { - if (Size() != 1) - return true; - StringWeightIterator<L, S> iter(*this); - return iter.Value() != kStringBad; -} - -template <typename L, StringType S> -inline typename StringWeight<L, S>::ReverseWeight -StringWeight<L, S>::Reverse() const { - ReverseWeight rw; - for (StringWeightIterator<L, S> iter(*this); !iter.Done(); iter.Next()) - rw.PushFront(iter.Value()); - return rw; -} - -template <typename L, StringType S> -inline ssize_t StringWeight<L, S>::Hash() const { - size_t h = 0; - for (StringWeightIterator<L, S> iter(*this); !iter.Done(); iter.Next()) - h ^= h<<1 ^ iter.Value(); - return static_cast<ssize_t>(h); -} - -// NB: This needs to be uncommented only if default fails for this the impl. -// -// template <typename L, StringType S> -// inline StringWeight<L, S> -// &StringWeight<L, S>::operator=(const StringWeight<L, S> &w) { -// if (this != &w) { -// Clear(); -// for (StringWeightIterator<L, S> iter(w); !iter.Done(); iter.Next()) -// PushBack(iter.Value()); -// } -// return *this; -// } - -template <typename L, StringType S> -inline bool operator==(const StringWeight<L, S> &w1, - const StringWeight<L, S> &w2) { - if (w1.Size() != w2.Size()) - return false; - - StringWeightIterator<L, S> iter1(w1); - StringWeightIterator<L, S> iter2(w2); - - for (; !iter1.Done() ; iter1.Next(), iter2.Next()) - if (iter1.Value() != iter2.Value()) - return false; - - return true; -} - -template <typename L, StringType S> -inline bool operator!=(const StringWeight<L, S> &w1, - const StringWeight<L, S> &w2) { - return !(w1 == w2); -} - -template <typename L, StringType S> -inline bool ApproxEqual(const StringWeight<L, S> &w1, - const StringWeight<L, S> &w2, - float delta = kDelta) { - return w1 == w2; -} - -template <typename L, StringType S> -inline ostream &operator<<(ostream &strm, const StringWeight<L, S> &w) { - StringWeightIterator<L, S> iter(w); - if (iter.Done()) - return strm << "Epsilon"; - else if (iter.Value() == kStringInfinity) - return strm << "Infinity"; - else if (iter.Value() == kStringBad) - return strm << "BadString"; - else - for (size_t i = 0; !iter.Done(); ++i, iter.Next()) { - if (i > 0) - strm << kStringSeparator; - strm << iter.Value(); - } - return strm; -} - -template <typename L, StringType S> -inline istream &operator>>(istream &strm, StringWeight<L, S> &w) { - string s; - strm >> s; - if (s == "Infinity") { - w = StringWeight<L, S>::Zero(); - } else if (s == "Epsilon") { - w = StringWeight<L, S>::One(); - } else { - w.Clear(); - char *p = 0; - for (const char *cs = s.c_str(); !p || *p != '\0'; cs = p + 1) { - int l = strtoll(cs, &p, 10); - if (p == cs || *p != 0 && *p != kStringSeparator) { - strm.clear(std::ios::badbit); - break; - } - w.PushBack(l); - } - } - return strm; -} - - -// Default is for the restricted left and right semirings. String -// equality is required (for non-Zero() input. This restriction -// is used in e.g. Determinize to ensure functional input. -template <typename L, StringType S> inline StringWeight<L, S> -Plus(const StringWeight<L, S> &w1, - const StringWeight<L, S> &w2) { - if (w1 == StringWeight<L, S>::Zero()) - return w2; - if (w2 == StringWeight<L, S>::Zero()) - return w1; - - if (w1 != w2) - LOG(FATAL) << "StringWeight::Plus: unequal arguments " - << "(non-functional FST?)"; - - return w1; -} - - -// Longest common prefix for left string semiring. -template <typename L> inline StringWeight<L, STRING_LEFT> -Plus(const StringWeight<L, STRING_LEFT> &w1, - const StringWeight<L, STRING_LEFT> &w2) { - if (w1 == StringWeight<L, STRING_LEFT>::Zero()) - return w2; - if (w2 == StringWeight<L, STRING_LEFT>::Zero()) - return w1; - - StringWeight<L, STRING_LEFT> sum; - StringWeightIterator<L, STRING_LEFT> iter1(w1); - StringWeightIterator<L, STRING_LEFT> iter2(w2); - for (; !iter1.Done() && !iter2.Done() && iter1.Value() == iter2.Value(); - iter1.Next(), iter2.Next()) - sum.PushBack(iter1.Value()); - return sum; -} - - -// Longest common suffix for right string semiring. -template <typename L> inline StringWeight<L, STRING_RIGHT> -Plus(const StringWeight<L, STRING_RIGHT> &w1, - const StringWeight<L, STRING_RIGHT> &w2) { - if (w1 == StringWeight<L, STRING_RIGHT>::Zero()) - return w2; - if (w2 == StringWeight<L, STRING_RIGHT>::Zero()) - return w1; - - StringWeight<L, STRING_RIGHT> sum; - StringWeightReverseIterator<L, STRING_RIGHT> iter1(w1); - StringWeightReverseIterator<L, STRING_RIGHT> iter2(w2); - for (; !iter1.Done() && !iter2.Done() && iter1.Value() == iter2.Value(); - iter1.Next(), iter2.Next()) - sum.PushFront(iter1.Value()); - return sum; -} - - -template <typename L, StringType S> -inline StringWeight<L, S> Times(const StringWeight<L, S> &w1, - const StringWeight<L, S> &w2) { - if (w1 == StringWeight<L, S>::Zero() || w2 == StringWeight<L, S>::Zero()) - return StringWeight<L, S>::Zero(); - - StringWeight<L, S> prod(w1); - for (StringWeightIterator<L, S> iter(w2); !iter.Done(); iter.Next()) - prod.PushBack(iter.Value()); - - return prod; -} - - -// Default is for left division in the left string and the -// left restricted string semirings. -template <typename L, StringType S> inline StringWeight<L, S> -Divide(const StringWeight<L, S> &w1, - const StringWeight<L, S> &w2, - DivideType typ) { - - if (typ != DIVIDE_LEFT) - LOG(FATAL) << "StringWeight::Divide: only left division is defined " - << "for the " << StringWeight<L, S>::Type() << " semiring"; - - if (w2 == StringWeight<L, S>::Zero()) - return StringWeight<L, S>(kStringBad); - else if (w1 == StringWeight<L, S>::Zero()) - return StringWeight<L, S>::Zero(); - - StringWeight<L, S> div; - StringWeightIterator<L, S> iter(w1); - for (int i = 0; !iter.Done(); iter.Next(), ++i) { - if (i >= w2.Size()) - div.PushBack(iter.Value()); - } - return div; -} - - -// Right division in the right string semiring. -template <typename L> inline StringWeight<L, STRING_RIGHT> -Divide(const StringWeight<L, STRING_RIGHT> &w1, - const StringWeight<L, STRING_RIGHT> &w2, - DivideType typ) { - - if (typ != DIVIDE_RIGHT) - LOG(FATAL) << "StringWeight::Divide: only right division is defined " - << "for the right string semiring"; - - if (w2 == StringWeight<L, STRING_RIGHT>::Zero()) - return StringWeight<L, STRING_RIGHT>(kStringBad); - else if (w1 == StringWeight<L, STRING_RIGHT>::Zero()) - return StringWeight<L, STRING_RIGHT>::Zero(); - - StringWeight<L, STRING_RIGHT> div; - StringWeightReverseIterator<L, STRING_RIGHT> iter(w1); - for (int i = 0; !iter.Done(); iter.Next(), ++i) { - if (i >= w2.Size()) - div.PushFront(iter.Value()); - } - return div; -} - - -// Right division in the right restricted string semiring. -template <typename L> inline StringWeight<L, STRING_RIGHT_RESTRICT> -Divide(const StringWeight<L, STRING_RIGHT_RESTRICT> &w1, - const StringWeight<L, STRING_RIGHT_RESTRICT> &w2, - DivideType typ) { - - if (typ != DIVIDE_RIGHT) - LOG(FATAL) << "StringWeight::Divide: only right division is defined " - << "for the right restricted string semiring"; - - if (w2 == StringWeight<L, STRING_RIGHT_RESTRICT>::Zero()) - return StringWeight<L, STRING_RIGHT_RESTRICT>(kStringBad); - else if (w1 == StringWeight<L, STRING_RIGHT_RESTRICT>::Zero()) - return StringWeight<L, STRING_RIGHT_RESTRICT>::Zero(); - - StringWeight<L, STRING_RIGHT_RESTRICT> div; - StringWeightReverseIterator<L, STRING_RIGHT_RESTRICT> iter(w1); - for (int i = 0; !iter.Done(); iter.Next(), ++i) { - if (i >= w2.Size()) - div.PushFront(iter.Value()); - } - return div; -} - - -// Product of string weight and an arbitray weight. -template <class L, class W, StringType S = STRING_LEFT> -struct GallicWeight : public ProductWeight<StringWeight<L, S>, W> { - typedef GallicWeight<L, typename W::ReverseWeight, REVERSE_STRING_TYPE(S)> - ReverseWeight; - - GallicWeight() {} - - GallicWeight(StringWeight<L, S> w1, W w2) - : ProductWeight<StringWeight<L, S>, W>(w1, w2) {} - - explicit GallicWeight(const string &s, int *nread = 0) - : ProductWeight<StringWeight<L, S>, W>(s, nread) {} - - GallicWeight(const ProductWeight<StringWeight<L, S>, W> &w) - : ProductWeight<StringWeight<L, S>, W>(w) {} -}; - -} // namespace fst; - -#endif // FST_LIB_STRING_WEIGHT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/symbol-table.cpp b/tools/thirdparty/OpenFst/fst/lib/symbol-table.cpp deleted file mode 100644 index 74c2dad..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/symbol-table.cpp +++ /dev/null @@ -1,158 +0,0 @@ -// symbol-table.cc -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Classes to provide symbol-to-integer and integer-to-symbol mappings. - -#include "fst/lib/symbol-table.h" -#include "fst/lib/util.h" - -#include <string.h> - -DEFINE_bool(fst_compat_symbols, true, - "Require symbol tables to match when appropriate"); - -namespace fst { - -// Maximum line length in textual symbols file. -const int kLineLen = 8096; - -// Identifies stream data as a symbol table (and its endianity) -static const int32 kSymbolTableMagicNumber = 2125658996; - -SymbolTableImpl* SymbolTableImpl::ReadText(const string &filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "SymbolTable::ReadText: Can't open symbol file: " - << filename; - return 0; - } - - SymbolTableImpl* impl = new SymbolTableImpl(filename); - - int64 nline = 0; - char line[kLineLen]; - while (strm.getline(line, kLineLen)) { - ++nline; - vector<char *> col; - SplitToVector(line, "\n\t ", &col, true); - if (col.size() == 0) // empty line - continue; - if (col.size() != 2) { - LOG(ERROR) << "SymbolTable::ReadText: Bad number of columns (skipping), " - << "file = " << filename << ", line = " << nline; - continue; - } - const char *symbol = col[0]; - const char *value = col[1]; - char *p; - int64 key = strtoll(value, &p, 10); - if (p < value + strlen(value) || key < 0) { - LOG(ERROR) << "SymbolTable::ReadText: Bad non-negative integer \"" - << value << "\" (skipping), " - << "file = " << filename << ", line = " << nline; - continue; - } - impl->AddSymbol(symbol, key); - } - - return impl; -} - -void SymbolTableImpl::RecomputeCheckSum() const { - check_sum_.Reset(); - for (size_t i = 0; i < symbols_.size(); ++i) { - check_sum_.Update(symbols_[i], strlen(symbols_[i])+1); - } - check_sum_finalized_ = true; -} - -int64 SymbolTableImpl::AddSymbol(const string& symbol, int64 key) { - hash_map<string, int64>::const_iterator it = - symbol_map_.find(symbol); - if (it == symbol_map_.end()) { // only add if not in table - check_sum_finalized_ = false; - - char *csymbol = new char[symbol.size() + 1]; - strcpy(csymbol, symbol.c_str()); - symbols_.push_back(csymbol); - key_map_[key] = csymbol; - symbol_map_[csymbol] = key; - - if (key >= available_key_) { - available_key_ = key + 1; - } - } - - return key; -} - -SymbolTableImpl* SymbolTableImpl::Read(istream &strm, - const string &source) { - int32 magic_number = 0; - ReadType(strm, &magic_number); - if (magic_number != kSymbolTableMagicNumber) { - LOG(ERROR) << "SymbolTable::Read: read failed"; - return 0; - } - string name; - ReadType(strm, &name); - SymbolTableImpl* impl = new SymbolTableImpl(name); - ReadType(strm, &impl->available_key_); - int64 size; - ReadType(strm, &size); - string symbol; - int64 key = 0; - for (size_t i = 0; i < size; ++i) { - ReadType(strm, &symbol); - ReadType(strm, &key); - impl->AddSymbol(symbol, key); - } - if (!strm) - LOG(ERROR) << "SymbolTable::Read: read failed"; - return impl; -} - -bool SymbolTableImpl::Write(ostream &strm) const { - WriteType(strm, kSymbolTableMagicNumber); - WriteType(strm, name_); - WriteType(strm, available_key_); - int64 size = symbols_.size(); - WriteType(strm, size); - for (size_t i = 0; i < symbols_.size(); ++i) { - const string symbol = symbols_[i]; - WriteType(strm, symbol); - hash_map<string, int64>::const_iterator it = symbol_map_.find(symbol); - WriteType(strm, it->second); - } - strm.flush(); - if (!strm) - LOG(ERROR) << "SymbolTable::Write: write failed"; - return strm; -} - -bool SymbolTableImpl::WriteText(ostream &strm) const { - for (size_t i = 0; i < symbols_.size(); ++i) { - char line[kLineLen]; - snprintf(line, kLineLen, "%s\t%lld\n", symbols_[i], Find(symbols_[i])); - strm.write(line, strlen(line)); - } - strm.flush(); - if (!strm) - LOG(ERROR) << "SymbolTable::WriteText: write failed"; - return strm; -} - -} // namespace fst diff --git a/tools/thirdparty/OpenFst/fst/lib/symbol-table.h b/tools/thirdparty/OpenFst/fst/lib/symbol-table.h deleted file mode 100644 index 0a67f29..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/symbol-table.h +++ /dev/null @@ -1,387 +0,0 @@ -// symbol-table.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Classes to provide symbol-to-integer and integer-to-symbol mappings. - -#ifndef FST_LIB_SYMBOL_TABLE_H__ -#define FST_LIB_SYMBOL_TABLE_H__ - -#include <ext/hash_map> -using __gnu_cxx::hash_map; -#include <fstream> -#include <iostream> -#include <string> -#include <vector> - -#include "fst/lib/compat.h" - - - -DECLARE_bool(fst_compat_symbols); - -namespace fst { - -class SymbolTableImpl { - friend class SymbolTableIterator; - public: - SymbolTableImpl(const string &name) - : name_(name), available_key_(0), ref_count_(1), - check_sum_finalized_(false) {} - ~SymbolTableImpl() { - for (size_t i = 0; i < symbols_.size(); ++i) - delete[] symbols_[i]; - } - - int64 AddSymbol(const string& symbol, int64 key); - - int64 AddSymbol(const string& symbol) { - int64 key = Find(symbol); - return (key == -1) ? AddSymbol(symbol, available_key_++) : key; - } - - void AddTable(SymbolTableImpl* table) { - for (size_t i = 0; i < table->symbols_.size(); ++i) { - AddSymbol(table->symbols_[i]); - } - } - - static SymbolTableImpl* ReadText(const string& filename); - - static SymbolTableImpl* Read(istream &strm, const string& source); - - bool Write(ostream &strm) const; - - bool WriteText(ostream &strm) const; - - // - // Return the string associated with the key. If the key is out of - // range (<0, >max), return an empty string. - string Find(int64 key) const { - hash_map<int64, string>::const_iterator it = - key_map_.find(key); - if (it == key_map_.end()) { - return ""; - } - return it->second; - } - - // - // Return the key associated with the symbol. If the symbol - // does not exists, return -1. - int64 Find(const string& symbol) const { - return Find(symbol.c_str()); - } - - // - // Return the key associated with the symbol. If the symbol - // does not exists, return -1. - int64 Find(const char* symbol) const { - hash_map<string, int64>::const_iterator it = - symbol_map_.find(symbol); - if (it == symbol_map_.end()) { - return -1; - } - return it->second; - } - - const string& Name() const { return name_; } - - int IncrRefCount() const { - return ++ref_count_; - } - int DecrRefCount() const { - return --ref_count_; - } - - string CheckSum() const { - if (!check_sum_finalized_) { - RecomputeCheckSum(); - check_sum_string_ = check_sum_.Digest(); - } - return check_sum_string_; - } - - int64 AvailableKey() const { - return available_key_; - } - - // private support methods - private: - void RecomputeCheckSum() const; - static SymbolTableImpl* Read1(istream &, const string &); - - string name_; - int64 available_key_; - vector<const char *> symbols_; - hash_map<int64, string> key_map_; - hash_map<string, int64> symbol_map_; - - mutable int ref_count_; - mutable bool check_sum_finalized_; - mutable MD5 check_sum_; - mutable string check_sum_string_; - - DISALLOW_EVIL_CONSTRUCTORS(SymbolTableImpl); -}; - - -class SymbolTableIterator; - -// -// \class SymbolTable -// \brief Symbol (string) to int and reverse mapping -// -// The SymbolTable implements the mappings of labels to strings and reverse. -// SymbolTables are used to describe the alphabet of the input and output -// labels for arcs in a Finite State Transducer. -// -// SymbolTables are reference counted and can therefore be shared across -// multiple machines. For example a language model grammar G, with a -// SymbolTable for the words in the language model can share this symbol -// table with the lexical representation L o G. -// -class SymbolTable { - friend class SymbolTableIterator; - public: - static const int64 kNoSymbol = -1; - - // Construct symbol table with a unique name. - SymbolTable(const string& name) : impl_(new SymbolTableImpl(name)) {} - - // Create a reference counted copy. - SymbolTable(const SymbolTable& table) : impl_(table.impl_) { - impl_->IncrRefCount(); - } - - // Derefence implentation object. When reference count hits 0, delete - // implementation. - ~SymbolTable() { - if (!impl_->DecrRefCount()) delete impl_; - } - - // create a reference counted copy - SymbolTable* Copy() const { - return new SymbolTable(*this); - } - - // Add a symbol with given key to table. A symbol table also - // keeps track of the last available key (highest key value in - // the symbol table). - // - // \param symbol string symbol to add - // \param key associated key for string symbol - // \return the key created by the symbol table. Symbols allready added to - // the symbol table will not get a different key. - int64 AddSymbol(const string& symbol, int64 key) { - return impl_->AddSymbol(symbol, key); - } - - // Add a symbol to the table. The associated value key is automatically - // assigned by the symbol table. - // - // \param symbol string to add to the table - // \return the value key assigned to the associated string symbol - int64 AddSymbol(const string& symbol) { - return impl_->AddSymbol(symbol); - } - - // Add another symbol table to this table. All key values will be offset - // by the current available key (highest key value in the symbol table). - // Note string symbols with the same key value with still have the same - // key value after the symbol table has been merged, but a different - // value. Adding symbol tables do not result in changes in the base table. - // - // Merging N symbol tables is often useful when combining the various - // name spaces of transducers to a unified representation. - // - // \param table the symbol table to add to this table - void AddTable(const SymbolTable& table) { - return impl_->AddTable(table.impl_); - } - - // return the name of the symbol table - const string& Name() const { - return impl_->Name(); - } - - // return the MD5 check-sum for this table. All new symbols added to - // the table will result in an updated checksum. - string CheckSum() const { - return impl_->CheckSum(); - } - - // read an ascii representation of the symbol table - static SymbolTable* ReadText(const string& filename) { - SymbolTableImpl* impl = SymbolTableImpl::ReadText(filename); - if (!impl) - return 0; - else - return new SymbolTable(impl); - } - - // read a binary dump of the symbol table - static SymbolTable* Read(istream &strm, const string& source) { - SymbolTableImpl* impl = SymbolTableImpl::Read(strm, source); - if (!impl) - return 0; - else - return new SymbolTable(impl); - } - - // read a binary dump of the symbol table - static SymbolTable* Read(const string& filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "SymbolTable::Read: Can't open file " << filename; - return 0; - } - return Read(strm, filename); - } - - bool Write(ostream &strm) const { - return impl_->Write(strm); - } - - bool Write(const string& filename) const { - ofstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "SymbolTable::Write: Can't open file " << filename; - return false; - } - return Write(strm); - } - - // Dump an ascii text representation of the symbol table - bool WriteText(ostream &strm) const { - return impl_->WriteText(strm); - } - - // Dump an ascii text representation of the symbol table - bool WriteText(const string& filename) const { - ofstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "SymbolTable::WriteText: Can't open file " << filename; - return false; - } - return WriteText(strm); - } - - // Return the string associated with the key. If the key is out of - // range (<0, >max), log error and return an empty string. - string Find(int64 key) const { - return impl_->Find(key); - } - - // Return the key associated with the symbol. If the symbol - // does not exists, log error and return -1 - int64 Find(const string& symbol) const { - return impl_->Find(symbol); - } - - // Return the key associated with the symbol. If the symbol - // does not exists, log error and return -1 - int64 Find(const char* symbol) const { - return impl_->Find(symbol); - } - - // return the current available key (i.e highest key number) in - // the symbol table - int64 AvailableKey(void) const { - return impl_->AvailableKey(); - } - - protected: - explicit SymbolTable(SymbolTableImpl* impl) : impl_(impl) {} - - const SymbolTableImpl* Impl() const { - return impl_; - } - - private: - SymbolTableImpl* impl_; - - - void operator=(const SymbolTable &table); // disallow -}; - - -// -// \class SymbolTableIterator -// \brief Iterator class for symbols in a symbol table -class SymbolTableIterator { - public: - // Constructor creates a refcounted copy of underlying implementation - SymbolTableIterator(const SymbolTable& symbol_table) { - impl_ = symbol_table.Impl(); - impl_->IncrRefCount(); - pos_ = 0; - size_ = impl_->symbols_.size(); - } - - // decrement implementation refcount, and delete if 0 - ~SymbolTableIterator() { - if (!impl_->DecrRefCount()) delete impl_; - } - - // is iterator done - bool Done(void) { - return (pos_ == size_); - } - - // return the Value() of the current symbol (in64 key) - int64 Value(void) { - return impl_->Find(impl_->symbols_[pos_]); - } - - // return the string of the current symbol - const char* Symbol(void) { - return impl_->symbols_[pos_]; - } - - // advance iterator forward - void Next(void) { - if (Done()) return; - ++pos_; - } - - // reset iterator - void Reset(void) { - pos_ = 0; - } - - private: - const SymbolTableImpl* impl_; - size_t pos_; - size_t size_; -}; - - -// Tests compatibilty between two sets of symbol tables -inline bool CompatSymbols(const SymbolTable *syms1, - const SymbolTable *syms2) { - if (!FLAGS_fst_compat_symbols) - return true; - else if (!syms1 && !syms2) - return true; - else if (syms1 && !syms2 || !syms1 && syms2) - return false; - else - return syms1->CheckSum() == syms2->CheckSum(); -} - -} // namespace fst - -#endif // FST_LIB_SYMBOL_TABLE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/synchronize.h b/tools/thirdparty/OpenFst/fst/lib/synchronize.h deleted file mode 100644 index cf416d6..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/synchronize.h +++ /dev/null @@ -1,459 +0,0 @@ -// synchronize.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Author: allauzen@cs.nyu.edu (Cyril Allauzen) -// -// \file -// Synchronize an FST with bounded delay. - -#ifndef FST_LIB_SYNCHRONIZE_H__ -#define FST_LIB_SYNCHRONIZE_H__ - -#include <algorithm> - -#include <ext/hash_map> -using __gnu_cxx::hash_map; - -#include "fst/lib/cache.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -typedef CacheOptions SynchronizeFstOptions; - - -// Implementation class for SynchronizeFst -template <class A> -class SynchronizeFstImpl - : public CacheImpl<A> { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::SetInputSymbols; - using FstImpl<A>::SetOutputSymbols; - - using CacheBaseImpl< CacheState<A> >::HasStart; - using CacheBaseImpl< CacheState<A> >::HasFinal; - using CacheBaseImpl< CacheState<A> >::HasArcs; - - typedef A Arc; - typedef typename A::Label Label; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - typedef basic_string<Label> String; - - struct Element { - Element() {} - - Element(StateId s, const String *i, const String *o) - : state(s), istring(i), ostring(o) {} - - StateId state; // Input state Id - const String *istring; // Residual input labels - const String *ostring; // Residual output labels - // Residual strings are represented by const pointers to - // basic_string<Label> and are stored in a hash_set. The pointed - // memory is owned by the hash_set string_set_. - }; - - SynchronizeFstImpl(const Fst<A> &fst, const SynchronizeFstOptions &opts) - : CacheImpl<A>(opts), fst_(fst.Copy()) { - SetType("synchronize"); - uint64 props = fst.Properties(kFstProperties, false); - SetProperties(SynchronizeProperties(props), kCopyProperties); - - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - } - - ~SynchronizeFstImpl() { - delete fst_; - // Extract pointers from the hash set - vector<const String*> strings; - typename StringSet::iterator it = string_set_.begin(); - for (; it != string_set_.end(); ++it) - strings.push_back(*it); - // Free the extracted pointers - for (size_t i = 0; i < strings.size(); ++i) - delete strings[i]; - } - - StateId Start() { - if (!HasStart()) { - StateId s = fst_->Start(); - if (s == kNoStateId) - return kNoStateId; - const String *empty = FindString(new String()); - StateId start = FindState(Element(fst_->Start(), empty, empty)); - SetStart(start); - } - return CacheImpl<A>::Start(); - } - - Weight Final(StateId s) { - if (!HasFinal(s)) { - const Element &e = elements_[s]; - Weight w = e.state == kNoStateId ? Weight::One() : fst_->Final(e.state); - if ((w != Weight::Zero()) && (e.istring)->empty() && (e.ostring)->empty()) - SetFinal(s, w); - else - SetFinal(s, Weight::Zero()); - } - return CacheImpl<A>::Final(s); - } - - size_t NumArcs(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumArcs(s); - } - - size_t NumInputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumInputEpsilons(s); - } - - size_t NumOutputEpsilons(StateId s) { - if (!HasArcs(s)) - Expand(s); - return CacheImpl<A>::NumOutputEpsilons(s); - } - - void InitArcIterator(StateId s, ArcIteratorData<A> *data) { - if (!HasArcs(s)) - Expand(s); - CacheImpl<A>::InitArcIterator(s, data); - } - - // Returns the first character of the string obtained by - // concatenating s and l. - Label Car(const String *s, Label l = 0) const { - if (!s->empty()) - return (*s)[0]; - else - return l; - } - - // Computes the residual string obtained by removing the first - // character in the concatenation of s and l. - const String *Cdr(const String *s, Label l = 0) { - String *r = new String(); - for (int i = 1; i < s->size(); ++i) - r->push_back((*s)[i]); - if (l && !(s->empty())) r->push_back(l); - return FindString(r); - } - - // Computes the concatenation of s and l. - const String *Concat(const String *s, Label l = 0) { - String *r = new String(); - for (int i = 0; i < s->size(); ++i) - r->push_back((*s)[i]); - if (l) r->push_back(l); - return FindString(r); - } - - // Tests if the concatenation of s and l is empty - bool Empty(const String *s, Label l = 0) const { - if (s->empty()) - return l == 0; - else - return false; - } - - // Finds the string pointed by s in the hash set. Transfers the - // pointer ownership to the hash set. - const String *FindString(const String *s) { - typename StringSet::iterator it = string_set_.find(s); - if (it != string_set_.end()) { - delete s; - return (*it); - } else { - string_set_.insert(s); - return s; - } - } - - // Finds state corresponding to an element. Creates new state - // if element not found. - StateId FindState(const Element &e) { - typename ElementMap::iterator eit = element_map_.find(e); - if (eit != element_map_.end()) { - return (*eit).second; - } else { - StateId s = elements_.size(); - elements_.push_back(e); - element_map_.insert(pair<const Element, StateId>(e, s)); - return s; - } - } - - - // Computes the outgoing transitions from a state, creating new destination - // states as needed. - void Expand(StateId s) { - Element e = elements_[s]; - - if (e.state != kNoStateId) - for (ArcIterator< Fst<A> > ait(*fst_, e.state); - !ait.Done(); - ait.Next()) { - const A &arc = ait.Value(); - if (!Empty(e.istring, arc.ilabel) && !Empty(e.ostring, arc.olabel)) { - const String *istring = Cdr(e.istring, arc.ilabel); - const String *ostring = Cdr(e.ostring, arc.olabel); - StateId d = FindState(Element(arc.nextstate, istring, ostring)); - AddArc(s, Arc(Car(e.istring, arc.ilabel), - Car(e.ostring, arc.olabel), arc.weight, d)); - } else { - const String *istring = Concat(e.istring, arc.ilabel); - const String *ostring = Concat(e.ostring, arc.olabel); - StateId d = FindState(Element(arc.nextstate, istring, ostring)); - AddArc(s, Arc(0 , 0, arc.weight, d)); - } - } - - Weight w = e.state == kNoStateId ? Weight::One() : fst_->Final(e.state); - if ((w != Weight::Zero()) && - ((e.istring)->size() + (e.ostring)->size() > 0)) { - const String *istring = Cdr(e.istring); - const String *ostring = Cdr(e.ostring); - StateId d = FindState(Element(kNoStateId, istring, ostring)); - AddArc(s, Arc(Car(e.istring), Car(e.ostring), w, d)); - } - SetArcs(s); - } - - private: - // Equality function for Elements, assume strings have been hashed. - class ElementEqual { - public: - bool operator()(const Element &x, const Element &y) const { - return x.state == y.state && - x.istring == y.istring && - x.ostring == y.ostring; - } - }; - - // Hash function for Elements to Fst states. - class ElementKey { - public: - size_t operator()(const Element &x) const { - size_t key = x.state; - key = (key << 1) ^ (x.istring)->size(); - for (size_t i = 0; i < (x.istring)->size(); ++i) - key = (key << 1) ^ (*x.istring)[i]; - key = (key << 1) ^ (x.ostring)->size(); - for (size_t i = 0; i < (x.ostring)->size(); ++i) - key = (key << 1) ^ (*x.ostring)[i]; - return key; - } - }; - - // Equality function for strings - class StringEqual { - public: - bool operator()(const String * const &x, const String * const &y) const { - if (x->size() != y->size()) return false; - for (size_t i = 0; i < x->size(); ++i) - if ((*x)[i] != (*y)[i]) return false; - return true; - } - }; - - // Hash function for set of strings - class StringKey{ - public: - size_t operator()(const String * const & x) const { - size_t key = x->size(); - for (size_t i = 0; i < x->size(); ++i) - key = (key << 1) ^ (*x)[i]; - return key; - } - }; - - - typedef hash_map<Element, StateId, ElementKey, ElementEqual> ElementMap; - typedef hash_set<const String*, StringKey, StringEqual> StringSet; - - const Fst<A> *fst_; - vector<Element> elements_; // mapping Fst state to Elements - ElementMap element_map_; // mapping Elements to Fst state - StringSet string_set_; - - DISALLOW_EVIL_CONSTRUCTORS(SynchronizeFstImpl); -}; - - -// Synchronizes a transducer. This version is a delayed Fst. The -// result will be an equivalent FST that has the property that during -// the traversal of a path, the delay is either zero or strictly -// increasing, where the delay is the difference between the number of -// non-epsilon output labels and input labels along the path. -// -// For the algorithm to terminate, the input transducer must have -// bounded delay, i.e., the delay of every cycle must be zero. -// -// Complexity: -// - A has bounded delay: exponential -// - A does not have bounded delay: does not terminate -// -// References: -// - Mehryar Mohri. Edit-Distance of Weighted Automata: General -// Definitions and Algorithms, International Journal of Computer -// Science, 14(6): 957-982 (2003). -template <class A> -class SynchronizeFst : public Fst<A> { - public: - friend class ArcIterator< SynchronizeFst<A> >; - friend class CacheStateIterator< SynchronizeFst<A> >; - friend class CacheArcIterator< SynchronizeFst<A> >; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - typedef CacheState<A> State; - - SynchronizeFst(const Fst<A> &fst) - : impl_(new SynchronizeFstImpl<A>(fst, SynchronizeFstOptions())) {} - - SynchronizeFst(const Fst<A> &fst, const SynchronizeFstOptions &opts) - : impl_(new SynchronizeFstImpl<A>(fst, opts)) {} - - SynchronizeFst(const SynchronizeFst<A> &fst) : impl_(fst.impl_) { - impl_->IncrRefCount(); - } - - virtual ~SynchronizeFst() { if (!impl_->DecrRefCount()) delete impl_; } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - virtual SynchronizeFst<A> *Copy() const { - return new SynchronizeFst<A>(*this); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual inline void InitStateIterator(StateIteratorData<A> *data) const; - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - private: - SynchronizeFstImpl<A> *Impl() { return impl_; } - - SynchronizeFstImpl<A> *impl_; - - void operator=(const SynchronizeFst<A> &fst); // Disallow -}; - - -// Specialization for SynchronizeFst. -template<class A> -class StateIterator< SynchronizeFst<A> > - : public CacheStateIterator< SynchronizeFst<A> > { - public: - explicit StateIterator(const SynchronizeFst<A> &fst) - : CacheStateIterator< SynchronizeFst<A> >(fst) {} -}; - - -// Specialization for SynchronizeFst. -template <class A> -class ArcIterator< SynchronizeFst<A> > - : public CacheArcIterator< SynchronizeFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const SynchronizeFst<A> &fst, StateId s) - : CacheArcIterator< SynchronizeFst<A> >(fst, s) { - if (!fst.impl_->HasArcs(s)) - fst.impl_->Expand(s); - } - - private: - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - - -template <class A> inline -void SynchronizeFst<A>::InitStateIterator(StateIteratorData<A> *data) const -{ - data->base = new StateIterator< SynchronizeFst<A> >(*this); -} - - - -// Synchronizes a transducer. This version writes the synchronized -// result to a MutableFst. The result will be an equivalent FST that -// has the property that during the traversal of a path, the delay is -// either zero or strictly increasing, where the delay is the -// difference between the number of non-epsilon output labels and -// input labels along the path. -// -// For the algorithm to terminate, the input transducer must have -// bounded delay, i.e., the delay of every cycle must be zero. -// -// Complexity: -// - A has bounded delay: exponential -// - A does not have bounded delay: does not terminate -// -// References: -// - Mehryar Mohri. Edit-Distance of Weighted Automata: General -// Definitions and Algorithms, International Journal of Computer -// Science, 14(6): 957-982 (2003). -template<class Arc> -void Synchronize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst) { - SynchronizeFstOptions opts; - opts.gc_limit = 0; // Cache only the last state for fastest copy. - *ofst = SynchronizeFst<Arc>(ifst, opts); -} - -} // namespace fst - -#endif // FST_LIB_SYNCHRONIZE_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/test-properties.h b/tools/thirdparty/OpenFst/fst/lib/test-properties.h deleted file mode 100644 index b167e0a..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/test-properties.h +++ /dev/null @@ -1,244 +0,0 @@ -// test-properties.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions to manipulate and test property bits - -#ifndef FST_LIB_TEST_PROPERTIES_H__ -#define FST_LIB_TEST_PROPERTIES_H__ - -#include <ext/hash_set> -using __gnu_cxx::hash_set; - - -#include "fst/lib/connect.h" -#include "fst/lib/dfs-visit.h" -#include "fst/lib/mutable-fst.h" - -DECLARE_bool(fst_verify_properties); - -namespace fst { - -// For a binary property, the bit is always returned set. -// For a trinary (i.e. two-bit) property, both bits are -// returned set iff either corresponding input bit is set. -inline uint64 KnownProperties(uint64 props) { - return kBinaryProperties | props & kTrinaryProperties | - (props & kPosTrinaryProperties) << 1 | - (props & kNegTrinaryProperties) >> 1; -} - -// Tests compatibility between two sets of properties -inline bool CompatProperties(uint64 props1, uint64 props2) { - uint64 known_props1 = KnownProperties(props1); - uint64 known_props2 = KnownProperties(props2); - uint64 known_props = known_props1 & known_props2; - uint64 incompat_props = (props1 & known_props) ^ (props2 & known_props); - if (incompat_props) { - uint64 prop = 1; - for (int i = 0; i < 64; ++i, prop <<= 1) - if (prop & incompat_props) - LOG(ERROR) << "CompatProperties: mismatch: " << PropertyNames[i] - << ": props1 = " << (props1 & prop ? "true" : "false") - << ", props2 = " << (props2 & prop ? "true" : "false"); - return false; - } else { - return true; - } -} - -// Computes FST property values defined in properties.h. The value of -// each property indicated in the mask will be determined and returned -// (these will never be unknown here). In the course of determining -// the properties specifically requested in the mask, certain other -// properties may be determined (those with little additional expense) -// and their values will be returned as well. The complete set of -// known properties (whether true or false) determined by this -// operation will be assigned to the the value pointed to by KNOWN. -// If 'use_stored' is true, pre-computed FST properties may be used -// when possible. This routine is seldom called directly; instead it -// is used to implement fst.Properties(mask, true). -template<class Arc> -uint64 ComputeProperties(const Fst<Arc> &fst, uint64 mask, uint64 *known, - bool use_stored) { - typedef typename Arc::Label Label; - typedef typename Arc::Weight Weight; - typedef typename Arc::StateId StateId; - - uint64 fst_props = fst.Properties(kFstProperties, false); // Fst-stored - - // Check stored FST properties first if allowed. - if (use_stored) { - uint64 known_props = KnownProperties(fst_props); - // If FST contains required info, return it. - if ((known_props & mask) == mask) { - *known = known_props; - return fst_props; - } - } - - // Compute (trinary) properties explicitly. - - // Initialize with binary properties (already known). - uint64 comp_props = fst_props & kBinaryProperties; - - // Compute these trinary properties with a DFS. We compute only those - // that need a DFS here, since we otherwise would like to avoid a DFS - // since its stack could grow large. - uint64 dfs_props = kCyclic | kAcyclic | kInitialCyclic | kInitialAcyclic | - kAccessible | kNotAccessible | - kCoAccessible | kNotCoAccessible; - if (mask & dfs_props) { - SccVisitor<Arc> scc_visitor(&comp_props); - DfsVisit(fst, &scc_visitor); - } - - // Compute any remaining trinary properties via a state and arcs iterations - if (mask & ~(kBinaryProperties | dfs_props)) { - comp_props |= kAcceptor | kNoEpsilons | kNoIEpsilons | kNoOEpsilons | - kILabelSorted | kOLabelSorted | kUnweighted | kTopSorted | kString; - if (mask & (kIDeterministic | kNonIDeterministic)) - comp_props |= kIDeterministic; - if (mask & (kODeterministic | kNonODeterministic)) - comp_props |= kODeterministic; - - hash_set<Label> *ilabels = 0; - hash_set<Label> *olabels = 0; - - StateId nfinal = 0; - for (StateIterator< Fst<Arc> > siter(fst); - !siter.Done(); - siter.Next()) { - StateId s = siter.Value(); - - Arc prev_arc(kNoLabel, kNoLabel, Weight::One(), 0); - // Create these only if we need to - if (mask & (kIDeterministic | kNonIDeterministic)) - ilabels = new hash_set<Label>; - if (mask & (kODeterministic | kNonODeterministic)) - olabels = new hash_set<Label>; - - for (ArcIterator< Fst<Arc> > aiter(fst, s); - !aiter.Done(); - aiter.Next()) { - const Arc &arc =aiter.Value(); - - if (ilabels && ilabels->find(arc.ilabel) != ilabels->end()) { - comp_props |= kNonIDeterministic; - comp_props &= ~kIDeterministic; - } - if (olabels && olabels->find(arc.olabel) != olabels->end()) { - comp_props |= kNonODeterministic; - comp_props &= ~kODeterministic; - } - if (arc.ilabel != arc.olabel) { - comp_props |= kNotAcceptor; - comp_props &= ~kAcceptor; - } - if (arc.ilabel == 0 && arc.olabel == 0) { - comp_props |= kEpsilons; - comp_props &= ~kNoEpsilons; - } - if (arc.ilabel == 0) { - comp_props |= kIEpsilons; - comp_props &= ~kNoIEpsilons; - } - if (arc.olabel == 0) { - comp_props |= kOEpsilons; - comp_props &= ~kNoOEpsilons; - } - if (prev_arc.ilabel != kNoLabel && arc.ilabel < prev_arc.ilabel) { - comp_props |= kNotILabelSorted; - comp_props &= ~kILabelSorted; - } - if (prev_arc.olabel != kNoLabel && arc.olabel < prev_arc.olabel) { - comp_props |= kNotOLabelSorted; - comp_props &= ~kOLabelSorted; - } - if (arc.weight != Weight::One() && arc.weight != Weight::Zero()) { - comp_props |= kWeighted; - comp_props &= ~kUnweighted; - } - if (arc.nextstate <= s) { - comp_props |= kNotTopSorted; - comp_props &= ~kTopSorted; - } - if (arc.nextstate != s + 1) { - comp_props |= kNotString; - comp_props &= ~kString; - } - prev_arc = arc; - if (ilabels) - ilabels->insert(arc.ilabel); - if (olabels) - olabels->insert(arc.olabel); - } - - if (nfinal > 0) { // final state not last - comp_props |= kNotString; - comp_props &= ~kString; - } - - Weight final = fst.Final(s); - - if (final != Weight::Zero()) { // final state - if (final != Weight::One()) { - comp_props |= kWeighted; - comp_props &= ~kUnweighted; - } - ++nfinal; - } else { // non-final state - if (fst.NumArcs(s) != 1) { - comp_props |= kNotString; - comp_props &= ~kString; - } - } - - delete ilabels; - delete olabels; - } - - if (fst.Start() != kNoStateId && fst.Start() != 0) { - comp_props |= kNotString; - comp_props &= ~kString; - } - } - - *known = KnownProperties(comp_props); - return comp_props; -} - -// This is a wrapper around ComputeProperties that will cause a fatal -// error if the stored properties and the computed properties are -// incompatible when 'FLAGS_fst_verify_properties' is true. This -// routine is seldom called directly; instead it is used to implement -// fst.Properties(mask, true). -template<class Arc> -uint64 TestProperties(const Fst<Arc> &fst, uint64 mask, uint64 *known) { - if (FLAGS_fst_verify_properties) { - uint64 stored_props = fst.Properties(kFstProperties, false); - uint64 computed_props = ComputeProperties(fst, mask, known, false); - if (!CompatProperties(stored_props, computed_props)) - LOG(FATAL) << "TestProperties: stored Fst properties incorrect" - << " (stored: props1, computed: props2)"; - return computed_props; - } else { - return ComputeProperties(fst, mask, known, true); - } -} - -} // namespace fst - -#endif // FST_LIB_TEST_PROPERTIES_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/topsort.h b/tools/thirdparty/OpenFst/fst/lib/topsort.h deleted file mode 100644 index 4c5706d..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/topsort.h +++ /dev/null @@ -1,107 +0,0 @@ -// topsort.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Topological sort of FSTs - -#ifndef FST_LIB_TOPSORT_H__ -#define FST_LIB_TOPSORT_H__ - -#include <algorithm> -#include <vector> - -#include "fst/lib/dfs-visit.h" -#include "fst/lib/fst.h" -#include "fst/lib/statesort.h" - -namespace fst { - -// DFS visitor class to return topological ordering. -template <class A> -class TopOrderVisitor { - public: - typedef A Arc; - typedef typename A::StateId StateId; - - // If acyclic, ORDER[i] gives the topological position of state Id i; - // otherwise unchanged. ACYCLIC will be true iff the FST has - // no cycles. - TopOrderVisitor(vector<StateId> *order, bool *acyclic) - : order_(order), acyclic_(acyclic) {} - - void InitVisit(const Fst<A> &fst) { - finish_ = new vector<StateId>; - *acyclic_ = true; - } - - bool InitState(StateId s, StateId r) { return true; } - - bool TreeArc(StateId s, const A &arc) { return true; } - - bool BackArc(StateId s, const A &arc) { return (*acyclic_ = false); } - - bool ForwardOrCrossArc(StateId s, const A &arc) { return true; } - - void FinishState(StateId s, StateId p, const A *) { finish_->push_back(s); } - - void FinishVisit() { - if (*acyclic_) { - order_->clear(); - for (StateId s = 0; s < (StateId)finish_->size(); ++s) - order_->push_back(kNoStateId); - for (StateId s = 0; s < (StateId)finish_->size(); ++s) - (*order_)[(*finish_)[finish_->size() - s - 1]] = s; - } - delete finish_; - } - - private: - vector<StateId> *order_; - bool *acyclic_; - vector<StateId> *finish_; // states in finishing-time order -}; - - -// Topologically sorts its input if acyclic, modifying it. Otherwise, -// the input is unchanged. When sorted, all transitions are from -// lower to higher state IDs. -// -// Complexity: -// - Time: O(V + E) -// - Space: O(V + E) -// where V = # of states and E = # of arcs. -template <class Arc> -bool TopSort(MutableFst<Arc> *fst) { - typedef typename Arc::StateId StateId; - - vector<StateId> order; - bool acyclic; - - TopOrderVisitor<Arc> top_order_visitor(&order, &acyclic); - DfsVisit(*fst, &top_order_visitor); - - if (acyclic) { - StateSort(fst, order); - fst->SetProperties(kAcyclic | kInitialAcyclic | kTopSorted, - kAcyclic | kInitialAcyclic | kTopSorted); - } else { - fst->SetProperties(kCyclic | kNotTopSorted, kCyclic | kNotTopSorted); - } - return acyclic; -} - -} // namespace fst - -#endif // FST_LIB_TOPSORT_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/union-find.h b/tools/thirdparty/OpenFst/fst/lib/union-find.h deleted file mode 100644 index a85edac..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/union-find.h +++ /dev/null @@ -1,107 +0,0 @@ -// union-find.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file Union-Find algorithm for dense sets of non-negative -// integers. Implemented using disjoint tree forests with rank -// heuristics and path compression. - -#ifndef __fst_union_find_inl_h__ -#define __fst_union_find_inl_h__ - -#include <stack> -#include <vector> - -namespace fst { - -// Union-Find algorithm for dense sets of non-negative integers -// (exact type: T). -template <class T> -class UnionFind { - public: - // Ctor: creates a disjoint set forest for the range [0;max). - // 'fail' is a value indicating that an element hasn't been - // initialized using MakeSet(...). The upper bound of the range - // can be reset (increased) using MakeSet(...). - UnionFind(T max, T fail) - : parent_(max, fail), rank_(max), fail_(fail) { } - - // Finds the representative of the set 'item' belongs to. - // Performs path compression if needed. - T FindSet(T item) { - if (item >= parent_.size() - || item == fail_ - || parent_[item] == fail_) return fail_; - - CHECK(exec_stack_.empty()); - T *p = &parent_[item]; - for (; *p != item; item = *p, p = &parent_[item]) { - exec_stack_.push(p); - } - for (; ! exec_stack_.empty(); exec_stack_.pop()) { - *exec_stack_.top() = *p; - } - return *p; - } - - // Creates the (destructive) union of the sets x and y belong to. - void Union(T x, T y) { - Link(FindSet(x), FindSet(y)); - } - - // Initialization of an element: creates a singleton set containing - // 'item'. The range [0;max) is reset if item >= max. - T MakeSet(T item) { - if (item >= parent_.size()) { - // New value in parent_ should be initialized to fail_ - parent_.resize(2 * item, fail_); - rank_.resize(2 * item); - } - parent_[item] = item; - return item; - } - - // Initialization of all elements starting from 0 to max - 1 to distinct sets - void MakeAllSet(T max) { - parent_.resize(max); - for (T item = 0; item < max; ++item) { - parent_[item] = item; - } - } - - private: - vector<T> parent_; // Parent nodes. - vector<int> rank_; // Rank of an element = min. depth in tree. - T fail_; // Value indicating lookup failure. - stack<T*> exec_stack_; // Used for path compression. - - // Links trees rooted in 'x' and 'y'. - void Link(T x, T y) { - if (x == y) return; - - if (rank_[x] > rank_[y]) { - parent_[y] = x; - } else { - parent_[x] = y; - if (rank_[x] == rank_[y]) { - ++rank_[y]; - } - } - } - DISALLOW_EVIL_CONSTRUCTORS(UnionFind); -}; - -} - -#endif // __fst_union_find_inl_h__ diff --git a/tools/thirdparty/OpenFst/fst/lib/union.h b/tools/thirdparty/OpenFst/fst/lib/union.h deleted file mode 100644 index ff95ef0..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/union.h +++ /dev/null @@ -1,155 +0,0 @@ -// union.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Functions and classes to compute the union of two FSTs. - -#ifndef FST_LIB_UNION_H__ -#define FST_LIB_UNION_H__ - -#include "fst/lib/mutable-fst.h" -#include "fst/lib/rational.h" - -namespace fst { - -// Computes the union (sum) of two FSTs. This version writes the -// union to an output MurableFst. If A transduces string x to y with -// weight a and B transduces string w to v with weight b, then their -// union transduces x to y with weight a and w to v with weight b. -// -// Complexity: -// - Time: (V2 + E2) -// - Space: O(V2 + E2) -// where Vi = # of states and Ei = # of arcs of the ith FST. -template <class Arc> -void Union(MutableFst<Arc> *fst1, const Fst<Arc> &fst2) { - typedef typename Arc::StateId StateId; - typedef typename Arc::Label Label; - typedef typename Arc::Weight Weight; - - StateId start2 = fst2.Start(); - if (start2 == kNoStateId) - return; - - StateId numstates1 = fst1->NumStates(); - bool initial_acyclic1 = fst1->Properties(kInitialAcyclic, true); - uint64 props1 = fst1->Properties(kFstProperties, false); - uint64 props2 = fst2.Properties(kFstProperties, false); - - for (StateIterator< Fst<Arc> > siter(fst2); - !siter.Done(); - siter.Next()) { - StateId s1 = fst1->AddState(); - StateId s2 = siter.Value(); - fst1->SetFinal(s1, fst2.Final(s2)); - for (ArcIterator< Fst<Arc> > aiter(fst2, s2); - !aiter.Done(); - aiter.Next()) { - Arc arc = aiter.Value(); - arc.nextstate += numstates1; - fst1->AddArc(s1, arc); - } - } - StateId start1 = fst1->Start(); - if (start1 == kNoStateId) { - fst1->SetStart(start2); - fst1->SetProperties(props2, kCopyProperties); - return; - } - - if (initial_acyclic1) { - fst1->AddArc(start1, Arc(0, 0, Weight::One(), start2 + numstates1)); - } else { - StateId nstart1 = fst1->AddState(); - fst1->SetStart(nstart1); - fst1->AddArc(nstart1, Arc(0, 0, Weight::One(), start1)); - fst1->AddArc(nstart1, Arc(0, 0, Weight::One(), start2 + numstates1)); - } - fst1->SetProperties(UnionProperties(props1, props2), kFstProperties); -} - - -// Computes the union of two FSTs; this version modifies its -// RationalFst argument. -template<class Arc> -void Union(RationalFst<Arc> *fst1, const Fst<Arc> &fst2) { - fst1->Impl()->AddUnion(fst2); -} - - -typedef RationalFstOptions UnionFstOptions; - - -// Computes the union (sum) of two FSTs. This version is a delayed -// Fst. If A transduces string x to y with weight a and B transduces -// string w to v with weight b, then their union transduces x to y -// with weight a and w to v with weight b. -// -// Complexity: -// - Time: O(v1 + e1 + v2 + e2) -// - Sapce: O(v1 + v2) -// where vi = # of states visited and ei = # of arcs visited of the -// ith FST. Constant time and space to visit an input state or arc -// is assumed and exclusive of caching. -template <class A> -class UnionFst : public RationalFst<A> { - public: - using RationalFst<A>::Impl; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - UnionFst(const Fst<A> &fst1, const Fst<A> &fst2) { - Impl()->InitUnion(fst1, fst2); - } - - UnionFst(const Fst<A> &fst1, const Fst<A> &fst2, const UnionFstOptions &opts) - : RationalFst<A>(opts) { - Impl()->InitUnion(fst1, fst2); - } - - UnionFst(const UnionFst<A> &fst) : RationalFst<A>(fst) {} - - virtual UnionFst<A> *Copy() const { return new UnionFst<A>(*this); } -}; - - -// Specialization for UnionFst. -template <class A> -class StateIterator< UnionFst<A> > : public StateIterator< RationalFst<A> > { - public: - explicit StateIterator(const UnionFst<A> &fst) - : StateIterator< RationalFst<A> >(fst) {} -}; - - -// Specialization for UnionFst. -template <class A> -class ArcIterator< UnionFst<A> > : public ArcIterator< RationalFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const UnionFst<A> &fst, StateId s) - : ArcIterator< RationalFst<A> >(fst, s) {} -}; - - -// Useful alias when using StdArc. -typedef UnionFst<StdArc> StdUnionFst; - -} // namespace fst - -#endif // FST_LIB_UNION_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/util.h b/tools/thirdparty/OpenFst/fst/lib/util.h deleted file mode 100644 index d899929..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/util.h +++ /dev/null @@ -1,69 +0,0 @@ -// util.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// FST utility inline definitions. - -#ifndef FST_LIB_UTIL_H__ -#define FST_LIB_UTIL_H__ - -#include <iostream> - -namespace fst { - -// -// UTILITIES FOR TYPE I/O -// - -// Read some types from an input stream. - -// Generic case. -template <typename T> -inline istream &ReadType(istream &strm, T *t) { - return strm.read(reinterpret_cast<char *>(t), sizeof(T)); -} - -// String case. -inline istream &ReadType(istream &strm, string *s) { - s->clear(); - int32 ns = 0; - strm.read(reinterpret_cast<char *>(&ns), sizeof(ns)); - for (int i = 0; i < ns; ++i) { - char c; - strm.read(&c, 1); - *s += c; - } - return strm; -} - -// Write some types to an output stream. - -// Generic case. -template <typename T> -inline ostream &WriteType(ostream &strm, const T t) { - return strm.write(reinterpret_cast<const char *>(&t), sizeof(T)); -} - -// String case. -inline ostream &WriteType(ostream &strm, const string s) { - int32 ns = s.size(); - strm.write(reinterpret_cast<const char *>(&ns), sizeof(ns)); - return strm.write(s.data(), ns); -} - - -} // namespace fst; - -#endif // FST_LIB_UTIL_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/vector-fst.h b/tools/thirdparty/OpenFst/fst/lib/vector-fst.h deleted file mode 100644 index 8e99521..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/vector-fst.h +++ /dev/null @@ -1,798 +0,0 @@ -// vector-fst.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Simple concrete, mutable FST whose states and arcs are stored in STL -// vectors. - -#ifndef FST_LIB_VECTOR_FST_H__ -#define FST_LIB_VECTOR_FST_H__ - -#include <string> -#include <string.h> - -#include "fst/lib/mutable-fst.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -template <class A> class VectorFst; - -// States and arcs implemented by STL vectors, templated on the -// State definition. This does not manage the Fst properties. -template <class State> -class VectorFstBaseImpl : public FstImpl<typename State::Arc> { - public: - typedef typename State::Arc Arc; - typedef typename Arc::Weight Weight; - typedef typename Arc::StateId StateId; - - VectorFstBaseImpl() : start_(kNoStateId) {} - - ~VectorFstBaseImpl() { - for (StateId s = 0; s < (StateId)states_.size(); ++s) - delete states_[s]; - } - - StateId Start() const { return start_; } - - Weight Final(StateId s) const { return states_[s]->final; } - - StateId NumStates() const { return states_.size(); } - - size_t NumArcs(StateId s) const { return states_[s]->arcs.size(); } - - void SetStart(StateId s) { start_ = s; } - - void SetFinal(StateId s, Weight w) { states_[s]->final = w; } - - StateId AddState() { - states_.push_back(new State); - return states_.size() - 1; - } - - StateId AddState(State *state) { - states_.push_back(state); - return states_.size() - 1; - } - - void AddArc(StateId s, const Arc &arc) { - states_[s]->arcs.push_back(arc); - } - - void DeleteStates(const vector<StateId>& dstates) { - vector<StateId> newid(states_.size(), 0); - for (size_t i = 0; i < dstates.size(); ++i) - newid[dstates[i]] = kNoStateId; - StateId nstates = 0; - for (StateId s = 0; s < (StateId)states_.size(); ++s) { - if (newid[s] != kNoStateId) { - newid[s] = nstates; - if (s != nstates) - states_[nstates] = states_[s]; - ++nstates; - } else { - delete states_[s]; - } - } - states_.resize(nstates); - for (StateId s = 0; s < (StateId)states_.size(); ++s) { - vector<Arc> &arcs = states_[s]->arcs; - size_t narcs = 0; - for (size_t i = 0; i < arcs.size(); ++i) { - StateId t = newid[arcs[i].nextstate]; - if (t != kNoStateId) { - arcs[i].nextstate = t; - if (i != narcs) - arcs[narcs] = arcs[i]; - ++narcs; - } else { - if (arcs[i].ilabel == 0) - --states_[s]->niepsilons; - if (arcs[i].olabel == 0) - --states_[s]->noepsilons; - } - } - arcs.resize(narcs); - } - if (Start() != kNoStateId) - SetStart(newid[Start()]); - } - - void DeleteStates() { - for (StateId s = 0; s < (StateId)states_.size(); ++s) - delete states_[s]; - states_.clear(); - SetStart(kNoStateId); - } - - void DeleteArcs(StateId s, size_t n) { - states_[s]->arcs.resize(states_[s]->arcs.size() - n); - } - - void DeleteArcs(StateId s) { states_[s]->arcs.clear(); } - - State *GetState(StateId s) { return states_[s]; } - - const State *GetState(StateId s) const { return states_[s]; } - - void SetState(StateId s, State *state) { states_[s] = state; } - - void ReserveStates(StateId n) { states_.reserve(n); } - - void ReserveArcs(StateId s, size_t n) { states_[s]->arcs.reserve(n); } - - // Provide information needed for generic state iterator - void InitStateIterator(StateIteratorData<Arc> *data) const { - data->base = 0; - data->nstates = states_.size(); - } - - // Provide information needed for generic arc iterator - void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const { - data->base = 0; - data->narcs = states_[s]->arcs.size(); - data->arcs = data->narcs > 0 ? &states_[s]->arcs[0] : 0; - data->ref_count = 0; - } - - private: - vector<State *> states_; // States represenation. - StateId start_; // initial state - - DISALLOW_EVIL_CONSTRUCTORS(VectorFstBaseImpl); -}; - -// Arcs implemented by an STL vector per state. -template <class A> -struct VectorState { - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - VectorState() : final(Weight::Zero()), niepsilons(0), noepsilons(0) {} - - Weight final; // Final weight - vector<A> arcs; // Arcs represenation - size_t niepsilons; // # of input epsilons - size_t noepsilons; // # of output epsilons -}; - -// This is a VectorFstBaseImpl container that holds VectorState's. It -// manages Fst properties and the # of input and output epsilons. -template <class A> -class VectorFstImpl : public VectorFstBaseImpl< VectorState<A> > { - public: - using FstImpl<A>::SetType; - using FstImpl<A>::SetProperties; - using FstImpl<A>::Properties; - using FstImpl<A>::WriteHeaderAndSymbols; - - using VectorFstBaseImpl<VectorState<A> >::Start; - using VectorFstBaseImpl<VectorState<A> >::NumStates; - - friend class MutableArcIterator< VectorFst<A> >; - - typedef VectorFstBaseImpl< VectorState<A> > BaseImpl; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - VectorFstImpl() { - SetType("vector"); - SetProperties(kNullProperties | kStaticProperties); - } - explicit VectorFstImpl(const Fst<A> &fst); - - static VectorFstImpl<A> *Read(istream &strm, const FstReadOptions &opts); - - size_t NumInputEpsilons(StateId s) const { return GetState(s)->niepsilons; } - - size_t NumOutputEpsilons(StateId s) const { return GetState(s)->noepsilons; } - - bool Write(ostream &strm, const FstWriteOptions &opts) const; - - void SetStart(StateId s) { - BaseImpl::SetStart(s); - SetProperties(Properties() & kSetStartProperties); - if (Properties() & kAcyclic) - SetProperties(Properties() | kInitialAcyclic); - } - - void SetFinal(StateId s, Weight w) { - Weight ow = Final(s); - if (ow != Weight::Zero() && ow != Weight::One()) - SetProperties(Properties() & ~kWeighted); - BaseImpl::SetFinal(s, w); - if (w != Weight::Zero() && w != Weight::One()) { - SetProperties(Properties() | kWeighted); - SetProperties(Properties() & ~kUnweighted); - } - SetProperties(Properties() & - (kSetFinalProperties | kWeighted | kUnweighted)); - } - - StateId AddState() { - StateId s = BaseImpl::AddState(); - SetProperties(Properties() & kAddStateProperties); - return s; - } - - void AddArc(StateId s, const A &arc) { - VectorState<A> *state = GetState(s); - if (arc.ilabel != arc.olabel) { - SetProperties(Properties() | kNotAcceptor); - SetProperties(Properties() & ~kAcceptor); - } - if (arc.ilabel == 0) { - ++state->niepsilons; - SetProperties(Properties() | kIEpsilons); - SetProperties(Properties() & ~kNoIEpsilons); - if (arc.olabel == 0) { - SetProperties(Properties() | kEpsilons); - SetProperties(Properties() & ~kNoEpsilons); - } - } - if (arc.olabel == 0) { - ++state->noepsilons; - SetProperties(Properties() | kOEpsilons); - SetProperties(Properties() & ~kNoOEpsilons); - } - if (!state->arcs.empty()) { - A &parc = state->arcs.back(); - if (parc.ilabel > arc.ilabel) { - SetProperties(Properties() | kNotILabelSorted); - SetProperties(Properties() & ~kILabelSorted); - } - if (parc.olabel > arc.olabel) { - SetProperties(Properties() | kNotOLabelSorted); - SetProperties(Properties() & ~kOLabelSorted); - } - } - if (arc.weight != Weight::Zero() && arc.weight != Weight::One()) { - SetProperties(Properties() | kWeighted); - SetProperties(Properties() & ~kUnweighted); - } - if (arc.nextstate <= s) { - SetProperties(Properties() | kNotTopSorted); - SetProperties(Properties() & ~kTopSorted); - } - SetProperties(Properties() & - (kAddArcProperties | kAcceptor | - kNoEpsilons | kNoIEpsilons | kNoOEpsilons | - kILabelSorted | kOLabelSorted | kUnweighted | kTopSorted)); - if (Properties() & kTopSorted) - SetProperties(Properties() | kAcyclic | kInitialAcyclic); - BaseImpl::AddArc(s, arc); - } - - void DeleteStates(const vector<StateId> &dstates) { - BaseImpl::DeleteStates(dstates); - SetProperties(Properties() & kDeleteStatesProperties); - } - - void DeleteStates() { - BaseImpl::DeleteStates(); - SetProperties(kNullProperties | kStaticProperties); - } - - void DeleteArcs(StateId s, size_t n) { - const vector<A> &arcs = GetState(s)->arcs; - for (size_t i = 0; i < n; ++i) { - size_t j = arcs.size() - i - 1; - if (arcs[j].ilabel == 0) - --GetState(s)->niepsilons; - if (arcs[j].olabel == 0) - --GetState(s)->noepsilons; - } - BaseImpl::DeleteArcs(s, n); - SetProperties(Properties() & kDeleteArcsProperties); - } - - void DeleteArcs(StateId s) { - GetState(s)->niepsilons = 0; - GetState(s)->noepsilons = 0; - BaseImpl::DeleteArcs(s); - SetProperties(Properties() & kDeleteArcsProperties); - } - - private: - // Properties always true of this Fst class - static const uint64 kStaticProperties = kExpanded | kMutable; - // Current file format version - static const int kFileVersion = 2; - // Minimum file format version supported - static const int kMinFileVersion = 2; - - DISALLOW_EVIL_CONSTRUCTORS(VectorFstImpl); -}; - -template <class A> -VectorFstImpl<A>::VectorFstImpl(const Fst<A> &fst) { - SetType("vector"); - SetProperties(fst.Properties(kCopyProperties, false) | kStaticProperties); - SetInputSymbols(fst.InputSymbols()); - SetOutputSymbols(fst.OutputSymbols()); - BaseImpl::SetStart(fst.Start()); - - for (StateIterator< Fst<A> > siter(fst); - !siter.Done(); - siter.Next()) { - StateId s = siter.Value(); - BaseImpl::AddState(); - BaseImpl::SetFinal(s, fst.Final(s)); - ReserveArcs(s, fst.NumArcs(s)); - for (ArcIterator< Fst<A> > aiter(fst, s); - !aiter.Done(); - aiter.Next()) { - const A &arc = aiter.Value(); - BaseImpl::AddArc(s, arc); - if (arc.ilabel == 0) - ++GetState(s)->niepsilons; - if (arc.olabel == 0) - ++GetState(s)->noepsilons; - } - } -} - -template <class A> -VectorFstImpl<A> *VectorFstImpl<A>::Read(istream &strm, - const FstReadOptions &opts) { - VectorFstImpl<A> *impl = new VectorFstImpl; - FstHeader hdr; - if (!impl->ReadHeaderAndSymbols(strm, opts, kMinFileVersion, &hdr)) - return 0; - impl->BaseImpl::SetStart(hdr.Start()); - impl->ReserveStates(hdr.NumStates()); - - for (StateId s = 0; s < hdr.NumStates(); ++s) { - impl->BaseImpl::AddState(); - VectorState<A> *state = impl->GetState(s); - state->final.Read(strm); - int64 narcs; - ReadType(strm, &narcs); - if (!strm) { - LOG(ERROR) << "VectorFst::Read: read failed: " << opts.source; - return 0; - } - impl->ReserveArcs(s, narcs); - for (size_t j = 0; j < narcs; ++j) { - A arc; - ReadType(strm, &arc.ilabel); - ReadType(strm, &arc.olabel); - arc.weight.Read(strm); - ReadType(strm, &arc.nextstate); - if (!strm) { - LOG(ERROR) << "VectorFst::Read: read failed: " << opts.source; - return 0; - } - impl->BaseImpl::AddArc(s, arc); - if (arc.ilabel == 0) - ++state->niepsilons; - if (arc.olabel == 0) - ++state->noepsilons; - } - } - return impl; -} - -// Converts a string into a weight. -template <class W> class WeightFromString { - public: - W operator()(const string &s); -}; - -// Generic case fails. -template <class W> inline -W WeightFromString<W>::operator()(const string &s) { - LOG(FATAL) << "VectorFst::Read: Obsolete file format"; - return W(); -} - -// TropicalWeight version. -template <> inline -TropicalWeight WeightFromString<TropicalWeight>::operator()(const string &s) { - float f; - memcpy(&f, s.data(), sizeof(f)); - return TropicalWeight(f); -} - -// LogWeight version. -template <> inline -LogWeight WeightFromString<LogWeight>::operator()(const string &s) { - float f; - memcpy(&f, s.data(), sizeof(f)); - return LogWeight(f); -} - -template <class A> -bool VectorFstImpl<A>::Write(ostream &strm, - const FstWriteOptions &opts) const { - FstHeader hdr; - hdr.SetStart(Start()); - hdr.SetNumStates(NumStates()); - WriteHeaderAndSymbols(strm, opts, kFileVersion, &hdr); - if (!strm) - return false; - - for (StateId s = 0; s < NumStates(); ++s) { - const VectorState<A> *state = GetState(s); - state->final.Write(strm); - int64 narcs = state->arcs.size(); - WriteType(strm, narcs); - for (size_t a = 0; a < narcs; ++a) { - const A &arc = state->arcs[a]; - WriteType(strm, arc.ilabel); - WriteType(strm, arc.olabel); - arc.weight.Write(strm); - WriteType(strm, arc.nextstate); - } - } - strm.flush(); - if (!strm) - LOG(ERROR) << "VectorFst::Write: write failed: " << opts.source; - return strm; -} - -// Simple concrete, mutable FST. Supports additional operations: -// ReserveStates and ReserveArcs (cf. STL vectors). This class -// attaches interface to implementation and handles reference -// counting. -template <class A> -class VectorFst : public MutableFst<A> { - public: - friend class StateIterator< VectorFst<A> >; - friend class ArcIterator< VectorFst<A> >; - friend class MutableArcIterator< VectorFst<A> >; - - typedef A Arc; - typedef typename A::Weight Weight; - typedef typename A::StateId StateId; - - VectorFst() : impl_(new VectorFstImpl<A>) {} - - VectorFst(const VectorFst<A> &fst) : MutableFst<A>(fst), impl_(fst.impl_) { - impl_->IncrRefCount(); - } - explicit VectorFst(const Fst<A> &fst) : impl_(new VectorFstImpl<A>(fst)) {} - - virtual ~VectorFst() { if (!impl_->DecrRefCount()) delete impl_; } - - VectorFst<A> &operator=(const VectorFst<A> &fst) { - if (this != &fst) { - if (!impl_->DecrRefCount()) delete impl_; - fst.impl_->IncrRefCount(); - impl_ = fst.impl_; - } - return *this; - } - - virtual VectorFst<A> &operator=(const Fst<A> &fst) { - if (this != &fst) { - if (!impl_->DecrRefCount()) delete impl_; - impl_ = new VectorFstImpl<A>(fst); - } - return *this; - } - - virtual StateId Start() const { return impl_->Start(); } - - virtual Weight Final(StateId s) const { return impl_->Final(s); } - - virtual StateId NumStates() const { return impl_->NumStates(); } - - virtual size_t NumArcs(StateId s) const { return impl_->NumArcs(s); } - - virtual size_t NumInputEpsilons(StateId s) const { - return impl_->NumInputEpsilons(s); - } - - virtual size_t NumOutputEpsilons(StateId s) const { - return impl_->NumOutputEpsilons(s); - } - - virtual uint64 Properties(uint64 mask, bool test) const { - if (test) { - uint64 known, test = TestProperties(*this, mask, &known); - impl_->SetProperties(test, known); - return test & mask; - } else { - return impl_->Properties(mask); - } - } - - virtual const string& Type() const { return impl_->Type(); } - - // Get a copy of this VectorFst - virtual VectorFst<A> *Copy() const { - impl_->IncrRefCount(); - return new VectorFst<A>(impl_); - } - - // Read a VectorFst from an input stream; return NULL on error - static VectorFst<A> *Read(istream &strm, const FstReadOptions &opts) { - VectorFstImpl<A>* impl = VectorFstImpl<A>::Read(strm, opts); - return impl ? new VectorFst<A>(impl) : 0; - } - - // Read a VectorFst from a file; return NULL on error - static VectorFst<A> *Read(const string &filename) { - ifstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "VectorFst::Read: Can't open file: " << filename; - return 0; - } - return Read(strm, FstReadOptions(filename)); - } - - // Write a VectorFst to an output stream; return false on error - virtual bool Write(ostream &strm, const FstWriteOptions &opts) const { - return impl_->Write(strm, opts); - } - - // Write a VectorFst to a file; return false on error - virtual bool Write(const string &filename) const { - if (!filename.empty()) { - ofstream strm(filename.c_str()); - if (!strm) { - LOG(ERROR) << "VectorFst::Write: Can't open file: " << filename; - return false; - } - return Write(strm, FstWriteOptions(filename)); - } else { - return Write(std::cout, FstWriteOptions("standard output")); - } - } - - virtual SymbolTable* InputSymbols() { - return impl_->InputSymbols(); - } - - virtual SymbolTable* OutputSymbols() { - return impl_->OutputSymbols(); - } - - virtual const SymbolTable* InputSymbols() const { - return impl_->InputSymbols(); - } - - virtual const SymbolTable* OutputSymbols() const { - return impl_->OutputSymbols(); - } - - virtual void SetStart(StateId s) { - MutateCheck(); - impl_->SetStart(s); - } - - virtual void SetFinal(StateId s, Weight w) { - MutateCheck(); - impl_->SetFinal(s, w); - } - - virtual void SetProperties(uint64 props, uint64 mask) { - impl_->SetProperties(props, mask); - } - - virtual StateId AddState() { - MutateCheck(); - return impl_->AddState(); - } - - virtual void AddArc(StateId s, const A &arc) { - MutateCheck(); - impl_->AddArc(s, arc); - } - - virtual void DeleteStates(const vector<StateId> &dstates) { - MutateCheck(); - impl_->DeleteStates(dstates); - } - - virtual void DeleteStates() { - MutateCheck(); - impl_->DeleteStates(); - } - - virtual void DeleteArcs(StateId s, size_t n) { - MutateCheck(); - impl_->DeleteArcs(s, n); - } - - virtual void DeleteArcs(StateId s) { - MutateCheck(); - impl_->DeleteArcs(s); - } - - virtual void SetInputSymbols(const SymbolTable* isyms) { - MutateCheck(); - impl_->SetInputSymbols(isyms); - } - - virtual void SetOutputSymbols(const SymbolTable* osyms) { - MutateCheck(); - impl_->SetOutputSymbols(osyms); - } - - void ReserveStates(StateId n) { - MutateCheck(); - impl_->ReserveStates(n); - } - - void ReserveArcs(StateId s, size_t n) { - MutateCheck(); - impl_->ReserveArcs(s, n); - } - - virtual void InitStateIterator(StateIteratorData<A> *data) const { - impl_->InitStateIterator(data); - } - - virtual void InitArcIterator(StateId s, ArcIteratorData<A> *data) const { - impl_->InitArcIterator(s, data); - } - - virtual inline - void InitMutableArcIterator(StateId s, MutableArcIteratorData<A> *); - - private: - explicit VectorFst(VectorFstImpl<A> *impl) : impl_(impl) {} - - void MutateCheck() { - // Copy on write - if (impl_->RefCount() > 1) { - impl_->DecrRefCount(); - impl_ = new VectorFstImpl<A>(*this); - } - } - - VectorFstImpl<A> *impl_; // FST's impl -}; - -// Specialization for VectorFst; see generic version in fst.h -// for sample usage (but use the VectorFst type!). This version -// should inline. -template <class A> -class StateIterator< VectorFst<A> > { - public: - typedef typename A::StateId StateId; - - explicit StateIterator(const VectorFst<A> &fst) - : nstates_(fst.impl_->NumStates()), s_(0) {} - - bool Done() const { return s_ >= nstates_; } - - StateId Value() const { return s_; } - - void Next() { ++s_; } - - void Reset() { s_ = 0; } - - private: - StateId nstates_; - StateId s_; - - DISALLOW_EVIL_CONSTRUCTORS(StateIterator); -}; - -// Specialization for VectorFst; see generic version in fst.h -// for sample usage (but use the VectorFst type!). This version -// should inline. -template <class A> -class ArcIterator< VectorFst<A> > { - public: - typedef typename A::StateId StateId; - - ArcIterator(const VectorFst<A> &fst, StateId s) - : arcs_(fst.impl_->GetState(s)->arcs), i_(0) {} - - bool Done() const { return i_ >= arcs_.size(); } - - const A& Value() const { return arcs_[i_]; } - - void Next() { ++i_; } - - void Reset() { i_ = 0; } - - void Seek(size_t a) { i_ = a; } - - private: - const vector<A>& arcs_; - size_t i_; - - DISALLOW_EVIL_CONSTRUCTORS(ArcIterator); -}; - -// Specialization for VectorFst; see generic version in fst.h -// for sample usage (but use the VectorFst type!). This version -// should inline. -template <class A> -class MutableArcIterator< VectorFst<A> > - : public MutableArcIteratorBase<A> { - public: - typedef typename A::StateId StateId; - typedef typename A::Weight Weight; - - MutableArcIterator(VectorFst<A> *fst, StateId s) : i_(0) { - fst->MutateCheck(); - state_ = fst->impl_->GetState(s); - properties_ = &fst->impl_->properties_; - } - - virtual bool Done() const { return i_ >= state_->arcs.size(); } - - virtual const A& Value() const { return state_->arcs[i_]; } - - virtual void Next() { ++i_; } - - virtual void Reset() { i_ = 0; } - - virtual void Seek(size_t a) { i_ = a; } - - virtual void SetValue(const A &arc) { - A& oarc = state_->arcs[i_]; - if (oarc.ilabel != oarc.olabel) - *properties_ &= ~kNotAcceptor; - if (oarc.ilabel == 0) { - --state_->niepsilons; - *properties_ &= ~kIEpsilons; - if (oarc.olabel == 0) - *properties_ &= ~kEpsilons; - } - if (oarc.olabel == 0) { - --state_->noepsilons; - *properties_ &= ~kOEpsilons; - } - if (oarc.weight != Weight::Zero() && oarc.weight != Weight::One()) - *properties_ &= ~kWeighted; - oarc = arc; - if (arc.ilabel != arc.olabel) - *properties_ |= kNotAcceptor; - if (arc.ilabel == 0) { - ++state_->niepsilons; - *properties_ |= kIEpsilons; - if (arc.olabel == 0) - *properties_ |= kEpsilons; - } - if (arc.olabel == 0) { - ++state_->noepsilons; - *properties_ |= kOEpsilons; - } - if (arc.weight != Weight::Zero() && arc.weight != Weight::One()) - *properties_ |= kWeighted; - *properties_ &= kSetArcProperties | kNotAcceptor | - kEpsilons | kIEpsilons | kOEpsilons | kWeighted; - } - - private: - struct VectorState<A> *state_; - uint64 *properties_; - size_t i_; - - DISALLOW_EVIL_CONSTRUCTORS(MutableArcIterator); -}; - -// Provide information needed for the generic mutable arc iterator -template <class A> inline -void VectorFst<A>::InitMutableArcIterator( - StateId s, MutableArcIteratorData<A> *data) { - data->base = new MutableArcIterator< VectorFst<A> >(this, s); -} - -// A useful alias when using StdArc. -typedef VectorFst<StdArc> StdVectorFst; - -} // namespace fst - -#endif // FST_LIB_VECTOR_FST_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/verify.h b/tools/thirdparty/OpenFst/fst/lib/verify.h deleted file mode 100644 index 4035129..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/verify.h +++ /dev/null @@ -1,118 +0,0 @@ -// verify.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// Function to test property bits - -#ifndef FST_LIB_VERIFY_H__ -#define FST_LIB_VERIFY_H__ - -#include "fst/lib/fst.h" -#include "fst/lib/test-properties.h" - -namespace fst { - -// Verifies that an Fst's contents are sane. -template<class Arc> -bool Verify(const Fst<Arc> &fst) { - typedef typename Arc::Label Label; - typedef typename Arc::Weight Weight; - typedef typename Arc::StateId StateId; - - StateId start = fst.Start(); - const SymbolTable *isyms = fst.InputSymbols(); - const SymbolTable *osyms = fst.OutputSymbols(); - - // Count states - StateId ns = 0; - for (StateIterator< Fst<Arc> > siter(fst); - !siter.Done(); - siter.Next()) - ++ns; - - if (start == kNoStateId && ns > 0) { - LOG(ERROR) << "Verify: Fst start state ID unset"; - return false; - } else if (start >= ns) { - LOG(ERROR) << "Verify: Fst start state ID exceeds number of states"; - return false; - } - - for (StateIterator< Fst<Arc> > siter(fst); - !siter.Done(); - siter.Next()) { - StateId s = siter.Value(); - size_t na = 0; - for (ArcIterator< Fst<Arc> > aiter(fst, s); - !aiter.Done(); - aiter.Next()) { - const Arc &arc =aiter.Value(); - if (arc.ilabel < 0) { - LOG(ERROR) << "Verify: Fst input label ID of arc at position " - << na << " of state " << s << " is negative"; - return false; - } else if (isyms && isyms->Find(arc.ilabel) == "") { - LOG(ERROR) << "Verify: Fst input label ID " << arc.ilabel - << " of arc at position " << na << " of state " << s - << " is missing from input symbol table \"" - << isyms->Name() << "\""; - return false; - } else if (arc.olabel < 0) { - LOG(ERROR) << "Verify: Fst output label ID of arc at position " - << na << " of state " << s << " is negative"; - return false; - } else if (osyms && osyms->Find(arc.olabel) == "") { - LOG(ERROR) << "Verify: Fst output label ID " << arc.olabel - << " of arc at position " << na << " of state " << s - << " is missing from output symbol table \"" - << osyms->Name() << "\""; - return false; - } else if (!arc.weight.Member() || arc.weight == Weight::Zero()) { - LOG(ERROR) << "Verify: Fst weight of arc at position " - << na << " of state " << s << " is invalid"; - return false; - } else if (arc.nextstate < 0) { - LOG(ERROR) << "Verify: Fst destination state ID of arc at position " - << na << " of state " << s << " is negative"; - return false; - } else if (arc.nextstate >= ns) { - LOG(ERROR) << "Verify: Fst destination state ID of arc at position " - << na << " of state " << s - << " exceeds number of states"; - return false; - } - ++na; - } - if (!fst.Final(s).Member()) { - LOG(ERROR) << "Verify: Fst final weight of state " << s << " is invalid"; - return false; - } - } - uint64 fst_props = fst.Properties(kFstProperties, false); - uint64 known_props; - uint64 test_props = ComputeProperties(fst, kFstProperties, &known_props, - false); - if (!CompatProperties(fst_props, test_props)) { - LOG(ERROR) << "Verify: stored Fst properties incorrect " - << "(props1 = stored props, props2 = tested)"; - return false; - } else { - return true; - } -} - -} // namespace fst - -#endif // FST_LIB_VERIFY_H__ diff --git a/tools/thirdparty/OpenFst/fst/lib/weight.h b/tools/thirdparty/OpenFst/fst/lib/weight.h deleted file mode 100644 index 7050f50..0000000 --- a/tools/thirdparty/OpenFst/fst/lib/weight.h +++ /dev/null @@ -1,139 +0,0 @@ -// weight.h -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// \file -// General weight set and associated semiring operation definitions. -// -// A semiring is specified by two binary operations Plus and Times and -// two designated elements Zero and One with the following properties: -// Plus: associative, commutative, and has Zero as its identity. -// Times: associative and has identity One, distributes w.r.t. Plus, and -// has Zero as an annihilator: -// Times(Zero(), a) == Times(a, Zero()) = Zero(). -// -// A left semiring distributes on the left; a right semiring is -// similarly defined. -// -// A Weight class is required to be (at least) a left or right semiring. -// -// In addition, the following should be defined for a Weight: -// Member: predicate on set membership. -// >>: reads textual representation of a weight. -// <<: prints textual representation of a weight. -// Read(istream &): reads binary representation of a weight. -// Write(ostrem &): writes binary representation of a weight. -// Hash: maps weight to ssize_t. -// ApproxEqual: approximate equality (for inexact weights) -// Quantize: quantizes wrt delta (for inexact weights) -// Divide: for all a,b,c s.t. Times(a, b) == c -// --> b = Divide(c, a, DIVIDE_LEFT) if a left semiring and b.Member() -// --> a = Divide(c, b, DIVIDE_RIGHT) if a right semiring and a.Member() -// --> b = Divide(c, a) -// = Divide(c, a, DIVIDE_ANY) -// = Divide(c, a, DIVIDE_LEFT) -// = Divide(c, a, DIVIDE_RIGHT) if a commutative semiring -// ReverseWeight: the type of the corresponding reverse weight. -// Typically the same type as Weight for a (both left and right) semiring. -// For the left string semiring, it is the right string semiring. -// Reverse: a mapping from Weight to ReverseWeight s.t. -// --> Reverse(Reverse(a)) = a -// --> Reverse(Plus(a, b)) = Plus(Reverse(a), Reverse(b)) -// --> Reverse(Times(a, b)) = Times(Reverse(b), Reverse(a)) -// Typically the identity mapping in a (both left and right) semiring. -// In the left string semiring, it maps to the reverse string -// in the right string semiring. -// Properties: specifies additional properties that hold: -// LeftSemiring: indicates weights form a left semiring. -// RightSemiring: indicates weights form a right semiring. -// TimesCommutative: for all a,b: Times(a,b) == Times(b,a) -// Idempotent: for all a: Plus(a, a) == a. -// Path Property: for all a, b: Plus(a, b) == a or Plus(a, b) == b. - - -#ifndef FST_LIB_WEIGHT_H__ -#define FST_LIB_WEIGHT_H__ - -#include <cctype> -#include <cmath> -#include <iostream> -#include <sstream> - -#include "fst/lib/compat.h" - -#include "fst/lib/util.h" - -namespace fst { - -// -// CONSTANT DEFINITIONS -// - -// A representable float near .001 -const float kDelta = 1.0F/1024.0F; - -// For all a,b,c: Times(c, Plus(a,b)) = Plus(Times(c,a), Times(c, b)) -const uint64 kLeftSemiring = 0x0000000000000001ULL; - -// For all a,b,c: Times(Plus(a,b), c) = Plus(Times(a,c), Times(b, c)) -const uint64 kRightSemiring = 0x0000000000000002ULL; - -const uint64 kSemiring = kLeftSemiring | kRightSemiring; - -// For all a,b: Times(a,b) = Times(b,a) -const uint64 kCommutative = 0x0000000000000004ULL; - -// For all a: Plus(a, a) = a -const uint64 kIdempotent = 0x0000000000000008ULL; - -// For all a,b: Plus(a,b) = a or Plus(a,b) = b -const uint64 kPath = 0x0000000000000010ULL; - - -// Determines direction of division. -enum DivideType { DIVIDE_LEFT, // left division - DIVIDE_RIGHT, // right division - DIVIDE_ANY }; // division in a commutative semiring - -// NATURAL ORDER -// -// By definition: -// a <= b iff a + b = a -// The natural order is a monotonic and negative partial order iff the -// semiring is idempotent and (left and right) distributive. It is a -// total order iff the semiring has the path property. See Mohri, -// "Semiring Framework and Algorithms for Shortest-Distance Problems", -// Journal of Automata, Languages and Combinatorics 7(3):321-350, -// 2002. We define the strict version of this order below. - -template <class W> -class NaturalLess { - public: - typedef W Weight; - - NaturalLess() { - uint64 props = kIdempotent | kLeftSemiring | kRightSemiring; - if (W::Properties() & props != props) - LOG(ERROR) << "NaturalLess: Weight type is not idempotent and " - << "(left and right) distributive: " << W::Type(); - } - - bool operator()(const W &w1, const W &w2) const { - return (Plus(w1, w2) == w1) && w1 != w2; - } -}; - -} // namespace fst; - -#endif // FST_LIB_WEIGHT_H__ |