-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhfst-optimized-lookup.cc
2056 lines (1862 loc) · 56.8 KB
/
hfst-optimized-lookup.cc
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
/*
Copyright 2009 University of Helsinki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
NOTE:
THIS SINGLE-FILE VERSION WAS PUT TOGETHER FROM A MULTI-FILE VERSION
SO THE CURRENT STRUCTURE IS NOT SO GREAT. TODO: FIX THIS.
TODO: USE THE EXISTING HFST-TOOLS FRAMEWORK BETTER.
*/
#include "hfst-optimized-lookup.h"
//#ifdef _MSC_VER
//# include <windows.h>
//#endif
#include <cstdarg>
#include <iostream> // DEBUG
static OutputType outputType = xerox;
static bool verboseFlag = false;
static bool displayWeightsFlag = false;
static bool displayUniqueFlag = false;
static bool echoInputsFlag = false;
static bool beFast = false;
static int maxAnalyses = INT_MAX;
static bool limit_reached = false;
static unsigned long call_counter = 0;
static double time_cutoff = 0.0;
static clock_t start_clock;
static float beam=-1;
static bool pipe_input = false;
static bool pipe_output = false;
bool print_usage(void)
{
std::cout <<
"\n" <<
"Usage: " << PACKAGE_NAME << " [OPTIONS] TRANSDUCER\n" <<
"Run a transducer on standard input (one word per line) and print analyses\n" <<
"NOTE: hfst-optimized-lookup does lookup from left to right as opposed to xfst\n" <<
" and foma lookup which is carried out from right to left. In order to do\n" <<
" lookup in a similar way as xfst and foma, invert the transducer first.\n" <<
"\n" <<
" -h, --help Print this help message\n" <<
" -V, --version Print version information\n" <<
" -v, --verbose Be verbose\n" <<
" -q, --quiet Don't be verbose (default)\n" <<
" -s, --silent Same as quiet\n" <<
" -e, --echo Echo inputs\n" <<
" (useful if redirecting lots of output to a file)\n" <<
" -w, --show-weights Print final analysis weights (if any)\n" <<
" -u, --unique Suppress duplicate analyses\n" <<
" -n N, --analyses=N Output no more than N analyses\n" <<
" (if the transducer is weighted, the N best analyses)\n" <<
" -b, --beam=B Output only analyses whose weight is within B from\n" <<
" the best analysis\n" <<
" -t, --time-cutoff=S Limit search after having used S seconds per input\n"
" -x, --xerox Xerox output format (default)\n" <<
" -f, --fast Be as fast as possible.\n" <<
" (with this option enabled -u and -n don't work and\n" <<
" output won't be ordered by weight).\n" <<
" -p, --pipe-mode[=STREAM] Control input and output streams.\n" <<
"\n" <<
"N must be a positive integer. B must be a non-negative float.\n" <<
"S must be a non-negative float. The default, 0.0, indicates no cutoff.\n"
"Options -n and -b are combined with AND, i.e. they both restrict the output.\n" <<
"\n" <<
"STREAM can be { input, output, both }. If not given, defaults to {both}.\n" <<
#ifdef _MSC_VER
"Input is read interactively via the console, i.e. line by line from the user.\n" <<
"If you redirect input from a file, use --pipe-mode=input. Output is by default\n" <<
"printed to the console. If you redirect output to a file, use --pipe-mode=output.\n" <<
#else
"Input is read interactively line by line from the user. If you redirect input\n" <<
"from a file, use --pipe-mode=input. --pipe-mode=output is ignored on non-windows\n" <<
"platforms.\n" <<
#endif
"\n" <<
"Report bugs to " << PACKAGE_BUGREPORT << "\n" <<
"\n";
return true;
}
bool print_version(void)
{
std::cout <<
"\n" <<
PACKAGE_STRING << std::endl <<
__DATE__ << " " __TIME__ << std::endl <<
"copyright (C) 2009 University of Helsinki\n";
return true;
}
bool print_short_help(void)
{
print_usage();
return true;
}
TransducerFile::TransducerFile(const char *p):
path(p),
file(p),
header(file.f),
alphabet(file.f, header.symbol_count())
{
transducer = instantiateTransducer(file.f, header, alphabet);
}
std::vector<std::vector<std::string> > TransducerFile::lookup(const char* input_text) {
SymbolNumber * input_string = (SymbolNumber*)(malloc(2000));
for (int i = 0; i < 1000; ++i)
{
input_string[i] = NO_SYMBOL_NUMBER;
}
int i = 0;
SymbolNumber k = NO_SYMBOL_NUMBER;
bool tokenization_failed = false;
for ( const char ** Str = &input_text; **Str != 0; )
{
k = transducer->find_next_key(Str);
if (k == NO_SYMBOL_NUMBER)
{
tokenization_failed = true;
break;
}
input_string[i] = k;
++i;
}
transducer->analyze(input_string);
std::vector<std::vector<std::string> > output;
if (tokenization_failed) {
// Return empty output vector
return output;
}
Transducer* t = dynamic_cast<Transducer*>(transducer);
if (t) {
DisplayVector analyses = t->get_display_vector();
for (DisplayVector::iterator it = analyses.begin(); it != analyses.end(); it++) {
std::vector<std::string> output_analysis;
for (std::vector<std::string>::iterator it2 = it->begin(); it2 != it->end(); it2++) {
output_analysis.push_back(*it2);
}
output.push_back(output_analysis);
}
} else {
throw std::runtime_error("Weighted transducers not yet supported");
}
return output;
}
#if BUILD_HFSTOL_MAIN
int main(int argc, char **argv)
{
int c;
while (true)
{
static struct option long_options[] =
{
// first the hfst-mandated options
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
{"quiet", no_argument, 0, 'q'},
{"silent", no_argument, 0, 's'},
// the hfst-optimized-lookup-specific options
{"echo-inputs", no_argument, 0, 'e'},
{"show-weights", no_argument, 0, 'w'},
{"beam", required_argument, 0, 'b'},
{"time-cutoff", required_argument, 0, 't'},
{"unique", no_argument, 0, 'u'},
{"xerox", no_argument, 0, 'x'},
{"fast", no_argument, 0, 'f'},
{"pipe-mode", optional_argument, 0, 'p'},
{"analyses", required_argument, 0, 'n'},
{0, 0, 0, 0 }
};
int option_index = 0;
c = getopt_long(argc, argv, "hVvqsewb:t:uxfn:p::", long_options, &option_index);
if (c == -1) // no more options to look at
break;
switch (c)
{
case 'h':
print_usage();
return EXIT_SUCCESS;
break;
case 'V':
print_version();
return EXIT_SUCCESS;
break;
case 'v':
#ifdef OL_DEBUG
printDebuggingInformationFlag = true;
preserveDiacriticRepresentationsFlag = true;
#endif
#ifdef TIMING
timingFlag = true;
#endif
verboseFlag = true;
break;
case 'q':
case 's':
#ifdef OL_DEBUG
printDebuggingInformationFlag = false;
preserveDiacriticRepresentationsFlag = false;
#endif
#ifdef TIMING
timingFlag = false;
#endif
verboseFlag = false;
displayWeightsFlag = true;
break;
case 'e':
echoInputsFlag = true;
break;
case 'w':
displayWeightsFlag = true;
break;
case 'u':
displayUniqueFlag = true;
break;
case 'b':
beam = atof(optarg);
if (beam < 0)
{
std::cerr << "Invalid argument for --beam\n";
return EXIT_FAILURE;
}
break;
case 't':
time_cutoff = atof(optarg);
if (time_cutoff < 0.0)
{
std::cerr << "Invalid argument for --time-cutoff\n";
return EXIT_FAILURE;
}
break;
case 'n':
maxAnalyses = atoi(optarg);
if (maxAnalyses < 1)
{
std::cerr << "Invalid or no argument for analyses count\n";
return EXIT_FAILURE;
}
break;
case 'x':
outputType = xerox;
break;
case 'f':
beFast = true;
break;
case 'p':
if (optarg == NULL)
{ pipe_input = true; pipe_output = true; }
else if (strcmp(optarg, "both") == 0 || strcmp(optarg, "BOTH") == 0)
{ pipe_input = true; pipe_output = true; }
else if (strcmp(optarg, "input") == 0 || strcmp(optarg, "INPUT") == 0 ||
strcmp(optarg, "in") == 0 || strcmp(optarg, "IN") == 0)
{ pipe_input = true; }
else if (strcmp(optarg, "output") == 0 || strcmp(optarg, "OUTPUT") == 0 ||
strcmp(optarg, "out") == 0 || strcmp(optarg, "OUT") == 0)
{ pipe_output = true; }
else
{ std::cerr << "--pipe-mode argument " << std::string(optarg) << " unrecognised\n\n";
return EXIT_FAILURE; }
break;
default:
std::cerr << "Invalid option\n\n";
print_short_help();
return EXIT_FAILURE;
break;
}
}
#ifdef _MSC_VER
//hfst::print_output_to_console(!pipe_output); // has no effect on windows or mac
#endif
// no more options, we should now be at the input filename
if ( (optind + 1) < argc)
{
std::cerr << "More than one input file given\n";
return EXIT_FAILURE;
}
else if ( (optind + 1) == argc)
{
FILE * f = fopen(argv[(optind)], "rb");
if (f == NULL)
{
std::cerr << "Could not open file " << argv[(optind)] << std::endl;
return 1;
}
return setup(f);
}
else
{
std::cerr << "No input file given\n";
return EXIT_FAILURE;
}
}
#endif
void TransducerHeader::skip_hfst3_header(FILE * f)
{
const char* header1 = "HFST";
unsigned int header_loc = 0; // how much of the header has been found
int c;
for(header_loc = 0; header_loc < strlen(header1) + 1; header_loc++)
{
c = getc(f);
if(c != header1[header_loc]) {
break;
}
}
if(header_loc == strlen(header1) + 1) // we found it
{
unsigned short remaining_header_len;
if (fread(&remaining_header_len,
sizeof(remaining_header_len), 1, f) != 1 ||
getc(f) != '\0') {
throw HeaderParsingException();
}
char * headervalue = new char[remaining_header_len];
if (fread(headervalue, remaining_header_len, 1, f) != 1)
{
throw HeaderParsingException();
}
if (headervalue[remaining_header_len - 1] != '\0') {
throw HeaderParsingException();
}
std::string header_tail(headervalue, remaining_header_len);
size_t type_field = header_tail.find("type");
if (type_field != std::string::npos) {
if (header_tail.find("HFST_OL") != type_field + 5 &&
header_tail.find("HFST_OLW") != type_field + 5) {
delete[] headervalue;
throw HeaderParsingException();
}
}
} else // nope. put back what we've taken
{
ungetc(c, f); // first the non-matching character
for(int i = header_loc - 1; i>=0; i--) {
// then the characters that did match (if any)
ungetc(header1[i], f);
}
}
}
void TransducerAlphabet::get_next_symbol(FILE * f, SymbolNumber k)
{
int byte;
char * sym = line;
while ( (byte = fgetc(f)) != 0 )
{
if (byte == EOF || sym - line >= LINE_SIZE)
{
throw HeaderParsingException();
}
*sym = byte;
++sym;
}
*sym = 0;
if (strlen(line) >= 5 && line[0] == '@' && line[strlen(line) - 1] == '@' && line[2] == '.')
{ // a special symbol needs to be parsed
std::string feat;
std::string val;
FlagDiacriticOperator op = P; // g++ worries about this falling through uninitialized
switch (line[1]) {
case 'P': op = P; break;
case 'N': op = N; break;
case 'R': op = R; break;
case 'D': op = D; break;
case 'C': op = C; break;
case 'U': op = U; break;
}
char * c = line;
// as long as we're working with utf-8, this should be ok
for (c +=3; *c != '.' && *c != '@'; c++) { feat.append(c,1); }
if (*c == '.')
{
for (++c; *c != '@'; c++) { val.append(c,1); }
}
if (feature_bucket.count(feat) == 0)
{
feature_bucket[feat] = feat_num;
++feat_num;
}
if (value_bucket.count(val) == 0)
{
value_bucket[val] = val_num;
++val_num;
}
operations.push_back(FlagDiacriticOperation(op, feature_bucket[feat], value_bucket[val]));
kt->operator[](k) = strdup("");
#if OL_FULL_DEBUG
std::cout << "symbol number " << k << " (flag) is \"" << line << "\"" << std::endl;
kt->operator[](k) = strdup(line);
#endif
return;
}
operations.push_back(FlagDiacriticOperation()); // dummy flag
#if OL_FULL_DEBUG
std::cout << "symbol number " << k << " is \"" << line << "\"" << std::endl;
#endif
kt->operator[](k) = strdup(line);
}
void LetterTrie::add_string(const char * p, SymbolNumber symbol_key)
{
if (*(p+1) == 0)
{
symbols[(unsigned char)(*p)] = symbol_key;
return;
}
if (letters[(unsigned char)(*p)] == NULL)
{
letters[(unsigned char)(*p)] = new LetterTrie();
}
letters[(unsigned char)(*p)]->add_string(p+1,symbol_key);
}
bool LetterTrie::has_key_starting_with(const char c) const
{
return letters[(unsigned char) c] != NULL;
}
SymbolNumber LetterTrie::find_key(const char ** p)
{
const char * old_p = *p;
++(*p);
if (letters[(unsigned char)(*old_p)] == NULL)
{
return symbols[(unsigned char)(*old_p)];
}
SymbolNumber s = letters[(unsigned char)(*old_p)]->find_key(p);
if (s == NO_SYMBOL_NUMBER)
{
--(*p);
return symbols[(unsigned char)(*old_p)];
}
return s;
}
void Encoder::read_input_symbols(KeyTable * kt)
{
for (SymbolNumber k = 0; k < number_of_input_symbols; ++k)
{
#if DEBUG
assert(kt->find(k) != kt->end());
#endif
const char * p = kt->operator[](k);
if ((strlen(p) == 1) && (unsigned char)(*p) <= 127
// we have a single char ascii symbol
&& !letters.has_key_starting_with(*p)) {
// make sure there isn't a longer symbol we would be shadowing
ascii_symbols[(unsigned char)(*p)] = k;
}
// If there's an ascii tokenized symbol shadowing this, remove it
if (strlen(p) > 1 &&
(unsigned char)(*p) <= 127 &&
ascii_symbols[(unsigned char)(*p)] != NO_SYMBOL_NUMBER) {
ascii_symbols[(unsigned char)(*p)] = NO_SYMBOL_NUMBER;
}
letters.add_string(p,k);
}
}
SymbolNumber Encoder::find_key(const char ** p)
{
if (ascii_symbols[(unsigned char)(**p)] == NO_SYMBOL_NUMBER)
{
return letters.find_key(p);
}
SymbolNumber s = ascii_symbols[(unsigned char)(**p)];
++(*p);
return s;
}
#if BUILD_HFSTOL_MAIN
void runTransducer (TransducerBase * T)
{
SymbolNumber * input_string = (SymbolNumber*)(malloc(2000));
for (int i = 0; i < 1000; ++i)
{
input_string[i] = NO_SYMBOL_NUMBER;
}
char * str = (char*)(malloc(MAX_IO_STRING*sizeof(char)));
*str = 0;
char * old_str = str;
while(true)
{
#ifdef WINDOWS
if (!pipe_input)
{
free(str);
std::string linestr("");
if (! hfst::get_line_from_console(linestr, MAX_IO_STRING*sizeof(char)))
break;
str = strdup(linestr.c_str());
old_str = str;
}
else
#endif
{
if (! std::cin.getline(str,MAX_IO_STRING))
break;
}
if (echoInputsFlag)
{
#ifdef WINDOWS
if (!pipe_output)
hfst_fprintf_console(stdout, "%s\n", str); // fix: add \r?
else
#endif
std::cout << str << std::endl;
}
int i = 0;
SymbolNumber k = NO_SYMBOL_NUMBER;
bool failed = false;
for ( const char ** Str = (const char **)&str; **Str != 0; )
{
k = T->find_next_key(Str);
#if OL_FULL_DEBUG
std::cout << "INPUT STRING ENTRY " << i << " IS " << k << std::endl;
#endif
if (k == NO_SYMBOL_NUMBER)
{
if (echoInputsFlag)
{
std::cout << std::endl;
}
failed = true;
break;
}
input_string[i] = k;
++i;
}
str = old_str;
if (failed)
{ // tokenization failed
if (outputType == xerox)
{
#ifdef WINDOWS
if (!pipe_output)
hfst_fprintf_console(stdout, "%s\t%s\t+?\n\n", str, str);
else
#endif
std::cout << str << "\t" << str << "\t+?" << std::endl << std::endl;
#ifdef WINDOWS
if (!pipe_output)
hfst_fprintf_console(stdout, "\n\n");
else
#endif
std::cout << std::endl << std::endl;
}
continue;
}
input_string[i] = NO_SYMBOL_NUMBER;
if (time_cutoff > 0.0) {
start_clock = clock();
call_counter = 0;
limit_reached = false;
}
T->analyze(input_string);
T->printAnalyses(std::string(str));
}
}
#endif
TransducerBase * instantiateTransducer(FILE * f, TransducerHeader& header, TransducerAlphabet& alphabet)
{
if (header.probe_flag(Has_unweighted_input_epsilon_cycles) ||
header.probe_flag(Has_input_epsilon_cycles))
{
std::cerr << "!! Warning: transducer has epsilon cycles !!\n"
<< "!! This is currently not handled - if they are encountered !!\n"
<< "!! program *will* segfault. !!\n";
}
if (alphabet.get_state_size() == 0)
{ // if the state size is zero, there are no flag diacritics to handle
if (header.probe_flag(Weighted) == false)
{
if (displayUniqueFlag)
{ // no flags, no weights, unique analyses only
return new TransducerUniq(f, header, alphabet);
} else if (!displayUniqueFlag)
{ // no flags, no weights, all analyses
return new Transducer(f, header, alphabet);
}
}
else if (header.probe_flag(Weighted) == true)
{
if (displayUniqueFlag)
{ // no flags, weights, unique analyses only
return new TransducerWUniq(f, header, alphabet);
} else if (!displayUniqueFlag)
{ // no flags, weights, all analyses
return new TransducerW(f, header, alphabet);
}
}
} else // handle flag diacritics
{
if (header.probe_flag(Weighted) == false)
{
if (displayUniqueFlag)
{ // flags, no weights, unique analyses only
// return new TransducerFdUniq(f, header, alphabet);
} else
{ // flags, no weights, all analyses
return new TransducerFd(f, header, alphabet);
}
}
else if (header.probe_flag(Weighted) == true)
{
if (displayUniqueFlag)
{ // flags, weights, unique analyses only
return new TransducerWFdUniq(f, header, alphabet);
} else
{ // flags, no weights, all analyses
return new TransducerWFd(f, header, alphabet);
}
}
}
throw std::runtime_error("should not happen");
}
#if BUILD_HFSTOL_MAIN
int setup(FILE * f)
{
try
{
TransducerHeader header(f);
TransducerAlphabet alphabet(f, header.symbol_count());
TransducerBase * T = instantiateTransducer(f,header, alphabet);
runTransducer(T);
delete T;
}
catch (const HeaderParsingException & e)
{
std::cerr << "Invalid transducer header.\n";
std::cerr << "The transducer must be in optimized lookup format.\n";
return EXIT_FAILURE;
}
return 0;
}
#endif
/**
* BEGIN old transducer.cc
*/
bool TransducerFd::PushState(FlagDiacriticOperation op)
{ // try to alter the flag diacritic state stack
switch (op.Operation()) {
case P: // positive set
statestack.push_back(statestack.back());
statestack.back()[op.Feature()] = op.Value();
return true;
case N: // negative set (literally, in this implementation)
statestack.push_back(statestack.back());
statestack.back()[op.Feature()] = -1*op.Value();
return true;
case R: // require
if (op.Value() == 0) // empty require
{
if (statestack.back()[op.Feature()] == 0)
{
return false;
}
else
{
statestack.push_back(statestack.back());
return true;
}
}
if (statestack.back()[op.Feature()] == op.Value())
{
statestack.push_back(statestack.back());
return true;
}
return false;
case D: // disallow
if (op.Value() == 0) // empty disallow
{
if (statestack.back()[op.Feature()] != 0)
{
return false;
}
else
{
statestack.push_back(statestack.back());
return true;
}
}
if (statestack.back()[op.Feature()] == op.Value()) // nonempty disallow
{
return false;
}
statestack.push_back(statestack.back());
return true;
case C: // clear
statestack.push_back(statestack.back());
statestack.back()[op.Feature()] = 0;
return true;
case U: // unification
if (statestack.back()[op.Feature()] == 0 || // if the feature is unset or
statestack.back()[op.Feature()] == op.Value() || // the feature is at this value already or
(statestack.back()[op.Feature()] < 0 &&
(statestack.back()[op.Feature()] * -1 != op.Value())) // the feature is negatively set to something else
)
{
statestack.push_back(statestack.back());
statestack.back()[op.Feature()] = op.Value();
return true;
}
return false;
}
throw; // for the compiler's peace of mind
}
bool TransitionIndex::matches(SymbolNumber s)
{
if (input_symbol == NO_SYMBOL_NUMBER)
{
return false;
}
if (s == NO_SYMBOL_NUMBER)
{
return true;
}
return input_symbol == s;
}
bool Transition::matches(SymbolNumber s)
{
if (input_symbol == NO_SYMBOL_NUMBER)
{
return false;
}
if (s == NO_SYMBOL_NUMBER)
{
return true;
}
return input_symbol == s;
}
void IndexTableReader::get_index_vector(void)
{
for (size_t i = 0;
i < number_of_table_entries;
++i)
{
size_t j = i * TransitionIndex::SIZE;
SymbolNumber * input = (SymbolNumber*)(TableIndices + j);
TransitionTableIndex * index =
(TransitionTableIndex*)(TableIndices + j + sizeof(SymbolNumber));
indices.push_back(new TransitionIndex(*input,*index));
}
}
void TransitionTableReader::Set(TransitionTableIndex pos)
{
if (pos >= TRANSITION_TARGET_TABLE_START)
{
position = pos - TRANSITION_TARGET_TABLE_START;
}
else
{
position = pos;
}
}
void TransitionTableReader::get_transition_vector(void)
{
for (size_t i = 0; i < number_of_table_entries; ++i)
{
size_t j = i * Transition::SIZE;
SymbolNumber * input = (SymbolNumber*)(TableTransitions + j);
SymbolNumber * output =
(SymbolNumber*)(TableTransitions + j + sizeof(SymbolNumber));
TransitionTableIndex * target =
(TransitionTableIndex*)(TableTransitions + j + 2 * sizeof(SymbolNumber));
transitions.push_back(new Transition(*input,
*output,
*target));
}
}
bool TransitionTableReader::Matches(SymbolNumber s)
{
Transition * t = transitions[position];
return t->matches(s);
}
bool TransitionTableReader::get_finality(TransitionTableIndex i)
{
if (i >= TRANSITION_TARGET_TABLE_START)
{
return transitions[i - TRANSITION_TARGET_TABLE_START]->final();
}
else
{
return transitions[i]->final();
}
}
void Transducer::set_symbol_table(void)
{
for(KeyTable::iterator it = keys->begin();
it != keys->end();
++it)
{
const char * key_name =
it->second;
symbol_table.push_back(key_name);
}
}
void Transducer::try_epsilon_transitions(SymbolNumber * input_symbol,
SymbolNumber * output_symbol,
SymbolNumber * original_output_string,
TransitionTableIndex i)
{
#if OL_FULL_DEBUG
std::cout << "try_epsilon_transitions " << i << std::endl;
#endif
while (transitions[i]->get_input() == 0)
{
*output_symbol = transitions[i]->get_output();
get_analyses(input_symbol,
output_symbol+1,
original_output_string,
transitions[i]->target());
++i;
}
}
void TransducerFd::try_epsilon_transitions(SymbolNumber * input_symbol,
SymbolNumber * output_symbol,
SymbolNumber * original_output_string,
TransitionTableIndex i)
{
#if OL_FULL_DEBUG
std::cout << "try_epsilon_transitions " << i << std::endl;
#endif
while (true)
{
if (transitions[i]->get_input() == 0) // epsilon
{
*output_symbol = transitions[i]->get_output();
get_analyses(input_symbol,
output_symbol+1,
original_output_string,
transitions[i]->target());
++i;
} else if (transitions[i]->get_input() != NO_SYMBOL_NUMBER &&
operations[transitions[i]->get_input()].isFlag())
{
if (PushState(operations[transitions[i]->get_input()]))
{
#if OL_FULL_DEBUG
std::cout << "flag diacritic " <<
symbol_table[transitions[i]->get_input()] << " allowed\n";
#endif
// flag diacritic allowed
*output_symbol = transitions[i]->get_output();
get_analyses(input_symbol,
output_symbol+1,
original_output_string,
transitions[i]->target());
statestack.pop_back();
}
else
{
#if OL_FULL_DEBUG
std::cout << "flag diacritic " <<
symbol_table[transitions[i]->get_input()] << " disallowed\n";
#endif
}
++i;
} else
{
return;
}
}
}
void Transducer::try_epsilon_indices(SymbolNumber * input_symbol,
SymbolNumber * output_symbol,
SymbolNumber * original_output_string,
TransitionTableIndex i)
{
#if OL_FULL_DEBUG
std::cout << "try_epsilon_indices " << i << std::endl;
#endif
if (indices[i]->get_input() == 0)
{
try_epsilon_transitions(input_symbol,
output_symbol,
original_output_string,
indices[i]->target() -
TRANSITION_TARGET_TABLE_START);
}
}
void Transducer::find_transitions(SymbolNumber input,
SymbolNumber * input_symbol,
SymbolNumber * output_symbol,
SymbolNumber * original_output_string,
TransitionTableIndex i)
{
#if OL_FULL_DEBUG
std::cout << "find_transitions " << i << "\t" << transitions[i]->get_input() << std::endl;
#endif
while (transitions[i]->get_input() != NO_SYMBOL_NUMBER)
{
if (transitions[i]->get_input() == input)
{
*output_symbol = transitions[i]->get_output();
get_analyses(input_symbol,
output_symbol+1,
original_output_string,
transitions[i]->target());
}
else
{
return;
}
++i;
}
}
void Transducer::find_index(SymbolNumber input,