-
Notifications
You must be signed in to change notification settings - Fork 0
/
catti-plugin.cpp
699 lines (546 loc) · 20.4 KB
/
catti-plugin.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
Copyright (C) 2012 Verónica Romero, Vicent Alabau
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation.
*/
//*****************************************
//
// \file catti-plugin.cpp
// \author Vicent Alabau
// \author Verónica Romero
// \brief CATTI plugin for Transcriptorium
//
// Copyright (C) 2012
//_________________________________________
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#include <cctype>
#include <algorithm>
#include <string.h>
#include <values.h>
#include <casmacat/IImtEngine.h>
#include <casmacat/IPluginFactory.h>
#include <casmacat/utils.h>
extern "C" {
#include "wg/word_graph.h"
#include "wg/CAT.h"
#include "wg/global.h"
#include "wg/librefx.h"
#include "wg/ecparser.h"
#include "wg/nbestsuffix.h"
#include "wg/auxiliar.h"
#include "wg/strop.h"
#include "wg/global.h"
}
#define FALSE 0
#define TRUE 1
using namespace std;
using namespace casmacat;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
class CattiSession: public IInteractiveMtSession {
public:
CattiSession() {};
CattiSession(const string &_wordgraph_fn,wg_t *&_unigram): wordgraph_fn(_wordgraph_fn) {
float GSF=MAXFLOAT;
float WIP=MAXFLOAT;
float ECF=-350;
int ECAlg=0;
int error;
char * wg_fn = strdup(wordgraph_fn.c_str());
//float w_beam=MAXFLOAT;
completions_t ipe_completions;
//unsigned char errorCorrecting;
//ipe_completions.completions=NULL;
vector<string> correct_suffix;
first_word_last_suffix = create_vector(1);
this->WG=NULL;
this->old_prefix=NULL;
// this->old_first_word_suffix=NULL;
this->unigram=_unigram;
// cerr << "catti-plugin: CattiSession creator\n";
if(!(error = set_input_sentence(wg_fn,GSF,WIP,ECF,&WG,ECAlg))) exit(EXIT_FAILURE);
//this->setPrefix("", CO_NONE, info);
if(!(error = ipeSetTargetText(""))) exit(EXIT_FAILURE);
this->setPrefix(vector<string>(), vector<string>(), FALSE, correct_suffix);
free(wg_fn);
};
virtual ~CattiSession() {
set_ec_state(NULL);
destroy_nbestsuffix();
if(WG!=NULL){
destroy_word_graph(&WG);
}
unigram=NULL;
// fprintf(stderr, "after unigram=NULL\n");
destroy_vector(first_word_last_suffix,1,0);
// if(!old_prefix.empty()){ free(old_prefix)};
cerr << "I, " << typeid(*this).name() << ", (CattiSession) am free!!!" << endl;
};
/* Set a partial validation for isle-driven, not left-to-rigth interactions */
virtual void setPartialValidation(const vector<string> &partial_translation,
const vector<bool> &validated,
vector<string> &corrected_translation,
vector<bool> &corrected_validated)
{
cerr << "WARNING: 'setPartialValidation' not implemented!!!\n";
}
/* Set prefix */
virtual void setPrefix(const vector<string> &prefix,
const vector<string> &suffix,
const bool last_token_is_partial,
vector<string> &corrected_suffix)
{
// cerr << "catti-plugin: setPrefix\n";
completions_t completions;
//char *prefix_sp = NULL;
int error;
//dict_t *wg_dict;
float w_beam=MAXFLOAT;
int ECAlg=0;
int pot=1;
unsigned char errorCorrecting;
//wg_t *unigram=NULL;
char *n_prefix=NULL;
destroy_vector(first_word_last_suffix,1,0);
first_word_last_suffix = create_vector(1);
completions.completions=NULL;
completions.alignments=NULL;
corrected_suffix.clear();
if(prefix.size()==0){
mt(n_prefix = (char *) malloc(sizeof(char)*1));
n_prefix[0]='\0';
}
else{
n_prefix=strdup(prefix[0].c_str());
if((prefix.size()>1)||(!last_token_is_partial)){
corrected_suffix.push_back(prefix[0].c_str());
}
for(int i=1;i<prefix.size();i++) {
mt(n_prefix=(char *) realloc(n_prefix,sizeof(char)*(strlen(n_prefix)+prefix[i].size()+2)));
strcat(n_prefix," ");
strcat(n_prefix,prefix[i].c_str());
if((i<prefix.size()-1)||(!last_token_is_partial)) corrected_suffix.push_back(prefix[i].c_str());
}
}
if(!last_token_is_partial){
mt(n_prefix=(char *) realloc(n_prefix,sizeof(char)*(strlen(n_prefix)+2)));
strcat(n_prefix, " ");
}
//cerr << "catti-plugin: n_prefix: '" << n_prefix << "'\n";
/* Set the maximum number of N-best transcriptions */
ipeSetMaxCompletions(1);
if(!(error = ipeSetTargetText(n_prefix))) exit(EXIT_FAILURE);
if(!(error = ipeProvideCompletions(&completions,strlen(n_prefix),w_beam,first_word_last_suffix,FALSE,pot,&errorCorrecting,this->WG,unigram,ECAlg))) exit(EXIT_FAILURE);
// if(!(error = ipeProvideCompletions(&completions,strlen(n_prefix),w_beam,first_word_last_suffix,FALSE,pot,&errorCorrecting,this->WG,this->WG,ECAlg))) exit(EXIT_FAILURE);
if(completions.completions[0]!=NULL){
// cerr << "catti-plugin: completions.completions[0] '" << completions.completions[0] << "'\n";
char **completionTok=tokenize(completions.completions[0]);
int i=0;
while(completionTok[i]!=NULL){
corrected_suffix.push_back(completionTok[i]);
i++;
}
// for(i=0;i<corrected_suffix.size();i++) cerr << "catti-plugin: suffix: " << corrected_suffix[i] << "\n";
i=0;
}
int c=0;
if (completions.completions != NULL){
while(completions.completions[c]!=NULL){
free(completions.completions[c]);
c++;
}
free(completions.completions);
}
completions.completions =NULL;
c=0;
if(completions.alignments !=NULL){
while(completions.alignments[c]!=NULL){
free(completions.alignments[c]);
c++;
}
free(completions.alignments);
}
completions.alignments=NULL;
if(n_prefix!=NULL) free(n_prefix);
// if(prefix_sp!=NULL) free(prefix_sp);
int i=0;
old_corrected_suffix.clear();
for(i=0;i<corrected_suffix.size();i++) old_corrected_suffix.push_back(corrected_suffix[i]);
}
virtual void rejectSuffix(const vector<string> &prefix,
const vector<string> &suffix,
const bool last_token_is_partial,
vector<string> &corrected_suffix)
{
completions_t completions;
//char *prefix_sp = NULL;
int error;
//int pref_word_length,suf_word_length,i;
//char *word=NULL;
dict_t *wg_dict;
//int *int_completion = NULL;
char *n_prefix=NULL;
float w_beam=MAXFLOAT;
int ECAlg=0;
int pot=1;
unsigned char errorCorrecting;
//int *int_trans = NULL;
//int *int_suffix = NULL;
completions.completions=NULL;
completions.alignments=NULL;
corrected_suffix.clear();
if(prefix.size()==0){
mt(n_prefix = (char *) malloc(sizeof(char)*1));
n_prefix[0]='\0';
}
else{
n_prefix=strdup(prefix[0].c_str());
if((prefix.size()>1)||(!last_token_is_partial)){
corrected_suffix.push_back(prefix[0].c_str());
}
for(int i=1;i<prefix.size();i++) {
mt(n_prefix=(char *) realloc(n_prefix,sizeof(char)*(strlen(n_prefix)+prefix[i].size()+2)));
strcat(n_prefix," ");
strcat(n_prefix,prefix[i].c_str());
if((i<prefix.size()-1)||(!last_token_is_partial)) {
corrected_suffix.push_back(prefix[i].c_str());
}
}
}
if(!last_token_is_partial){
mt(n_prefix=(char *) realloc(n_prefix,sizeof(char)*(strlen(n_prefix)+2)));
strcat(n_prefix, " ");
}
// cerr << "catti-plugin: n_prefix: '" << n_prefix << "'\n";
/* Set the maximum number of N-best transcriptions */
ipeSetMaxCompletions(1);
if(!(error = ipeSetTargetText(n_prefix))) exit(EXIT_FAILURE);
// if(prefix.size()>0){
// if(!old_prefix.empty()){
// if(old_prefix.compare(prefix[0])!=0){
// destroy_vector(first_word_last_suffix,1,0);
// first_word_last_suffix = create_vector(1);
// }
// old_prefix.clear();
// }
// old_prefix.assign(prefix[0]);
// }
if(old_prefix!=NULL){
if( strcmp(old_prefix,n_prefix)!=0) {
destroy_vector(first_word_last_suffix,1,0);
first_word_last_suffix = create_vector(1);
}
free(old_prefix);
}
old_prefix = (char *) malloc(sizeof(char)*strlen(n_prefix)+1);
strcpy(old_prefix,n_prefix);
// cerr << "catti-plugin: old_prefix: '" << old_prefix << "'\n";
int i=0;
/*Obtain de dictionary of the word_graph */
wg_dict=this->WG->dict;
int int_rejectedWord;
if(last_token_is_partial){
char *rejectedWord = strdup(prefix[prefix.size()-1].c_str());
// cerr << "catti-plugin: rejectedWord '" << rejectedWord << "'\n";
// if(old_first_word_suffix!=NULL){ //Comprobamos si es una palabra completa y por lo tanto no cambia.
if( ((suffix.size()>0) && (old_corrected_suffix.size()>0) && (old_corrected_suffix.size() == prefix.size())) ||((suffix.size()>0) && (old_corrected_suffix.size()>0) && (old_corrected_suffix.size() > prefix.size()) && (old_corrected_suffix[prefix.size()].compare(suffix[0])!=0))){
mt(rejectedWord=(char *) realloc(rejectedWord,sizeof(char)*(strlen(rejectedWord)+strlen(suffix[0].c_str())+1)));
strcat(rejectedWord,suffix[0].c_str());
}
//}
set_token_dict(unigram->dict,rejectedWord);
int_rejectedWord=set_token_dict(wg_dict,rejectedWord);
//cerr << "catti-plugin: rejectedWord '" << rejectedWord << "'\n";
// old_first_word_suffix = (char *) malloc(sizeof(char)*strlen(suffix[0].c_str())+1);
// strcpy(old_first_word_suffix,suffix[0].c_str());
}
else{
if(suffix.size()>0){
int_rejectedWord=set_token_dict(wg_dict,(char *)suffix[0].c_str());
//cerr << "catti-plugin: suffix[0]'" << suffix[0] << "'\n";
}else{
int_rejectedWord=get_id_dict(wg_dict,"</s>");
}
}
// cerr << "catti-plugin: int_rejectWord '" << int_rejectedWord << "'\n";
/*The rejected word is added in first_word_last_suffix */
mt(first_word_last_suffix->element = (void **) realloc(first_word_last_suffix->element, (first_word_last_suffix->size+1)*sizeof(void *)));
first_word_last_suffix->size++;
first_word_last_suffix->element[first_word_last_suffix->size-1] = (void *) int_rejectedWord;
if(completions.completions!=NULL){cerr << "catti-plugin: completions.completions[0] before'" << completions.completions[0] << "'\n"; }
/* cerr << "catti-plugin: strlen(n_prefix)'" << strlen(n_prefix) << "'\n";
cerr << "catti-plugin: w_beam'" << w_beam << "'\n";
cerr << "first_word_last_suffix'" << first_word_last_suffix->element[0] << "'\n";
cerr << "get_token_dict(wg_dict,first_word_last_suffix->element[0])'" << get_token_dict(wg_dict, int_rejectedWord) << "'\n";
cerr << "catti-plugin: pot'" << pot << "'\n";
cerr << "catti-plugin: EAClg'" << ECAlg << "'\n";*/
/* Compute the posible sufix*/
if(!(error = ipeProvideCompletions(&completions,strlen(n_prefix),w_beam,first_word_last_suffix,FALSE,pot,&errorCorrecting,this->WG,unigram,ECAlg))) exit(EXIT_FAILURE);
// if(!(error = ipeProvideCompletions(&completions,strlen(n_prefix),w_beam,first_word_last_suffix,FALSE,pot,&errorCorrecting,this->WG,this->WG,ECAlg))) exit(EXIT_FAILURE);
if(completions.completions[0]!=NULL){
cerr << "catti-plugin: completions.completions[0] '" << completions.completions[0] << "'\n";
char **completionTok=tokenize(completions.completions[0]);
int i=0;
while(completionTok[i]!=NULL){
corrected_suffix.push_back(completionTok[i]);
i++;
}
for(i=0;i<corrected_suffix.size();i++) cerr << "catti-plugin: suffix: " << corrected_suffix[i] << "\n";
i=0;
}
int c=0;
if (completions.completions != NULL){
while(completions.completions[c]!=NULL){
free(completions.completions[c]);
c++;
}
free(completions.completions);
}
completions.completions=NULL;
c=0;
if(completions.alignments !=NULL){
while(completions.alignments[c]!=NULL){
free(completions.alignments[c]);
c++;
}
free(completions.alignments);
}
completions.alignments=NULL;
if(n_prefix!=NULL) free(n_prefix);
// if(prefix_sp!=NULL) free(prefix_sp);
// if(unigram!=NULL) destroy_word_graph(&unigram);
//int i=0;
old_corrected_suffix.clear();
for(i=0;i<corrected_suffix.size();i++) old_corrected_suffix.push_back(corrected_suffix[i]);
}
private:
const string wordgraph_fn;
wg_t *WG;
//string old_prefix;
vector<string> old_corrected_suffix;
char *old_prefix;
vector_t *first_word_last_suffix;
wg_t *unigram;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
class CattiEngine: public IInteractiveMtEngine {
public:
std::map<std::string,std::string> WGsPath;
wg_t *unigram;
CattiEngine(const std::map<std::string,std::string> &_WGsPath_fn, wg_t *&_unigram): WGsPath(_WGsPath_fn) {
unigram=_unigram;
}
CattiEngine() {};
virtual ~CattiEngine() {
unigram=NULL;
cerr << "I, " << typeid(*this).name() << ", (CattiEngine) am free!!!" << endl;
};
/* Update catti models will not be implemented */
virtual void validate(const vector<string> &source,
const vector<string> &target,
const vector<bool> &validated)
{
cerr << "WARNING: 'validate' not implemented!!!\n";
}
/* Return the most likely hypothesis for WG stored in source[0] */
virtual void translate(const std::vector<std::string> &source,
std::vector<std::string> &target)
{
//XXX: Implement this!
// for (size_t i = 0; i < len; i++) target.push_back(result[i]);
// cerr << "catti-plugin: translate\n";
float GSF=MAXFLOAT;
float WIP=MAXFLOAT;
float ECF=-350;
float w_beam=MAXFLOAT;
wg_t *WG=NULL;
completions_t ipe_completions;
int error;
vector_t *first_word_last_suffix = create_vector(1);
int ECAlg=0;
int pot=1;
unsigned char errorCorrecting;
// wg_t *unigram=NULL;
char *wg_fn =NULL;
ipe_completions.completions=NULL;
ipe_completions.alignments=NULL;
if(source.empty()){
cerr << "The id is empty\n";
exit(EXIT_FAILURE);
}
if (WGsPath.count(source[0])>0){
wg_fn = strdup(WGsPath[source[0]].c_str());
cerr << "catti-plugin: WGsPath: " << WGsPath[source[0]] << "\n";
}
else{
cerr << "The id: " << source[0] << "is not found\n";
exit(EXIT_FAILURE);
}
//printf("setting source: '%s'\n", source[0]);
if(!(error = set_input_sentence(wg_fn,GSF,WIP,ECF,&WG,ECAlg))) exit(EXIT_FAILURE);
if(!(error = ipeSetTargetText(""))) exit(EXIT_FAILURE);
if(!(error = ipeProvideCompletions(&ipe_completions,0,w_beam,first_word_last_suffix,FALSE,pot,&errorCorrecting,WG,unigram,ECAlg))) exit(EXIT_FAILURE);
if(ipe_completions.completions[0]!=NULL){
target.push_back(ipe_completions.completions[0]);
// cerr << "catti-plugin: target: " << target[0] << "\n";
}
int c=0;
if (ipe_completions.completions != NULL){
while(ipe_completions.completions[c]!=NULL){
free(ipe_completions.completions[c]);
c++;
}
free(ipe_completions.completions);
}
ipe_completions.completions=NULL;
c=0;
if(ipe_completions.alignments !=NULL){
while(ipe_completions.alignments[c]!=NULL){
free(ipe_completions.alignments[c]);
c++;
}
free(ipe_completions.alignments);
}
ipe_completions.alignments=NULL;
free(wg_fn);
set_ec_state(NULL);
destroy_nbestsuffix();
if(WG!=NULL){
destroy_word_graph(&WG);
}
destroy_vector(first_word_last_suffix,1,0);
}
/* Update catti models will not be implemented */
virtual void update(const std::vector<std::string> &source,
const std::vector<std::string> &target)
{
cerr << "WARNING: 'update' not implemented!!!\n";
}
/**
* initialize Catti session
*/
virtual IInteractiveMtSession *newSession(const vector<string> &source) {
if (WGsPath.count(source[0])>0)
return new CattiSession(WGsPath[source[0]],unigram);
else
cerr << "The id: '" << source[0] << "is not found\n";
}
/**
* delete Catti session
*/
virtual void deleteSession(IInteractiveMtSession *session) {
delete session;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
class CattiFactory: public IInteractiveMtFactory {
public:
std::map<std::string,std::string> WGs;
//std::string unigramFile;
wg_t *unigram;
CattiFactory() { unigram=NULL; }
// do not forget to free all allocated resources
// otherwise define the destructor with an empty body
virtual ~CattiFactory() {
if(unigram!=NULL) destroy_word_graph(&unigram);
cerr << "I, " << typeid(*this).name() << ", am free!!!" << endl;
}
/**
* initialize the Catti engine with main-like parameters
*/
virtual int init(int argc, char *argv[], Context *context = 0) {
//XXX: Implement this!
if (argc != 2) { // invalid number of arguments
return EXIT_FAILURE;
}
// initialize structures with parameters from args
string pathWG;
string unigramFile;
string conf(argv[1]);
std::string line;
ifstream FileConf(conf.c_str(), fstream::in);
if(FileConf.fail()) {
cerr << "Error al abrir el archivo" << endl;
return EXIT_FAILURE;
}
else
{
while(FileConf.good() and not FileConf.eof())
{
getline(FileConf,line);
if (line.size() > 0) {
char * linestr = strdup(line.c_str());
char **lineTok=tokenize(linestr);
if (lineTok != NULL and lineTok[0] != NULL and lineTok[1] != NULL) {
//std::string tok0(lineTok[0]);
if(strcmp(lineTok[0],"-W")==0) pathWG = lineTok[1];
if(strcmp(lineTok[0],"-G")==0) unigramFile = lineTok[1];
}
else {
cerr << "Wrong Configure line: '" << line << "'\n";
}
free(linestr);
/* int i=0;
if(lineTok !=NULL){
while(lineTok[i]!=NULL){
free(lineTok[i]);
i++;
}
free(lineTok);
}*/
}
}
}
FileConf.close();
cerr << "WG file: '" << pathWG.c_str() << "'" << endl;
cerr << "Unigram: '" << unigramFile.c_str() << "'" << endl;
ifstream fileWG(pathWG.c_str(), fstream::in);
if(fileWG.fail()) {
cerr << "Error al abrir el archivo de WGs" << endl;
return EXIT_FAILURE;
}
else
{
while(fileWG.good() and not fileWG.eof())
{
getline(fileWG,line);
if (line.size() > 0) {
char * linestr = strdup(line.c_str());
char **lineTok=tokenize(linestr);
if (lineTok != NULL and lineTok[0] != NULL and lineTok[1] != NULL) {
WGs[lineTok[0]]=lineTok[1];
}
else {
cerr << "Wrong WG line: '" << line << "'\n";
}
free(linestr);
int i=0;
}
}
}
fileWG.close();
cerr << WGs.size() << " WGs loaded\n";
disable_pospro=TRUE;
unigram=NULL;
int error=0;
if(!(error = set_input_unigram((char *)unigramFile.c_str(),&unigram))) exit(EXIT_FAILURE);
return EXIT_SUCCESS;
}
virtual string getVersion() { return "Catti Plugin v0.1"; }
virtual IInteractiveMtEngine *createInstance(const std::string &specialization_id = "") {
return new CattiEngine(WGs,unigram);
}
virtual void deleteInstance(IInteractiveMtEngine *instance) {
delete instance;
}
};
EXPORT_CASMACAT_PLUGIN(IInteractiveMtEngine, CattiFactory);