relabel.h revision f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Licensed under the Apache License, Version 2.0 (the "License");
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// you may not use this file except in compliance with the License.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You may obtain a copy of the License at
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     http://www.apache.org/licenses/LICENSE-2.0
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Unless required by applicable law or agreed to in writing, software
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distributed under the License is distributed on an "AS IS" BASIS,
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See the License for the specific language governing permissions and
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// limitations under the License.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2005-2010 Google, Inc.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: jpr@google.com (Jake Ratkiewicz)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef FST_SCRIPT_RELABEL_H_
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FST_SCRIPT_RELABEL_H_
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <utility>
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using std::pair; using std::make_pair;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::vector;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fst/script/arg-packs.h>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fst/script/fst-class.h>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fst/relabel.h>
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace fst {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace script {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef args::Package<MutableFstClass *,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const SymbolTable *, const SymbolTable *, bool,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const SymbolTable *, const SymbolTable *,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      bool> RelabelArgs1;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<class Arc>
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Relabel(RelabelArgs1 *args) {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MutableFst<Arc> *ofst = args->arg1->GetMutableFst<Arc>();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Relabel(ofst, args->arg2, args->arg3, args->arg4,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          args->arg5, args->arg6, args->arg7);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef args::Package<MutableFstClass*,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const vector<pair<int64, int64> > &,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const vector<pair<int64, int64> > > RelabelArgs2;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<class Arc>
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void Relabel(RelabelArgs2 *args) {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MutableFst<Arc> *ofst = args->arg1->GetMutableFst<Arc>();
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In case int64 is not the same as Arc::Label,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // copy the reassignments
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef typename Arc::Label Label;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  vector<pair<Label, Label> > converted_ipairs(args->arg2.size());
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  copy(args->arg2.begin(), args->arg2.end(), converted_ipairs.begin());
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  vector<pair<Label, Label> > converted_opairs(args->arg3.size());
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  copy(args->arg3.begin(), args->arg3.end(), converted_opairs.begin());
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
66  Relabel(ofst, converted_ipairs, converted_opairs);
67}
68
69// 3
70typedef args::Package<MutableFstClass*, const SymbolTable*,
71                      const SymbolTable*> RelabelArgs3;
72template<class Arc>
73void Relabel(args::Package<MutableFstClass*, const SymbolTable*,
74             const SymbolTable*> *args) {
75  MutableFst<Arc> *fst = args->arg1->GetMutableFst<Arc>();
76  Relabel(fst, args->arg2, args->arg3);
77}
78
79
80// 1
81void Relabel(MutableFstClass *ofst,
82             const SymbolTable *old_isyms, const SymbolTable *relabel_isyms,
83             bool attach_new_isyms,
84             const SymbolTable *old_osyms, const SymbolTable *relabel_osyms,
85             bool attch_new_osyms);
86
87// 2
88void Relabel(MutableFstClass *ofst,
89             const vector<pair<int64, int64> > &ipairs,
90             const vector<pair<int64, int64> > &opairs);
91
92
93// 3
94void Relabel(MutableFstClass *fst,
95             const SymbolTable *new_isymbols,
96             const SymbolTable *new_osymbols);
97
98
99}  // namespace script
100}  // namespace fst
101
102#endif  // FST_SCRIPT_RELABEL_H_
103