forked from UnitexGramLab/unitex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCassys_transducer.cpp
548 lines (455 loc) · 14.7 KB
/
Cassys_transducer.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
/*
* Unitex
*
* Copyright (C) 2001-2017 Université Paris-Est Marne-la-Vallée <unitex@univ-mlv.fr>
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
*/
/*
* Cassys_transducer.cpp
*
* Created on: 8 oct. 2012
* Author: David Nott, Nathalie Friburger (nathalie.friburger@univ-tours.fr)
*/
#include <ctype.h>
#include "File.h"
#include "Cassys.h"
#include "Cassys_transducer.h"
#ifndef HAS_UNITEX_NAMESPACE
#define HAS_UNITEX_NAMESPACE 1
#endif
namespace unitex {
/**
* Adds the transducer 'fileName' to the linked list of transducer 'current_list'. The mode of the transductor is assumed to be by
* 'set_last_transducer_linked_list_mode' function
*/
struct transducer_name_and_mode_linked_list* add_transducer_linked_list_new_name(
struct transducer_name_and_mode_linked_list *current_list,
const char*filename,
int repeat_mode, int generic_graph)
{
struct transducer_name_and_mode_linked_list* new_item=(struct transducer_name_and_mode_linked_list*)malloc(sizeof(struct transducer_name_and_mode_linked_list));
if (new_item==NULL) {
fatal_alloc_error("add_transducer_linked_list_new_name");
exit(1);
}
new_item->transducer_filename = strdup(filename);
new_item->transducer_mode=IGNORE_OUTPUTS;
new_item->repeat_mode=repeat_mode;
new_item->generic_graph = generic_graph;
new_item->next=NULL;
if (new_item->transducer_filename==NULL) {
fatal_alloc_error("add_transducer_linked_list_new_name");
exit(1);
}
if (current_list==NULL)
return new_item;
struct transducer_name_and_mode_linked_list *browse_current_list = current_list;
while (browse_current_list->next != NULL)
browse_current_list = browse_current_list->next;
browse_current_list->next=new_item;
return current_list;
}
int extract_cassys_generic_mark(const char *line) {
int i = 0;
// filename
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (isspace(line[i])) {
i++;
}
// merge or replace policy
while (isalpha(line[i])) {
i++;
}
while (isspace(line[i])) {
i++;
}
// disabled or enabled
while (isalpha(line[i])) {
i++;
}
while (isspace(line[i])) {
i++;
}
//fix until
i++;
//generic graph
if(line[i] != '\0') {
while (line[i] != '\0' && isspace(line[i])) {
i++;
}
if(line[i] != '\0' && line[i] == '@')
return 1;
}
return 0;
}
struct transducer_name_and_mode_linked_list* add_transducer_linked_list_new_name(
struct transducer_name_and_mode_linked_list *current_list,
const char*filename)
{
return add_transducer_linked_list_new_name(current_list, filename, 1,0);
}
void set_last_transducer_linked_list_mode(struct transducer_name_and_mode_linked_list *current_list,OutputPolicy mode)
{
struct transducer_name_and_mode_linked_list *browse_current_list = current_list;
if (current_list == NULL)
return;
while (browse_current_list->next != NULL)
browse_current_list = browse_current_list->next;
browse_current_list->transducer_mode = mode;
}
OutputPolicy GetOutputPolicyFromString(const char*option_name)
{
if (strcmp(option_name, "M") == 0 || strcmp(option_name, "MERGE") == 0 || strcmp(
option_name, "Merge") == 0) {
return MERGE_OUTPUTS;
}
if (strcmp(option_name, "R") == 0 || strcmp(option_name, "REPLACE") == 0
|| strcmp(option_name, "Replace") == 0) {
return REPLACE_OUTPUTS;
}
return IGNORE_OUTPUTS;
}
void set_last_transducer_linked_list_mode_by_string(struct transducer_name_and_mode_linked_list *current_list,const char*option_name)
{
set_last_transducer_linked_list_mode(current_list,GetOutputPolicyFromString(option_name));
}
void free_transducer_name_and_mode_linked_list(struct transducer_name_and_mode_linked_list *list)
{
while (list!=NULL) {
struct transducer_name_and_mode_linked_list *list_next = list->next;
free(list->transducer_filename);
free(list);
list = list_next;
}
}
void translate_path_separator_to_native_in_filename(char* filename) {
char * walk = filename;
while ((*walk) != '\0') {
char c = *walk;
if ((c == '\\') || (c == '/')) {
*walk = PATH_SEPARATOR_CHAR;
}
walk++;
}
}
struct transducer_name_and_mode_linked_list *load_transducer_list_file(const char *transducer_list_name, int translate_path_separator_to_native) {
U_FILE *file_transducer_list;
struct transducer_name_and_mode_linked_list * res=NULL;
file_transducer_list = u_fopen(ASCII, transducer_list_name,U_READ);
if( file_transducer_list == NULL){
fatal_error("Cannot open file %s\n",transducer_list_name);
exit(1);
}
char line[1024];
int i=1;
while (cassys_fgets(line,1024,file_transducer_list) != NULL){
char *transducer_file_name;
char *enabled_policy;
int repeat_policy;
int generic_graph;
OutputPolicy transducer_policy;
remove_cassys_comments(line);
transducer_file_name = extract_cassys_transducer_name(line);
if ((translate_path_separator_to_native != 0) && (transducer_file_name != NULL)) {
translate_path_separator_to_native_in_filename(transducer_file_name);
}
//u_printf("transducer name read =%s\n",transducer_file_name);
transducer_policy = extract_cassys_transducer_policy(line);
enabled_policy = extract_cassys_disabled(line);
repeat_policy = extract_cassys_tranducer_star(line);
generic_graph = extract_cassys_generic_mark(line);
if (transducer_file_name != NULL && transducer_policy != IGNORE_OUTPUTS && (strcmp("",enabled_policy)==0 || strcmp("Enabled",enabled_policy)==0)) {
res=add_transducer_linked_list_new_name(res,transducer_file_name, repeat_policy, generic_graph);
set_last_transducer_linked_list_mode(res,transducer_policy);
}
else {
if (transducer_file_name == NULL) {
u_printf("Line %d : Empty line\n",i);
} else if (transducer_policy == IGNORE_OUTPUTS) {
u_printf("Line %d : Transducer policy not recognized\n",i);
}
if(strcmp("Disabled",enabled_policy)!=0){
u_printf("Line %d : Could not recognize whether transducer is enabled\n",i);
} else {
u_printf("transducer %s is Disabled\n",transducer_file_name);
}
}
free(enabled_policy);
free(transducer_file_name);
i++;
}
u_fclose(file_transducer_list);
return res;
}
/** if a filename must be concatenated, we must remove the absolute prefix on filename to concat
* (by example, replace 'c:\folder\sub\file' by 'folder\sub\file')
*/
static const char*skip_absolute_prefix(const char* filename) {
// if Windows filename begin with 'c:', we skip two char
if ((*(filename)) != '\0')
if ((*(filename + 1)) == ':')
filename += 2;
if (((*filename) == '\\') | ((*filename) == '/'))
filename++;
return filename;
}
struct fifo *load_transducer_from_linked_list(const struct transducer_name_and_mode_linked_list *list,const char* transducer_filename_prefix){
struct fifo *transducer_fifo = new_fifo();
int i=1;
while (list != NULL){
char *transducer_file_name;
OutputPolicy transducer_policy;
transducer *t;
int repeat_policy;
int generic_graph;
transducer_file_name = list->transducer_filename;
//u_printf("transducer name read =%s\n",transducer_file_name);
transducer_policy = list->transducer_mode;
repeat_policy = list->repeat_mode;
generic_graph = list->generic_graph;
if (transducer_file_name != NULL && transducer_policy != IGNORE_OUTPUTS) {
//u_printf("transducer to be loaded\n");
t = (transducer*) malloc(sizeof(transducer) * 1);
if (t == NULL) {
fatal_alloc_error("load_transducer_from_linked_list");
exit(1);
}
size_t transducer_filename_prefix_len = 0;
if (transducer_filename_prefix != NULL)
transducer_filename_prefix_len = strlen(transducer_filename_prefix);
t->transducer_file_name = (char*)malloc(sizeof(char)*(transducer_filename_prefix_len+strlen(transducer_file_name)+1));
if(t->transducer_file_name == NULL){
fatal_alloc_error("load_transducer_from_linked_list");
exit(1);
}
t->transducer_file_name[0] = '\0';
if (transducer_filename_prefix != NULL)
strcpy(t->transducer_file_name, transducer_filename_prefix);
const char* transducer_file_name_to_add = (transducer_filename_prefix_len > 0) ? skip_absolute_prefix(transducer_file_name) : transducer_file_name;
strcat(t->transducer_file_name, transducer_file_name_to_add);
t->output_policy = transducer_policy;
t->repeat_mode = repeat_policy;
t->generic_graph = generic_graph;
struct any value;
value._ptr = t;
put_any(transducer_fifo,value);
if (!is_empty(transducer_fifo)) {
u_printf("transducer %s successfully loaded\n",
t->transducer_file_name);
}
}
else {
if (transducer_file_name == NULL) {
u_printf("Transducer %d : Empty filename\n",i);
} else if (transducer_policy == IGNORE_OUTPUTS) {
u_printf("Transducer %d : Transducer mode not recognized\n",i);
}
}
i++;
list=list->next;
}
return transducer_fifo;
}
/**
* \brief Suppress Cassys comment line.
*/
void remove_cassys_comments(char *line){
int i=0;
while(line[i] != '\0' && line[i] != '#'){
i++;
}
line[i]='\0';
}
/**
* \brief \b fgets working with \b U_FILE and storing \b char
*
* Needed to process configuration file
*
* @param[out] line the text read
* @param[in] n max number of character read
* @param[in] u file descriptor
*
* @return NULL if no character has been read before \c EOF has been encountered, \c line otherwise
*/
char *cassys_fgets(char *line, int n, U_FILE *u) {
int i = 0;
int c;
c = u_fgetc(u);
if (c == EOF) {
return NULL;
}
while (c != EOF && c != '\n' && i < n) {
line[i] = (char) c;
c=u_fgetc(u);
i++;
}
line[i] = '\0';
//u_printf("fgets result =%s\n",line);
return line;
}
/**
*
*/
char* extract_cassys_transducer_name(const char *line){
char *transducer_name;
int i=0;
while(line[i]!='"' && line[i] != '\0'){
i++;
}
if(line[i] == '\0'){
return NULL;
}
i++;
int j=i;
while(line[i]!='"' && line[i] != '\0'){
i++;
}
if(line[i] == '\0'){
return NULL;
}
transducer_name = (char*)malloc(sizeof(char)*((i-j)+1));
if(transducer_name == NULL){
fatal_alloc_error("extract_cassys_transducer_name");
exit(1);
}
strncpy(transducer_name,line+j,(i-j));
transducer_name[i-j]='\0';
return transducer_name;
}
int extract_cassys_tranducer_star(const char *line) {
int i = 0;
// filename
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (isspace(line[i])) {
i++;
}
// merge or replace policy
while (isalpha(line[i])) {
i++;
}
while (isspace(line[i])) {
i++;
}
// disabled or enabled
while (isalpha(line[i])) {
i++;
}
while (isspace(line[i])) {
i++;
}
if(line[i]=='*'){
return INFINITY;
}
return 1;
}
char *extract_cassys_disabled(const char *line){
char *enabled_policy;
int i = 0;
// filename
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (isspace(line[i])) {
i++;
}
// merge or replace policy
while (isalpha(line[i])) {
i++;
}
while (isspace(line[i])) {
i++;
}
int j=0;
char option_name[FILENAME_MAX];
while(isalpha(line[i])){
option_name[j]=line[i];
i++;j++;
}
option_name[j]='\0';
enabled_policy = (char *)malloc(sizeof(char)*strlen(option_name)+1);
if (enabled_policy == NULL) {
fatal_alloc_error("extract_cassys_disabled memory allocation\n");
exit(1);
}
strcpy(enabled_policy,option_name);
return enabled_policy;
}
OutputPolicy extract_cassys_transducer_policy(const char *line) {
int i = 0;
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while (line[i] != '"' && line[i] != '\0') {
i++;
}
i++;
while(isspace(line[i])){
i++;
}
char option_name[FILENAME_MAX];
int j=0;
while(isalpha(line[i])){
option_name[j]=line[i];
i++;j++;
}
option_name[j]='\0';
//u_printf("extract option =%s\n",option_name);
if (strcmp(option_name, "M") == 0 || strcmp(option_name, "MERGE") == 0 || strcmp(
option_name, "Merge") == 0) {
return MERGE_OUTPUTS;
}
if (strcmp(option_name, "R") == 0 || strcmp(option_name, "REPLACE") == 0
|| strcmp(option_name, "Replace") == 0) {
return REPLACE_OUTPUTS;
}
return IGNORE_OUTPUTS;
}
bool is_debug_mode(transducer *t, const VersatileEncodingConfig* vec) {
U_FILE *graph_file;
graph_file = u_fopen(vec, t->transducer_file_name, U_READ);
if (graph_file == NULL) {
return false;
}
unichar c = (unichar)u_fgetc(graph_file);
u_fclose(graph_file);
if(c=='d'){
return true;
} else {
return false;
}
}
}