-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopfst_beamsearch.cc
More file actions
249 lines (162 loc) · 5.54 KB
/
opfst_beamsearch.cc
File metadata and controls
249 lines (162 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <fst/fstlib.h>
#include <vector>
#include <string>
#include <cstdlib>
using namespace fst;
using namespace std;
typedef int StateId;
typedef TropicalWeight Weight;
typedef pair<int, float> mypair;
namespace{
template <class M>
class RSP3: public RhoMatcher< SigmaMatcher< PhiMatcher< M > > >
{
public:
typedef typename M::FST FST;
typedef typename M::Arc Arc;
typedef typename Arc::StateId StateId;
typedef typename Arc::Label Label;
typedef typename Arc::Weight Weight;
RSP3(const RSP3<M> &matcher, bool safe = false)
: RhoMatcher< SigmaMatcher< PhiMatcher< M > > >(matcher, safe)
{}
virtual RSP3<M> *Copy(bool safe = false) const {
return new RSP3<M>(*this, safe);
}
virtual ~RSP3() {
delete matcher_;
}
RSP3(const FST &fst,
MatchType match_type)
: RhoMatcher< SigmaMatcher< PhiMatcher< M > > >
(fst, match_type, -3, MATCHER_REWRITE_ALWAYS,
new SigmaMatcher< PhiMatcher< M> >
(fst, match_type, -2, MATCHER_REWRITE_ALWAYS,
new PhiMatcher< M >
(fst, match_type, -1, MATCHER_REWRITE_ALWAYS)))
{}
M *matcher_;
};
void successors(StdComposeFst f, StateId s,multimap<int, float> &myvec){
multimap<int, float> m;
vector<double> temp;
for(ArcIterator< StdComposeFst > iter(f,s); !iter.Done(); iter.Next()) {
StdArc arc = iter.Value();
temp.push_back(arc.weight.Value());
myvec.insert ( pair<int,float>(arc.nextstate,arc.weight.Value()) );
}
}
bool ss(const mypair &lhs, const mypair &rhs) {
return lhs.first < rhs.first;
}
bool ss1(const mypair &lhs, const mypair &rhs) {
return lhs.second < rhs.second;
}
void pickbest(list<mypair> &myvec)
{
list<mypair>::iterator it;
myvec.sort(ss);
for (it=myvec.begin(); it!=myvec.end(); ++it){
if( (*it).first == (*(++it)).first){
--it;
if( (*it).second > (*(++it)).second ){
--it;
myvec.remove((*it));
}else{
myvec.remove((*it));
}
}
--it;
}
myvec.sort(ss1);
}
}
void op_beamsearch(vector<int> &vertices1,
vector<int> &vertices2,
vector<int> &inputs,
vector<int> &outputs,
vector<float> &costs,
StdVectorFst *fst1,
StdVectorFst *fst2,
int B) {
typedef RhoMatcher< SortedMatcher<StdFst> > RM;
typedef SigmaMatcher< SortedMatcher<StdFst> > SM;
typedef PhiMatcher< SortedMatcher<StdFst> > PM;
typedef RSP3< SortedMatcher<StdFst> > RSP5;
ComposeFstOptions <StdArc, RSP5> opts;
opts.matcher1 = new RSP5(*fst1, MATCH_OUTPUT);
opts.matcher2 = new RSP5(*fst2, MATCH_INPUT);
StdComposeFst result(*fst1, *fst2, opts);
StdVectorFst r = (StdVectorFst)result;
r.Write("result.fst");
int g = 0;
vector<int> hash_table;
hash_table.push_back(result.Start());
vector<int> BEAM;
BEAM.push_back(result.Start());
multimap<int, int> track;
multimap<int, float> another_set;
while(BEAM.size()){
list<mypair> SET; // the empty set
for(int i=0;i<BEAM.size();i++){
StateId state = BEAM[i];
multimap<int, float> temp;
successors(result, state, temp);
for(multimap<int, float>::iterator it=temp.begin(); it!=temp.end(); ++it){
track.insert( pair<int,int>( (*it).first, BEAM[i] ));
SET.push_back(*it);
another_set.insert(pair<int,float>( (*it).first, it->second ) );
}
}
pickbest(SET);
BEAM.erase(BEAM.begin(),BEAM.begin()+BEAM.size());
g = g + 1;
while((SET.size()) && (B > BEAM.size())){
// set is not empty and the number of nodes in BEAM is less than B
int state = SET.front().first;
SET.pop_front();
bool isPresent = (std::find(hash_table.begin(), hash_table.end(), state) != hash_table.end());
if(!isPresent){
hash_table.push_back(state);
BEAM.push_back(state);
}
}
}
vector<int> path;
vector<float> cost;
int i=0;
multimap<int, int>::iterator pos;
pos=track.end();
--pos;
path.push_back(pos->first);
path.push_back(pos->second);
for(multimap<int, int>::iterator it=pos; it->second!=0;){
it=track.find(it->second);
path.push_back(it->second);
i++;
}
int z=0;
multimap<int, float>::iterator c;
for(int i=path.size()-1;i>=0;i--){
if(z!=0){
c = another_set.find(z);
cost.push_back(c->second);
}
z++;
}
float final_cost=0;
for(int i=0;i<cost.size();i++){
final_cost=final_cost+cost[i];
}
for(int i=path.size()-1;i>0;i--){
int z1 =path[i];
int z2 =path[i-1];
for(ArcIterator< StdVectorFst > iter(r,z1); !iter.Done(); iter.Next()) {
StdArc arc = iter.Value();
int x =iter.Value().nextstate;
if(x==z2){
cout<<char(arc.olabel);
}
}
}
}