Cambridge SMT System
fstutils.multiunion.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef FSTUTILS_MULTIUNION_HPP
16 #define FSTUTILS_MULTIUNION_HPP
17 
24 namespace fst {
25 
31 template<class Arc>
33 
34  private:
35 
37  std::vector< boost::shared_ptr< Fst<Arc> const > > fsts_;
38 
39  public:
42 
47  inline void Add ( boost::shared_ptr< Fst<Arc> const > fst ) {
48  fsts_.push_back ( fst );
49  };
50 
52  inline void Add ( Fst<Arc> const *fst ) {
53  fsts_.push_back ( boost::shared_ptr< Fst<Arc> const > ( fst ) );
54  };
55 
56  /*
57  * \brief Proceeds to create the union of list of fsts contained so far in fsts_.
58  * Note that it is possible to create intermediate union-fsts.
59  */
60  VectorFst<Arc> *operator () () {
61  if ( !fsts_.size() ) return NULL;
62  VectorFst<Arc> * aux2 = NULL;
63  if ( fsts_.size() > 1 ) {
64  boost::scoped_ptr< UnionFst<Arc> > aux ( new UnionFst<Arc> ( *fsts_[0],
65  *fsts_[1] ) );
66  for ( uint k = 2; k < fsts_.size(); ++k ) Union ( & ( *aux ), *fsts_[k] );
67  aux2 = new VectorFst<Arc> ( *aux );
68  } else aux2 = new VectorFst<Arc> ( *fsts_[0] );
69  return aux2;
70  };
71 
72  private:
73  DISALLOW_COPY_AND_ASSIGN ( MultiUnionRational );
74 
75 };
76 
81 template<class Arc >
83  private:
84  uint counter_;
85  const uint unionindex_;
86  VectorFst<Arc> head_;
87 
88  // List of pairs label/FSA.
89  std::vector< pair< typename Arc::Label, const Fst<Arc> * > > pairlabelfsts_;
90  std::vector<boost::shared_ptr< Fst<Arc> const > > fsts_;
91 
92  public:
95  counter_ ( 1 ),
96  unionindex_ ( 1000000000 ) {
97  head_.AddState();
98  head_.AddState();
99  head_.SetStart ( 0 );
100  head_.SetFinal ( 1, Arc::Weight::One() );
101  pairlabelfsts_.push_back ( pair< typename Arc::Label, const Fst<Arc> * >
102  ( unionindex_, &head_ ) );
103  };
104 
109  inline void Add ( Fst<Arc> const *fst ) {
111  fsts_.push_back ( boost::shared_ptr< Fst<Arc> const > ( fst ) );
113  head_.AddArc ( 0, Arc ( unionindex_ + counter_, unionindex_ + counter_,
114  Arc::Weight::One(), 1 ) );
115  pairlabelfsts_.push_back ( pair< typename Arc::Label, const Fst<Arc> * >
116  ( unionindex_ + counter_, fst ) );
117  ++counter_;
118  };
119 
120  /*
121  * \brief Proceeds to create the union of list of fsts contained so far in fsts_.
122  * Note that it is possible to create intermediate union-fsts.
123  */
124  VectorFst<Arc> * operator () () {
125  return new VectorFst<Arc> ( ReplaceFst<Arc> ( pairlabelfsts_,
126  ReplaceFstOptions<Arc> ( unionindex_, true ) ) );
127  };
128 
129  private:
130  DISALLOW_COPY_AND_ASSIGN ( MultiUnionReplace );
131 };
132 
133 } //end namespace
134 
135 #endif
VectorFst< Arc > * operator()()
Definition: fstio.hpp:27
void Add(Fst< Arc > const *fst)
Adds an fst to the list.
This class creates the Union of an arbitrarily large number of fsts. This implementation uses one RTN...
This class creates the Union of an arbitrarily large number of fsts. This implementation was suggeste...
MultiUnionReplace()
Constructor, initializes the head fst of the RTN.
void Add(boost::shared_ptr< Fst< Arc > const > fst)
Adds an fst to the list.
void Add(Fst< Arc > const *fst)
Also adds an fst, but takes a pointer as input. Note that we are using internally a list of shared_pt...
MultiUnionRational()
Empty Constructor.