algo_test.h
40.4 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
// See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Regression test for various FST algorithms.
#ifndef FST_TEST_ALGO_TEST_H_
#define FST_TEST_ALGO_TEST_H_
#include <fst/log.h>
#include <fst/fstlib.h>
#include "./rand-fst.h"
DECLARE_int32(repeat); // defined in ./algo_test.cc
namespace fst {
// Mapper to change input and output label of every transition into
// epsilons.
template <class A>
class EpsMapper {
public:
EpsMapper() {}
A operator()(const A &arc) const {
return A(0, 0, arc.weight, arc.nextstate);
}
uint64 Properties(uint64 props) const {
props &= ~kNotAcceptor;
props |= kAcceptor;
props &= ~kNoIEpsilons & ~kNoOEpsilons & ~kNoEpsilons;
props |= kIEpsilons | kOEpsilons | kEpsilons;
props &= ~kNotILabelSorted & ~kNotOLabelSorted;
props |= kILabelSorted | kOLabelSorted;
return props;
}
MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
MapSymbolsAction InputSymbolsAction() const { return MAP_COPY_SYMBOLS; }
MapSymbolsAction OutputSymbolsAction() const { return MAP_COPY_SYMBOLS; }
};
// Generic - no lookahead.
template <class Arc>
void LookAheadCompose(const Fst<Arc> &ifst1, const Fst<Arc> &ifst2,
MutableFst<Arc> *ofst) {
Compose(ifst1, ifst2, ofst);
}
// Specialized and epsilon olabel acyclic - lookahead.
void LookAheadCompose(const Fst<StdArc> &ifst1, const Fst<StdArc> &ifst2,
MutableFst<StdArc> *ofst) {
std::vector<StdArc::StateId> order;
bool acyclic;
TopOrderVisitor<StdArc> visitor(&order, &acyclic);
DfsVisit(ifst1, &visitor, OutputEpsilonArcFilter<StdArc>());
if (acyclic) { // no ifst1 output epsilon cycles?
StdOLabelLookAheadFst lfst1(ifst1);
StdVectorFst lfst2(ifst2);
LabelLookAheadRelabeler<StdArc>::Relabel(&lfst2, lfst1, true);
Compose(lfst1, lfst2, ofst);
} else {
Compose(ifst1, ifst2, ofst);
}
}
// This class tests a variety of identities and properties that must
// hold for various algorithms on weighted FSTs.
template <class Arc, class WeightGenerator>
class WeightedTester {
public:
typedef typename Arc::Label Label;
typedef typename Arc::StateId StateId;
typedef typename Arc::Weight Weight;
WeightedTester(time_t seed, const Fst<Arc> &zero_fst, const Fst<Arc> &one_fst,
const Fst<Arc> &univ_fst, WeightGenerator *weight_generator)
: seed_(seed),
zero_fst_(zero_fst),
one_fst_(one_fst),
univ_fst_(univ_fst),
weight_generator_(weight_generator) {}
void Test(const Fst<Arc> &T1, const Fst<Arc> &T2, const Fst<Arc> &T3) {
TestRational(T1, T2, T3);
TestMap(T1);
TestCompose(T1, T2, T3);
TestSort(T1);
TestOptimize(T1);
TestSearch(T1);
}
private:
// Tests rational operations with identities
void TestRational(const Fst<Arc> &T1, const Fst<Arc> &T2,
const Fst<Arc> &T3) {
{
VLOG(1) << "Check destructive and delayed union are equivalent.";
VectorFst<Arc> U1(T1);
Union(&U1, T2);
UnionFst<Arc> U2(T1, T2);
CHECK(Equiv(U1, U2));
}
{
VLOG(1) << "Check destructive and delayed concatenation are equivalent.";
VectorFst<Arc> C1(T1);
Concat(&C1, T2);
ConcatFst<Arc> C2(T1, T2);
CHECK(Equiv(C1, C2));
VectorFst<Arc> C3(T2);
Concat(T1, &C3);
CHECK(Equiv(C3, C2));
}
{
VLOG(1) << "Check destructive and delayed closure* are equivalent.";
VectorFst<Arc> C1(T1);
Closure(&C1, CLOSURE_STAR);
ClosureFst<Arc> C2(T1, CLOSURE_STAR);
CHECK(Equiv(C1, C2));
}
{
VLOG(1) << "Check destructive and delayed closure+ are equivalent.";
VectorFst<Arc> C1(T1);
Closure(&C1, CLOSURE_PLUS);
ClosureFst<Arc> C2(T1, CLOSURE_PLUS);
CHECK(Equiv(C1, C2));
}
{
VLOG(1) << "Check union is associative (destructive).";
VectorFst<Arc> U1(T1);
Union(&U1, T2);
Union(&U1, T3);
VectorFst<Arc> U3(T2);
Union(&U3, T3);
VectorFst<Arc> U4(T1);
Union(&U4, U3);
CHECK(Equiv(U1, U4));
}
{
VLOG(1) << "Check union is associative (delayed).";
UnionFst<Arc> U1(T1, T2);
UnionFst<Arc> U2(U1, T3);
UnionFst<Arc> U3(T2, T3);
UnionFst<Arc> U4(T1, U3);
CHECK(Equiv(U2, U4));
}
{
VLOG(1) << "Check union is associative (destructive delayed).";
UnionFst<Arc> U1(T1, T2);
Union(&U1, T3);
UnionFst<Arc> U3(T2, T3);
UnionFst<Arc> U4(T1, U3);
CHECK(Equiv(U1, U4));
}
{
VLOG(1) << "Check concatenation is associative (destructive).";
VectorFst<Arc> C1(T1);
Concat(&C1, T2);
Concat(&C1, T3);
VectorFst<Arc> C3(T2);
Concat(&C3, T3);
VectorFst<Arc> C4(T1);
Concat(&C4, C3);
CHECK(Equiv(C1, C4));
}
{
VLOG(1) << "Check concatenation is associative (delayed).";
ConcatFst<Arc> C1(T1, T2);
ConcatFst<Arc> C2(C1, T3);
ConcatFst<Arc> C3(T2, T3);
ConcatFst<Arc> C4(T1, C3);
CHECK(Equiv(C2, C4));
}
{
VLOG(1) << "Check concatenation is associative (destructive delayed).";
ConcatFst<Arc> C1(T1, T2);
Concat(&C1, T3);
ConcatFst<Arc> C3(T2, T3);
ConcatFst<Arc> C4(T1, C3);
CHECK(Equiv(C1, C4));
}
if (Weight::Properties() & kLeftSemiring) {
VLOG(1) << "Check concatenation left distributes"
<< " over union (destructive).";
VectorFst<Arc> U1(T1);
Union(&U1, T2);
VectorFst<Arc> C1(T3);
Concat(&C1, U1);
VectorFst<Arc> C2(T3);
Concat(&C2, T1);
VectorFst<Arc> C3(T3);
Concat(&C3, T2);
VectorFst<Arc> U2(C2);
Union(&U2, C3);
CHECK(Equiv(C1, U2));
}
if (Weight::Properties() & kRightSemiring) {
VLOG(1) << "Check concatenation right distributes"
<< " over union (destructive).";
VectorFst<Arc> U1(T1);
Union(&U1, T2);
VectorFst<Arc> C1(U1);
Concat(&C1, T3);
VectorFst<Arc> C2(T1);
Concat(&C2, T3);
VectorFst<Arc> C3(T2);
Concat(&C3, T3);
VectorFst<Arc> U2(C2);
Union(&U2, C3);
CHECK(Equiv(C1, U2));
}
if (Weight::Properties() & kLeftSemiring) {
VLOG(1) << "Check concatenation left distributes over union (delayed).";
UnionFst<Arc> U1(T1, T2);
ConcatFst<Arc> C1(T3, U1);
ConcatFst<Arc> C2(T3, T1);
ConcatFst<Arc> C3(T3, T2);
UnionFst<Arc> U2(C2, C3);
CHECK(Equiv(C1, U2));
}
if (Weight::Properties() & kRightSemiring) {
VLOG(1) << "Check concatenation right distributes over union (delayed).";
UnionFst<Arc> U1(T1, T2);
ConcatFst<Arc> C1(U1, T3);
ConcatFst<Arc> C2(T1, T3);
ConcatFst<Arc> C3(T2, T3);
UnionFst<Arc> U2(C2, C3);
CHECK(Equiv(C1, U2));
}
if (Weight::Properties() & kLeftSemiring) {
VLOG(1) << "Check T T* == T+ (destructive).";
VectorFst<Arc> S(T1);
Closure(&S, CLOSURE_STAR);
VectorFst<Arc> C(T1);
Concat(&C, S);
VectorFst<Arc> P(T1);
Closure(&P, CLOSURE_PLUS);
CHECK(Equiv(C, P));
}
if (Weight::Properties() & kRightSemiring) {
VLOG(1) << "Check T* T == T+ (destructive).";
VectorFst<Arc> S(T1);
Closure(&S, CLOSURE_STAR);
VectorFst<Arc> C(S);
Concat(&C, T1);
VectorFst<Arc> P(T1);
Closure(&P, CLOSURE_PLUS);
CHECK(Equiv(C, P));
}
if (Weight::Properties() & kLeftSemiring) {
VLOG(1) << "Check T T* == T+ (delayed).";
ClosureFst<Arc> S(T1, CLOSURE_STAR);
ConcatFst<Arc> C(T1, S);
ClosureFst<Arc> P(T1, CLOSURE_PLUS);
CHECK(Equiv(C, P));
}
if (Weight::Properties() & kRightSemiring) {
VLOG(1) << "Check T* T == T+ (delayed).";
ClosureFst<Arc> S(T1, CLOSURE_STAR);
ConcatFst<Arc> C(S, T1);
ClosureFst<Arc> P(T1, CLOSURE_PLUS);
CHECK(Equiv(C, P));
}
}
// Tests map-based operations.
void TestMap(const Fst<Arc> &T) {
{
VLOG(1) << "Check destructive and delayed projection are equivalent.";
VectorFst<Arc> P1(T);
Project(&P1, PROJECT_INPUT);
ProjectFst<Arc> P2(T, PROJECT_INPUT);
CHECK(Equiv(P1, P2));
}
{
VLOG(1) << "Check destructive and delayed inversion are equivalent.";
VectorFst<Arc> I1(T);
Invert(&I1);
InvertFst<Arc> I2(T);
CHECK(Equiv(I1, I2));
}
{
VLOG(1) << "Check Pi_1(T) = Pi_2(T^-1) (destructive).";
VectorFst<Arc> P1(T);
VectorFst<Arc> I1(T);
Project(&P1, PROJECT_INPUT);
Invert(&I1);
Project(&I1, PROJECT_OUTPUT);
CHECK(Equiv(P1, I1));
}
{
VLOG(1) << "Check Pi_2(T) = Pi_1(T^-1) (destructive).";
VectorFst<Arc> P1(T);
VectorFst<Arc> I1(T);
Project(&P1, PROJECT_OUTPUT);
Invert(&I1);
Project(&I1, PROJECT_INPUT);
CHECK(Equiv(P1, I1));
}
{
VLOG(1) << "Check Pi_1(T) = Pi_2(T^-1) (delayed).";
ProjectFst<Arc> P1(T, PROJECT_INPUT);
InvertFst<Arc> I1(T);
ProjectFst<Arc> P2(I1, PROJECT_OUTPUT);
CHECK(Equiv(P1, P2));
}
{
VLOG(1) << "Check Pi_2(T) = Pi_1(T^-1) (delayed).";
ProjectFst<Arc> P1(T, PROJECT_OUTPUT);
InvertFst<Arc> I1(T);
ProjectFst<Arc> P2(I1, PROJECT_INPUT);
CHECK(Equiv(P1, P2));
}
{
VLOG(1) << "Check destructive relabeling";
static const int kNumLabels = 10;
// set up relabeling pairs
std::vector<Label> labelset(kNumLabels);
for (size_t i = 0; i < kNumLabels; ++i) labelset[i] = i;
for (size_t i = 0; i < kNumLabels; ++i) {
using std::swap;
swap(labelset[i], labelset[rand() % kNumLabels]);
}
std::vector<std::pair<Label, Label>> ipairs1(kNumLabels);
std::vector<std::pair<Label, Label>> opairs1(kNumLabels);
for (size_t i = 0; i < kNumLabels; ++i) {
ipairs1[i] = std::make_pair(i, labelset[i]);
opairs1[i] = std::make_pair(labelset[i], i);
}
VectorFst<Arc> R(T);
Relabel(&R, ipairs1, opairs1);
std::vector<std::pair<Label, Label>> ipairs2(kNumLabels);
std::vector<std::pair<Label, Label>> opairs2(kNumLabels);
for (size_t i = 0; i < kNumLabels; ++i) {
ipairs2[i] = std::make_pair(labelset[i], i);
opairs2[i] = std::make_pair(i, labelset[i]);
}
Relabel(&R, ipairs2, opairs2);
CHECK(Equiv(R, T));
VLOG(1) << "Check on-the-fly relabeling";
RelabelFst<Arc> Rdelay(T, ipairs1, opairs1);
RelabelFst<Arc> RRdelay(Rdelay, ipairs2, opairs2);
CHECK(Equiv(RRdelay, T));
}
{
VLOG(1) << "Check encoding/decoding (destructive).";
VectorFst<Arc> D(T);
uint32 encode_props = 0;
if (rand() % 2) encode_props |= kEncodeLabels;
if (rand() % 2) encode_props |= kEncodeWeights;
EncodeMapper<Arc> encoder(encode_props, ENCODE);
Encode(&D, &encoder);
Decode(&D, encoder);
CHECK(Equiv(D, T));
}
{
VLOG(1) << "Check encoding/decoding (delayed).";
uint32 encode_props = 0;
if (rand() % 2) encode_props |= kEncodeLabels;
if (rand() % 2) encode_props |= kEncodeWeights;
EncodeMapper<Arc> encoder(encode_props, ENCODE);
EncodeFst<Arc> E(T, &encoder);
VectorFst<Arc> Encoded(E);
DecodeFst<Arc> D(Encoded, encoder);
CHECK(Equiv(D, T));
}
{
VLOG(1) << "Check gallic mappers (constructive).";
ToGallicMapper<Arc> to_mapper;
FromGallicMapper<Arc> from_mapper;
VectorFst<GallicArc<Arc>> G;
VectorFst<Arc> F;
ArcMap(T, &G, to_mapper);
ArcMap(G, &F, from_mapper);
CHECK(Equiv(T, F));
}
{
VLOG(1) << "Check gallic mappers (delayed).";
ToGallicMapper<Arc> to_mapper;
FromGallicMapper<Arc> from_mapper;
ArcMapFst<Arc, GallicArc<Arc>, ToGallicMapper<Arc>> G(T, to_mapper);
ArcMapFst<GallicArc<Arc>, Arc, FromGallicMapper<Arc>> F(G, from_mapper);
CHECK(Equiv(T, F));
}
}
// Tests compose-based operations.
void TestCompose(const Fst<Arc> &T1, const Fst<Arc> &T2, const Fst<Arc> &T3) {
if (!(Weight::Properties() & kCommutative)) return;
VectorFst<Arc> S1(T1);
VectorFst<Arc> S2(T2);
VectorFst<Arc> S3(T3);
ILabelCompare<Arc> icomp;
OLabelCompare<Arc> ocomp;
ArcSort(&S1, ocomp);
ArcSort(&S2, ocomp);
ArcSort(&S3, icomp);
{
VLOG(1) << "Check composition is associative.";
ComposeFst<Arc> C1(S1, S2);
ComposeFst<Arc> C2(C1, S3);
ComposeFst<Arc> C3(S2, S3);
ComposeFst<Arc> C4(S1, C3);
CHECK(Equiv(C2, C4));
}
{
VLOG(1) << "Check composition left distributes over union.";
UnionFst<Arc> U1(S2, S3);
ComposeFst<Arc> C1(S1, U1);
ComposeFst<Arc> C2(S1, S2);
ComposeFst<Arc> C3(S1, S3);
UnionFst<Arc> U2(C2, C3);
CHECK(Equiv(C1, U2));
}
{
VLOG(1) << "Check composition right distributes over union.";
UnionFst<Arc> U1(S1, S2);
ComposeFst<Arc> C1(U1, S3);
ComposeFst<Arc> C2(S1, S3);
ComposeFst<Arc> C3(S2, S3);
UnionFst<Arc> U2(C2, C3);
CHECK(Equiv(C1, U2));
}
VectorFst<Arc> A1(S1);
VectorFst<Arc> A2(S2);
VectorFst<Arc> A3(S3);
Project(&A1, PROJECT_OUTPUT);
Project(&A2, PROJECT_INPUT);
Project(&A3, PROJECT_INPUT);
{
VLOG(1) << "Check intersection is commutative.";
IntersectFst<Arc> I1(A1, A2);
IntersectFst<Arc> I2(A2, A1);
CHECK(Equiv(I1, I2));
}
{
VLOG(1) << "Check all epsilon filters leads to equivalent results.";
typedef Matcher<Fst<Arc>> M;
ComposeFst<Arc> C1(S1, S2);
ComposeFst<Arc> C2(
S1, S2, ComposeFstOptions<Arc, M, AltSequenceComposeFilter<M>>());
ComposeFst<Arc> C3(S1, S2,
ComposeFstOptions<Arc, M, MatchComposeFilter<M>>());
CHECK(Equiv(C1, C2));
CHECK(Equiv(C1, C3));
if ((Weight::Properties() & kIdempotent) ||
S1.Properties(kNoOEpsilons, false) ||
S2.Properties(kNoIEpsilons, false)) {
ComposeFst<Arc> C4(
S1, S2, ComposeFstOptions<Arc, M, TrivialComposeFilter<M>>());
CHECK(Equiv(C1, C4));
}
if (S1.Properties(kNoOEpsilons, false) &&
S2.Properties(kNoIEpsilons, false)) {
ComposeFst<Arc> C5(S1, S2,
ComposeFstOptions<Arc, M, NullComposeFilter<M>>());
CHECK(Equiv(C1, C5));
}
}
{
VLOG(1) << "Check look-ahead filters lead to equivalent results.";
VectorFst<Arc> C1, C2;
Compose(S1, S2, &C1);
LookAheadCompose(S1, S2, &C2);
CHECK(Equiv(C1, C2));
}
}
// Tests sorting operations
void TestSort(const Fst<Arc> &T) {
ILabelCompare<Arc> icomp;
OLabelCompare<Arc> ocomp;
{
VLOG(1) << "Check arc sorted Fst is equivalent to its input.";
VectorFst<Arc> S1(T);
ArcSort(&S1, icomp);
CHECK(Equiv(T, S1));
}
{
VLOG(1) << "Check destructive and delayed arcsort are equivalent.";
VectorFst<Arc> S1(T);
ArcSort(&S1, icomp);
ArcSortFst<Arc, ILabelCompare<Arc>> S2(T, icomp);
CHECK(Equiv(S1, S2));
}
{
VLOG(1) << "Check ilabel sorting vs. olabel sorting with inversions.";
VectorFst<Arc> S1(T);
VectorFst<Arc> S2(T);
ArcSort(&S1, icomp);
Invert(&S2);
ArcSort(&S2, ocomp);
Invert(&S2);
CHECK(Equiv(S1, S2));
}
{
VLOG(1) << "Check topologically sorted Fst is equivalent to its input.";
VectorFst<Arc> S1(T);
TopSort(&S1);
CHECK(Equiv(T, S1));
}
{
VLOG(1) << "Check reverse(reverse(T)) = T";
for (int i = 0; i < 2; ++i) {
VectorFst<ReverseArc<Arc>> R1;
VectorFst<Arc> R2;
bool require_superinitial = i == 1;
Reverse(T, &R1, require_superinitial);
Reverse(R1, &R2, require_superinitial);
CHECK(Equiv(T, R2));
}
}
}
// Tests optimization operations
void TestOptimize(const Fst<Arc> &T) {
uint64 tprops = T.Properties(kFstProperties, true);
uint64 wprops = Weight::Properties();
VectorFst<Arc> A(T);
Project(&A, PROJECT_INPUT);
{
VLOG(1) << "Check connected FST is equivalent to its input.";
VectorFst<Arc> C1(T);
Connect(&C1);
CHECK(Equiv(T, C1));
}
if ((wprops & kSemiring) == kSemiring &&
(tprops & kAcyclic || wprops & kIdempotent)) {
VLOG(1) << "Check epsilon-removed FST is equivalent to its input.";
VectorFst<Arc> R1(T);
RmEpsilon(&R1);
CHECK(Equiv(T, R1));
VLOG(1) << "Check destructive and delayed epsilon removal"
<< "are equivalent.";
RmEpsilonFst<Arc> R2(T);
CHECK(Equiv(R1, R2));
VLOG(1) << "Check an FST with a large proportion"
<< " of epsilon transitions:";
// Maps all transitions of T to epsilon-transitions and append
// a non-epsilon transition.
VectorFst<Arc> U;
ArcMap(T, &U, EpsMapper<Arc>());
VectorFst<Arc> V;
V.SetStart(V.AddState());
Arc arc(1, 1, Weight::One(), V.AddState());
V.AddArc(V.Start(), arc);
V.SetFinal(arc.nextstate, Weight::One());
Concat(&U, V);
// Check that epsilon-removal preserves the shortest-distance
// from the initial state to the final states.
std::vector<Weight> d;
ShortestDistance(U, &d, true);
Weight w = U.Start() < d.size() ? d[U.Start()] : Weight::Zero();
VectorFst<Arc> U1(U);
RmEpsilon(&U1);
ShortestDistance(U1, &d, true);
Weight w1 = U1.Start() < d.size() ? d[U1.Start()] : Weight::Zero();
CHECK(ApproxEqual(w, w1, kTestDelta));
RmEpsilonFst<Arc> U2(U);
ShortestDistance(U2, &d, true);
Weight w2 = U2.Start() < d.size() ? d[U2.Start()] : Weight::Zero();
CHECK(ApproxEqual(w, w2, kTestDelta));
}
if ((wprops & kSemiring) == kSemiring && tprops & kAcyclic) {
VLOG(1) << "Check determinized FSA is equivalent to its input.";
DeterminizeFst<Arc> D(A);
CHECK(Equiv(A, D));
{
VLOG(1) << "Check determinized FST is equivalent to its input.";
DeterminizeFstOptions<Arc> opts;
opts.type = DETERMINIZE_NONFUNCTIONAL;
DeterminizeFst<Arc> DT(T, opts);
CHECK(Equiv(T, DT));
}
if ((wprops & (kPath | kCommutative)) == (kPath | kCommutative)) {
VLOG(1) << "Check pruning in determinization";
VectorFst<Arc> P;
Weight threshold = (*weight_generator_)();
DeterminizeOptions<Arc> opts;
opts.weight_threshold = threshold;
Determinize(A, &P, opts);
CHECK(P.Properties(kIDeterministic, true));
CHECK(PruneEquiv(A, P, threshold));
}
if ((wprops & kPath) == kPath) {
VLOG(1) << "Check min-determinization";
// Ensures no input epsilons
VectorFst<Arc> R(T);
std::vector<std::pair<Label, Label>> ipairs, opairs;
ipairs.push_back(std::pair<Label, Label>(0, 1));
Relabel(&R, ipairs, opairs);
VectorFst<Arc> M;
DeterminizeOptions<Arc> opts;
opts.type = DETERMINIZE_DISAMBIGUATE;
Determinize(R, &M, opts);
CHECK(M.Properties(kIDeterministic, true));
CHECK(MinRelated(M, R));
}
int n;
{
VLOG(1) << "Check size(min(det(A))) <= size(det(A))"
<< " and min(det(A)) equiv det(A)";
VectorFst<Arc> M(D);
n = M.NumStates();
Minimize(&M, static_cast<MutableFst<Arc> *>(nullptr), kDelta);
CHECK(Equiv(D, M));
CHECK(M.NumStates() <= n);
n = M.NumStates();
}
if (n && (wprops & kIdempotent) == kIdempotent &&
A.Properties(kNoEpsilons, true)) {
VLOG(1) << "Check that Revuz's algorithm leads to the"
<< " same number of states as Brozozowski's algorithm";
// Skip test if A is the empty machine or contains epsilons or
// if the semiring is not idempotent (to avoid floating point
// errors)
VectorFst<Arc> R;
Reverse(A, &R);
RmEpsilon(&R);
DeterminizeFst<Arc> DR(R);
VectorFst<Arc> RD;
Reverse(DR, &RD);
DeterminizeFst<Arc> DRD(RD);
VectorFst<Arc> M(DRD);
CHECK_EQ(n + 1, M.NumStates()); // Accounts for the epsilon transition
// to the initial state
}
}
if ((wprops & kSemiring) == kSemiring && tprops & kAcyclic) {
VLOG(1) << "Check disambiguated FSA is equivalent to its input.";
VectorFst<Arc> R(A), D;
RmEpsilon(&R);
Disambiguate(R, &D);
CHECK(Equiv(R, D));
VLOG(1) << "Check disambiguated FSA is unambiguous";
CHECK(Unambiguous(D));
/* TODO(riley): find out why this fails
if ((wprops & (kPath | kCommutative)) == (kPath | kCommutative)) {
VLOG(1) << "Check pruning in disambiguation";
VectorFst<Arc> P;
Weight threshold = (*weight_generator_)();
DisambiguateOptions<Arc> opts;
opts.weight_threshold = threshold;
Disambiguate(R, &P, opts);
CHECK(Unambiguous(P));
CHECK(PruneEquiv(A, P, threshold));
}
*/
}
if (Arc::Type() == LogArc::Type() || Arc::Type() == StdArc::Type()) {
VLOG(1) << "Check reweight(T) equiv T";
std::vector<Weight> potential;
VectorFst<Arc> RI(T);
VectorFst<Arc> RF(T);
while (potential.size() < RI.NumStates())
potential.push_back((*weight_generator_)());
Reweight(&RI, potential, REWEIGHT_TO_INITIAL);
CHECK(Equiv(T, RI));
Reweight(&RF, potential, REWEIGHT_TO_FINAL);
CHECK(Equiv(T, RF));
}
if ((wprops & kIdempotent) || (tprops & kAcyclic)) {
VLOG(1) << "Check pushed FST is equivalent to input FST.";
// Pushing towards the final state.
if (wprops & kRightSemiring) {
VectorFst<Arc> P1;
Push<Arc, REWEIGHT_TO_FINAL>(T, &P1, kPushLabels);
CHECK(Equiv(T, P1));
VectorFst<Arc> P2;
Push<Arc, REWEIGHT_TO_FINAL>(T, &P2, kPushWeights);
CHECK(Equiv(T, P2));
VectorFst<Arc> P3;
Push<Arc, REWEIGHT_TO_FINAL>(T, &P3, kPushLabels | kPushWeights);
CHECK(Equiv(T, P3));
}
// Pushing towards the initial state.
if (wprops & kLeftSemiring) {
VectorFst<Arc> P1;
Push<Arc, REWEIGHT_TO_INITIAL>(T, &P1, kPushLabels);
CHECK(Equiv(T, P1));
VectorFst<Arc> P2;
Push<Arc, REWEIGHT_TO_INITIAL>(T, &P2, kPushWeights);
CHECK(Equiv(T, P2));
VectorFst<Arc> P3;
Push<Arc, REWEIGHT_TO_INITIAL>(T, &P3, kPushLabels | kPushWeights);
CHECK(Equiv(T, P3));
}
}
if ((wprops & (kPath | kCommutative)) == (kPath | kCommutative)) {
VLOG(1) << "Check pruning algorithm";
{
VLOG(1) << "Check equiv. of constructive and destructive algorithms";
Weight thresold = (*weight_generator_)();
VectorFst<Arc> P1(T);
Prune(&P1, thresold);
VectorFst<Arc> P2;
Prune(T, &P2, thresold);
CHECK(Equiv(P1, P2));
}
{
VLOG(1) << "Check prune(reverse) equiv reverse(prune)";
Weight thresold = (*weight_generator_)();
VectorFst<ReverseArc<Arc>> R;
VectorFst<Arc> P1(T);
VectorFst<Arc> P2;
Prune(&P1, thresold);
Reverse(T, &R);
Prune(&R, thresold.Reverse());
Reverse(R, &P2);
CHECK(Equiv(P1, P2));
}
{
VLOG(1) << "Check: ShortestDistance(A - prune(A))"
<< " > ShortestDistance(A) times Threshold";
Weight threshold = (*weight_generator_)();
VectorFst<Arc> P;
Prune(A, &P, threshold);
CHECK(PruneEquiv(A, P, threshold));
}
}
if (tprops & kAcyclic) {
VLOG(1) << "Check synchronize(T) equiv T";
SynchronizeFst<Arc> S(T);
CHECK(Equiv(T, S));
}
}
// Tests search operations
void TestSearch(const Fst<Arc> &T) {
uint64 wprops = Weight::Properties();
VectorFst<Arc> A(T);
Project(&A, PROJECT_INPUT);
if ((wprops & (kPath | kRightSemiring)) == (kPath | kRightSemiring)) {
VLOG(1) << "Check 1-best weight.";
VectorFst<Arc> path;
ShortestPath(T, &path);
Weight tsum = ShortestDistance(T);
Weight psum = ShortestDistance(path);
CHECK(ApproxEqual(tsum, psum, kTestDelta));
}
if ((wprops & (kPath | kSemiring)) == (kPath | kSemiring)) {
VLOG(1) << "Check n-best weights";
VectorFst<Arc> R(A);
RmEpsilon(&R, /*connect=*/ true, Arc::Weight::Zero(), kNoStateId,
kDelta);
int nshortest = rand() % kNumRandomShortestPaths + 2;
VectorFst<Arc> paths;
ShortestPath(R, &paths, nshortest, /*unique=*/ true,
/*first_path=*/ false, Weight::Zero(), kNumShortestStates,
kDelta);
std::vector<Weight> distance;
ShortestDistance(paths, &distance, true, kDelta);
StateId pstart = paths.Start();
if (pstart != kNoStateId) {
ArcIterator<Fst<Arc>> piter(paths, pstart);
for (; !piter.Done(); piter.Next()) {
StateId s = piter.Value().nextstate;
Weight nsum = s < distance.size()
? Times(piter.Value().weight, distance[s])
: Weight::Zero();
VectorFst<Arc> path;
ShortestPath(R, &path, 1, false, false, Weight::Zero(), kNoStateId,
kDelta);
Weight dsum = ShortestDistance(path, kDelta);
CHECK(ApproxEqual(nsum, dsum, kTestDelta));
ArcMap(&path, RmWeightMapper<Arc>());
VectorFst<Arc> S;
Difference(R, path, &S);
R = S;
}
}
}
}
// Tests if two FSTS are equivalent by checking if random
// strings from one FST are transduced the same by both FSTs.
template <class A>
bool Equiv(const Fst<A> &fst1, const Fst<A> &fst2) {
VLOG(1) << "Check FSTs for sanity (including property bits).";
CHECK(Verify(fst1));
CHECK(Verify(fst2));
// Ensures seed used once per instantiation.
static UniformArcSelector<A> uniform_selector(seed_);
RandGenOptions<UniformArcSelector<A>> opts(uniform_selector,
kRandomPathLength);
return RandEquivalent(fst1, fst2, kNumRandomPaths, kTestDelta, opts);
}
// Tests FSA is unambiguous
bool Unambiguous(const Fst<Arc> &fst) {
VectorFst<StdArc> sfst, dfst;
VectorFst<LogArc> lfst1, lfst2;
Map(fst, &sfst, RmWeightMapper<Arc, StdArc>());
Determinize(sfst, &dfst);
Map(fst, &lfst1, RmWeightMapper<Arc, LogArc>());
Map(dfst, &lfst2, RmWeightMapper<StdArc, LogArc>());
return Equiv(lfst1, lfst2);
}
// Ensures input-epsilon free transducers fst1 and fst2 have the
// same domain and that for each string pair '(is, os)' in fst1,
// '(is, os)' is the minimum weight match to 'is' in fst2.
template <class A>
bool MinRelated(const Fst<A> &fst1, const Fst<A> &fst2) {
// Same domain
VectorFst<Arc> P1(fst1), P2(fst2);
Project(&P1, PROJECT_INPUT);
Project(&P2, PROJECT_INPUT);
if (!Equiv(P1, P2)) {
LOG(ERROR) << "Inputs not equivalent";
return false;
}
// Ensures seed used once per instantiation.
static UniformArcSelector<A> uniform_selector(seed_);
RandGenOptions<UniformArcSelector<A>> opts(uniform_selector,
kRandomPathLength);
VectorFst<Arc> path, paths1, paths2;
for (ssize_t n = 0; n < kNumRandomPaths; ++n) {
RandGen(fst1, &path, opts);
Invert(&path);
Map(&path, RmWeightMapper<Arc>());
Compose(path, fst2, &paths1);
Weight sum1 = ShortestDistance(paths1);
Compose(paths1, path, &paths2);
Weight sum2 = ShortestDistance(paths2);
if (!ApproxEqual(Plus(sum1, sum2), sum2, kTestDelta)) {
LOG(ERROR) << "Sums not equivalent: " << sum1 << " " << sum2;
return false;
}
}
return true;
}
// Tests ShortestDistance(A - P) >=
// ShortestDistance(A) times Threshold.
template <class A>
bool PruneEquiv(const Fst<A> &fst, const Fst<A> &pfst, Weight threshold) {
VLOG(1) << "Check FSTs for sanity (including property bits).";
CHECK(Verify(fst));
CHECK(Verify(pfst));
DifferenceFst<Arc> D(fst, DeterminizeFst<Arc>(RmEpsilonFst<Arc>(
ArcMapFst<Arc, Arc, RmWeightMapper<Arc>>(
pfst, RmWeightMapper<Arc>()))));
Weight sum1 = Times(ShortestDistance(fst), threshold);
Weight sum2 = ShortestDistance(D);
return ApproxEqual(Plus(sum1, sum2), sum1, kTestDelta);
}
// Random seed.
int seed_;
// FST with no states
VectorFst<Arc> zero_fst_;
// FST with one state that accepts epsilon.
VectorFst<Arc> one_fst_;
// FST with one state that accepts all strings.
VectorFst<Arc> univ_fst_;
// Generates weights used in testing.
WeightGenerator *weight_generator_;
// Maximum random path length.
static const int kRandomPathLength;
// Number of random paths to explore.
static const int kNumRandomPaths;
// Maximum number of nshortest paths.
static const int kNumRandomShortestPaths;
// Maximum number of nshortest states.
static const int kNumShortestStates;
// Delta for equivalence tests.
static const float kTestDelta;
WeightedTester(const WeightedTester &) = delete;
WeightedTester &operator=(const WeightedTester &) = delete;
};
template <class A, class WG>
const int WeightedTester<A, WG>::kRandomPathLength = 25;
template <class A, class WG>
const int WeightedTester<A, WG>::kNumRandomPaths = 100;
template <class A, class WG>
const int WeightedTester<A, WG>::kNumRandomShortestPaths = 100;
template <class A, class WG>
const int WeightedTester<A, WG>::kNumShortestStates = 10000;
template <class A, class WG>
const float WeightedTester<A, WG>::kTestDelta = .05;
// This class tests a variety of identities and properties that must
// hold for various algorithms on unweighted FSAs and that are not tested
// by WeightedTester. Only the specialization does anything interesting.
template <class Arc>
class UnweightedTester {
public:
UnweightedTester(const Fst<Arc> &zero_fsa, const Fst<Arc> &one_fsa,
const Fst<Arc> &univ_fsa) {}
void Test(const Fst<Arc> &A1, const Fst<Arc> &A2, const Fst<Arc> &A3) {}
};
// Specialization for StdArc. This should work for any commutative,
// idempotent semiring when restricted to the unweighted case
// (being isomorphic to the boolean semiring).
template <>
class UnweightedTester<StdArc> {
public:
typedef StdArc Arc;
typedef Arc::Label Label;
typedef Arc::StateId StateId;
typedef Arc::Weight Weight;
UnweightedTester(const Fst<Arc> &zero_fsa, const Fst<Arc> &one_fsa,
const Fst<Arc> &univ_fsa)
: zero_fsa_(zero_fsa), one_fsa_(one_fsa), univ_fsa_(univ_fsa) {}
void Test(const Fst<Arc> &A1, const Fst<Arc> &A2, const Fst<Arc> &A3) {
TestRational(A1, A2, A3);
TestIntersect(A1, A2, A3);
TestOptimize(A1);
}
private:
// Tests rational operations with identities
void TestRational(const Fst<Arc> &A1, const Fst<Arc> &A2,
const Fst<Arc> &A3) {
{
VLOG(1) << "Check the union contains its arguments (destructive).";
VectorFst<Arc> U(A1);
Union(&U, A2);
CHECK(Subset(A1, U));
CHECK(Subset(A2, U));
}
{
VLOG(1) << "Check the union contains its arguments (delayed).";
UnionFst<Arc> U(A1, A2);
CHECK(Subset(A1, U));
CHECK(Subset(A2, U));
}
{
VLOG(1) << "Check if A^n c A* (destructive).";
VectorFst<Arc> C(one_fsa_);
int n = rand() % 5;
for (int i = 0; i < n; ++i) Concat(&C, A1);
VectorFst<Arc> S(A1);
Closure(&S, CLOSURE_STAR);
CHECK(Subset(C, S));
}
{
VLOG(1) << "Check if A^n c A* (delayed).";
int n = rand() % 5;
Fst<Arc> *C = new VectorFst<Arc>(one_fsa_);
for (int i = 0; i < n; ++i) {
ConcatFst<Arc> *F = new ConcatFst<Arc>(*C, A1);
delete C;
C = F;
}
ClosureFst<Arc> S(A1, CLOSURE_STAR);
CHECK(Subset(*C, S));
delete C;
}
}
// Tests intersect-based operations.
void TestIntersect(const Fst<Arc> &A1, const Fst<Arc> &A2,
const Fst<Arc> &A3) {
VectorFst<Arc> S1(A1);
VectorFst<Arc> S2(A2);
VectorFst<Arc> S3(A3);
ILabelCompare<Arc> comp;
ArcSort(&S1, comp);
ArcSort(&S2, comp);
ArcSort(&S3, comp);
{
VLOG(1) << "Check the intersection is contained in its arguments.";
IntersectFst<Arc> I1(S1, S2);
CHECK(Subset(I1, S1));
CHECK(Subset(I1, S2));
}
{
VLOG(1) << "Check union distributes over intersection.";
IntersectFst<Arc> I1(S1, S2);
UnionFst<Arc> U1(I1, S3);
UnionFst<Arc> U2(S1, S3);
UnionFst<Arc> U3(S2, S3);
ArcSortFst<Arc, ILabelCompare<Arc>> S4(U3, comp);
IntersectFst<Arc> I2(U2, S4);
CHECK(Equiv(U1, I2));
}
VectorFst<Arc> C1;
VectorFst<Arc> C2;
Complement(S1, &C1);
Complement(S2, &C2);
ArcSort(&C1, comp);
ArcSort(&C2, comp);
{
VLOG(1) << "Check S U S' = Sigma*";
UnionFst<Arc> U(S1, C1);
CHECK(Equiv(U, univ_fsa_));
}
{
VLOG(1) << "Check S n S' = {}";
IntersectFst<Arc> I(S1, C1);
CHECK(Equiv(I, zero_fsa_));
}
{
VLOG(1) << "Check (S1' U S2') == (S1 n S2)'";
UnionFst<Arc> U(C1, C2);
IntersectFst<Arc> I(S1, S2);
VectorFst<Arc> C3;
Complement(I, &C3);
CHECK(Equiv(U, C3));
}
{
VLOG(1) << "Check (S1' n S2') == (S1 U S2)'";
IntersectFst<Arc> I(C1, C2);
UnionFst<Arc> U(S1, S2);
VectorFst<Arc> C3;
Complement(U, &C3);
CHECK(Equiv(I, C3));
}
}
// Tests optimization operations
void TestOptimize(const Fst<Arc> &A) {
{
VLOG(1) << "Check determinized FSA is equivalent to its input.";
DeterminizeFst<Arc> D(A);
CHECK(Equiv(A, D));
}
{
VLOG(1) << "Check disambiguated FSA is equivalent to its input.";
VectorFst<Arc> R(A), D;
RmEpsilon(&R);
Disambiguate(R, &D);
CHECK(Equiv(R, D));
}
{
VLOG(1) << "Check minimized FSA is equivalent to its input.";
int n;
{
RmEpsilonFst<Arc> R(A);
DeterminizeFst<Arc> D(R);
VectorFst<Arc> M(D);
Minimize(&M, static_cast<MutableFst<Arc> *>(nullptr), kDelta);
CHECK(Equiv(A, M));
n = M.NumStates();
}
if (n) { // Skip test if A is the empty machine
VLOG(1) << "Check that Hopcroft's and Revuz's algorithms lead to the"
<< " same number of states as Brozozowski's algorithm";
VectorFst<Arc> R;
Reverse(A, &R);
RmEpsilon(&R);
DeterminizeFst<Arc> DR(R);
VectorFst<Arc> RD;
Reverse(DR, &RD);
DeterminizeFst<Arc> DRD(RD);
VectorFst<Arc> M(DRD);
CHECK_EQ(n + 1, M.NumStates()); // Accounts for the epsilon transition
// to the initial state
}
}
}
// Tests if two FSAS are equivalent.
bool Equiv(const Fst<Arc> &fsa1, const Fst<Arc> &fsa2) {
VLOG(1) << "Check FSAs for sanity (including property bits).";
CHECK(Verify(fsa1));
CHECK(Verify(fsa2));
VectorFst<Arc> vfsa1(fsa1);
VectorFst<Arc> vfsa2(fsa2);
RmEpsilon(&vfsa1);
RmEpsilon(&vfsa2);
DeterminizeFst<Arc> dfa1(vfsa1);
DeterminizeFst<Arc> dfa2(vfsa2);
// Test equivalence using union-find algorithm
bool equiv1 = Equivalent(dfa1, dfa2);
// Test equivalence by checking if (S1 - S2) U (S2 - S1) is empty
ILabelCompare<Arc> comp;
VectorFst<Arc> sdfa1(dfa1);
ArcSort(&sdfa1, comp);
VectorFst<Arc> sdfa2(dfa2);
ArcSort(&sdfa2, comp);
DifferenceFst<Arc> dfsa1(sdfa1, sdfa2);
DifferenceFst<Arc> dfsa2(sdfa2, sdfa1);
VectorFst<Arc> ufsa(dfsa1);
Union(&ufsa, dfsa2);
Connect(&ufsa);
bool equiv2 = ufsa.NumStates() == 0;
// Check two equivalence tests match
CHECK((equiv1 && equiv2) || (!equiv1 && !equiv2));
return equiv1;
}
// Tests if FSA1 is a subset of FSA2 (disregarding weights).
bool Subset(const Fst<Arc> &fsa1, const Fst<Arc> &fsa2) {
VLOG(1) << "Check FSAs (incl. property bits) for sanity";
CHECK(Verify(fsa1));
CHECK(Verify(fsa2));
VectorFst<StdArc> vfsa1;
VectorFst<StdArc> vfsa2;
RmEpsilon(&vfsa1);
RmEpsilon(&vfsa2);
ILabelCompare<StdArc> comp;
ArcSort(&vfsa1, comp);
ArcSort(&vfsa2, comp);
IntersectFst<StdArc> ifsa(vfsa1, vfsa2);
DeterminizeFst<StdArc> dfa1(vfsa1);
DeterminizeFst<StdArc> dfa2(ifsa);
return Equivalent(dfa1, dfa2);
}
// Returns complement Fsa
void Complement(const Fst<Arc> &ifsa, MutableFst<Arc> *ofsa) {
RmEpsilonFst<Arc> rfsa(ifsa);
DeterminizeFst<Arc> dfa(rfsa);
DifferenceFst<Arc> cfsa(univ_fsa_, dfa);
*ofsa = cfsa;
}
// FSA with no states
VectorFst<Arc> zero_fsa_;
// FSA with one state that accepts epsilon.
VectorFst<Arc> one_fsa_;
// FSA with one state that accepts all strings.
VectorFst<Arc> univ_fsa_;
};
// This class tests a variety of identities and properties that must
// hold for various FST algorithms. It randomly generates FSTs, using
// function object 'weight_generator' to select weights. 'WeightTester'
// and 'UnweightedTester' are then called.
template <class Arc, class WeightGenerator>
class AlgoTester {
public:
typedef typename Arc::Label Label;
typedef typename Arc::StateId StateId;
typedef typename Arc::Weight Weight;
AlgoTester(WeightGenerator generator, int seed)
: weight_generator_(generator) {
one_fst_.AddState();
one_fst_.SetStart(0);
one_fst_.SetFinal(0, Weight::One());
univ_fst_.AddState();
univ_fst_.SetStart(0);
univ_fst_.SetFinal(0, Weight::One());
for (int i = 0; i < kNumRandomLabels; ++i)
univ_fst_.AddArc(0, Arc(i, i, Weight::One(), 0));
weighted_tester_ = new WeightedTester<Arc, WeightGenerator>(
seed, zero_fst_, one_fst_, univ_fst_, &weight_generator_);
unweighted_tester_ =
new UnweightedTester<Arc>(zero_fst_, one_fst_, univ_fst_);
}
~AlgoTester() {
delete weighted_tester_;
delete unweighted_tester_;
}
void MakeRandFst(MutableFst<Arc> *fst) {
RandFst<Arc, WeightGenerator>(kNumRandomStates, kNumRandomArcs,
kNumRandomLabels, kAcyclicProb,
&weight_generator_, fst);
}
void Test() {
VLOG(1) << "weight type = " << Weight::Type();
for (int i = 0; i < FLAGS_repeat; ++i) {
// Random transducers
VectorFst<Arc> T1;
VectorFst<Arc> T2;
VectorFst<Arc> T3;
MakeRandFst(&T1);
MakeRandFst(&T2);
MakeRandFst(&T3);
weighted_tester_->Test(T1, T2, T3);
VectorFst<Arc> A1(T1);
VectorFst<Arc> A2(T2);
VectorFst<Arc> A3(T3);
Project(&A1, PROJECT_OUTPUT);
Project(&A2, PROJECT_INPUT);
Project(&A3, PROJECT_INPUT);
ArcMap(&A1, rm_weight_mapper_);
ArcMap(&A2, rm_weight_mapper_);
ArcMap(&A3, rm_weight_mapper_);
unweighted_tester_->Test(A1, A2, A3);
}
}
private:
// Generates weights used in testing.
WeightGenerator weight_generator_;
// FST with no states
VectorFst<Arc> zero_fst_;
// FST with one state that accepts epsilon.
VectorFst<Arc> one_fst_;
// FST with one state that accepts all strings.
VectorFst<Arc> univ_fst_;
// Tests weighted FSTs
WeightedTester<Arc, WeightGenerator> *weighted_tester_;
// Tests unweighted FSTs
UnweightedTester<Arc> *unweighted_tester_;
// Mapper to remove weights from an Fst
RmWeightMapper<Arc> rm_weight_mapper_;
// Maximum number of states in random test Fst.
static const int kNumRandomStates;
// Maximum number of arcs in random test Fst.
static const int kNumRandomArcs;
// Number of alternative random labels.
static const int kNumRandomLabels;
// Probability to force an acyclic Fst
static const float kAcyclicProb;
// Maximum random path length.
static const int kRandomPathLength;
// Number of random paths to explore.
static const int kNumRandomPaths;
AlgoTester(const AlgoTester &) = delete;
AlgoTester &operator=(const AlgoTester &) = delete;
};
template <class A, class G>
const int AlgoTester<A, G>::kNumRandomStates = 10;
template <class A, class G>
const int AlgoTester<A, G>::kNumRandomArcs = 25;
template <class A, class G>
const int AlgoTester<A, G>::kNumRandomLabels = 5;
template <class A, class G>
const float AlgoTester<A, G>::kAcyclicProb = .25;
template <class A, class G>
const int AlgoTester<A, G>::kRandomPathLength = 25;
template <class A, class G>
const int AlgoTester<A, G>::kNumRandomPaths = 100;
} // namespace fst
#endif // FST_TEST_ALGO_TEST_H_