forked from UnitexGramLab/unitex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCassys.cpp
1840 lines (1665 loc) · 78.3 KB
/
Cassys.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*
*/
/*
*
* Created on: 29 avr. 2010
* Authors: David Nott, Nathalie Friburger (nathalie.friburger@univ-tours.fr)
*/
#include "Text_tokens.h"
#include "Cassys.h"
#include <ctype.h>
#include "File.h"
#include "Snt.h"
#include "List_ustring.h"
#include "Tokenization.h"
#include "Copyright.h"
#include "DirHelper.h"
#include "Offsets.h"
#include "Cassys_lexical_tags.h"
#include "Cassys_concord.h"
#include "UnusedParameter.h"
#include "SyncTool.h"
#include "StringParsing.h"
#ifndef HAS_UNITEX_NAMESPACE
#define HAS_UNITEX_NAMESPACE 1
#endif
namespace unitex {
const char *optstring_Cassys = ":bp:t:a:w:l:Vhk:q:g:dvuNncm:s:ir:f:T:L:C:O$:x:";
const struct option_TS lopts_Cassys[] = {
{"text", required_argument_TS, NULL, 't'},
{"alphabet", required_argument_TS, NULL, 'a'},
{"morpho",required_argument_TS,NULL,'w'},
{"transducers_list", required_argument_TS, NULL,'l'},
{"input_encoding",required_argument_TS,NULL,'k'},
{"output_encoding",required_argument_TS,NULL,'q'},
{"no_create_directory",no_argument_TS,NULL,'d'},
{"negation_operator",required_argument_TS,NULL,'g'},
{"transducer_policy",required_argument_TS,NULL,'m'},
{"transducer_file",required_argument_TS,NULL,'s'},
{"transducer_dir",required_argument_TS,NULL,'r'},
{"in_place", no_argument_TS,NULL,'i'},
{"dump_token_graph", no_argument_TS, NULL, 'u' },
{"no_dump_token_graph", no_argument_TS, NULL, 'N' },
{"realign_token_graph_pointer", no_argument_TS, NULL, 'n' },
{"display_time", no_argument_TS, NULL, 'c' },
{"translate_path_separator_to_native", no_argument_TS, NULL, 'v' },
{"working_dir", required_argument_TS, NULL, 'p' },
{"cleanup_working_files", no_argument_TS, NULL, 'b' },
{"tokenize_argument", required_argument_TS, NULL, 'T' },
{"locate_argument", required_argument_TS, NULL, 'L' },
{"concord_argument", required_argument_TS, NULL, 'C' },
{"uima", required_argument_TS, NULL, 'f' },
{"input_offsets",required_argument_TS,NULL,'$'},
{"produce_offsets_file",no_argument_TS,NULL,'O'},
{"only_verify_arguments",no_argument_TS,NULL,'V'},
{"standoff",optional_argument_TS,NULL,'x'},
{"lang",required_argument_TS,NULL,'A'},
{"help", no_argument_TS,NULL,'h'}
};
const char* usage_Cassys =
"Usage : Cassys [options]\n"
"\n"
"OPTION :\n"
"-a ALPH/--alphabet=ALPH: the language alphabet file\n"
"-r X/--transducer_dir=X: take transducer on directory X (so you don't specify \n"
" full path for each transducer; note that X must be (back)slash terminated\n"
"-w DIC/--morpho=DIC: specifies that DIC is a .bin dictionary\n"
" to use in morphological mode. Use as many\n"
" -m XXX as there are .bin to use. You can also\n"
" separate several .bin with semi-colons.\n"
"-l TRANSDUCERS_LIST/--transducers_list=TRANSDUCERS_LIST the transducers list file with their output policy\n"
"-s transducer.fst2/--transducer_file=transducer.fst2 a transducer to apply\n"
"-m output_policy/--transducer_policy=output_policy the output policy of the transducer specified\n"
"--input_offsets=XXX base offset file to be used (optional)\n"
"-O/--produce_offsets_file produce offsets file (automatic if --input_offsets=XXX is used)\n"
"-t TXT/--text=TXT the text file to be modified, with extension .snt\n"
"-i/--in_place mean uses the same csc/snt directories for each transducer\n"
"-p X/--working_dir=X uses directory X for intermediate working file\n"
"-b/--cleanup_working_files remove intermediate working file after usage\n"
"-u/--dump_token_graph create a .dot file with graph dump infos\n"
"-N/--no_dump_token_graph create a .dot file with graph dump infos\n"
"-n/--realign_token_graph_pointer create a.dot file will not depends to pointer allocation to be deterministic\n"
"-v/--translate_path_separator_to_native replace path separator in csc by native separator for portable csc file\n"
"-c/--display_time display time used in each unitex tool\n"
"-d/--no_create_directory mean the all snt/csc directories already exist and don't need to be created\n"
" -g minus/--negation_operator=minus: uses minus as negation operator for Unitex 2.0 graphs\n"
" -g tilde/--negation_operator=tilde: uses tilde as negation operator (default)\n"
"-T ARG/--tokenize_argument=ARG specify additionnal argument for Tokenize internal call (several possible)\n"
"-L ARG/--locate_argument=ARG specify additionnal argument for Locate internal call (several possible)\n"
"-C ARG/--concord_argument=ARG specify additionnal argument for Concord internal call (several possible)\n"
" -V/--only-verify-arguments: only verify arguments syntax and exit\n"
" -h/--help display this help\n"
"\n"
"Applies a list of grammar to a text and saves the matching sequence index in a\n"
"file named \"concord.ind\" stored in the text directory.\n\n"
"The target text file has to be a preprocessed snt file with its _snt/ directory.\n"
"The transducer list file is a file in which each line contains the path to a transducer.\n"
"followed by the output policy to be applied to this transducer.\n"
"Instead a list file, you can specify each file and each output policy by a set of couple\n"
"of -s/--transducer_file and -m/--transducer_policy argument to enumerate the list\n"
"The policy may be MERGE or REPLACE.\n"
"The file option, the alphabet option and the transducer list file option are mandatory.\n"
"\n";
static void usage() {
display_copyright_notice();
u_printf(usage_Cassys);
}
struct grfInfo {
int entity_loc;
int annotation_loc;
unichar *entity_format;
unichar **ignore;
int ignore_count;
unichar **accept;
int accept_count;
unichar *annotation;
unichar *entities;
int entity_count;
int negLeftContxt_loc;
};
unichar** load_file_in_memory(const char* tmp_file, VersatileEncodingConfig *vec,
int *total_lines) {
int num_lines = 0;
unichar **grf_lines = NULL;
unichar *line = NULL;
size_t size_buffer_line = 0;
U_FILE *grf_file = u_fopen(vec,tmp_file,U_READ);
if(grf_file != NULL) {
while(u_fgets_dynamic_buffer(&line, &size_buffer_line, grf_file) != EOF) {
grf_lines = (unichar**) realloc(grf_lines, (num_lines+2) * sizeof(unichar*));
grf_lines[num_lines] = (unichar*) malloc(sizeof(unichar) * (u_strlen(line) + 1));
u_strcpy(grf_lines[num_lines],line);
num_lines++;
grf_lines[num_lines] = NULL;
}
if(line != NULL)
free(line);
u_fclose(grf_file);
}
*total_lines = num_lines;
return grf_lines;
}
void free_file_in_memory(unichar** grf_lines)
{
if (grf_lines == NULL) return;
unichar** walk = grf_lines;
while ((*walk) != NULL) {
free(*walk);
walk++;
}
free(grf_lines);
}
void free_grf_info(grfInfo *infos, int num) {
for (int i = 0;i < num;i++) {
free(infos[i].entity_format);
free(infos[i].annotation);
if (infos[i].entities!=NULL) free(infos[i].entities);
if (infos[i].ignore) {
unichar** walk = infos[i].ignore;
while ((*walk) != NULL) {
free(*walk);
walk++;
}
free(infos[i].ignore);
}
if (infos[i].accept) {
unichar** walk = infos[i].accept;
while ((*walk) != NULL) {
free(*walk);
walk++;
}
free(infos[i].accept);
}
}
free(infos);
}
grfInfo *extract_info(unichar **lines, int *num_annot, int total_lines, int *loc,
unichar **start_line, int **locations) {
DISCARD_UNUSED_PARAMETER(locations)
int start = -1;
int num_info = 0;
int count = 0;
struct grfInfo *infos = NULL;
if (lines != NULL) {
int num_lines = 0;
while (num_lines < total_lines && lines[num_lines] != NULL) {
size_t num_char = u_strlen(lines[num_lines]);
if (num_char == 1 && lines[num_lines][0] == '#') {
start = num_lines;
}
else if (num_lines == start + 2) {
*loc = num_lines;
if ((*start_line) != NULL) free(*start_line);
*start_line = (unichar*)malloc(sizeof(unichar) * (num_char + 1));
u_strcpy(*start_line, lines[num_lines]);
}
else if (num_char > 3 && lines[num_lines][0] == '"' && lines[num_lines][1] == '$' && lines[num_lines][2] == 'G') {
infos = (grfInfo*)realloc(infos, (num_info + 1) * sizeof(grfInfo));
infos[num_info].accept = NULL;
infos[num_info].ignore = NULL;
infos[num_info].annotation = NULL;
infos[num_info].entity_format = (unichar*)malloc(sizeof(unichar) * (num_char + 1));
infos[num_info].entity_format[0] = '"';
infos[num_info].entity_format[1] = '%';
infos[num_info].entity_format[2] = 'S';
infos[num_info].entity_loc = num_lines - (*loc);
infos[num_info].annotation_loc = 0;
infos[num_info].entity_count = 0;
infos[num_info].entities = NULL;
infos[num_info].negLeftContxt_loc = total_lines - (*loc) + count*3; //negative context is made up of 3 lines
count++;
int spaces = 0;
for (size_t i = 3; i <= num_char; i++) {
infos[num_info].entity_format[i] = (unichar)lines[num_lines][i];
if (spaces == 4 && lines[num_lines][i] > 47 && lines[num_lines][i] < 58) { //is digit
infos[num_info].annotation_loc = 10 * infos[num_info].annotation_loc + lines[num_lines][i] - 48;
}
if (lines[num_lines][i] == ' ')
spaces++;
}
// start extract tag information
int annot_pos = infos[num_info].annotation_loc + *loc;
int n = u_strlen(lines[annot_pos]);
int j, k;
int division = -1;
int annot_end = -1;
int ignore_cnt = 0;
int is_accept = 0;
for (j = 0; j < n; j++) {
if (lines[annot_pos][j] == '/') {
division = j;
}
else if(lines[annot_pos][j] == '"') {
annot_end = j;
if(j > 0 && lines[annot_pos][j-1] == '}')
annot_end = j-1;
}
}
unichar *temp_annot = (unichar*)malloc(sizeof(unichar) * division);
for (k = 1; k < division; k++)
temp_annot[k - 1] = lines[annot_pos][k];
temp_annot[k - 1] = '\0';
if (u_strcmp(temp_annot, "<E>") != 0) {
unichar *saveptr = NULL;
const unichar DELIMITER[] = { '~', 0 };
unichar *ignore_token = u_strtok_r(temp_annot, DELIMITER, &saveptr);
unichar *ignore = NULL;
while (ignore_token) {
ignore = ignore_token;
if (u_strcmp(ignore, temp_annot) == 0) {
is_accept = 1;
break;
}
ignore_token = u_strtok_r(NULL, DELIMITER, &saveptr);
infos[num_info].ignore = (unichar**)realloc(infos[num_info].ignore, sizeof(unichar*) * (ignore_cnt + 2));
infos[num_info].ignore[ignore_cnt] = (unichar*)malloc(sizeof(unichar) * (u_strlen(ignore) + 1));
u_strcpy(infos[num_info].ignore[ignore_cnt], ignore);
ignore_cnt++;
infos[num_info].ignore[ignore_cnt] = NULL;
}
if (is_accept && infos[num_info].ignore == NULL) {
int accept_cnt = 0;
int prev = 0;
for (k = 0; k < division; k++) {
if (k > 0 && temp_annot[k] == '+' && temp_annot[k - 1] != '\\') {
infos[num_info].accept = (unichar**)realloc(infos[num_info].accept, sizeof(unichar*) * (accept_cnt + 2));
infos[num_info].accept[accept_cnt] = (unichar*)malloc(sizeof(unichar) * (k - prev + 1));
infos[num_info].accept[accept_cnt + 1] = NULL;
n = 0;
for (j = prev; j < k; j++)
if (temp_annot[j] != '\\') {
infos[num_info].accept[accept_cnt][n++] = temp_annot[j];
}
infos[num_info].accept[accept_cnt][n] = '\0';
prev = k+1;
accept_cnt++;
}
}
infos[num_info].accept = (unichar**)realloc(infos[num_info].accept, sizeof(unichar*) * (accept_cnt + 2));
infos[num_info].accept[accept_cnt] = (unichar*)malloc(sizeof(unichar) * (k - prev + 1));
infos[num_info].accept[accept_cnt + 1] = NULL;
n = 0;
for (j = prev; j < k; j++)
if (temp_annot[j] != '\\') {
infos[num_info].accept[accept_cnt][n++] = temp_annot[j];
}
infos[num_info].accept[accept_cnt][n] = '\0';
accept_cnt++;
infos[num_info].accept_count = accept_cnt;
}
}
free(temp_annot);
infos[num_info].ignore_count = ignore_cnt;
infos[num_info].annotation = (unichar*)malloc(sizeof(unichar) * (annot_end - division));
for (k = 0, j = division + 1; j < annot_end; k++, j++)
infos[num_info].annotation[k] = lines[annot_pos][j];
infos[num_info].annotation[k] = '\0';
//end extract tag information
num_info++;
}
num_lines++;
}
}
*num_annot = num_info;
return infos;
}
unichar **extract_entities(const char *token_list, const char *token_list_backup, VersatileEncodingConfig *vec, int num, int *updates, grfInfo *infos) {
unichar **entity_string = NULL;
entity_string = (unichar**) malloc(sizeof(unichar*) * num);
for(int i = 0; i < num; i++)
entity_string[i] = NULL;
U_FILE *dico = u_fopen(vec, token_list, U_READ); //token list in the current _snt folder
if(dico == NULL)
dico = u_fopen(vec, token_list_backup, U_READ); //token list in the previous _snt folder
if(dico != NULL && infos != NULL) {
int * num_entity = (int*)malloc(sizeof(int)*(num+1));
for(int i = 0; i < num; i++)
num_entity[i] = 0;
unichar *line = NULL;
size_t size_buffer_line = 0;
while(u_fgets_dynamic_buffer(&line, &size_buffer_line, dico) != EOF) {
int line_len = u_strlen(line);
for(int k = 0; k < num; k++) {
int annot_len = u_strlen(infos[k].annotation);
if (line != NULL && line[0] == '{' && line_len > annot_len) {
int j = 0;
while(line[j] != '\0') {
int i = 0;
while(line[j] != '\0' && line[j] != infos[k].annotation[i])
j++;
while(line[j] != '\0' && infos[k].annotation[i] != '\0' && line[j] == infos[k].annotation[i]) {
i++;
j++;
if(line[j]=='\\' && j+1<line_len) { //in case the annotation is protected
if(infos[k].annotation[i] == line[j+1])
j++;
}
}
if(infos[k].annotation[i] == '\0') {
int reverse_i = j; //We will go in reverse to find the entity
int num_paren = 1;
while(reverse_i >= 0) {
if(line[reverse_i] =='{' && num_paren == 1) {
break;
}
else if(line[reverse_i] =='{' && num_paren > 0 ) {
num_paren--;
}
else if(line[reverse_i] =='}') {
num_paren++;
}
reverse_i--;
}
reverse_i++;
int start = reverse_i;
int end = 0;
int annot_start = 0;
int annot = -1;
unichar *prev_char = NULL;
unichar *entity_whole = NULL; // test on reverse_i is a quick fix to pass valgrind validation
if (infos[k].ignore == NULL && infos[k].accept == NULL) {
end = j-annot_len;
entity_whole = (unichar*) malloc(sizeof(unichar) * (end - reverse_i+1));
int entity_i = 0;
for(int x = (reverse_i > 0) ? reverse_i : 0; x < end; x++) {
if (line[x] == '\\' || line[x] == '{' || line[x] == '}')
;
else if (line[x] == ',') {
if ((x + 1 < end && line[x + 1] == '.') ||
(x + 2 < end && line[x + 1] == '\\' && line[x + 2] == '.')) {
while(x < end && line[x] != '}')
x++;
}
else {
entity_whole[entity_i++] = line[x];
}
}
else {
entity_whole[entity_i++] = line[x];
}
}
entity_whole[entity_i] = '\0';
}
else {
for(int x = (reverse_i > 0) ? reverse_i : 0; x < j; x++) {
if(line[x] == '{') {
start = x+1;
if (x > 0) {
if (prev_char!=NULL) free(prev_char);
prev_char = (unichar*) malloc(sizeof(unichar) * 2);
prev_char[0] = line[x - 1];
if(line[x - 1] == '\\')
prev_char[0] = line[x - 2];
prev_char[1] = '\0';
}
}
else if(line[x] == ',') {
annot = 0;
end = x;
}
else if(annot == 0 && line[x] == '.') {
annot = -1;
annot_start = x + 1;
}
else if(line[x] == '\\' || line[x] == '+' || line[x] == '}') {
if(annot_start > 0 && start > 0) {
annot = -1;
int matches = 0;
unichar *annot_ = (unichar*) malloc(sizeof(unichar) * ((x - annot_start)+1));
int z = 0;
for(int y = annot_start; y < x; y++)
if(line[y] != '\\') {
annot_[z++] = (unichar) line[y];
}
annot_[z] = '\0';
if(infos[k].ignore != NULL) {
for(int m = 0; m < infos[k].ignore_count; m++)
if(u_strncmp(annot_,infos[k].ignore[m],u_strlen(infos[k].ignore[m])) == 0) {
matches = 1;
break;
}
}
if(infos[k].accept != NULL) {
matches = 1;
for(int m = 0; m < infos[k].accept_count; m++)
if(u_strncmp(annot_,infos[k].accept[m],u_strlen(infos[k].accept[m])) == 0) {
matches = 0;
break;
}
}
if(matches == 0) {
unichar *entity = NULL;
entity = (unichar*) malloc(sizeof(unichar) * ((end - start)+1));
z = 0;
int nb_expand=0;
for(int y = start; y < end; y++) {
if(line[y] != '\\') {
if(line[y] == '/') {
nb_expand++;
entity = (unichar*) realloc(entity,sizeof(unichar) * ((end - start)+1+nb_expand));
entity[z++] = '\\';
}
entity[z++] = line[y];
}
}
entity[z] = '\0';
int entity_len = z + 1;
if(entity_whole == NULL) {
entity_whole = (unichar*) malloc(sizeof(unichar) * (entity_len+1));
u_strcpy(entity_whole,entity);
}
else {
int current_len = u_strlen(entity_whole);
entity_whole = (unichar*) realloc(entity_whole,sizeof(unichar) * (current_len + entity_len + 2));
if(prev_char[0] == '{') { //In case of multi level annotation, there could be multiple '\{' characters before the entity
prev_char[0] = ' ' ;
}
if(infos[k].accept_count==1) {
//if same element appears more than once then we store
// them separately
prev_char[0] = '+' ;
}
u_strcat(entity_whole,prev_char);
u_strcat(entity_whole,entity);
}
if(entity != NULL)
free(entity);
start = -1;
}
if(annot_ != NULL)
free(annot_);
annot_start = -1;
}
}
}
}
if(entity_whole != NULL) {
int entity_len = u_strlen(entity_whole);
unichar* protected_entity = NULL;
protected_entity = (unichar*) malloc(sizeof(unichar) * ((1+entity_len)*2));
entity_len = escape(entity_whole,protected_entity,P_PLUS_COLON_SLASH);
if(infos[k].entity_count == 0) {
infos[k].entities = (unichar*) malloc(sizeof(unichar) * (entity_len+1));
u_strcpy(infos[k].entities,protected_entity);
infos[k].entity_count++;
*updates = *updates + 1;
}
else {
int current_len = u_strlen(infos[k].entities);
infos[k].entities = (unichar*) realloc(infos[k].entities, sizeof(unichar) * (current_len + entity_len + 2));
infos[k].entities[current_len] = '+';
for(int a = current_len + 1, b = 0; a < current_len + entity_len + 2; a++)
infos[k].entities[a] = protected_entity[b++];
infos[k].entities[current_len + entity_len + 1] = '\0';
}
free(entity_whole);
free(protected_entity);
}
if (prev_char != NULL)
free(prev_char);
}
}
}
}
}
if(line !=NULL)
free(line);
u_fclose(dico);
free(num_entity);
}
return entity_string;
}
static void print_entity_param(U_FILE* out, unichar* format, const unichar*param)
{
int len_format = (int)u_strlen(format);
int i;
for (i = 0; (i + 1) < len_format; i++) {
if (((*(format + i)) == '%') && ((*(format + i + 1)) == 'S')) {
break;
}
}
/*if ((i + 1) == len_format) {
u_fprintf(out, "%S", format);
return;
}*/
u_fprintf(out, "\"%S", param);
//*(format + i) = '\0';
//u_fprintf(out, "%S", format);
//u_printf( "%S", format);
//*(format + i) = '%';
u_fprintf(out, "%S", format+3);
}
int update_tmp_graph(const char *transducer, VersatileEncodingConfig *vec, unichar **lines, int total_lines, const unichar *start, int start_loc, int num_entities, int num_info, grfInfo *infos, int mode) {
int status = 0;
U_FILE *graph_file = u_fopen(vec,transducer,U_WRITE);
if (graph_file !=NULL) {
if(lines != NULL) {
int num_lines = 0;
int updates = 0;
while(num_lines < total_lines && lines[num_lines] !=NULL) {
size_t num_char = u_strlen(lines[num_lines]);
if (num_lines == start_loc -1 && mode) {
u_fprintf(graph_file,"%d\n",total_lines-start_loc+3*num_info); //total number of lines should be updated to reflect the newly added negative contexts
}
else if(mode && num_lines == start_loc) {
int spaces = 0;
for(int i = 0; start[i] !='\0'; i++) {
u_fprintf(graph_file,"%C",start[i]);
if(start[i] == ' ')
spaces++;
if(spaces == 3)
break;
}
u_fprintf(graph_file,"%d ",num_entities);
for(int i = 0; i < num_info; i++) {
if(infos[i].entity_count > 0) {
u_fprintf(graph_file,"%d ",infos[i].negLeftContxt_loc); //point to negative context
}
}
u_fprintf(graph_file,"\n");
}
else if(mode && num_char > 3 && lines[num_lines][0] == '"' && lines[num_lines][1] == '$' && lines[num_lines][2] == 'G') {
int loc = num_lines - start_loc;
for(int i = 0; i < num_info; i++) {
if(infos[i].entity_loc == loc) {
print_entity_param(graph_file,infos[i].entity_format, infos[i].entities);
u_fprintf(graph_file,"\n");
}
}
updates++;
}
else if(mode) {
int loc = num_lines - start_loc;
int found = 0;
for(int i = 0; i < num_info; i++) {
if(infos[i].annotation_loc == loc) {
u_fprintf(graph_file, "\"<E>");
int j = 0;
while(lines[num_lines][j] != '\0' && lines[num_lines][j] != '/')
j++;
for(; j<(int)u_strlen(lines[num_lines]); j++) {
u_fprintf(graph_file,"%C",lines[num_lines][j]);
}
u_fprintf(graph_file,"\n");
found++;
}
}
if(found == 0) {
u_fprintf(graph_file,"%S\n",lines[num_lines]);
}
}
else {
u_fprintf(graph_file,"%S\n",lines[num_lines]);
}
num_lines++;
}
if(mode) { // add the negative left context for each path
for(int i = 0; i < num_info; i++) {
u_fprintf(graph_file,"\"$![\" 10 20 1"); //use random coordinates
u_fprintf(graph_file," %d \n",infos[i].negLeftContxt_loc+1);
u_fprintf(graph_file,"\"<");
for(int j=2; infos[i].annotation[j]!='\0'; j++ ) {
u_fprintf(graph_file,"%C",infos[i].annotation[j]);
}
u_fprintf(graph_file,">\" 10 20 1 %d \n",infos[i].negLeftContxt_loc+2);
u_fprintf(graph_file,"\"$]\" 10 20 1 %d \n",infos[i].entity_loc); // point back to the entity
}
}
}
u_fclose(graph_file);
status = 1;
}
return status;
}
typedef struct {
char transducer_list_file_name[FILENAME_MAX];
char text_file_name[FILENAME_MAX];
char alphabet_file_name[FILENAME_MAX];
char name_uima_offsets_file[FILENAME_MAX];
char name_input_offsets_file[FILENAME_MAX];
char transducer_filename_prefix[FILENAME_MAX];
char extension_text_name[FILENAME_MAX];
char language[FILENAME_MAX];
char stdoff_file[FILENAME_MAX];
} Cassys_text_buffer;
typedef struct {
char last_labeled_text_name[FILENAME_MAX];
char orig_grf[FILENAME_MAX];
char template_name_without_extension[FILENAME_MAX];
// char graph_file_name_n[FILENAME_MAX];
char result_file_name_XML[FILENAME_MAX];
char text_name_without_extension[FILENAME_MAX];
char path[FILENAME_MAX];
char last_resulting_text_path[FILENAME_MAX];
char result_file_name_path_XML[FILENAME_MAX];
char result_file_name_path_offset[FILENAME_MAX + 0x20];
char result_file_name_raw[FILENAME_MAX];
char result_file_name_path_raw[FILENAME_MAX];
char graph_file_name[FILENAME_MAX];
} Cascade_text_buffer;
int main_Cassys(int argc,char* const argv[]) {
if (argc==1) {
usage();
return SUCCESS_RETURN_CODE;
}
Cassys_text_buffer* textbuf = (Cassys_text_buffer*)malloc(sizeof(Cassys_text_buffer));
if (textbuf == NULL) {
alloc_error("main_Cassys");
return ALLOC_ERROR_CODE;
}
char* morpho_dic = NULL;
bool has_transducer_list = false;
bool has_text_file_name = false;
bool has_alphabet = false;
char negation_operator[0x20];
VersatileEncodingConfig vec=VEC_DEFAULT;
int must_create_directory = 1;
int in_place = 0;
int realign_token_graph_pointer = 0;
int translate_path_separator_to_native = 0;
int dump_graph = 0; // By default, don't build a .dot file.
int display_perf = 0;
int produce_offsets_file = 0;
int istex_param = 0;
// define CASSYS_DEFAULT_TEMP_WORK_DIR with a default location (probably in virtual system file) to
// build a version of k6 which uses this temp location and perform cleanup
#ifdef CASSYS_DEFAULT_TEMP_WORK_DIR
#define CASSYS_STRINGIZE(x) #x
#define CASSYS_STRINGIZE2(x) CASSYS_STRINGIZE(x)
#define CASSYS_DEFAULT_TEMP_WORK_DIR_AS_STRING CASSYS_STRINGIZE2(CASSYS_DEFAULT_TEMP_WORK_DIR)
#endif
#ifdef CASSYS_DEFAULT_TEMP_WORK_DIR_AS_STRING
int must_do_temp_cleanup = 1;
char* temp_work_dir = strdup(CASSYS_DEFAULT_TEMP_WORK_DIR_AS_STRING);
#else
int must_do_temp_cleanup = 0;
char* temp_work_dir = NULL;
#endif
struct transducer_name_and_mode_linked_list* transducer_name_and_mode_linked_list_arg=NULL;
vector_ptr* tokenize_additional_args = new_vector_ptr();
if (tokenize_additional_args == NULL) {
alloc_error("main_Cassys");
free(temp_work_dir);
free(textbuf);
return ALLOC_ERROR_CODE;
}
vector_ptr* locate_additional_args = new_vector_ptr();
if (locate_additional_args == NULL) {
alloc_error("main_Cassys");
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(textbuf);
return ALLOC_ERROR_CODE;
}
vector_ptr* concord_additional_args = new_vector_ptr();
if (tokenize_additional_args == NULL) {
alloc_error("main_Cassys");
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(textbuf);
return ALLOC_ERROR_CODE;
}
// decode the command line
int val;
int index = 1;
negation_operator[0]='\0';
textbuf->transducer_filename_prefix[0]='\0';
textbuf->name_uima_offsets_file[0] = '\0';
textbuf->name_input_offsets_file[0] = '\0';
textbuf->language[0] = '\0';
textbuf->stdoff_file[0] = '\0';
bool only_verify_arguments = false;
UnitexGetOpt options;
while (EOF != (val=options.parse_long(argc, argv, optstring_Cassys,
lopts_Cassys, &index))) {
switch (val) {
case 'V': only_verify_arguments = true;
break;
case 'h': usage();
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return SUCCESS_RETURN_CODE;
case 'k': if (options.vars()->optarg[0]=='\0') {
error("Empty input_encoding argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
}
decode_reading_encoding_parameter(&(vec.mask_encoding_compatibility_input),options.vars()->optarg);
break;
case 'q': if (options.vars()->optarg[0]=='\0') {
error("Empty output_encoding argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
}
decode_writing_encoding_parameter(&(vec.encoding_output),&(vec.bom_output),options.vars()->optarg);
break;
case 't': {
if (options.vars()->optarg[0] == '\0') {
error("Command line error : Empty file name argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
}
get_extension(options.vars()->optarg, textbuf->extension_text_name);
if (strcmp(textbuf->extension_text_name, ".snt") != 0) {
error("Command line error : File name argument %s must be a preprocessed snt file\n",
options.vars()->optarg);
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
}
strcpy(textbuf->text_file_name, options.vars()->optarg);
has_text_file_name = true;
break;
}
case 'l': {
if(options.vars()->optarg[0] == '\0'){
error("Command line error : Empty transducer list argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
strcpy(textbuf->transducer_list_file_name, options.vars()->optarg);
has_transducer_list = true;
}
break;
}
case 'r': {
if(options.vars()->optarg[0] == '\0'){
error("Command line error : Empty transducer directory argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
strcpy(textbuf->transducer_filename_prefix, options.vars()->optarg);
has_transducer_list = true;
}
break;
}
case 's': {
if(options.vars()->optarg[0] == '\0'){
error("Command line error : Empty transducer filename argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
transducer_name_and_mode_linked_list_arg=add_transducer_linked_list_new_name(transducer_name_and_mode_linked_list_arg,options.vars()->optarg);
}
break;
}
case 'm': {
if(options.vars()->optarg[0] == '\0'){
error("Command line error : Empty transducer mode argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
set_last_transducer_linked_list_mode_by_string(transducer_name_and_mode_linked_list_arg,options.vars()->optarg);
}
break;
}
case 'a':{
if (options.vars()->optarg[0] == '\0') {
error("Command line error : Empty alphabet argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
strcpy(textbuf->alphabet_file_name, options.vars()->optarg);
has_alphabet = true;
}
break;
}
case '$':{
if (options.vars()->optarg[0] == '\0') {
error("Command line error : Empty input offsets file argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
strcpy(textbuf->name_input_offsets_file, options.vars()->optarg);
has_alphabet = true;
}
break;
}
case 'O': {
produce_offsets_file = 1;
break;
}
case 'f':{
if (options.vars()->optarg[0] == '\0') {
error("Command line error : Empty uima offsets file argument\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
} else {
strcpy(textbuf->name_uima_offsets_file, options.vars()->optarg);
has_alphabet = true;
}
break;
}
case 'g': if (options.vars()->optarg[0]=='\0') {
error("You must specify an argument for negation operator\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
}
if ((strcmp(options.vars()->optarg,"minus")!=0) && (strcmp(options.vars()->optarg,"-")!=0) &&
(strcmp(options.vars()->optarg,"tilde")!=0) && (strcmp(options.vars()->optarg,"~")!=0))
{
error("You must specify a valid argument for negation operator\n");
free_transducer_name_and_mode_linked_list(transducer_name_and_mode_linked_list_arg);
free_vector_ptr(concord_additional_args, free);
free_vector_ptr(locate_additional_args, free);
free_vector_ptr(tokenize_additional_args, free);
free(temp_work_dir);
free(morpho_dic);
free(textbuf);
return USAGE_ERROR_CODE;
}
strcpy(negation_operator,options.vars()->optarg);