[go: up one dir, main page]

Menu

[r11]: / src / refx.c  Maximize  Restore  History

Download this file

219 lines (174 with data), 7.1 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "op.h"
#include "fsm.h"
#include "arpa.h"
#include "refx.h"
#include "l2w.h"
#include "parse.h"
struct options usage(int argc, char *argv[]) {
op_parser_t *parser;
struct options opt;
parser = op_create("%p -m <transducer file> -t <test file>", PACKAGE_STRING, 1);
if (parser == NULL) {
fprintf(stderr, "No s'ha pogut crear el parser d'opcions.\n");
exit(EXIT_FAILURE);
}
op_add_string(parser, 'm', "model", "STRING", "model file without extension", &opt.transducer, NULL);
op_add_string(parser, 't', "test", "FILE", "test file", &opt.test, NULL);
op_add_string(parser, 's', "sep", "STRING", "separator test file", &opt.separator, "#");
op_add_int(parser, 'b', "beam", "N", "Beam-search parameter", &opt.beam, 50);
op_add_flag(parser, 'p', "smooth", "Backoff smoothing always", &opt.algorithm);
op_add_int(parser, 'f', "format", "N", "1 => v2\n2 => arpa", &opt.type, v2);
op_add_string(parser, 'C', "control", "STRING", "control characters", &opt.prefix, NULL);
op_add_int(parser, 'F', "filter", "N", "Filter model from test", &opt.simbextmax, 0);
op_add_int(parser, 'n', "nprobs", "N", "Number of probabilities", &opt.ngram, 1);
op_add_float(parser, 'g', "lambda1", "N", "Weight of the model 1: p(f,e)", &opt.l[0], 1);
op_add_float(parser, 'i', "lambda2", "N", "Weight of the model 2: p(f|e)", &opt.l[1], 1);
op_add_float(parser, 'd', "lambda3", "N", "Weight of the model 3: p(e|f)", &opt.l[2], 1);
op_add_float(parser, 'l', "lambda4", "N", "Weight of the model 4: Word bonus", &opt.l[3], 1);
op_add_float(parser, 'x', "lambda5", "N", "Weight of the model 5: Exponential", &opt.l[4], 1);
op_add_float(parser, 'y', "lambda6", "N", "Weight of the model 6: Word penalty", &opt.l[5], 1);
op_add_float(parser, 'j', "lambda0", "N", "Weight of the model 0: Language model", &opt.norm, 1);
op_add_string(parser, 'a', "model", "STRING", "model file without extension", &opt.automata, NULL);
op_add_string(parser, 'c', "categ", "FILE", "lema-word file", &opt.table, NULL);
op_add_string(parser, 'w', "words", "FILE", "words test file", &opt.corpus, NULL);
op_add_string(parser, 'e', "eagles", "FILE", "eagles test file", &opt.eagles, NULL);
op_add_flag(parser, 'r', "reord", "Show input/output parsing", &opt.reord);
op_add_int(parser, 'H', "hash", "N", "Hash table size", &opt.hts, HASH_SIZE);
op_add_flag(parser, 'W', "wordgraphs", "Get wordgraphs from test", &opt.wordgraphs);
op_parse(parser, argc, argv);
if ((opt.transducer == NULL) || (opt.test == NULL)) {
op_usage(parser, stderr);
exit(EXIT_FAILURE);
}
op_destroy(parser);
return opt;
}
void refx(struct options *opt) {
T_REDGEN *t = NULL, *a = NULL, *WG = NULL;
FILE *fd_test, *fd_eagles, *fd_corpus;
char line[MAXLINE], *middle;
char eagles[MAXLINE], corpus[MAXLINE];
char source[MAXLINE], reference[MAXLINE], candidate[MAXLINE];
char words[MAXLINE], **sentence;
int i, n, n_id;
ea **lea;
l_int *output;
int *UNK;
h_t *table = NULL;
h_t *vocab = NULL;
char **alfa, **target;
time_t clock;
char *next, c1, c2;
char model[MAXLINE], lm[MAXLINE];
h_t *d = NULL;
int contador = 0;
sprintf(model, "%s.tr", opt->transducer);
if (opt->type == arpa) {
c1 = opt->prefix[0];
c2 = opt->prefix[1];
clock = time(NULL); fprintf(stderr, "Reading ARPA language model...... %s", asctime(localtime(&clock)));
t = arpa2fsm(opt->transducer, opt->test,
opt->simbextmax, opt->hts, opt->separator, opt->reord + 1, c1, c2, Training, NULL, opt);
clock = time(NULL); fprintf(stderr, "...done %s", asctime(localtime(&clock)));
}
else {
clock = time(NULL); fprintf(stderr, "Reading SFST translation model... %s", asctime(localtime(&clock)));
t = read_fsm(model, opt->hts);
clock = time(NULL); fprintf(stderr, "...done %s", asctime(localtime(&clock)));
}
UNK = hs("<unk>", 6, t->table_out);
/* if (opt->reord) {
alfa = read_table(opt, &table, &vocab);
if ((fd_eagles = fopen(opt->eagles, "r")) == NULL ) {
fprintf(stderr, "Failed open: eagles test file <%s>\n", opt->eagles);
exit(EXIT_FAILURE);
}
if ((fd_corpus = fopen(opt->corpus, "r")) == NULL ) {
fprintf(stderr, "Failed open: words test file <%s>\n", opt->corpus);
exit(EXIT_FAILURE);
}
} */
if (opt->automata != NULL) {
sprintf(lm, "%s.tr", opt->automata);
clock = time(NULL); fprintf(stderr, "Reading SFST language model... %s", asctime(localtime(&clock)));
a = read_fsm(lm, opt->hts);
clock = time(NULL); fprintf(stderr, "...done %s", asctime(localtime(&clock)));
}
lea = reserva_memoria_trellis(opt->beam);
clock = time(NULL); fprintf(stderr, "\nTranslating... %s", asctime(localtime(&clock)));
if ((fd_test = fopen(opt->test, "r")) == NULL ) {
fprintf(stderr, "Failed open: test file <%s>\n", opt->test);
exit(EXIT_FAILURE);
}
while (fgets(line, MAXLINE, fd_test) != NULL) {
contador++;
line[strlen(line) - 1] = '\0';
if ((middle = strstr(line, opt->separator)) != NULL) {
strncpy(source, line, middle - line -1);
source[middle - line -1] = '\0';
strcpy(reference, middle+strlen(opt->separator)+1);
}
else {
strcpy(source, line);
*reference = '\0';
}
next = line;
for(n = 1; next != middle; n++) {
next = strchr(next, ' ');
if (next != NULL) {
*next = '\0';
next++;
}
}
sentence = (char **) malloc(n*sizeof(char *));
next = line;
for (i = 1; i < n; i++) {
sentence[i] = strdup(next);
next += strlen(next) + 1;
}
/* if (opt->reord == 1) {
fgets(eagles, MAXLINE, fd_eagles);
fgets(words, MAXLINE, fd_corpus);
words[strlen(words) - 1] = '\0';
if ((middle = strstr(words, opt->separator)) != NULL) strcpy(reference, middle+4);
else *reference = '\0';
} */
output = parse(t, UNK, sentence, words, n, lea, opt, eagles, table, vocab, a, contador);
//if (opt->reord) target = alfa;
*candidate = '\0';
get_output(target, t->alfa_out, output, candidate);
if (!strcmp(reference, candidate))
printf("%lf: %s %s %s\n", lea[1][0].score, source, opt->separator, reference);
else printf("%lf: %s %s %s %s %s\n", lea[1][0].score, source, opt->separator, reference, opt->separator, candidate);
free_output(output);
for (i = 1; i < n; i++) free(sentence[i]);
free(sentence);
}
fclose(fd_test);
//fflush(stdout);
clock = time(NULL); fprintf(stderr, "...done %s", asctime(localtime(&clock)));
//fflush(stderr);
free_trellis(lea);
if (t != NULL) {
clock = time(NULL); fprintf(stderr, "\nFreeing memory... %s", asctime(localtime(&clock)));
if (a != NULL) free_fsm(a);
free_fsm(t);
clock = time(NULL); fprintf(stderr, "...done %s", asctime(localtime(&clock)));
}
/* if (opt->reord == 1) {
fclose(fd_corpus);
fclose(fd_eagles);
hd(vocab, 1);
hd(table, 0);
free(alfa);
} */
}
int main(int argc, char *argv[]) {
struct options options;
options = usage(argc, argv);
refx(&options);
}