-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-old.cpp
486 lines (441 loc) · 12.8 KB
/
main-old.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "utils.h"
#include "Alignment.h"
#include "JKArgs.h"
#include "PhraseTable.h"
#include "Decode.h"
#ifdef WIN32
#include <Windows.h>
#include <direct.h>
#define CHDIR _chdir
#define GETCWD _getcwd
#else
#include <unistd.h>
#define CHDIR chdir
#define GETCWD getcwd
#endif
void usage()
{
cerr<<"pbmt -extractPhrase -src=[sourcefile] -tar=[targetfile] -align=[alignment] [-s2tLex=file] [-t2sLex=file] [-w=weightfile]"<<endl
<<" [-normalize=true|false] [-reverse] [-srcLengthLimit=int] [-tarLengthLimit=int] [-maxNumOfUnAlignedWords=int]"<<endl
<<" [-printTopEntrys=int] [-onlyonefeat] [-threshold=double] [moore=bool]"<<endl
<<"pbmt -makeLexDic -src=[sourcefile] -tar=[targetfile] -align=[alignment] [-w=weightfile] [-s2t=filename] [-t2s=filename]"<<endl
<<"pbmt -decode -pt=[phrase table file] -dmax=[max distortion limit] -beam=[beam size] -weights=[d:d:d...]"<<endl
<<"pbmt -qsubMFT -curDir=dir -stepLength=int -nStep=int -dConfig=trainConfig -o=ruleFile [-walltime=time] [-pmem=mem] [-queue=node]"<<endl
<<"pbmt -rule_refinement -i=raw_rules"<<endl
<<"pbmt -reprintLexDic -i=file"<<endl
;
exit(1);
}
/*
void usage()
{
cerr<<"decoder --pt [phrase table file] --dmax [max distortion limit] --beam [beam size] --weights [d:d:d...]"<<endl;
exit(1);
}
*/
typedef struct{
int ind;
double score;
}EntryInfo;
void rule_refinement(JKArgs& args)
{
if(!args.is_set("i"))usage();
ifstream is(args.value("i").c_str());
map<string,EntryInfo> srcPhrases,tarPhrases;
while(is.good())
{
string curline="";
getline(is,curline);
if(curline=="")break;
PhraseRuleEntry pre;
pre.read(curline);
if(srcPhrases.find(pre.srcPhrase)==srcPhrases.end())
{
EntryInfo ei;
ei.ind=(int)srcPhrases.size();
ei.score=pre.s2tScore;
srcPhrases[pre.srcPhrase]=ei;
}
else
srcPhrases[pre.srcPhrase].score+=pre.s2tScore;
if(tarPhrases.find(pre.tarPhrase)==tarPhrases.end())
{
EntryInfo ei;
ei.ind=(int)tarPhrases.size();
ei.score=pre.s2tScore;
tarPhrases[pre.tarPhrase]=ei;
}
else
tarPhrases[pre.tarPhrase].score+=pre.s2tScore;
}
is.clear();
is.seekg(0,ios::beg);
PhraseTable pt;
while(is.good())
{
string curline="";
getline(is,curline);
if(curline==""){pt.print(cout);break;}
PhraseRuleEntry pre;
pre.read(curline);
pre.s2tScore/=srcPhrases[pre.srcPhrase].score;
pre.t2sScore/=tarPhrases[pre.tarPhrase].score;
if(pt.size()>0&&pt.data().find(pre.srcPhrase)==pt.data().end())
{
pt.print(cout);
pt.clear();
}
pt.add(pre.srcPhrase,pre.tarPhrase,pre);
}
}
void makeLexDic(string srcfilename,string tarfilename, string alignfilename, string weightFilename, Dic<double>& s2tLexDic, Dic<double>& t2sLexDic)
{
ifstream fsrc(srcfilename.c_str()),ftar(tarfilename.c_str()),falign(alignfilename.c_str()),fweight(weightFilename.c_str());
while(!falign.eof()&&!fsrc.eof()&&!ftar.eof())
{
string align="",src="",tar="";
getline(falign,align);
getline(fsrc,src);
getline(ftar,tar);
double fraccount=1;
if(fweight.good())fweight>>fraccount;
vector<pair<int,int> > alignment;
vector<string> srcSent,tarSent;
string2alignment(align,alignment);
stringToVector(src,srcSent);
stringToVector(tar,tarSent);
map<int, int> alignedSrcPos,alignedTarPos;
for(int i=0;i<(int)alignment.size();i++)
{
pair<int,int> a=alignment[i];
string srcWord=srcSent[a.first];
string tarWord=tarSent[a.second];
s2tLexDic.add(srcWord,tarWord,fraccount);
t2sLexDic.add(tarWord,srcWord,fraccount);
alignedSrcPos[a.first];
alignedTarPos[a.second];
}
for(int i=0;i<(int)srcSent.size();i++)
{
if(alignedSrcPos.find(i)==alignedSrcPos.end())
t2sLexDic.add("NULL",srcSent[i],fraccount);
}
for(int i=0;i<(int)tarSent.size();i++)
{
if(alignedTarPos.find(i)==alignedTarPos.end())
s2tLexDic.add("NULL",tarSent[i],fraccount);
}
}
s2tLexDic.normalize();
t2sLexDic.normalize();
falign.close();
fsrc.close();
ftar.close();
}
void makeLexDic(JKArgs& args)
{
if(!args.is_set("src")||!args.is_set("tar")||!args.is_set("align"))
usage();
string srcfilename=args.value("src");
string tarfilename=args.value("tar");
string alignfilename=args.value("align");
string s2tDicName="s2t.lex";
string t2sDicName="t2s.lex";
string weightFilename="";
if(args.is_set("w"))weightFilename=args.value("w");
if(args.is_set("s2t"))
s2tDicName=args.value("s2t");
if(args.is_set("t2s"))
t2sDicName=args.value("t2s");
ofstream s2t(s2tDicName.c_str());
ofstream t2s(t2sDicName.c_str());
Dic<double> s2tLexDic;
Dic<double> t2sLexDic;
makeLexDic(srcfilename,tarfilename,alignfilename,weightFilename,s2tLexDic,t2sLexDic);
s2tLexDic.print(s2t);
t2sLexDic.print(t2s);
s2t.close();
t2s.close();
}
void extractPhrase(JKArgs& args)
{
if(!args.is_set("src")||!args.is_set("tar")||!args.is_set("align"))
usage();
ifstream fsrc(args.value("src").c_str()),ftar(args.value("tar").c_str()),falign(args.value("align").c_str());
ifstream fweight;
ofstream os;
double threshold=0;
if(args.is_set("threshold"))threshold=atof(args.value("threshold").c_str());
if(args.is_set("o"))os.open(args.value("o").c_str());
if(args.is_set("w"))fweight.open(args.value("w").c_str());
Dic<double> s2tLexDic,t2sLexDic;
if(!args.is_set("s2tLex")||!args.is_set("t2sLex"))
{
cerr<<"making lex dic on the fly"<<endl;
makeLexDic(args.value("src"),args.value("tar"),args.value("align"),args.value("w"),s2tLexDic,t2sLexDic);
}
else
{
s2tLexDic.load(args.value("s2tLex"));
t2sLexDic.load(args.value("t2sLex"));
}
bool reverse=false,normalize=false;
bool moore=false;
if(args.is_set("moore"))if(args.value("moore")=="true")moore=true;
if(args.is_set("reverse"))
reverse=true;
if(args.is_set("normalize"))
if(args.value("normalize")=="true")
normalize=true;
int srcLengthLimit=8,tarLengthLimit=12;
if(args.is_set("srcLengthLimit"))
srcLengthLimit=atoi(args.value("srcLengthLimit").c_str());
if(args.is_set("tarLengthLimit"))
tarLengthLimit=atoi(args.value("tarLengthLimit").c_str());
int maxNumOfUnAlignedWords=2;
if(args.is_set("maxNumOfUnAlignedWords"))
maxNumOfUnAlignedWords=atoi(args.value("maxNumOfUnAlignedWords").c_str());
double start=1,stop=1E10;
if(args.is_set("range"))
{
string range=args.value("range");
start=atoi(range.substr(0,range.find("-")).c_str());
stop=atof(range.substr(range.find("-")+1).c_str());
cerr<<"start:"<<start<<",stop:"<<stop<<endl;
}
PhraseTable phraseTable;
int count=0;
while(!falign.eof())
{
string align="",src="",tar="";
double weight=1;
getline(falign,align);
getline(fsrc,src);
getline(ftar,tar);
if(fweight.good())fweight>>weight;
//Alignment alignment(align,maxNumOfUnAlignedWords);
count++;
if(count<start)continue;
if(count>stop)break;
if(weight==0||weight<threshold)continue;
if(moore)weight=1;
vector<PhraseRuleEntry> phrases;
phraseExtractor(src,tar,align,s2tLexDic,t2sLexDic,phrases,srcLengthLimit,tarLengthLimit,maxNumOfUnAlignedWords,weight);
for(size_t i=0;i<phrases.size();i++)
{
PhraseRuleEntry& pre=phrases[i];
if(reverse)
{
string tmp=pre.srcPhrase;
pre.srcPhrase=pre.tarPhrase;
pre.tarPhrase=tmp;
}
if(os.good())phraseTable.add(pre.srcPhrase,pre.tarPhrase,pre);
}
}
if(args.is_set("printTopEntrys"))
{
int topN=atoi(args.value("printTopEntrys").c_str());
phraseTable.printTopEntrys(topN,"topEntrys.txt");
}
if(normalize)
phraseTable.normalize();
bool onlyonefeat=false;
if(args.is_set("onlyonefeat"))
onlyonefeat=true;
if(os.good())phraseTable.print(os,onlyonefeat);
string signal="mftsignal";
if(args.is_set("signal"))signal=args.value("signal");
ofstream o_sg(signal.c_str());
o_sg.close();
}
void qsubMFT(JKArgs& args)
{
//"jkparser -qsubMFT -curDir=dir -stepLength=int -nStep=int -dConfig=trainConfig -o=ruleFile [-walltime=time] [-pmem=mem] [-queue=node]"
string curDir="";
int stepLength=5000;
int nStep=6;
string dConfig="";
int sleepTime=5;
if(!args.is_set("curDir")||!args.is_set("stepLength")||
!args.is_set("nStep")||!args.is_set("dConfig")||!args.is_set("o"))
usage();
curDir=args.value("curDir");
stepLength=atoi(args.value("stepLength").c_str());
nStep=atoi(args.value("nStep").c_str());
dConfig=args.value("dConfig");
string output=args.value("o");
int dlevel=atoi(args.value("debug").c_str());
if(args.is_set("sleepTime"))sleepTime=atoi(args.value("sleepTime").c_str());
JKArgs dArgs;
dArgs.init(dConfig.c_str());
string walltime="20:00:00";
string pmem="7g";
string queue="isi";
if(args.is_set("walltime"))
walltime=args.value("walltime");
if(args.is_set("pmem"))
pmem=args.value("pmem");
if(args.is_set("queue"))
queue=args.value("queue");
cerr<<"walltime="<<walltime<<endl;
string signal="mftsignal";
if(dArgs.is_set("signal"))signal=dArgs.value("signal");
string logerror=output+"-logerror";
vector<string> v_signal,v_logerror,v_log,v_sh;
CHDIR(curDir.c_str());
string cmd="";
cmd="cd "+curDir;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
for(int iter=0;iter<nStep;iter++)
{
int start=stepLength*iter+1;
int stop=start+stepLength-1;
string range=intToString(start)+"-"+intToString(stop);
string n_signal=signal+"."+range;
v_signal.push_back(n_signal);
string n_logerror=logerror+"."+range;
string n_log=output+"."+range;
v_logerror.push_back(n_logerror);
v_log.push_back(n_log);
string fd_cmd="( ./pbmt -config="+dConfig+" -range="+range+" -signal="+n_signal;
fd_cmd+=" > "+n_log+" ) >& "+n_logerror;
string fd_sh=curDir+"/mft."+range+".sh";
v_sh.push_back(fd_sh);
cmd="echo cd "+curDir+" > "+fd_sh;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
cmd="echo \""+fd_cmd+" \" >> "+fd_sh;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
cmd="qsub -l walltime="+walltime+" -l pmem="+pmem+" -q "+queue+" "+fd_sh;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
}
while(true)
{
bool allDone=true;
for(int iter=0;iter<nStep;iter++)
{
ifstream signalFile(v_signal[iter].c_str());
if(!signalFile.good())
allDone=false;
signalFile.close();
}
if(allDone)
break;
#ifndef WIN32
sleep(sleepTime);
#else
Sleep(sleepTime*100);
#endif
}
cmd="rm "+output+" "+logerror;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
cmd="cat "+vectorToString(v_log)+" > "+output;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
cmd="cat "+vectorToString(v_logerror)+" > "+logerror;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
string debugInfo=output+"-debugInfo";
cmd="mkdir "+debugInfo;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
cmd="mv "+vectorToString(v_log)+" "+vectorToString(v_logerror)+" "+vectorToString(v_sh)+" "+vectorToString(v_signal)\
+" *sh.e* *sh.o* "+debugInfo;
if(dlevel)cerr<<cmd<<endl;
else system(cmd.c_str());
}
void decode(JKArgs& args)
{
//pbmt -decode -pt=[phrase table file] -dmax=[max distortion limit] -beam=[beam size] -weights=[d:d:d...]"<<endl;
if(!args.is_set("pt"))usage();
//pt.print(cout);
int dmax=4;
int beamsize=100;
Features weights;
weights.s2t=weights.t2s=weights.s2tLex=weights.t2sLex=weights.length=weights.distort=0;
weights.s2t=1;
weights.distort=1;
if(args.is_set("dmax"))dmax=atoi(args.value("dmax").c_str());
if(args.is_set("beam"))beamsize=atoi(args.value("beam").c_str());
if(args.is_set("weights"))weights.init(args.value("weights"));
bool debug=false;
int tlimit=10;
if(args.is_set("tlimit"))tlimit=atoi(args.value("tlimit").c_str());
if(args.is_set("debug")) debug=true;
PhraseTable pt;
pt.load(args.value("pt"));
pt.logrize(10);
pt.makeVector();
pt.sortVector(weights);
while(!cin.eof())
{
string curline="";
getline(cin,curline);
if(curline=="")continue;
SearchSpace space;
space.init(curline);
space.setupAuxSpace(pt,weights);
space.copeUNK(pt);
space.beamSearch(pt,weights,beamsize,dmax,tlimit,debug);
cout<<space.getNthTranslation(1)<<endl;
}
}
void reprintLexDic(JKArgs& args)
{
if(!args.is_set("i"))usage();
Dic<double> s2tLexDic;
s2tLexDic.load(args.value("i"));
s2tLexDic.print(cout);
}
int main(int argc, char** argv)
{
JKArgs args(argc,argv);
if(args.is_set("config"))args.init(args.value("config").c_str());
args.init(argc,argv);
if(args.is_set("extractPhrase"))
extractPhrase(args);
else if(args.is_set("makeLexDic"))
makeLexDic(args);
else if(args.is_set("decode"))
decode(args);
else if(args.is_set("qsubMFT"))
qsubMFT(args);
else if(args.is_set("rule_refinement"))
rule_refinement(args);
else if(args.is_set("reprintLexDic"))
reprintLexDic(args);
else
usage();
}
void reformArgs(int argc, char* argv[], vector<string>& lines)
{
lines.clear();
for(int i=1;i<argc;i++)
{
if(argv[i][0]=='-')
lines.push_back(argv[i]+1);
else
{
lines.back()+="=";
lines.back()+=argv[i];
}
}
//for(size_t i=0;i<lines.size();i++)
// cerr<<lines[i]<<endl;
}
/*
int main(int argc, char** argv)
{
vector<string> argsInLines;
reformArgs(argc,argv,argsInLines);
JKArgs args;
args.init(argsInLines);
// args.init(argc,argv);
decode(args);
}
*/