-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatok.go
1135 lines (940 loc) · 24.8 KB
/
datok.go
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
package datok
/**
* The file reader is basically a port of foma2js,
* licensed under the Apache License, version 2,
* and written by Mans Hulden.
*/
// The maximum number of states is 1.073.741.823 (30bit),
// with a loadfactor of ~70, this means roughly 70 million
// states in the FSA, which is sufficient for the current
// job.
//
// Serialization is little endian.
// TODO:
// - replace maxSize with the check value
// - Check if final states can be optional.
// - Introduce ELM (Morita et al. 2001) to speed
// up construction. Can be ignored on serialization
// to improve compression.
// - Add checksum to serialization.
// - Replace/Enhance table with a map
// - Provide a bufio.Scanner compatible interface.
// - Mark epsilon transitions in bytes
import (
"bufio"
"compress/gzip"
"encoding/binary"
"io"
"math"
"os"
"sort"
"log"
)
const (
DEBUG = false
DAMAGIC = "DATOK"
VERSION = uint16(1)
FIRSTBIT uint32 = 1 << 31
SECONDBIT uint32 = 1 << 30
RESTBIT uint32 = ^uint32(0) &^ (FIRSTBIT | SECONDBIT)
)
// Serialization is always little endian
var bo binary.ByteOrder = binary.LittleEndian
type mapping struct {
source int
target uint32
}
type bc struct {
base uint32
check uint32
}
// DaTokenizer represents a tokenizer implemented as a
// Double Array FSA.
type DaTokenizer struct {
sigma map[rune]int
sigmaASCII [256]int
maxSize int
transCount int
array []bc
// Special symbols in sigma
epsilon int
unknown int
identity int
final int
tokenend int
}
// ToDoubleArray turns the intermediate tokenizer representation
// into a double array representation.
//
// This is based on Mizobuchi et al (2000), p.128
func (auto *Automaton) ToDoubleArray() *DaTokenizer {
dat := &DaTokenizer{
sigma: make(map[rune]int),
transCount: -1,
final: auto.final,
unknown: auto.unknown,
identity: auto.identity,
epsilon: auto.epsilon,
tokenend: auto.tokenend,
}
dat.resize(dat.final)
// Init with identity
if dat.identity != -1 {
for i := 0; i < 256; i++ {
dat.sigmaASCII[i] = dat.identity
}
}
for num, sym := range auto.sigmaRev {
if int(sym) < 256 {
dat.sigmaASCII[int(sym)] = num
}
dat.sigma[sym] = num
}
mark := 0
size := 0
var base uint32
var atrans *edge
var s, s1 int
var t, t1 uint32
var diff int
// Create a mapping from s (in Ms aka Intermediate FSA)
// to t (in Mt aka Double Array FSA)
table := make([]*mapping, auto.arcCount+1)
// tableQueue := make([]int, tok.arcCount+1)
// Initialize with the start state
table[size] = &mapping{source: 1, target: 1}
// tableQueue[size] = 1
size++
// Allocate space for the outgoing symbol range
A := make([]int, 0, auto.sigmaCount)
// tableLookup := make([]uint32, tok.arcCount+2) // make(map[int]uint32)
// tableLookup[1] = 1
// block_begin_pos := uint32(1)
for mark < size {
s = table[mark].source // This is a state in Ms
t = table[mark].target // This is a state in Mt
// s = tableQueue[mark]
// t = tableLookup[s]
mark++
// Following the paper, here the state t can be remembered
// in the set of states St
A = A[:0]
auto.getSet(s, &A)
// Set base to the first free slot in the double array
// base = dat.xCheck(A)
// base = dat.xCheckSkip(A)
// base = dat.xCheckNiu(A, &block_begin_pos)
base = dat.xCheckSkipNiu(A)
dat.array[t].setBase(base)
// TODO:
// Sort the outgoing transitions based on the
// outdegree of .end
// Iterate over all outgoing symbols
for _, a := range A {
if a != auto.final {
atrans = auto.transitions[s][a]
// Aka g(s, a)
s1 = atrans.end
// Store the transition
t1 = base + uint32(a)
dat.array[t1].setCheck(t)
// Set maxSize
// New: dat.maxSize = max(dat.maxSize, int(t1))
if dat.maxSize < int(t1) {
dat.maxSize = int(t1)
}
if DEBUG {
log.Println("Translate transition",
s, "->", s1, "(", a, ")", "to", t, "->", t1)
}
// Mark the state as being the target of a nontoken transition
if atrans.nontoken {
dat.array[t1].setNonToken(true)
if DEBUG {
log.Println("Set", t1, "to nontoken")
}
}
// Mark the state as being the target of a tokenend transition
if atrans.tokenend {
dat.array[t1].setTokenEnd(true)
if DEBUG {
log.Println("Set", t1, "to tokenend")
}
}
// Check for representative states
r := stateAlreadyInTable(s1, table, size)
// r := tableLookup[s1]
// No representative found
if r == 0 {
// Remember the mapping
table[size] = &mapping{source: s1, target: t1}
// tableQueue[size] = s1
// tableLookup[s1] = t1
size++
} else {
// Overwrite with the representative state
dat.array[t1].setBase(r)
dat.array[t1].setSeparate(true)
}
} else {
// Store a final transition
dat.array[base+uint32(dat.final)].setCheck(t)
// Find max
// see https://dev.to/jobinrjohnson/branchless-programming-does-it-really-matter-20j4
diff = dat.maxSize - (int(base) + dat.final)
dat.maxSize -= (diff & (diff >> 31))
}
}
}
// Following Mizobuchi et al (2000) the size of the
// FSA should be stored in check(1).
// We make the size a bit larger so we never have to check for boundaries.
dat.setSize(dat.maxSize + dat.final)
if len(dat.array) < dat.maxSize+dat.final {
dat.array = append(dat.array, make([]bc, dat.final)...)
}
dat.array = dat.array[:dat.maxSize+dat.final]
return dat
}
// Check the table if a mapping of s
// exists and return this as a representative.
// Currently iterates through the whole table
// in a bruteforce manner.
func stateAlreadyInTable(s int, table []*mapping, size int) uint32 {
for x := 0; x < size; x++ {
if table[x].source == s {
return table[x].target
}
}
return 0
}
// Type of tokenizer
func (DaTokenizer) Type() string {
return DAMAGIC
}
// Resize double array when necessary
func (dat *DaTokenizer) resize(l int) {
// TODO:
// This is a bit too aggressive atm and should be calmed down.
if len(dat.array) <= l {
dat.array = append(dat.array, make([]bc, l)...)
}
}
// Set base value in double array
func (bc *bc) setBase(v uint32) {
bc.base = v
}
// Get base value in double array
func (bc *bc) getBase() uint32 {
return bc.base & RESTBIT
}
// Set check value in double array
func (bc *bc) setCheck(v uint32) {
bc.check = v
}
// Get check value in double array
func (bc *bc) getCheck() uint32 {
return bc.check & RESTBIT
}
// Returns true if a state is separate pointing to a representative
func (bc *bc) isSeparate() bool {
return bc.base&FIRSTBIT != 0
}
// Mark a state as separate pointing to a representative
func (bc *bc) setSeparate(sep bool) {
if sep {
bc.base |= FIRSTBIT
} else {
bc.base &= (RESTBIT | SECONDBIT)
}
}
// Returns true if a state is the target of a nontoken transition
func (bc *bc) isNonToken() bool {
return bc.check&FIRSTBIT != 0
}
// Mark a state as being the target of a nontoken transition
func (bc *bc) setNonToken(sep bool) {
if sep {
bc.check |= FIRSTBIT
} else {
bc.check &= (RESTBIT | SECONDBIT)
}
}
// Returns true if a state is the target of a tokenend transition
func (bc *bc) isTokenEnd() bool {
return bc.check&SECONDBIT != 0
}
// Mark a state as being the target of a tokenend transition
func (bc *bc) setTokenEnd(sep bool) {
if sep {
bc.check |= SECONDBIT
} else {
bc.check &= (RESTBIT | FIRSTBIT)
}
}
// Set size of double array
func (dat *DaTokenizer) setSize(v int) {
dat.array[1].setCheck(uint32(v))
}
// Get size of double array
func (dat *DaTokenizer) GetSize() int {
return int(dat.array[1].getCheck())
}
// Based on Mizobuchi et al (2000), p. 124
// This iterates for every state through the complete double array
// structure until it finds a gap that fits all outgoing transitions
// of the state. This is extremely slow, but is only necessary in the
// construction phase of the tokenizer.
func (dat *DaTokenizer) xCheck(symbols []int) uint32 {
// Start at the first entry of the double array list
base := uint32(1)
OVERLAP:
// Resize the array if necessary
dat.resize(int(base) + dat.final)
for _, a := range symbols {
if dat.array[int(base)+a].getCheck() != 0 {
base++
goto OVERLAP
}
}
return base
}
// This is an implementation of xCheck with the skip-improvement
// proposed by Morita et al. (2001)
func (dat *DaTokenizer) xCheckSkip(symbols []int) uint32 {
// Start at the first entry of the double array list
base := uint32(math.Abs(float64(dat.maxSize-1) * .9))
OVERLAP:
// Resize the array if necessary
dat.resize(int(base) + dat.final)
for _, a := range symbols {
if dat.array[int(base)+a].getCheck() != 0 {
base++
goto OVERLAP
}
}
return base
}
// This is an implementation of xCheck with the skip-improvement
// proposed by Morita et al. (2001) for higher outdegrees as
// proposed by Niu et al. (2013)
func (dat *DaTokenizer) xCheckSkipNiu(symbols []int) uint32 {
// Start at the first entry of the double array list
base := uint32(1)
// Or skip the first few entries
if len(symbols) >= 3 {
base = uint32(math.Abs(float64(dat.maxSize-1)*.9)) + 1
}
OVERLAP:
// Resize the array if necessary
dat.resize(int(base) + dat.final + 1)
for _, a := range symbols {
if dat.array[int(base)+a].getCheck() != 0 {
base++
goto OVERLAP
}
}
return base
}
// This is an implementation of xCheck wit an improvement
// proposed by Niu et al. (2013)
func (dat *DaTokenizer) xCheckNiu(symbols []int, block_begin_pos *uint32) uint32 {
// Start at the first entry of the double array list
base := uint32(1)
if len(symbols) > 3 {
sort.Ints(symbols)
if *block_begin_pos > uint32(symbols[0]) {
dat.resize(int(*block_begin_pos) + dat.final)
*block_begin_pos += uint32(symbols[len(symbols)-1] + 1)
return *block_begin_pos - uint32(symbols[0])
}
}
OVERLAP:
// Resize the array if necessary
dat.resize(int(base) + dat.final)
for _, a := range symbols {
if dat.array[int(base)+a].getCheck() != 0 {
base++
goto OVERLAP
}
}
return base
}
// List all outgoing transitions for a state
// for testing purposes
func (dat *DaTokenizer) outgoing(t uint32) []int {
valid := make([]int, 0, len(dat.sigma))
for _, a := range dat.sigma {
t1 := dat.array[t].getBase() + uint32(a)
if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t {
valid = append(valid, a)
}
}
for _, a := range []int{dat.epsilon, dat.unknown, dat.identity, dat.final} {
t1 := dat.array[t].getBase() + uint32(a)
if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t {
valid = append(valid, -1*a)
}
}
sort.Ints(valid)
return valid
}
// TransCount as the number of transitions aka arcs in the
// finite state automaton
func (dat *DaTokenizer) TransCount() int {
// Cache the transCount
if dat.transCount > 0 {
return dat.transCount
}
dat.transCount = 0
for x := 1; x < len(dat.array); x++ {
// Hopefully branchless
if dat.array[x].getBase() != 0 {
dat.transCount++
}
}
return dat.transCount
}
// LoadFactor as defined in Kanda et al (2018),
// i.e. the proportion of non-empty elements to all elements.
func (dat *DaTokenizer) LoadFactor() float64 {
return float64(dat.TransCount()) / float64(len(dat.array)) * 100
}
// Save stores the double array data in a file
func (dat *DaTokenizer) Save(file string) (n int64, err error) {
f, err := os.Create(file)
if err != nil {
log.Println(err)
return 0, err
}
defer f.Close()
gz := gzip.NewWriter(f)
defer gz.Close()
n, err = dat.WriteTo(gz)
if err != nil {
log.Println(err)
return n, err
}
gz.Flush()
return n, nil
}
// WriteTo stores the double array data in an io.Writer.
func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) {
wb := bufio.NewWriter(w)
defer wb.Flush()
// Store magical header
all, err := wb.Write([]byte(DAMAGIC))
if err != nil {
log.Println(err)
return int64(all), err
}
// Get sigma as a list
sigmalist := make([]rune, len(dat.sigma)+16)
max := 0
for sym, num := range dat.sigma {
sigmalist[num] = sym
// Find max
max -= ((max - num) & ((max - num) >> 31))
// if num > max {
// max = num
// }
}
sigmalist = sigmalist[:max+1]
buf := make([]byte, 0, 16)
bo.PutUint16(buf[0:2], VERSION)
bo.PutUint16(buf[2:4], uint16(dat.epsilon))
bo.PutUint16(buf[4:6], uint16(dat.unknown))
bo.PutUint16(buf[6:8], uint16(dat.identity))
bo.PutUint16(buf[8:10], uint16(dat.final))
bo.PutUint16(buf[10:12], uint16(len(sigmalist)))
bo.PutUint32(buf[12:16], uint32(len(dat.array)*2)) // Legacy support
more, err := wb.Write(buf[0:16])
if err != nil {
log.Println(err)
return int64(all), err
}
all += more
// Write sigma
for _, sym := range sigmalist {
more, err = wb.WriteRune(sym)
if err != nil {
log.Println(err)
return int64(all), err
}
all += more
}
if err != nil {
log.Println(err)
return int64(all), err
}
// Test marker - could be checksum
more, err = wb.Write([]byte("T"))
if err != nil {
log.Println(err)
return int64(all), err
}
all += more
// for x := 0; x < len(dat.array); x++ {
for _, bc := range dat.array {
bo.PutUint32(buf[0:4], bc.base)
more, err = wb.Write(buf[0:4])
if err != nil {
log.Println(err)
return int64(all), err
}
all += more
if more != 4 {
log.Println("Can not write base uint32")
return int64(all), err
}
bo.PutUint32(buf[0:4], bc.check)
more, err = wb.Write(buf[0:4])
if err != nil {
log.Println(err)
return int64(all), err
}
all += more
if more != 4 {
log.Println("Can not write check uint32")
return int64(all), err
}
}
return int64(all), err
}
// LoadDatokFile reads a double array represented tokenizer
// from a file.
func LoadDatokFile(file string) *DaTokenizer {
f, err := os.Open(file)
if err != nil {
log.Println(err)
return nil
}
defer f.Close()
gz, err := gzip.NewReader(f)
if err != nil {
log.Println(err)
return nil
}
defer gz.Close()
// Todo: Read the whole file!
return ParseDatok(gz)
}
// LoadDatokFile reads a double array represented tokenizer
// from an io.Reader
func ParseDatok(ior io.Reader) *DaTokenizer {
// Initialize tokenizer with default values
dat := &DaTokenizer{
sigma: make(map[rune]int),
epsilon: 0,
unknown: 0,
identity: 0,
final: 0,
transCount: 0,
}
r := bufio.NewReader(ior)
buf := make([]byte, 1024)
buf = buf[0:len(DAMAGIC)]
_, err := r.Read(buf)
if err != nil {
log.Println(err)
return nil
}
if string(DAMAGIC) != string(buf) {
log.Println("Not a datok file")
return nil
}
more, err := io.ReadFull(r, buf[0:16])
if err != nil {
log.Println(err)
return nil
}
if more != 16 {
log.Println("Read bytes do not fit")
return nil
}
version := bo.Uint16(buf[0:2])
if version != VERSION {
log.Println("Version not compatible")
return nil
}
dat.epsilon = int(bo.Uint16(buf[2:4]))
dat.unknown = int(bo.Uint16(buf[4:6]))
dat.identity = int(bo.Uint16(buf[6:8]))
dat.final = int(bo.Uint16(buf[8:10]))
sigmaCount := int(bo.Uint16(buf[10:12]))
arraySize := int(bo.Uint32(buf[12:16])) / 2 // Legacy support
// Shouldn't be relevant though
dat.maxSize = arraySize - 1
// Init with identity
if dat.identity != -1 {
for i := 0; i < 256; i++ {
dat.sigmaASCII[i] = dat.identity
}
}
for x := 0; x < sigmaCount; x++ {
sym, _, err := r.ReadRune()
if err == nil && sym != 0 {
if int(sym) < 256 {
dat.sigmaASCII[int(sym)] = x
}
dat.sigma[sym] = x
}
}
_, err = io.ReadFull(r, buf[0:1])
if err != nil {
log.Print(err)
return nil
}
if string("T") != string(buf[0:1]) {
log.Println("Not a datok file")
return nil
}
// Read based on length
dat.array = make([]bc, arraySize)
dataArray, err := io.ReadAll(r)
if err == io.EOF {
log.Println(err)
return nil
}
if len(dataArray) < arraySize*8 {
log.Println("Not enough bytes read")
return nil
}
for x := 0; x < arraySize; x++ {
dat.array[x].base = bo.Uint32(dataArray[x*8 : (x*8)+4])
dat.array[x].check = bo.Uint32(dataArray[(x*8)+4 : (x*8)+8])
}
return dat
}
// Show the current state of the buffer,
// for testing puroses
func showBuffer(buffer []rune, buffo int, buffi int) string {
out := make([]rune, 0, 1024)
for x := 0; x < len(buffer); x++ {
if buffi == x {
out = append(out, '^')
}
if buffo == x {
out = append(out, '[', buffer[x], ']')
} else {
out = append(out, buffer[x])
}
}
return string(out)
}
// Show the current state of the buffer,
// for testing puroses
func showBufferNew(buffer []rune, bufft int, buffc int, buffi int) string {
out := make([]rune, 0, 1024)
for x := 0; x < len(buffer); x++ {
if buffi == x {
out = append(out, '^')
}
if bufft == x {
out = append(out, '|')
}
if buffc == x {
out = append(out, '[', buffer[x], ']')
} else {
out = append(out, buffer[x])
}
}
return string(out)
}
// Transduce input to ouutput
func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool {
return dat.TransduceTokenWriter(r, NewTokenWriter(w, SIMPLE))
}
// TransduceTokenWriter transduces an input string against
// the double array FSA. The rules are always greedy. If the
// automaton fails, it takes the last possible token ending
// branch.
//
// Based on Mizobuchi et al (2000), p. 129,
// with additional support for IDENTITY, UNKNOWN
// and EPSILON transitions and NONTOKEN and TOKENEND handling.
func (dat *DaTokenizer) TransduceTokenWriter(r io.Reader, w *TokenWriter) bool {
var a int
var t0 uint32
t := uint32(1) // Initial state
var ok, rewindBuffer bool
// Remember the last position of a possible tokenend,
// in case the automaton fails.
epsilonState := uint32(0)
epsilonOffset := 0
// Remember if the last transition was epsilon
sentenceEnd := false
// Remember if a text end was already set
textEnd := false
// Implement a low level buffer for full control,
// however - it is probably better to introduce
// this on a higher level with a io.Reader interface
// The buffer stores a single word and may have white
// space at the end (but not at the beginning).
//
// This is the only backtracking requirement because of
// epsilon transitions, to support tokenizations like:
// "this is an example|.| And it works." vs
// "this is an example.com| application."
//
// TODO:
// Store a translation buffer as well, so characters don't
// have to be translated multiple times!
buffer := make([]rune, 1024)
bufft := 0 // Buffer token offset
buffc := 0 // Buffer current symbol
buffi := 0 // Buffer length
// The buffer is organized as follows:
// [ t[....c..]..i]
reader := bufio.NewReader(r)
defer w.Flush()
var char rune
var err error
eof := false
eot := false
newchar := true
PARSECHAR:
for {
if newchar {
// Get from reader if buffer is empty
if buffc >= buffi {
if eof {
break
}
char, _, err = reader.ReadRune()
// No more runes to read
if err != nil {
eof = true
break
}
buffer[buffi] = char
buffi++
}
char = buffer[buffc]
if DEBUG {
log.Println("Current char", string(char), int(char), showBufferNew(buffer, bufft, buffc, buffi))
}
eot = false
// TODO:
// Better not repeatedly check for a!
// Possibly keep a buffer with a.
if int(char) < 256 {
eot = int(char) == EOT
a = dat.sigmaASCII[int(char)]
} else {
a, ok = dat.sigma[char]
// Use identity symbol if character is not in sigma
if !ok && dat.identity != -1 {
a = dat.identity
}
}
t0 = t
// Check for epsilon transitions and remember
if dat.array[dat.array[t0].getBase()+uint32(dat.epsilon)].getCheck() == t0 {
// Remember state for backtracking to last tokenend state
epsilonState = t0
epsilonOffset = buffc
if DEBUG {
log.Println("epsilonOffset is set to", buffc)
}
}
}
// Checks a transition based on t0, a and buffo
t = dat.array[t0].getBase() + uint32(a)
ta := dat.array[t]
if DEBUG {
// Char is only relevant if set
log.Println("Check", t0, "-", a, "(", string(char), ")", "->", t)
if false {
log.Println(dat.outgoing(t0))
}
}
// Check if the transition is invalid according to the double array
if t > dat.array[1].getCheck() || ta.getCheck() != t0 {
if DEBUG {
log.Println("Match is not fine!", t, "and", ta.getCheck(), "vs", t0)
}
if !ok && a == dat.identity {
// Try again with unknown symbol, in case identity failed
// Char is only relevant when set
if DEBUG {
log.Println("UNKNOWN symbol", string(char), "->", dat.unknown)
}
a = dat.unknown
} else if a != dat.epsilon && epsilonState != 0 {
// Try again with epsilon symbol, in case everything else failed
t0 = epsilonState
epsilonState = 0 // reset
buffc = epsilonOffset
a = dat.epsilon
if DEBUG {
log.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi))
}
} else {
if DEBUG {
log.Println("Fail!")
}
// w.Fail(bufft)
// The following procedure means the automaton fails to consume a certain character.
// In the tokenization scenario, this means, the tokenizer will drop the old or current data as a
// token and start blank at the root node of the automaton for the remaining data.
// It may be beneficial to have something like a "drop()" event to capture these cases,
// as they are likely the result of a bad automaton design.
// Hopefully this is branchless code
if buffc-bufft <= 0 {
buffc++
if buffc == 0 {
eof = true
break
}
}
if DEBUG {
log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBufferNew(buffer, bufft, buffc, buffi))
}
w.Token(bufft, buffer[:buffc])
sentenceEnd = false
textEnd = false
if DEBUG {
log.Println("-> Rewind buffer", bufft, buffc, buffi, epsilonOffset)
}
copy(buffer[0:], buffer[buffc:buffi])
buffi -= buffc
epsilonState = 0
buffc = 0
bufft = 0
a = dat.epsilon
// Restart from root state
t = uint32(1)
newchar = true
// goto PARSECHARM
continue
}
newchar = false
eot = false
continue
}
// Transition was successful
rewindBuffer = false
// Transition consumes a character
if a != dat.epsilon {
buffc++
// Transition does not produce a character
// Hopefully this is branchless
if buffc-bufft == 1 && ta.isNonToken() {
if DEBUG {
log.Println("Nontoken forward", showBufferNew(buffer, bufft, buffc, buffi))
}
bufft++
// rewindBuffer = true
}