summaryrefslogtreecommitdiff
path: root/python/cdec/sa/rulefactory.pxi
blob: 86b994b941fcee0f8cab5d019d82e44204f1db78 (plain)
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
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
# Implementation of the algorithms described in 
# Lopez, EMNLP-CoNLL 2007
# Much faster than the Python numbers reported there.
# Note to reader: this code is closer to C than Python
import gc
import itertools

from libc.stdlib cimport malloc, realloc, free
from libc.string cimport memset, memcpy
from libc.math cimport fmod, ceil, floor, log

from collections import defaultdict, Counter, namedtuple

FeatureContext = namedtuple('FeatureContext',
    ['fphrase', 
     'ephrase', 
     'paircount', 
     'fcount', 
     'fsample_count',
     'input_span',
     'matches',
     'input_match',
     'test_sentence',
     'f_text',
     'e_text',
     'meta',
     'online'
    ])

OnlineFeatureContext = namedtuple('OnlineFeatureContext',
    ['fcount',
     'fsample_count',
     'paircount',
     'bilex_f',
     'bilex_e',
     'bilex_fe',
     'bilex_ef'
    ])

cdef class OnlineStats:
    cdef public samples_f
    cdef public phrases_f
    cdef public phrases_e
    cdef public phrases_fe
    cdef public phrases_al
    cdef public bilex_f
    cdef public bilex_e
    cdef public bilex_fe
    cdef public bilex_ef

    def __cinit__(self):
        # Keep track of everything that can be sampled:
        self.samples_f = defaultdict(int)
        
        # Phrase counts
        self.phrases_f = defaultdict(int)
        self.phrases_e = defaultdict(int)
        self.phrases_fe = defaultdict(lambda: defaultdict(int))
        self.phrases_al = defaultdict(lambda: defaultdict(tuple))

        # Bilexical counts
        self.bilex_f = defaultdict(int)
        self.bilex_e = defaultdict(int)
        self.bilex_fe = defaultdict(lambda: defaultdict(int))
        self.bilex_ef = defaultdict(lambda: defaultdict(int))

cdef int PRECOMPUTE = 0
cdef int MERGE = 1
cdef int BAEZA_YATES = 2

# NOTE: was encoded as a non-terminal in the previous version
cdef int EPSILON = sym_fromstring('*EPS*', True)

cdef class TrieNode:
    cdef public children

    def __cinit__(self):
        self.children = {}

cdef class ExtendedTrieNode(TrieNode):
    cdef public phrase
    cdef public phrase_location
    cdef public suffix_link

    def __cinit__(self, phrase=None, phrase_location=None, suffix_link=None):
        self.phrase = phrase
        self.phrase_location = phrase_location
        self.suffix_link = suffix_link


cdef class TrieTable:
    cdef public int extended
    cdef public int count
    cdef public root
    def __cinit__(self, extended=False):
        self.count = 0
        self.extended = extended
        if extended:
            self.root = ExtendedTrieNode()
        else:
            self.root = TrieNode()

# linked list structure for storing matches in BaselineRuleFactory
cdef struct match_node:
    int* match
    match_node* next

# encodes information needed to find a (hierarchical) phrase
# in the text.    If phrase is contiguous, that's just a range
# in the suffix array; if discontiguous, it is the set of 
# actual locations (packed into an array)
cdef class PhraseLocation:
    cdef int sa_low
    cdef int sa_high
    cdef int arr_low
    cdef int arr_high
    cdef IntList arr
    cdef int num_subpatterns

    # returns true if sent_id is contained
    cdef int contains(self, int sent_id):
        return 1

    def __cinit__(self, int sa_low=-1, int sa_high=-1, int arr_low=-1, int arr_high=-1,
            arr=None, int num_subpatterns=1):
        self.sa_low = sa_low
        self.sa_high = sa_high
        self.arr_low = arr_low
        self.arr_high = arr_high
        self.arr = arr
        self.num_subpatterns = num_subpatterns
            

cdef class Sampler:
    '''A Sampler implements a logic for choosing
    samples from a population range'''

    cdef int sample_size
    cdef IntList sa

    def __cinit__(self, int sample_size, SuffixArray fsarray):
        self.sample_size = sample_size
        self.sa = fsarray.sa
        if sample_size > 0:
            logger.info("Sampling strategy: uniform, max sample size = %d", sample_size)
        else:
            logger.info("Sampling strategy: no sampling")

    def sample(self, PhraseLocation phrase_location):
        '''Returns a sample of the locations for
        the phrase.    If there are less than self.sample_size
        locations, return all of them; otherwise, return
        up to self.sample_size locations.    In the latter case,
        we choose to sample UNIFORMLY -- that is, the locations
        are chosen at uniform intervals over the entire set, rather
        than randomly.    This makes the algorithm deterministic, which
        is good for things like MERT'''
        cdef IntList sample
        cdef double i, stepsize
        cdef int num_locations, val, j

        sample = IntList()
        if phrase_location.arr is None:
            num_locations = phrase_location.sa_high - phrase_location.sa_low
            if self.sample_size == -1 or num_locations <= self.sample_size:
                sample._extend_arr(self.sa.arr + phrase_location.sa_low, num_locations)
            else:
                stepsize = float(num_locations)/float(self.sample_size)
                i = phrase_location.sa_low
                while i < phrase_location.sa_high and sample.len < self.sample_size:
                    '''Note: int(i) not guaranteed to have the desired
                    effect, according to the python documentation'''
                    if fmod(i,1.0) > 0.5:
                        val = int(ceil(i))
                    else:
                        val = int(floor(i))
                    sample._append(self.sa.arr[val])
                    i = i + stepsize
        else:
            num_locations = (phrase_location.arr_high - phrase_location.arr_low) / phrase_location.num_subpatterns
            if self.sample_size == -1 or num_locations <= self.sample_size:
                sample = phrase_location.arr
            else:
                stepsize = float(num_locations)/float(self.sample_size)
                i = phrase_location.arr_low
                while i < num_locations and sample.len < self.sample_size * phrase_location.num_subpatterns:
                    '''Note: int(i) not guaranteed to have the desired
                    effect, according to the python documentation'''
                    if fmod(i,1.0) > 0.5:
                        val = int(ceil(i))
                    else:
                        val = int(floor(i))
                    j = phrase_location.arr_low + (val*phrase_location.num_subpatterns)
                    sample._extend_arr(phrase_location.arr.arr + j, phrase_location.num_subpatterns)
                    i = i + stepsize
        return sample


# struct used to encapsulate a single matching
cdef struct Matching:
    int* arr
    int start
    int end
    int sent_id
    int size


cdef void assign_matching(Matching* m, int* arr, int start, int step, int* sent_id_arr):
    m.arr = arr
    m.start = start
    m.end = start + step
    m.sent_id = sent_id_arr[arr[start]]
    m.size = step


cdef int* append_combined_matching(int* arr, Matching* loc1, Matching* loc2, 
                                int offset_by_one, int num_subpatterns, int* result_len):
    cdef int i, new_len

    new_len = result_len[0] + num_subpatterns
    arr = <int*> realloc(arr, new_len*sizeof(int))

    for i from 0 <= i < loc1.size:
        arr[result_len[0]+i] = loc1.arr[loc1.start+i]
    if num_subpatterns > loc1.size:
        arr[new_len-1] = loc2.arr[loc2.end-1]
    result_len[0] = new_len
    return arr


cdef int* extend_arr(int* arr, int* arr_len, int* appendix, int appendix_len):
    cdef int new_len
    
    new_len = arr_len[0] + appendix_len
    arr = <int*> realloc(arr, new_len*sizeof(int))
    memcpy(arr+arr_len[0], appendix, appendix_len*sizeof(int))
    arr_len[0] = new_len
    return arr

cdef int median(int low, int high, int step):
    return low + (((high - low)/step)/2)*step


cdef void find_comparable_matchings(int low, int high, int* arr, int step, int loc, int* loc_minus, int* loc_plus):
    # Returns (minus, plus) indices for the portion of the array
    # in which all matchings have the same first index as the one
    # starting at loc
    loc_plus[0] = loc + step
    while loc_plus[0] < high and arr[loc_plus[0]] == arr[loc]:
        loc_plus[0] = loc_plus[0] + step
    loc_minus[0] = loc
    while loc_minus[0]-step >= low and arr[loc_minus[0]-step] == arr[loc]:
        loc_minus[0] = loc_minus[0] - step


cdef class HieroCachingRuleFactory:
    '''This RuleFactory implements a caching 
    method using TrieTable, which makes phrase
    generation somewhat speedier -- phrases only
    need to be extracted once (however, it is
    quite possible they need to be scored 
    for each input sentence, for contextual models)'''

    cdef TrieTable rules
    cdef Sampler sampler
    cdef Scorer scorer

    cdef int max_chunks
    cdef int max_target_chunks
    cdef int max_length
    cdef int max_target_length
    cdef int max_nonterminals
    cdef int max_initial_size
    cdef int train_max_initial_size
    cdef int min_gap_size
    cdef int train_min_gap_size
    cdef int category

    cdef precomputed_index
    cdef precomputed_collocations
    cdef precompute_file
    cdef max_rank
    cdef int precompute_rank, precompute_secondary_rank
    cdef bint use_baeza_yates
    cdef bint use_index
    cdef bint use_collocations
    cdef float by_slack_factor

    cdef prev_norm_prefix
    cdef float extract_time
    cdef float intersect_time
    cdef SuffixArray fsa
    cdef DataArray fda
    cdef DataArray eda
    
    cdef Alignment alignment
    cdef IntList eid2symid
    cdef IntList fid2symid
    cdef bint tight_phrases
    cdef bint require_aligned_terminal
    cdef bint require_aligned_chunks

    cdef IntList findexes
    cdef IntList findexes1

    cdef bint online
    cdef online_stats

    def __cinit__(self,
            # compiled alignment object (REQUIRED)
            Alignment alignment,
            # parameter for double-binary search; doesn't seem to matter much
            float by_slack_factor=1.0,
            # name of generic nonterminal used by Hiero
            char* category="[X]",
            # maximum number of contiguous chunks of terminal symbols in RHS of a rule. If None, defaults to max_nonterminals+1
            max_chunks=None,
            # maximum span of a grammar rule in TEST DATA
            unsigned max_initial_size=10,
            # maximum number of symbols (both T and NT) allowed in a rule
            unsigned max_length=5,
            # maximum number of nonterminals allowed in a rule (set >2 at your own risk)
            unsigned max_nonterminals=2,
            # maximum number of contiguous chunks of terminal symbols in target-side RHS of a rule. If None, defaults to max_nonterminals+1
            max_target_chunks=None,
            # maximum number of target side symbols (both T and NT) allowed in a rule. If None, defaults to max_initial_size
            max_target_length=None,
            # minimum span of a nonterminal in the RHS of a rule in TEST DATA
            unsigned min_gap_size=2,
            # filename of file containing precomputed collocations
            precompute_file=None,
            # maximum frequency rank of patterns used to compute triples (don't set higher than 20).
            unsigned precompute_secondary_rank=20,
            # maximum frequency rank of patterns used to compute collocations (no need to set higher than maybe 200-300)
            unsigned precompute_rank=100,
            # require extracted rules to have at least one aligned word
            bint require_aligned_terminal=True,
            # require each contiguous chunk of extracted rules to have at least one aligned word
            bint require_aligned_chunks=False,
            # maximum span of a grammar rule extracted from TRAINING DATA
            unsigned train_max_initial_size=10,
            # minimum span of an RHS nonterminal in a rule extracted from TRAINING DATA
            unsigned train_min_gap_size=2,
            # False if phrases should be loose (better but slower), True otherwise
            bint tight_phrases=True,
            # True to require use of double-binary alg, false otherwise
            bint use_baeza_yates=True,
            # True to enable used of precomputed collocations
            bint use_collocations=True,
            # True to enable use of precomputed inverted indices
            bint use_index=True):
        '''Note: we make a distinction between the min_gap_size
        and max_initial_size used in test and train.    The latter
        are represented by train_min_gap_size and train_max_initial_size,
        respectively.    This is because Chiang's model does not require
        them to be the same, therefore we don't either.'''
        self.rules = TrieTable(True) # cache
        self.rules.root = ExtendedTrieNode(phrase_location=PhraseLocation())
        if alignment is None:
            raise Exception("Must specify an alignment object")
        self.alignment = alignment

        # grammar parameters and settings
        # NOTE: setting max_nonterminals > 2 is not currently supported in Hiero
        self.max_length = max_length
        self.max_nonterminals = max_nonterminals
        self.max_initial_size = max_initial_size
        self.train_max_initial_size = train_max_initial_size
        self.min_gap_size = min_gap_size
        self.train_min_gap_size = train_min_gap_size
        self.category = sym_fromstring(category, False)

        if max_chunks is None:
            self.max_chunks = self.max_nonterminals + 1
        else:
            self.max_chunks = max_chunks

        if max_target_chunks is None:
            self.max_target_chunks = self.max_nonterminals + 1
        else:
            self.max_target_chunks = max_target_chunks

        if max_target_length is None:
            self.max_target_length = max_initial_size
        else:
            self.max_target_length = max_target_length

        # algorithmic parameters and settings
        self.precomputed_collocations = {}
        self.precomputed_index = {}
        self.use_index = use_index
        self.use_collocations = use_collocations
        self.max_rank = {}
        self.precompute_file = precompute_file
        self.precompute_rank = precompute_rank
        self.precompute_secondary_rank = precompute_secondary_rank
        self.use_baeza_yates = use_baeza_yates
        self.by_slack_factor = by_slack_factor
        self.tight_phrases = tight_phrases

        if require_aligned_chunks:
            # one condition is a stronger version of the other.
            self.require_aligned_chunks = self.require_aligned_terminal = True
        elif require_aligned_terminal:
            self.require_aligned_chunks = False
            self.require_aligned_terminal = True
        else:
            self.require_aligned_chunks = self.require_aligned_terminal = False

        # diagnostics
        self.prev_norm_prefix = ()

        self.findexes = IntList(initial_len=10)
        self.findexes1 = IntList(initial_len=10)
        
        # Online stats 
        
        # True after data is added
        self.online = False
        self.online_stats = defaultdict(OnlineStats)
        
    def configure(self, SuffixArray fsarray, DataArray edarray,
            Sampler sampler, Scorer scorer):
        '''This gives the RuleFactory access to the Context object.
        Here we also use it to precompute the most expensive intersections
        in the corpus quickly.'''
        self.fsa = fsarray
        self.fda = fsarray.darray
        self.eda = edarray
        self.fid2symid = self.set_idmap(self.fda)
        self.eid2symid = self.set_idmap(self.eda)
        self.precompute()
        self.sampler = sampler
        self.scorer = scorer

    cdef set_idmap(self, DataArray darray):
        cdef int word_id, new_word_id, N
        cdef IntList idmap
        
        N = len(darray.id2word)
        idmap = IntList(initial_len=N)
        for word_id from 0 <= word_id < N:
            new_word_id = sym_fromstring(darray.id2word[word_id], True)
            idmap.arr[word_id] = new_word_id
        return idmap


    def pattern2phrase(self, pattern):
        # pattern is a tuple, which we must convert to a hiero Phrase
        result = ()
        arity = 0
        for word_id in pattern:
            if word_id == -1:
                arity = arity + 1
                new_id = sym_setindex(self.category, arity)
            else:
                new_id = sym_fromstring(self.fda.id2word[word_id], True)
            result = result + (new_id,)
        return Phrase(result)

    def pattern2phrase_plus(self, pattern):
        # returns a list containing both the pattern, and pattern
        # suffixed/prefixed with the NT category.
        patterns = []
        result = ()
        arity = 0
        for word_id in pattern:
            if word_id == -1:
                arity = arity + 1
                new_id = sym_setindex(self.category, arity)
            else:
                new_id = sym_fromstring(self.fda.id2word[word_id], True)
            result = result + (new_id,)
        patterns.append(Phrase(result))
        patterns.append(Phrase(result + (sym_setindex(self.category, 1),)))
        patterns.append(Phrase((sym_setindex(self.category, 1),) + result))
        return patterns

    def precompute(self):
        cdef Precomputation pre
        
        if self.precompute_file is not None:
            start_time = monitor_cpu()
            logger.info("Reading precomputed data from file %s... ", self.precompute_file)
            pre = Precomputation(from_binary=self.precompute_file)
            # check parameters of precomputation -- some are critical and some are not
            if pre.max_nonterminals != self.max_nonterminals:
                logger.warn("Precomputation done with max nonterminals %d, decoder uses %d", pre.max_nonterminals, self.max_nonterminals)
            if pre.max_length != self.max_length:
                logger.warn("Precomputation done with max terminals %d, decoder uses %d", pre.max_length, self.max_length)
            if pre.train_max_initial_size != self.train_max_initial_size:
                raise Exception("Precomputation done with max initial size %d, decoder uses %d" % (pre.train_max_initial_size, self.train_max_initial_size))
            if pre.train_min_gap_size != self.train_min_gap_size:
                raise Exception("Precomputation done with min gap size %d, decoder uses %d" % (pre.train_min_gap_size, self.train_min_gap_size))
            if self.use_index:
                logger.info("Converting %d hash keys on precomputed inverted index... ", len(pre.precomputed_index))
                for pattern, arr in pre.precomputed_index.iteritems():
                    phrases = self.pattern2phrase_plus(pattern)
                    for phrase in phrases:
                        self.precomputed_index[phrase] = arr
            if self.use_collocations:
                logger.info("Converting %d hash keys on precomputed collocations... ", len(pre.precomputed_collocations))
                for pattern, arr in pre.precomputed_collocations.iteritems():
                    phrase = self.pattern2phrase(pattern)
                    self.precomputed_collocations[phrase] = arr
            stop_time = monitor_cpu()
            logger.info("Processing precomputations took %f seconds", stop_time - start_time)


    def get_precomputed_collocation(self, phrase):
        if phrase in self.precomputed_collocations:
            arr = self.precomputed_collocations[phrase]
            return PhraseLocation(arr=arr, arr_low=0, arr_high=len(arr), num_subpatterns=phrase.arity()+1)
        return None


    cdef int* baeza_yates_helper(self, int low1, int high1, int* arr1, int step1,
                        int low2, int high2, int* arr2, int step2,
                        int offset_by_one, int len_last, int num_subpatterns, int* result_len):
        cdef int i1, i2, j1, j2, med1, med2, med1_plus, med1_minus, med2_minus, med2_plus
        cdef int d_first, qsetsize, dsetsize, tmp, search_low, search_high
        cdef int med_result_len, low_result_len, high_result_len
        cdef long comparison
        cdef int* result
        cdef int* low_result
        cdef int* med_result
        cdef int* high_result
        cdef Matching loc1, loc2

        result = <int*> malloc(0*sizeof(int*))

        d_first = 0
        if high1 - low1 > high2 - low2:
            d_first = 1

        # First, check to see if we are at any of the recursive base cases
        # Case 1: one of the sets is empty
        if low1 >= high1 or low2 >= high2:
            return result

        # Case 2: sets are non-overlapping
        assign_matching(&loc1, arr1, high1-step1, step1, self.fda.sent_id.arr)
        assign_matching(&loc2, arr2, low2, step2, self.fda.sent_id.arr)
        if self.compare_matchings(&loc1, &loc2, offset_by_one, len_last) == -1:
            return result

        assign_matching(&loc1, arr1, low1, step1, self.fda.sent_id.arr)
        assign_matching(&loc2, arr2, high2-step2, step2, self.fda.sent_id.arr)
        if self.compare_matchings(&loc1, &loc2, offset_by_one, len_last) == 1:
            return result

        # Case 3: query set and data set do not meet size mismatch constraints;
        # We use mergesort instead in this case
        qsetsize = (high1-low1) / step1
        dsetsize = (high2-low2) / step2
        if d_first:
            tmp = qsetsize
            qsetsize = dsetsize
            dsetsize = tmp

        if self.by_slack_factor * qsetsize * log(dsetsize) / log(2) > dsetsize:
            free(result)
            return self.merge_helper(low1, high1, arr1, step1, low2, high2, arr2, step2, offset_by_one, len_last, num_subpatterns, result_len)

        # binary search.    There are two flavors, depending on
        # whether the queryset or dataset is first
        if d_first:
            med2 = median(low2, high2, step2)
            assign_matching(&loc2, arr2, med2, step2, self.fda.sent_id.arr)

            search_low = low1
            search_high = high1
            while search_low < search_high:
                med1 = median(search_low, search_high, step1)
                find_comparable_matchings(low1, high1, arr1, step1, med1, &med1_minus, &med1_plus)
                comparison = self.compare_matchings_set(med1_minus, med1_plus, arr1, step1, &loc2, offset_by_one, len_last)
                if comparison == -1:
                    search_low = med1_plus
                elif comparison == 1:
                    search_high = med1_minus
                else:
                    break
        else:
            med1 = median(low1, high1, step1)
            find_comparable_matchings(low1, high1, arr1, step1, med1, &med1_minus, &med1_plus)

            search_low = low2
            search_high = high2
            while search_low < search_high:
                med2 = median(search_low, search_high, step2)
                assign_matching(&loc2, arr2, med2, step2, self.fda.sent_id.arr)
                comparison = self.compare_matchings_set(med1_minus, med1_plus, arr1, step1, &loc2, offset_by_one, len_last)
                if comparison == -1:
                    search_high = med2
                elif comparison == 1:
                    search_low = med2 + step2
                else:
                    break

        med_result_len = 0
        med_result = <int*> malloc(0*sizeof(int*))
        if search_high > search_low:
            # Then there is a match for the median element of Q
            # What we want to find is the group of all bindings in the first set
            # s.t. their first element == the first element of med1.    Then we
            # want to store the bindings for all of those elements.    We can
            # subsequently throw all of them away.
            med2_minus = med2
            med2_plus = med2 + step2
            i1 = med1_minus
            while i1 < med1_plus:
                assign_matching(&loc1, arr1, i1, step1, self.fda.sent_id.arr)
                while med2_minus-step2 >= low2:
                    assign_matching(&loc2, arr2, med2_minus-step2, step2, self.fda.sent_id.arr)
                    if self.compare_matchings(&loc1, &loc2, offset_by_one, len_last) < 1:
                        med2_minus = med2_minus - step2
                    else:
                        break
                i2 = med2_minus
                while i2 < high2:
                    assign_matching(&loc2, arr2, i2, step2, self.fda.sent_id.arr)
                    comparison = self.compare_matchings(&loc1, &loc2, offset_by_one, len_last)
                    if comparison == 0:
                        pass
                        med_result = append_combined_matching(med_result, &loc1, &loc2, offset_by_one, num_subpatterns, &med_result_len)
                    if comparison == -1:
                        break
                    i2 = i2 + step2
                if i2 > med2_plus:
                    med2_plus = i2
                i1 = i1 + step1

            tmp = med1_minus
            med1_minus = med1_plus
            med1_plus = tmp
        else:
            # No match; need to figure out the point of division in D and Q
            med2_minus = med2
            med2_plus = med2
            if d_first:
                med2_minus = med2_minus + step2
                if comparison == -1:
                    med1_minus = med1_plus
                if comparison == 1:
                    med1_plus = med1_minus
            else:
                tmp = med1_minus
                med1_minus = med1_plus
                med1_plus = tmp
                if comparison == 1:
                    med2_minus = med2_minus + step2
                    med2_plus = med2_plus + step2

        low_result_len = 0
        low_result = self.baeza_yates_helper(low1, med1_plus, arr1, step1, low2, med2_plus, arr2, step2, offset_by_one, len_last, num_subpatterns, &low_result_len)
        high_result_len = 0
        high_result = self.baeza_yates_helper(med1_minus, high1, arr1, step1, med2_minus, high2, arr2, step2, offset_by_one, len_last, num_subpatterns, &high_result_len)

        result = extend_arr(result, result_len, low_result, low_result_len)
        result = extend_arr(result, result_len, med_result, med_result_len)
        result = extend_arr(result, result_len, high_result, high_result_len)
        free(low_result)
        free(med_result)
        free(high_result)

        return result



    cdef long compare_matchings_set(self, int i1_minus, int i1_plus, int* arr1, int step1,
                            Matching* loc2, int offset_by_one, int len_last):
        """
        Compares a *set* of bindings, all with the same first element,
        to a single binding.    Returns -1 if all comparisons == -1, 1 if all
        comparisons == 1, and 0 otherwise.
        """
        cdef int i1, comparison, prev_comparison
        cdef Matching l1_stack
        cdef Matching* loc1

        loc1 = &l1_stack

        i1 = i1_minus
        while i1 < i1_plus:
            assign_matching(loc1, arr1, i1, step1, self.fda.sent_id.arr)
            comparison = self.compare_matchings(loc1, loc2, offset_by_one, len_last)
            if comparison == 0:
                prev_comparison = 0
                break
            elif i1 == i1_minus:
                prev_comparison = comparison
            else:
                if comparison != prev_comparison:
                    prev_comparison = 0
                    break
            i1 = i1 + step1
        return prev_comparison


    cdef long compare_matchings(self, Matching* loc1, Matching* loc2, int offset_by_one, int len_last):
        cdef int i

        if loc1.sent_id > loc2.sent_id:
            return 1
        if loc2.sent_id > loc1.sent_id:
            return -1

        if loc1.size == 1 and loc2.size == 1:
            if loc2.arr[loc2.start] - loc1.arr[loc1.start] <= self.train_min_gap_size:
                return 1

        elif offset_by_one:
            for i from 1 <= i < loc1.size:
                if loc1.arr[loc1.start+i] > loc2.arr[loc2.start+i-1]:
                    return 1
                if loc1.arr[loc1.start+i] < loc2.arr[loc2.start+i-1]:
                    return -1

        else:
            if loc1.arr[loc1.start]+1 > loc2.arr[loc2.start]:
                return 1
            if loc1.arr[loc1.start]+1 < loc2.arr[loc2.start]:
                return -1

            for i from 1 <= i < loc1.size:
                if loc1.arr[loc1.start+i] > loc2.arr[loc2.start+i]:
                    return 1
                if loc1.arr[loc1.start+i] < loc2.arr[loc2.start+i]:
                    return -1

        if loc2.arr[loc2.end-1] + len_last - loc1.arr[loc1.start] > self.train_max_initial_size:
            return -1
        return 0


    cdef int* merge_helper(self, int low1, int high1, int* arr1, int step1,
                    int low2, int high2, int* arr2, int step2,
                    int offset_by_one, int len_last, int num_subpatterns, int* result_len):
        cdef int i1, i2, j1, j2
        cdef long comparison
        cdef int* result
        cdef Matching loc1, loc2

        result_len[0] = 0
        result = <int*> malloc(0*sizeof(int))

        i1 = low1
        i2 = low2
        while i1 < high1 and i2 < high2:
            
            # First, pop all unneeded loc2's off the stack
            assign_matching(&loc1, arr1, i1, step1, self.fda.sent_id.arr)
            while i2 < high2:
                assign_matching(&loc2, arr2, i2, step2, self.fda.sent_id.arr)
                if self.compare_matchings(&loc1, &loc2, offset_by_one, len_last) == 1:
                    i2 = i2 + step2
                else:
                    break

            # Next: process all loc1's with the same starting val
            j1 = i1
            while i1 < high1 and arr1[j1] == arr1[i1]:
                assign_matching(&loc1, arr1, i1, step1, self.fda.sent_id.arr)
                j2 = i2
                while j2 < high2:
                    assign_matching(&loc2, arr2, j2, step2, self.fda.sent_id.arr)
                    comparison = self.compare_matchings(&loc1, &loc2, offset_by_one, len_last)
                    if comparison == 0:
                        result = append_combined_matching(result, &loc1, &loc2, offset_by_one, num_subpatterns, result_len)
                    if comparison == 1:
                        pass
                    if comparison == -1:
                        break
                    else:
                        j2 = j2 + step2
                i1 = i1 + step1
        return result


    cdef void sort_phrase_loc(self, IntList arr, PhraseLocation loc, Phrase phrase):
        cdef int i, j
        cdef VEB veb
        cdef IntList result

        if phrase in self.precomputed_index:
            loc.arr = self.precomputed_index[phrase]
        else:
            loc.arr = IntList(initial_len=loc.sa_high-loc.sa_low)
            veb = VEB(arr.len)
            for i from loc.sa_low <= i < loc.sa_high:
                veb._insert(arr.arr[i])
            i = veb.veb.min_val
            for j from 0 <= j < loc.sa_high-loc.sa_low:
                loc.arr.arr[j] = i
                i = veb._findsucc(i)
        loc.arr_low = 0
        loc.arr_high = loc.arr.len


    cdef intersect_helper(self, Phrase prefix, Phrase suffix,
                PhraseLocation prefix_loc, PhraseLocation suffix_loc, int algorithm):

        cdef IntList arr1, arr2, result
        cdef int low1, high1, step1, low2, high2, step2, offset_by_one, len_last, num_subpatterns, result_len
        cdef int* result_ptr

        result_len = 0

        if sym_isvar(suffix[0]):
            offset_by_one = 1
        else:
            offset_by_one = 0

        len_last = len(suffix.getchunk(suffix.arity()))

        if prefix_loc.arr is None:
            self.sort_phrase_loc(self.fsa.sa, prefix_loc, prefix)
        arr1 = prefix_loc.arr
        low1 = prefix_loc.arr_low
        high1 = prefix_loc.arr_high
        step1 = prefix_loc.num_subpatterns

        if suffix_loc.arr is None:
            self.sort_phrase_loc(self.fsa.sa, suffix_loc, suffix)
        arr2 = suffix_loc.arr
        low2 = suffix_loc.arr_low
        high2 = suffix_loc.arr_high
        step2 = suffix_loc.num_subpatterns

        num_subpatterns = prefix.arity()+1

        if algorithm == MERGE:
            result_ptr = self.merge_helper(low1, high1, arr1.arr, step1,
                                    low2, high2, arr2.arr, step2,
                                    offset_by_one, len_last, num_subpatterns, &result_len)
        else:
            result_ptr = self.baeza_yates_helper(low1, high1, arr1.arr, step1,
                                    low2, high2, arr2.arr, step2,
                                    offset_by_one, len_last, num_subpatterns, &result_len)

        if result_len == 0:
            free(result_ptr)
            return None
        else:
            result = IntList()
            free(result.arr)
            result.arr = result_ptr
            result.len = result_len
            result.size = result_len
            return PhraseLocation(arr_low=0, arr_high=result_len, arr=result, num_subpatterns=num_subpatterns)

    cdef loc2str(self, PhraseLocation loc):
        cdef int i, j
        result = "{"
        i = 0
        while i < loc.arr_high:
            result = result + "("
            for j from i <= j < i + loc.num_subpatterns:
                result = result + ("%d " %loc.arr[j])
            result = result + ")"
            i = i + loc.num_subpatterns
        result = result + "}"
        return result

    cdef PhraseLocation intersect(self, prefix_node, suffix_node, Phrase phrase):
        cdef Phrase prefix, suffix
        cdef PhraseLocation prefix_loc, suffix_loc, result

        prefix = prefix_node.phrase
        suffix = suffix_node.phrase
        prefix_loc = prefix_node.phrase_location
        suffix_loc = suffix_node.phrase_location

        result = self.get_precomputed_collocation(phrase)
        if result is not None:
            intersect_method = "precomputed"

        if result is None:
            if self.use_baeza_yates:
                result = self.intersect_helper(prefix, suffix, prefix_loc, suffix_loc, BAEZA_YATES)
                intersect_method="double binary"
            else:
                result = self.intersect_helper(prefix, suffix, prefix_loc, suffix_loc, MERGE)
                intersect_method="merge"
        return result

    def advance(self, frontier, res, fwords):
        cdef unsigned na
        nf = []
        for toskip, (i, alt, pathlen) in frontier:
            spanlen = fwords[i][alt][2]
            if toskip == 0:
                res.append((i, alt, pathlen))
            ni = i + spanlen
            if ni < len(fwords) and pathlen + 1 < self.max_initial_size:
                for na in range(len(fwords[ni])):
                    nf.append((toskip - 1, (ni, na, pathlen + 1)))
        if len(nf) > 0:
            return self.advance(nf, res, fwords)
        else:
            return res
        
    def get_all_nodes_isteps_away(self, skip, i, spanlen, pathlen, fwords, next_states, reachable_buffer):
        cdef unsigned alt_it
        frontier = []
        if i+spanlen+skip >= len(next_states):
            return frontier
        key = tuple([i,spanlen])
        reachable = []
        if key in reachable_buffer:
            reachable = reachable_buffer[key]
        else:
            reachable = self.reachable(fwords, i, spanlen)
            reachable_buffer[key] = reachable
        for nextreachable in reachable:
            for next_id in next_states[nextreachable]:
                jump = self.shortest(fwords,i,next_id)
                if jump < skip:
                    continue
                if pathlen+jump <= self.max_initial_size:
                    for alt_id in range(len(fwords[next_id])):
                        if fwords[next_id][alt_id][0] != EPSILON:
                            newel = (next_id,alt_id,pathlen+jump)
                            if newel not in frontier:
                                frontier.append((next_id,alt_id,pathlen+jump))
        return frontier

    def reachable(self, fwords, ifrom, dist):
        ret = []
        if ifrom >= len(fwords):
            return ret
        for alt_id in range(len(fwords[ifrom])):
            if fwords[ifrom][alt_id][0] == EPSILON:
                ret.extend(self.reachable(fwords,ifrom+fwords[ifrom][alt_id][2],dist))
            else:
                if dist == 0:
                    if ifrom not in ret:
                        ret.append(ifrom)
                else:
                    for ifromchild in self.reachable(fwords,ifrom+fwords[ifrom][alt_id][2],dist-1):
                        if ifromchild not in ret:
                            ret.append(ifromchild)
                    
        return ret

    def shortest(self, fwords, ifrom, ito):
        cdef unsigned alt_id
        min = 1000
        if ifrom > ito:
            return min
        if ifrom == ito:
            return 0
        for alt_id in range(len(fwords[ifrom])):
            currmin = self.shortest(fwords,ifrom+fwords[ifrom][alt_id][2],ito)
            if fwords[ifrom][alt_id][0] != EPSILON:
                currmin += 1
            if currmin < min:
                min = currmin
        return min

    def get_next_states(self, _columns, curr_idx, min_dist=2):
        result = []
        candidate = [[curr_idx,0]]

        while len(candidate) > 0:
            curr = candidate.pop()
            if curr[0] >= len(_columns):
                continue
            if curr[0] not in result and min_dist <= curr[1] <= self.max_initial_size:
                result.append(curr[0]);
            curr_col = _columns[curr[0]]
            for alt in curr_col:
                next_id = curr[0]+alt[2]
                jump = 1
                if alt[0] == EPSILON:
                    jump = 0
                if next_id not in result and min_dist <= curr[1]+jump <= self.max_initial_size+1:
                    candidate.append([next_id,curr[1]+jump])
        return sorted(result);

    def input(self, fwords, meta, ctx_name=None):
        '''When this function is called on the RuleFactory,
        it looks up all of the rules that can be used to translate
        the input sentence'''
        cdef int i, j, k, flen, arity, num_subpatterns, num_samples, alt, alt_id, nualt
        cdef float start_time
        cdef PhraseLocation phrase_location
        cdef IntList sample, chunklen
        cdef Matching matching
        cdef Phrase hiero_phrase

        flen = len(fwords)
        start_time = monitor_cpu()
        self.extract_time = 0.0
        self.intersect_time = 0.0
        nodes_isteps_away_buffer = {}
        hit = 0
        reachable_buffer = {}

        # Phrase pairs processed by suffix array extractor.  Do not re-extract
        # during online extraction.  This is probably the hackiest part of
        # online grammar extraction.
        seen_phrases = set()
        
        # Do not cache between sentences
        self.rules.root = ExtendedTrieNode(phrase_location=PhraseLocation())

        frontier = []
        for i in range(len(fwords)):
            for alt in range(0, len(fwords[i])):
                if fwords[i][alt][0] != EPSILON:
                    frontier.append((i, i, (i,), alt, 0, self.rules.root, (), False))

        xroot = None
        x1 = sym_setindex(self.category, 1)
        if x1 in self.rules.root.children:
            xroot = self.rules.root.children[x1]
        else:
            xroot = ExtendedTrieNode(suffix_link=self.rules.root, phrase_location=PhraseLocation())
            self.rules.root.children[x1] = xroot

        for i in range(self.min_gap_size, len(fwords)):
            for alt in range(0, len(fwords[i])):
                if fwords[i][alt][0] != EPSILON:
                    frontier.append((i-self.min_gap_size, i, (i,), alt, self.min_gap_size, xroot, (x1,), True))

        next_states = []
        for i in range(len(fwords)):
            next_states.append(self.get_next_states(fwords,i,self.min_gap_size))

        while len(frontier) > 0:
            new_frontier = []
            for k, i, input_match, alt, pathlen, node, prefix, is_shadow_path in frontier:
                word_id = fwords[i][alt][0]
                spanlen = fwords[i][alt][2]
                # TODO get rid of k -- pathlen is replacing it
                if word_id == EPSILON:
                    # skipping because word_id is epsilon
                    if i+spanlen >= len(fwords):
                        continue
                    for nualt in range(0,len(fwords[i+spanlen])):
                        frontier.append((k, i+spanlen, input_match, nualt, pathlen, node, prefix, is_shadow_path))
                    continue
                
                phrase = prefix + (word_id,)
                hiero_phrase = Phrase(phrase)
                arity = hiero_phrase.arity()

                lookup_required = False
                if word_id in node.children:
                    if node.children[word_id] is None:
                        # Path dead-ends at this node
                        continue
                    else:
                        # Path continues at this node
                        node = node.children[word_id]
                else:
                    if node.suffix_link is None:
                        # Current node is root; lookup required
                        lookup_required = True
                    else:
                        if word_id in node.suffix_link.children:
                            if node.suffix_link.children[word_id] is None:
                                # Suffix link reports path is dead end
                                node.children[word_id] = None
                                continue
                            else:
                                # Suffix link indicates lookup is reqired
                                lookup_required = True
                        else:
                            #ERROR: We never get here
                            raise Exception("Keyword trie error")
                # checking whether lookup_required
                if lookup_required:
                    new_node = None
                    if is_shadow_path:
                        # Extending shadow path
                        # on the shadow path we don't do any search, we just use info from suffix link
                        new_node = ExtendedTrieNode(phrase_location=node.suffix_link.children[word_id].phrase_location,
                                suffix_link=node.suffix_link.children[word_id],
                                phrase=hiero_phrase)
                    else:
                        if arity > 0:
                            # Intersecting because of arity > 0
                            intersect_start_time = monitor_cpu()
                            phrase_location = self.intersect(node, node.suffix_link.children[word_id], hiero_phrase)
                            intersect_stop_time = monitor_cpu()
                            self.intersect_time += intersect_stop_time - intersect_start_time
                        else:
                            # Suffix array search
                            phrase_location = node.phrase_location
                            sa_range = self.fsa.lookup(sym_tostring(phrase[-1]), len(phrase)-1, phrase_location.sa_low, phrase_location.sa_high)
                            if sa_range is not None:
                                phrase_location = PhraseLocation(sa_low=sa_range[0], sa_high=sa_range[1])
                            else:
                                phrase_location = None

                        if phrase_location is None:
                            node.children[word_id] = None
                            # Search failed
                            continue
                        # Search succeeded
                        suffix_link = self.rules.root
                        if node.suffix_link is not None:
                            suffix_link = node.suffix_link.children[word_id]
                        new_node = ExtendedTrieNode(phrase_location=phrase_location,
                                suffix_link=suffix_link,
                                phrase=hiero_phrase)
                    node.children[word_id] = new_node
                    node = new_node

                    '''Automatically add a trailing X node, if allowed --
                    This should happen before we get to extraction (so that
                    the node will exist if needed)'''
                    if arity < self.max_nonterminals:
                        xcat_index = arity+1
                        xcat = sym_setindex(self.category, xcat_index)
                        suffix_link_xcat_index = xcat_index
                        if is_shadow_path:
                            suffix_link_xcat_index = xcat_index-1
                        suffix_link_xcat = sym_setindex(self.category, suffix_link_xcat_index)
                        node.children[xcat] = ExtendedTrieNode(phrase_location=node.phrase_location,
                                suffix_link=node.suffix_link.children[suffix_link_xcat],
                                phrase= Phrase(phrase + (xcat,)))

                    # sample from range
                    if not is_shadow_path:
                        sample = self.sampler.sample(node.phrase_location)
                        num_subpatterns = (<PhraseLocation> node.phrase_location).num_subpatterns
                        chunklen = IntList(initial_len=num_subpatterns)
                        for j from 0 <= j < num_subpatterns:
                            chunklen.arr[j] = hiero_phrase.chunklen(j)
                        extracts = []
                        j = 0
                        extract_start = monitor_cpu()
                        while j < sample.len:
                            extract = []

                            assign_matching(&matching, sample.arr, j, num_subpatterns, self.fda.sent_id.arr)
                            loc = tuple(sample[j:j+num_subpatterns])
                            extract = self.extract(hiero_phrase, &matching, chunklen.arr, num_subpatterns)
                            extracts.extend([(e, loc) for e in extract])
                            j = j + num_subpatterns

                        num_samples = sample.len/num_subpatterns
                        extract_stop = monitor_cpu()
                        self.extract_time = self.extract_time + extract_stop - extract_start
                        if len(extracts) > 0:
                            fcount = Counter()
                            fphrases = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
                            for (f, e, count, als), loc in extracts:
                                fcount[f] += count
                                fphrases[f][e][als].append(loc)
                            for f, elist in fphrases.iteritems():
                                for e, alslist in elist.iteritems():
                                    alignment, max_locs = max(alslist.iteritems(), key=lambda x: len(x[1]))
                                    locs = tuple(itertools.chain.from_iterable(alslist.itervalues()))
                                    count = len(locs)
                                    scores = self.scorer.score(FeatureContext(
                                               f, e, count, fcount[f], num_samples,
                                               (k,i+spanlen), locs, input_match, 
                                               fwords, self.fda, self.eda,
                                               meta,
                                               # Include online stats.  None if none.
                                               self.online_ctx_lookup(f, e, ctx_name)))
                                    # Phrase pair processed
                                    if self.online:
                                        seen_phrases.add((f, e))
                                    yield Rule(self.category, f, e, scores, alignment)

                if len(phrase) < self.max_length and i+spanlen < len(fwords) and pathlen+1 <= self.max_initial_size:
                    for alt_id in range(len(fwords[i+spanlen])):
                        new_frontier.append((k, i+spanlen, input_match, alt_id, pathlen + 1, node, phrase, is_shadow_path))
                    num_subpatterns = arity
                    if not is_shadow_path:
                        num_subpatterns = num_subpatterns + 1
                    if len(phrase)+1 < self.max_length and arity < self.max_nonterminals and num_subpatterns < self.max_chunks:
                        xcat = sym_setindex(self.category, arity+1)
                        xnode = node.children[xcat]
                        # I put spanlen=1 below
                        key = tuple([self.min_gap_size, i, 1, pathlen])
                        frontier_nodes = []
                        if key in nodes_isteps_away_buffer:
                            frontier_nodes = nodes_isteps_away_buffer[key]
                        else:
                            frontier_nodes = self.get_all_nodes_isteps_away(self.min_gap_size, i, 1, pathlen, fwords, next_states, reachable_buffer)
                            nodes_isteps_away_buffer[key] = frontier_nodes
                        
                        for i, alt, pathlen in frontier_nodes:
                            new_frontier.append((k, i, input_match + (i,), alt, pathlen, xnode, phrase +(xcat,), is_shadow_path))
            frontier = new_frontier
        
        # Online rule extraction and scoring
        if self.online:
            stats = self.online_stats[ctx_name]
            f_syms = tuple(word[0][0] for word in fwords)
            for f, lex_i, lex_j in self.get_f_phrases(f_syms):
                spanlen = (lex_j - lex_i) + 1
                if not sym_isvar(f[0]):
                    spanlen += 1
                if not sym_isvar(f[1]):
                    spanlen += 1
                for e in stats.phrases_fe.get(f, ()):
                    if (f, e) not in seen_phrases:
                        # Don't add multiple instances of the same phrase here
                        seen_phrases.add((f, e))
                        scores = self.scorer.score(FeatureContext(
                                f, e, 0, 0, 0,
                                spanlen, None, None, 
                                fwords, self.fda, self.eda,
                                meta,
                                self.online_ctx_lookup(f, e, ctx_name)))
                        alignment = stats.phrases_al[f][e]
                        yield Rule(self.category, f, e, scores, alignment)
             
        stop_time = monitor_cpu()
        logger.info("Total time for rule lookup, extraction, and scoring = %f seconds", (stop_time - start_time))
        gc.collect()
        logger.info("    Extract time = %f seconds", self.extract_time)
        logger.info("    Intersect time = %f seconds", self.intersect_time)


    cdef int find_fixpoint(self, 
                        int f_low, f_high, 
                        int* f_links_low, int* f_links_high, 
                        int* e_links_low, int* e_links_high,
                        int e_in_low, int e_in_high, 
                        int* e_low, int* e_high,
                        int* f_back_low, int* f_back_high, 
                        int f_sent_len, int e_sent_len,
                        int max_f_len, int max_e_len, 
                        int min_fx_size, int min_ex_size,
                        int max_new_x,
                        int allow_low_x, int allow_high_x, 
                        int allow_arbitrary_x, int write_log):
        cdef int e_low_prev, e_high_prev, f_low_prev, f_high_prev, new_x, new_low_x, new_high_x

        e_low[0] = e_in_low
        e_high[0] = e_in_high
        self.find_projection(f_low, f_high, f_links_low, f_links_high, e_low, e_high)
        if e_low[0] == -1:
            # low-priority corner case: if phrase w is unaligned,
            # but we don't require aligned terminals, then returning
            # an error here might prevent extraction of allowed
            # rule X -> X_1 w X_2 / X_1 X_2.    This is probably
            # not worth the bother, though.
            return 0
        elif e_in_low != -1 and e_low[0] != e_in_low:
            if e_in_low - e_low[0] < min_ex_size:
                e_low[0] = e_in_low - min_ex_size
                if e_low[0] < 0:
                    return 0

        if e_high[0] - e_low[0] > max_e_len:
            return 0
        elif e_in_high != -1 and e_high[0] != e_in_high:
            if e_high[0] - e_in_high < min_ex_size:
                e_high[0] = e_in_high + min_ex_size
                if e_high[0] > e_sent_len:
                    return 0

        f_back_low[0] = -1
        f_back_high[0] = -1
        f_low_prev = f_low
        f_high_prev = f_high
        new_x = 0
        new_low_x = 0
        new_high_x = 0

        while True:

            if f_back_low[0] == -1:
                self.find_projection(e_low[0], e_high[0], e_links_low, e_links_high, f_back_low, f_back_high)
            else:
                self.find_projection(e_low[0], e_low_prev, e_links_low, e_links_high, f_back_low, f_back_high)
                self.find_projection(e_high_prev, e_high[0], e_links_low, e_links_high, f_back_low, f_back_high)

            if f_back_low[0] > f_low:
                f_back_low[0] = f_low

            if f_back_high[0] < f_high:
                f_back_high[0] = f_high

            if f_back_low[0] == f_low_prev and f_back_high[0] == f_high_prev:
                return 1

            if allow_low_x == 0 and f_back_low[0] < f_low:
                # FAIL: f phrase is not tight
                return 0

            if f_back_high[0] - f_back_low[0] > max_f_len:
                # FAIL: f back projection is too wide
                return 0

            if allow_high_x == 0 and f_back_high[0] > f_high:
                # FAIL: extension on high side not allowed
                return 0

            if f_low != f_back_low[0]:
                if new_low_x == 0:
                    if new_x >= max_new_x:
                        # FAIL: extension required on low side violates max # of gaps
                        return 0
                    else:
                        new_x = new_x + 1
                        new_low_x = 1
                if f_low - f_back_low[0] < min_fx_size:
                    f_back_low[0] = f_low - min_fx_size
                    if f_back_high[0] - f_back_low[0] > max_f_len:
                        # FAIL: extension required on low side violates max initial length
                        return 0
                    if f_back_low[0] < 0:
                        # FAIL: extension required on low side violates sentence boundary
                        return 0

            if f_high != f_back_high[0]:
                if new_high_x == 0:
                    if new_x >= max_new_x:
                        # FAIL: extension required on high side violates max # of gaps
                        return 0
                    else:
                        new_x = new_x + 1
                        new_high_x = 1
                if f_back_high[0] - f_high < min_fx_size:
                    f_back_high[0] = f_high + min_fx_size
                    if f_back_high[0] - f_back_low[0] > max_f_len:
                        # FAIL: extension required on high side violates max initial length
                        return 0
                    if f_back_high[0] > f_sent_len:
                        # FAIL: extension required on high side violates sentence boundary
                        return 0

            e_low_prev = e_low[0]
            e_high_prev = e_high[0]

            self.find_projection(f_back_low[0], f_low_prev, f_links_low, f_links_high, e_low, e_high)
            self.find_projection(f_high_prev, f_back_high[0], f_links_low, f_links_high, e_low, e_high)
            if e_low[0] == e_low_prev and e_high[0] == e_high_prev:
                return 1
            if allow_arbitrary_x == 0:
                # FAIL: arbitrary expansion not permitted
                return 0
            if e_high[0] - e_low[0] > max_e_len:
                # FAIL: re-projection violates sentence max phrase length
                return 0
            f_low_prev = f_back_low[0]
            f_high_prev = f_back_high[0]


    cdef find_projection(self, int in_low, int in_high, int* in_links_low, int* in_links_high, 
                        int* out_low, int* out_high):
        cdef int i
        for i from in_low <= i < in_high:
            if in_links_low[i] != -1:
                if out_low[0] == -1 or in_links_low[i] < out_low[0]:
                    out_low[0] = in_links_low[i]
                if out_high[0] == -1 or in_links_high[i] > out_high[0]:
                    out_high[0] = in_links_high[i]


    cdef int* int_arr_extend(self, int* arr, int* arr_len, int* data, int data_len):
        cdef int new_len
        new_len = arr_len[0] + data_len
        arr = <int*> realloc(arr, new_len*sizeof(int))
        memcpy(arr+arr_len[0], data, data_len*sizeof(int))
        arr_len[0] = new_len
        return arr


    cdef extract_phrases(self, int e_low, int e_high, int* e_gap_low, int* e_gap_high, int* e_links_low, int num_gaps,
                        int f_low, int f_high, int* f_gap_low, int* f_gap_high, int* f_links_low, 
                        int sent_id, int e_sent_len, int e_sent_start):
        cdef int i, j, k, m, n, *e_gap_order, e_x_low, e_x_high, e_x_gap_low, e_x_gap_high
        cdef int *e_gaps1, *e_gaps2, len1, len2, step, num_chunks
        cdef IntList ephr_arr
        cdef result

        result = []
        len1 = 0
        e_gaps1 = <int*> malloc(0)
        ephr_arr = IntList()

        e_gap_order = <int*> malloc(num_gaps*sizeof(int))
        if num_gaps > 0:
            e_gap_order[0] = 0
            for i from 1 <= i < num_gaps:
                for j from 0 <= j < i:
                    if e_gap_low[i] < e_gap_low[j]:
                        for k from j <= k < i:
                            e_gap_order[k+1] = e_gap_order[k]
                        e_gap_order[j] = i
                        break
                else:
                    e_gap_order[i] = i

        e_x_low = e_low
        e_x_high = e_high
        if not self.tight_phrases:
            while e_x_low > 0 and e_high - e_x_low < self.train_max_initial_size and e_links_low[e_x_low-1] == -1:
                e_x_low = e_x_low - 1
            while e_x_high < e_sent_len and e_x_high - e_low < self.train_max_initial_size and e_links_low[e_x_high] == -1:
                e_x_high = e_x_high + 1

        for i from e_x_low <= i <= e_low:
            e_gaps1 = self.int_arr_extend(e_gaps1, &len1, &i, 1)

        for i from 0 <= i < num_gaps:
            e_gaps2 = <int*> malloc(0)
            len2 = 0

            j = e_gap_order[i]
            e_x_gap_low = e_gap_low[j]
            e_x_gap_high = e_gap_high[j]
            if not self.tight_phrases:
                while e_x_gap_low > e_x_low and e_links_low[e_x_gap_low-1] == -1:
                    e_x_gap_low = e_x_gap_low - 1
                while e_x_gap_high < e_x_high and e_links_low[e_x_gap_high] == -1:
                    e_x_gap_high = e_x_gap_high + 1

            k = 0
            step = 1+(i*2)
            while k < len1:
                for m from e_x_gap_low <= m <= e_gap_low[j]:
                    if m >= e_gaps1[k+step-1]:
                        for n from e_gap_high[j] <= n <= e_x_gap_high:
                            if n-m >= 1:    # extractor.py doesn't restrict target-side gap length 
                                e_gaps2 = self.int_arr_extend(e_gaps2, &len2, e_gaps1+k, step)
                                e_gaps2 = self.int_arr_extend(e_gaps2, &len2, &m, 1)
                                e_gaps2 = self.int_arr_extend(e_gaps2, &len2, &n, 1)
                k = k + step
            free(e_gaps1)
            e_gaps1 = e_gaps2
            len1 = len2

        step = 1+(num_gaps*2)
        e_gaps2 = <int*> malloc(0)
        len2 = 0
        for i from e_high <= i <= e_x_high:
            j = 0
            while j < len1:
                if i - e_gaps1[j] <= self.train_max_initial_size and i >= e_gaps1[j+step-1]:
                    e_gaps2 = self.int_arr_extend(e_gaps2, &len2, e_gaps1+j, step)
                    e_gaps2 = self.int_arr_extend(e_gaps2, &len2, &i, 1)
                j = j + step
        free(e_gaps1)
        e_gaps1 = e_gaps2
        len1 = len2

        step = (num_gaps+1)*2
        i = 0
        
        cdef IntList indexes
        while i < len1:
            ephr_arr._clear()
            num_chunks = 0
            indexes = IntList()
            for j from 0 <= j < num_gaps+1:
                if e_gaps1[i+2*j] < e_gaps1[i+(2*j)+1]:
                    num_chunks = num_chunks + 1
                for k from e_gaps1[i+2*j] <= k < e_gaps1[i+(2*j)+1]:
                    indexes.append(k)
                    ephr_arr._append(self.eid2symid[self.eda.data.arr[e_sent_start+k]])
                if j < num_gaps:
                    indexes.append(sym_setindex(self.category, e_gap_order[j]+1))
                    ephr_arr._append(sym_setindex(self.category, e_gap_order[j]+1))
            i = i + step
            if ephr_arr.len <= self.max_target_length and num_chunks <= self.max_target_chunks:
                result.append((Phrase(ephr_arr),indexes))

        free(e_gaps1)
        free(e_gap_order)
        return result

    cdef IntList create_alignments(self, int* sent_links, int num_links,
            IntList findexes, IntList eindexes):
        cdef unsigned i
        cdef IntList ret = IntList()
        for i in range(findexes.len):
            s = findexes.arr[i]
            if s < 0: continue
            idx = 0
            while idx < num_links * 2:
                if sent_links[idx] == s:
                    j = eindexes.index(sent_links[idx+1])
                    ret.append(i * ALIGNMENT_CODE + j)
                idx += 2
        return ret
                
    cdef extract(self, Phrase phrase, Matching* matching, int* chunklen, int num_chunks):
        cdef int* sent_links, *e_links_low, *e_links_high, *f_links_low, *f_links_high
        cdef int *f_gap_low, *f_gap_high, *e_gap_low, *e_gap_high, num_gaps, gap_start
        cdef int i, j, k, e_i, f_i, num_links, num_aligned_chunks, met_constraints, x
        cdef int f_low, f_high, e_low, e_high, f_back_low, f_back_high
        cdef int e_sent_start, e_sent_end, f_sent_start, f_sent_end, e_sent_len, f_sent_len
        cdef int e_word_count, f_x_low, f_x_high, e_x_low, e_x_high, phrase_len
        cdef float pair_count
        cdef extracts, phrase_list
        cdef IntList fphr_arr
        cdef Phrase fphr
        cdef reason_for_failure

        fphr_arr = IntList()
        phrase_len = phrase.n
        extracts = []
        sent_links = self.alignment._get_sent_links(matching.sent_id, &num_links)

        e_sent_start = self.eda.sent_index.arr[matching.sent_id]
        e_sent_end = self.eda.sent_index.arr[matching.sent_id+1]
        e_sent_len = e_sent_end - e_sent_start - 1
        f_sent_start = self.fda.sent_index.arr[matching.sent_id]
        f_sent_end = self.fda.sent_index.arr[matching.sent_id+1]
        f_sent_len = f_sent_end - f_sent_start - 1

        self.findexes1.reset()
        sofar = 0
        for i in range(num_chunks):
            for j in range(chunklen[i]):
                self.findexes1.append(matching.arr[matching.start+i]+j-f_sent_start);
                sofar += 1
            if i+1 < num_chunks:
                self.findexes1.append(phrase[sofar])
                sofar += 1
            

        e_links_low = <int*> malloc(e_sent_len*sizeof(int))
        e_links_high = <int*> malloc(e_sent_len*sizeof(int))
        f_links_low = <int*> malloc(f_sent_len*sizeof(int))
        f_links_high = <int*> malloc(f_sent_len*sizeof(int))
        f_gap_low = <int*> malloc((num_chunks+1)*sizeof(int))
        f_gap_high = <int*> malloc((num_chunks+1)*sizeof(int))
        e_gap_low = <int*> malloc((num_chunks+1)*sizeof(int))
        e_gap_high = <int*> malloc((num_chunks+1)*sizeof(int))
        memset(f_gap_low, 0, (num_chunks+1)*sizeof(int))
        memset(f_gap_high, 0, (num_chunks+1)*sizeof(int))
        memset(e_gap_low, 0, (num_chunks+1)*sizeof(int))
        memset(e_gap_high, 0, (num_chunks+1)*sizeof(int))

        reason_for_failure = ""

        for i from 0 <= i < e_sent_len:
            e_links_low[i] = -1
            e_links_high[i] = -1
        for i from 0 <= i < f_sent_len:
            f_links_low[i] = -1
            f_links_high[i] = -1

        # this is really inefficient -- might be good to 
        # somehow replace with binary search just for the f
        # links that we care about (but then how to look up 
        # when we want to check something on the e side?)
        i = 0
        while i < num_links*2:
            f_i = sent_links[i]
            e_i = sent_links[i+1]
            if f_links_low[f_i] == -1 or f_links_low[f_i] > e_i:
                f_links_low[f_i] = e_i
            if f_links_high[f_i] == -1 or f_links_high[f_i] < e_i + 1:
                f_links_high[f_i] = e_i + 1
            if e_links_low[e_i] == -1 or e_links_low[e_i] > f_i:
                e_links_low[e_i] = f_i
            if e_links_high[e_i] == -1 or e_links_high[e_i] < f_i + 1:
                e_links_high[e_i] = f_i + 1
            i = i + 2
        
        als = []
        for x in range(matching.start,matching.end):
            al = (matching.arr[x]-f_sent_start,f_links_low[matching.arr[x]-f_sent_start])
            als.append(al)
        # check all source-side alignment constraints
        met_constraints = 1
        if self.require_aligned_terminal:
            num_aligned_chunks = 0
            for i from 0 <= i < num_chunks:
                for j from 0 <= j < chunklen[i]:
                    if f_links_low[matching.arr[matching.start+i]+j-f_sent_start] > -1:
                        num_aligned_chunks = num_aligned_chunks + 1
                        break
            if num_aligned_chunks == 0:
                reason_for_failure = "No aligned terminals"
                met_constraints = 0
            if self.require_aligned_chunks and num_aligned_chunks < num_chunks:
                reason_for_failure = "Unaligned chunk"
                met_constraints = 0

        if met_constraints and self.tight_phrases:
            # outside edge constraints are checked later
            for i from 0 <= i < num_chunks-1:
                if f_links_low[matching.arr[matching.start+i]+chunklen[i]-f_sent_start] == -1:
                    reason_for_failure = "Gaps are not tight phrases"
                    met_constraints = 0
                    break
                if f_links_low[matching.arr[matching.start+i+1]-1-f_sent_start] == -1:
                    reason_for_failure = "Gaps are not tight phrases"
                    met_constraints = 0
                    break

        f_low = matching.arr[matching.start] - f_sent_start
        f_high = matching.arr[matching.start + matching.size - 1] + chunklen[num_chunks-1] - f_sent_start
        if met_constraints:

            if self.find_fixpoint(f_low, f_high, f_links_low, f_links_high, e_links_low, e_links_high, 
                                -1, -1, &e_low, &e_high, &f_back_low, &f_back_high, f_sent_len, e_sent_len,
                                self.train_max_initial_size, self.train_max_initial_size, 
                                self.train_min_gap_size, 0,
                                self.max_nonterminals - num_chunks + 1, 1, 1, 0, 0):
                gap_error = 0
                num_gaps = 0

                if f_back_low < f_low:
                    f_gap_low[0] = f_back_low
                    f_gap_high[0] = f_low
                    num_gaps = 1
                    gap_start = 0
                    phrase_len = phrase_len+1
                    if phrase_len > self.max_length:
                        gap_error = 1
                    if self.tight_phrases:
                        if f_links_low[f_back_low] == -1 or f_links_low[f_low-1] == -1:
                            gap_error = 1
                            reason_for_failure = "Inside edges of preceding subphrase are not tight"
                else:
                    gap_start = 1
                    if self.tight_phrases and f_links_low[f_low] == -1:
                        # this is not a hard error.    we can't extract this phrase
                        # but we still might be able to extract a superphrase
                        met_constraints = 0

                for i from 0 <= i < matching.size - 1:
                    f_gap_low[1+i] = matching.arr[matching.start+i] + chunklen[i] - f_sent_start
                    f_gap_high[1+i] = matching.arr[matching.start+i+1] - f_sent_start
                    num_gaps = num_gaps + 1

                if f_high < f_back_high:
                    f_gap_low[gap_start+num_gaps] = f_high
                    f_gap_high[gap_start+num_gaps] = f_back_high
                    num_gaps = num_gaps + 1
                    phrase_len = phrase_len+1
                    if phrase_len > self.max_length:
                        gap_error = 1
                    if self.tight_phrases:
                        if f_links_low[f_back_high-1] == -1 or f_links_low[f_high] == -1:
                            gap_error = 1
                            reason_for_failure = "Inside edges of following subphrase are not tight"
                else:
                    if self.tight_phrases and f_links_low[f_high-1] == -1:
                        met_constraints = 0

                if gap_error == 0:
                    e_word_count = e_high - e_low
                    for i from 0 <= i < num_gaps: # check integrity of subphrases
                        if self.find_fixpoint(f_gap_low[gap_start+i], f_gap_high[gap_start+i], 
                                            f_links_low, f_links_high, e_links_low, e_links_high,
                                            -1, -1, e_gap_low+gap_start+i, e_gap_high+gap_start+i, 
                                            f_gap_low+gap_start+i, f_gap_high+gap_start+i,
                                            f_sent_len, e_sent_len, 
                                            self.train_max_initial_size, self.train_max_initial_size, 
                                            0, 0, 0, 0, 0, 0, 0) == 0:
                            gap_error = 1
                            reason_for_failure = "Subphrase [%d, %d] failed integrity check" % (f_gap_low[gap_start+i], f_gap_high[gap_start+i])
                            break

                if gap_error == 0:
                    i = 1
                    self.findexes.reset()
                    if f_back_low < f_low:
                        fphr_arr._append(sym_setindex(self.category, i))
                        i = i+1
                        self.findexes.append(sym_setindex(self.category, i))
                    self.findexes.extend(self.findexes1)
                    for j from 0 <= j < phrase.n:
                        if sym_isvar(phrase.syms[j]):
                            fphr_arr._append(sym_setindex(self.category, i))
                            i = i + 1
                        else:
                            fphr_arr._append(phrase.syms[j])
                    if f_back_high > f_high:
                        fphr_arr._append(sym_setindex(self.category, i))
                        self.findexes.append(sym_setindex(self.category, i))

                    fphr = Phrase(fphr_arr)
                    if met_constraints:
                        phrase_list = self.extract_phrases(e_low, e_high, e_gap_low + gap_start, e_gap_high + gap_start, e_links_low, num_gaps,
                                            f_back_low, f_back_high, f_gap_low + gap_start, f_gap_high + gap_start, f_links_low,
                                            matching.sent_id, e_sent_len, e_sent_start)
                        if len(phrase_list) > 0:
                            pair_count = 1.0 / len(phrase_list)
                        else:
                            pair_count = 0
                            reason_for_failure = "Didn't extract anything from [%d, %d] -> [%d, %d]" % (f_back_low, f_back_high, e_low, e_high)
                        for phrase2, eindexes in phrase_list:
                            als1 = self.create_alignments(sent_links,num_links,self.findexes,eindexes)        
                            extracts.append((fphr, phrase2, pair_count, tuple(als1)))
                    if (num_gaps < self.max_nonterminals and
                        phrase_len < self.max_length and
                        f_back_high - f_back_low + self.train_min_gap_size <= self.train_max_initial_size):
                        if (f_back_low == f_low and 
                                f_low >= self.train_min_gap_size and
                                ((not self.tight_phrases) or (f_links_low[f_low-1] != -1 and f_links_low[f_back_high-1] != -1))):
                            f_x_low = f_low-self.train_min_gap_size
                            met_constraints = 1
                            if self.tight_phrases:
                                while f_x_low >= 0 and f_links_low[f_x_low] == -1:
                                    f_x_low = f_x_low - 1
                            if f_x_low < 0 or f_back_high - f_x_low > self.train_max_initial_size:
                                met_constraints = 0

                            if (met_constraints and
                                (self.find_fixpoint(f_x_low, f_back_high,
                                            f_links_low, f_links_high, e_links_low, e_links_high, 
                                            e_low, e_high, &e_x_low, &e_x_high, &f_x_low, &f_x_high, 
                                            f_sent_len, e_sent_len, 
                                            self.train_max_initial_size, self.train_max_initial_size, 
                                            1, 1, 1, 1, 0, 1, 0) == 1) and
                                ((not self.tight_phrases) or f_links_low[f_x_low] != -1) and
                                self.find_fixpoint(f_x_low, f_low,    # check integrity of new subphrase
                                            f_links_low, f_links_high, e_links_low, e_links_high,
                                            -1, -1, e_gap_low, e_gap_high, f_gap_low, f_gap_high, 
                                            f_sent_len, e_sent_len,
                                            self.train_max_initial_size, self.train_max_initial_size,
                                            0, 0, 0, 0, 0, 0, 0)):
                                fphr_arr._clear()
                                i = 1
                                self.findexes.reset()
                                self.findexes.append(sym_setindex(self.category, i))
                                fphr_arr._append(sym_setindex(self.category, i))
                                i = i+1
                                self.findexes.extend(self.findexes1)
                                for j from 0 <= j < phrase.n:
                                    if sym_isvar(phrase.syms[j]):
                                        fphr_arr._append(sym_setindex(self.category, i))
                                        i = i + 1
                                    else:
                                        fphr_arr._append(phrase.syms[j])
                                if f_back_high > f_high:
                                    fphr_arr._append(sym_setindex(self.category, i))
                                    self.findexes.append(sym_setindex(self.category, i))
                                fphr = Phrase(fphr_arr)
                                phrase_list = self.extract_phrases(e_x_low, e_x_high, e_gap_low, e_gap_high, e_links_low, num_gaps+1,
                                                    f_x_low, f_x_high, f_gap_low, f_gap_high, f_links_low, matching.sent_id, 
                                                    e_sent_len, e_sent_start)
                                if len(phrase_list) > 0:
                                    pair_count = 1.0 / len(phrase_list)
                                else:
                                    pair_count = 0
                                for phrase2, eindexes in phrase_list:
                                    als2 = self.create_alignments(sent_links,num_links,self.findexes,eindexes)        
                                    extracts.append((fphr, phrase2, pair_count, tuple(als2)))

                        if (f_back_high == f_high and 
                            f_sent_len - f_high >= self.train_min_gap_size and
                            ((not self.tight_phrases) or (f_links_low[f_high] != -1 and f_links_low[f_back_low] != -1))):
                            f_x_high = f_high+self.train_min_gap_size
                            met_constraints = 1
                            if self.tight_phrases:
                                while f_x_high <= f_sent_len and f_links_low[f_x_high-1] == -1:
                                    f_x_high = f_x_high + 1
                            if f_x_high > f_sent_len or f_x_high - f_back_low > self.train_max_initial_size:
                                met_constraints = 0
                            
                            if (met_constraints and 
                                self.find_fixpoint(f_back_low, f_x_high, 
                                            f_links_low, f_links_high, e_links_low, e_links_high,
                                            e_low, e_high, &e_x_low, &e_x_high, &f_x_low, &f_x_high, 
                                            f_sent_len, e_sent_len, 
                                            self.train_max_initial_size, self.train_max_initial_size, 
                                            1, 1, 1, 0, 1, 1, 0) and
                                ((not self.tight_phrases) or f_links_low[f_x_high-1] != -1) and
                                self.find_fixpoint(f_high, f_x_high,
                                            f_links_low, f_links_high, e_links_low, e_links_high,
                                            -1, -1, e_gap_low+gap_start+num_gaps, e_gap_high+gap_start+num_gaps, 
                                            f_gap_low+gap_start+num_gaps, f_gap_high+gap_start+num_gaps, 
                                            f_sent_len, e_sent_len,
                                            self.train_max_initial_size, self.train_max_initial_size,
                                            0, 0, 0, 0, 0, 0, 0)):
                                fphr_arr._clear()
                                i = 1
                                self.findexes.reset()
                                if f_back_low < f_low:
                                    fphr_arr._append(sym_setindex(self.category, i))
                                    i = i+1
                                    self.findexes.append(sym_setindex(self.category, i))
                                self.findexes.extend(self.findexes1)
                                for j from 0 <= j < phrase.n:
                                    if sym_isvar(phrase.syms[j]):
                                        fphr_arr._append(sym_setindex(self.category, i))
                                        i = i + 1
                                    else:
                                        fphr_arr._append(phrase.syms[j])
                                fphr_arr._append(sym_setindex(self.category, i))
                                self.findexes.append(sym_setindex(self.category, i))
                                fphr = Phrase(fphr_arr)
                                phrase_list = self.extract_phrases(e_x_low, e_x_high, e_gap_low+gap_start, e_gap_high+gap_start, e_links_low, num_gaps+1,
                                                    f_x_low, f_x_high, f_gap_low+gap_start, f_gap_high+gap_start, f_links_low, 
                                                    matching.sent_id, e_sent_len, e_sent_start)
                                if len(phrase_list) > 0:
                                    pair_count = 1.0 / len(phrase_list)
                                else:
                                    pair_count = 0
                                for phrase2, eindexes in phrase_list:
                                    als3 = self.create_alignments(sent_links,num_links,self.findexes,eindexes)        
                                    extracts.append((fphr, phrase2, pair_count, tuple(als3)))
                        if (num_gaps < self.max_nonterminals - 1 and 
                            phrase_len+1 < self.max_length and
                            f_back_high == f_high and 
                            f_back_low == f_low and 
                            f_back_high - f_back_low + (2*self.train_min_gap_size) <= self.train_max_initial_size and
                            f_low >= self.train_min_gap_size and
                            f_high <= f_sent_len - self.train_min_gap_size and
                            ((not self.tight_phrases) or (f_links_low[f_low-1] != -1 and f_links_low[f_high] != -1))):

                            met_constraints = 1
                            f_x_low = f_low-self.train_min_gap_size
                            if self.tight_phrases:
                                while f_x_low >= 0 and f_links_low[f_x_low] == -1:
                                    f_x_low = f_x_low - 1
                            if f_x_low < 0:
                                met_constraints = 0

                            f_x_high = f_high+self.train_min_gap_size
                            if self.tight_phrases:
                                while f_x_high <= f_sent_len and f_links_low[f_x_high-1] == -1:
                                    f_x_high = f_x_high + 1
                            if f_x_high > f_sent_len or f_x_high - f_x_low > self.train_max_initial_size:
                                met_constraints = 0

                            if (met_constraints and
                                (self.find_fixpoint(f_x_low, f_x_high,
                                                f_links_low, f_links_high, e_links_low, e_links_high,
                                                e_low, e_high, &e_x_low, &e_x_high, &f_x_low, &f_x_high, 
                                                f_sent_len, e_sent_len,
                                                self.train_max_initial_size, self.train_max_initial_size, 
                                                1, 1, 2, 1, 1, 1, 1) == 1) and
                                ((not self.tight_phrases) or (f_links_low[f_x_low] != -1 and f_links_low[f_x_high-1] != -1)) and
                                self.find_fixpoint(f_x_low, f_low,
                                                f_links_low, f_links_high, e_links_low, e_links_high,
                                                -1, -1, e_gap_low, e_gap_high, f_gap_low, f_gap_high, 
                                                f_sent_len, e_sent_len,
                                                self.train_max_initial_size, self.train_max_initial_size,
                                                0, 0, 0, 0, 0, 0, 0) and
                                self.find_fixpoint(f_high, f_x_high,
                                                f_links_low, f_links_high, e_links_low, e_links_high,
                                                -1, -1, e_gap_low+1+num_gaps, e_gap_high+1+num_gaps, 
                                                f_gap_low+1+num_gaps, f_gap_high+1+num_gaps, 
                                                f_sent_len, e_sent_len,
                                                self.train_max_initial_size, self.train_max_initial_size,
                                                0, 0, 0, 0, 0, 0, 0)):
                                fphr_arr._clear()
                                i = 1
                                self.findexes.reset()
                                self.findexes.append(sym_setindex(self.category, i))
                                fphr_arr._append(sym_setindex(self.category, i))
                                i = i+1
                                self.findexes.extend(self.findexes1)
                                for j from 0 <= j < phrase.n:
                                    if sym_isvar(phrase.syms[j]):
                                        fphr_arr._append(sym_setindex(self.category, i))
                                        i = i + 1
                                    else:
                                        fphr_arr._append(phrase.syms[j])
                                fphr_arr._append(sym_setindex(self.category, i))
                                self.findexes.append(sym_setindex(self.category, i))
                                fphr = Phrase(fphr_arr)
                                phrase_list = self.extract_phrases(e_x_low, e_x_high, e_gap_low, e_gap_high, e_links_low, num_gaps+2,
                                                    f_x_low, f_x_high, f_gap_low, f_gap_high, f_links_low, 
                                                    matching.sent_id, e_sent_len, e_sent_start)
                                if len(phrase_list) > 0:
                                    pair_count = 1.0 / len(phrase_list)
                                else:
                                    pair_count = 0
                                for phrase2, eindexes in phrase_list:
                                    als4 = self.create_alignments(sent_links,num_links,self.findexes,eindexes)        
                                    extracts.append((fphr, phrase2, pair_count, tuple(als4)))
            else:
                reason_for_failure = "Unable to extract basic phrase"

        free(sent_links)
        free(f_links_low)
        free(f_links_high)
        free(e_links_low)
        free(e_links_high)
        free(f_gap_low)
        free(f_gap_high)
        free(e_gap_low)
        free(e_gap_high)

        return extracts

    #
    # Online grammar extraction handling
    #
    
    # Aggregate stats from a training instance
    # (Extract rules, update counts)
    def add_instance(self, f_words, e_words, alignment, ctx_name=None):

        self.online = True
        
        # Rules extracted from this instance
        # Track span of lexical items (terminals) to make
        # sure we don't extract the same rule for the same
        # span more than once.
        # (f, e, al, lex_f_i, lex_f_j)
        rules = set()

        f_len = len(f_words)
        e_len = len(e_words)

        # Pre-compute alignment info
        al = [[] for i in range(f_len)]
        fe_span = [[e_len + 1, -1] for i in range(f_len)]
        ef_span = [[f_len + 1, -1] for i in range(e_len)]
        for f, e in alignment:
            al[f].append(e)
            fe_span[f][0] = min(fe_span[f][0], e)
            fe_span[f][1] = max(fe_span[f][1], e)
            ef_span[e][0] = min(ef_span[e][0], f)
            ef_span[e][1] = max(ef_span[e][1], f)

        # Target side word coverage
        cover = [0] * e_len
        # Non-terminal coverage
        f_nt_cover = [0] * f_len
        e_nt_cover = [0] * e_len
        
        # Extract all possible hierarchical phrases starting at a source index
        # f_ i and j are current, e_ i and j are previous
        # We care _considering_ f_j, so it is not yet in counts
        def extract(f_i, f_j, e_i, e_j, min_bound, wc, links, nt, nt_open):
            # Phrase extraction limits
            if f_j > (f_len - 1) or (f_j - f_i) + 1 > self.max_initial_size:
                return
            # Unaligned word
            if not al[f_j]:
                # Adjacent to non-terminal: extend (non-terminal now open)
                if nt and nt[-1][2] == f_j - 1:
                    nt[-1][2] += 1
                    extract(f_i, f_j + 1, e_i, e_j, min_bound, wc, links, nt, True)
                    nt[-1][2] -= 1
                # Unless non-terminal already open, always extend with word
                # Make sure adding a word doesn't exceed length
                if not nt_open and wc < self.max_length:
                    extract(f_i, f_j + 1, e_i, e_j, min_bound, wc + 1, links, nt, False)
                return
            # Aligned word
            link_i = fe_span[f_j][0]
            link_j = fe_span[f_j][1]
            new_e_i = min(link_i, e_i)
            new_e_j = max(link_j, e_j)
            # Check reverse links of newly covered words to see if they violate left
            # bound (return) or extend minimum right bound for chunk
            new_min_bound = min_bound
            # First aligned word creates span
            if e_j == -1: 
                for i from new_e_i <= i <= new_e_j:
                    if ef_span[i][0] < f_i:
                        return
                    new_min_bound = max(new_min_bound, ef_span[i][1])
            # Other aligned words extend span
            else:
                for i from new_e_i <= i < e_i:
                    if ef_span[i][0] < f_i:
                        return
                    new_min_bound = max(new_min_bound, ef_span[i][1])
                for i from e_j < i <= new_e_j:
                    if ef_span[i][0] < f_i:
                        return
                    new_min_bound = max(new_min_bound, ef_span[i][1])
            # Extract, extend with word (unless non-terminal open)
            if not nt_open:
                nt_collision = False
                for link in al[f_j]:
                    if e_nt_cover[link]:
                        nt_collision = True
                # Non-terminal collisions block word extraction and extension, but
                # may be okay for continuing non-terminals
                if not nt_collision and wc < self.max_length:
                    plus_links = []
                    for link in al[f_j]:
                        plus_links.append((f_j, link))
                        cover[link] += 1
                    links.append(plus_links)
                    if links and f_j >= new_min_bound:
                        rules.add(self.form_rule(f_i, new_e_i, f_words[f_i:f_j + 1], e_words[new_e_i:new_e_j + 1], nt, links))
                    extract(f_i, f_j + 1, new_e_i, new_e_j, new_min_bound, wc + 1, links, nt, False)
                    links.pop()
                    for link in al[f_j]:
                        cover[link] -= 1
            # Try to add a word to current non-terminal (if any), extract, extend
            if nt and nt[-1][2] == f_j - 1:
                # Add to non-terminal, checking for collisions
                old_last_nt = nt[-1][:]
                nt[-1][2] = f_j
                if link_i < nt[-1][3]:
                    if not span_check(cover, link_i, nt[-1][3] - 1):
                        nt[-1] = old_last_nt
                        return
                    span_inc(cover, link_i, nt[-1][3] - 1)
                    span_inc(e_nt_cover, link_i, nt[-1][3] - 1)
                    nt[-1][3] = link_i
                if link_j > nt[-1][4]:
                    if not span_check(cover, nt[-1][4] + 1, link_j):
                        nt[-1] = old_last_nt
                        return
                    span_inc(cover, nt[-1][4] + 1, link_j)
                    span_inc(e_nt_cover, nt[-1][4] + 1, link_j)
                    nt[-1][4] = link_j
                if links and f_j >= new_min_bound:
                    rules.add(self.form_rule(f_i, new_e_i, f_words[f_i:f_j + 1], e_words[new_e_i:new_e_j + 1], nt, links))
                extract(f_i, f_j + 1, new_e_i, new_e_j, new_min_bound, wc, links, nt, False)
                nt[-1] = old_last_nt
                if link_i < nt[-1][3]:
                    span_dec(cover, link_i, nt[-1][3] - 1)
                    span_dec(e_nt_cover, link_i, nt[-1][3] - 1)
                if link_j > nt[-1][4]:
                    span_dec(cover, nt[-1][4] + 1, link_j)
                    span_dec(e_nt_cover, nt[-1][4] + 1, link_j)
            # Try to start a new non-terminal, extract, extend
            if (not nt or f_j - nt[-1][2] > 1) and wc < self.max_length and len(nt) < self.max_nonterminals:
                # Check for collisions
                if not span_check(cover, link_i, link_j):
                    return
                span_inc(cover, link_i, link_j)
                span_inc(e_nt_cover, link_i, link_j)
                nt.append([(nt[-1][0] + 1) if nt else 1, f_j, f_j, link_i, link_j])
                # Require at least one word in phrase
                if links and f_j >= new_min_bound:
                    rules.add(self.form_rule(f_i, new_e_i, f_words[f_i:f_j + 1], e_words[new_e_i:new_e_j + 1], nt, links))
                extract(f_i, f_j + 1, new_e_i, new_e_j, new_min_bound, wc + 1, links, nt, False)
                nt.pop()
                span_dec(cover, link_i, link_j)
                span_dec(e_nt_cover, link_i, link_j)

        # Try to extract phrases from every f index
        for f_i from 0 <= f_i < f_len:
            # Skip if phrases won't be tight on left side
            if not al[f_i]:
                continue
            extract(f_i, f_i, f_len + 1, -1, f_i, 0, [], [], False)
        
        stats = self.online_stats[ctx_name]

        # Update possible phrases (samples)
        # This could be more efficiently integrated with extraction
        # at the cost of readability
        for f, lex_i, lex_j in self.get_f_phrases(f_words):
            stats.samples_f[f] += 1
            
        # Update phrase counts
        for rule in rules:
            (f_ph, e_ph, al) = rule[:3]
            stats.phrases_f[f_ph] += 1
            stats.phrases_e[e_ph] += 1
            stats.phrases_fe[f_ph][e_ph] += 1
            if not stats.phrases_al[f_ph][e_ph]:
                stats.phrases_al[f_ph][e_ph] = al
            
        # Update Bilexical counts
        aligned_fe = [list() for _ in range(len(f_words))]
        aligned_ef = [list() for _ in range(len(e_words))]
        null_word = sym_fromstring('NULL', True)
        for (i, j) in alignment:
            aligned_fe[i].append(j)
            aligned_ef[j].append(i)
        for f_i in range(len(f_words)):
            stats.bilex_f[f_words[f_i]] += 1
            e_i_aligned = aligned_fe[f_i]
            lc = len(e_i_aligned)
            if lc > 0:
                for e_i in e_i_aligned:
                    stats.bilex_fe[f_words[f_i]][e_words[e_i]] += (1.0) / lc
            else:
                stats.bilex_fe[f_words[f_i]][null_word] += 1
        for e_i in range(len(e_words)):
            stats.bilex_e[e_words[e_i]] += 1
            f_i_aligned = aligned_ef[e_i]
            lc = len(f_i_aligned)
            if lc > 0:
                for f_i in f_i_aligned:
                    stats.bilex_ef[e_words[e_i]][f_words[f_i]] += (1.0) / lc
            else:
                stats.bilex_ef[e_words[e_i]][null_word] += 1

    # Create a rule from source, target, non-terminals, and alignments
    def form_rule(self, f_i, e_i, f_span, e_span, nt, al):
    
        # Substitute in non-terminals
        nt_inv = sorted(nt, cmp=lambda x, y: cmp(x[3], y[3]))
        f_sym = list(f_span[:])
        off = f_i
        for next_nt in nt:
            nt_len = (next_nt[2] - next_nt[1]) + 1
            i = 0
            while i < nt_len:
                f_sym.pop(next_nt[1] - off)
                i += 1
            f_sym.insert(next_nt[1] - off, sym_setindex(self.category, next_nt[0]))
            off += (nt_len - 1)
        e_sym = list(e_span[:])
        off = e_i
        for next_nt in nt_inv:
            nt_len = (next_nt[4] - next_nt[3]) + 1
            i = 0
            while i < nt_len:
                e_sym.pop(next_nt[3] - off)
                i += 1
            e_sym.insert(next_nt[3] - off, sym_setindex(self.category, next_nt[0]))
            off += (nt_len - 1)
    
        # Adjusting alignment links takes some doing
        links = [list(link) for sub in al for link in sub]
        links_inv = sorted(links, cmp=lambda x, y: cmp(x[1], y[1]))
        links_len = len(links)
        nt_len = len(nt)
        nt_i = 0
        off = f_i
        i = 0
        while i < links_len:
            while nt_i < nt_len and links[i][0] > nt[nt_i][1]:
                off += (nt[nt_i][2] - nt[nt_i][1])
                nt_i += 1
            links[i][0] -= off
            i += 1
        nt_i = 0
        off = e_i
        i = 0
        while i < links_len:
            while nt_i < nt_len and links_inv[i][1] > nt_inv[nt_i][3]:
                off += (nt_inv[nt_i][4] - nt_inv[nt_i][3])
                nt_i += 1
            links_inv[i][1] -= off
            i += 1
        
        # Find lexical span
        lex_f_i = f_i
        lex_f_j = f_i + (len(f_span) - 1)
        if nt:
            if nt[0][1] == lex_f_i:
                lex_f_i += (nt[0][2] - nt[0][1]) + 1
            if nt[-1][2] == lex_f_j:
                lex_f_j -= (nt[-1][2] - nt[-1][1]) + 1

        # Create rule (f_phrase, e_phrase, links, f_link_min, f_link_max)
        f = Phrase(f_sym)
        e = Phrase(e_sym)
        a = tuple(self.alignment.link(i, j) for i, j in links)
        return (f, e, a, lex_f_i, lex_f_j)

    # Rule string from rule
    def fmt_rule(self, f, e, a):
        a_str = ' '.join('{0}-{1}'.format(*self.alignment.unlink(packed)) for packed in a)
        return '[X] ||| {0} ||| {1} ||| {2}'.format(f, e, a_str)
    
    # Lookup online stats for phrase pair (f, e).  Return None if no match.
    # IMPORTANT: use get() to avoid adding items to defaultdict
    def online_ctx_lookup(self, f, e, ctx_name=None):
        if self.online:
            stats = self.online_stats[ctx_name]
            fcount = stats.phrases_f.get(f, 0)
            fsample_count = stats.samples_f.get(f, 0)
            d = stats.phrases_fe.get(f, None)
            paircount = d.get(e, 0) if d else 0
            return OnlineFeatureContext(fcount, fsample_count, paircount, stats.bilex_f, stats.bilex_e, stats.bilex_fe)
        return None
    
    # Find all phrases that we might try to extract
    # (Used for EGivenFCoherent)
    # Return set of (fphrase, lex_i, lex_j)
    def get_f_phrases(self, f_words):

        f_len = len(f_words)
        phrases = set() # (fphrase, lex_i, lex_j)
        
        def extract(f_i, f_j, lex_i, lex_j, wc, ntc, syms):
            # Phrase extraction limits
            if f_j > (f_len - 1) or (f_j - f_i) + 1 > self.max_initial_size:
                return
            # Extend with word
            if wc + ntc < self.max_length:
                syms.append(f_words[f_j])
                f = Phrase(syms)
                new_lex_i = min(lex_i, f_j)
                new_lex_j = max(lex_j, f_j)
                phrases.add((f, new_lex_i, new_lex_j))
                extract(f_i, f_j + 1, new_lex_i, new_lex_j, wc + 1, ntc, syms)
                syms.pop()
            # Extend with existing non-terminal
            if syms and sym_isvar(syms[-1]):
                # Don't re-extract the same phrase
                extract(f_i, f_j + 1, lex_i, lex_j, wc, ntc, syms)
            # Extend with new non-terminal
            if wc + ntc < self.max_length:
                if not syms or (ntc < self.max_nonterminals and not sym_isvar(syms[-1])):
                    syms.append(sym_setindex(self.category, ntc + 1))
                    f = Phrase(syms)
                    if wc > 0:
                        phrases.add((f, lex_i, lex_j))
                    extract(f_i, f_j + 1, lex_i, lex_j, wc, ntc + 1, syms)
                    syms.pop()
            
        # Try to extract phrases from every f index
        for f_i from 0 <= f_i < f_len:
            extract(f_i, f_i, f_len, -1, 0, 0, [])

        return phrases

    # Drop online stats for a context
    def drop_ctx(self, ctx_name=None):
        self.online_stats.pop(ctx_name, None)
    
# Spans are _inclusive_ on both ends [i, j]
def span_check(vec, i, j):
    k = i
    while k <= j:
        if vec[k]:
            return False
        k += 1
    return True

def span_inc(vec, i, j):
    k = i
    while k <= j:
        vec[k] += 1
        k += 1

def span_dec(vec, i, j):
    k = i
    while k <= j:
        vec[k] -= 1
        k += 1