Akron | 7f1097f | 2021-09-21 16:00:29 +0200 | [diff] [blame] | 1 | package datok |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 2 | |
| 3 | /** |
| 4 | * The file reader is basically a port of foma2js, |
| 5 | * licensed under the Apache License, version 2, |
| 6 | * and written by Mans Hulden. |
| 7 | */ |
| 8 | |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 9 | // The maximum number of states is 1.073.741.823 (30bit), |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 10 | // with a loadfactor of ~70, this means roughly 70 million |
| 11 | // states in the FSA, which is sufficient for the current |
| 12 | // job. |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 13 | // |
| 14 | // Serialization is little endian. |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 15 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 16 | // TODO: |
| 17 | // - replace maxSize with the check value |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 18 | // - Check if final states can be optional. |
| 19 | // - Introduce ELM (Morita et al. 2001) to speed |
| 20 | // up construction. Can be ignored on serialization |
| 21 | // to improve compression. |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 22 | // - Add checksum to serialization. |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 23 | // - Replace/Enhance table with a map |
| 24 | // - Provide a bufio.Scanner compatible interface. |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 25 | // - Mark epsilon transitions in bytes |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 26 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 27 | import ( |
| 28 | "bufio" |
| 29 | "compress/gzip" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 30 | "encoding/binary" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 31 | "io" |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 32 | "math" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 33 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 34 | "sort" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 35 | |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 36 | "log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | const ( |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 40 | DEBUG = false |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 41 | DAMAGIC = "DATOK" |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 42 | VERSION = uint16(1) |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 43 | FIRSTBIT uint32 = 1 << 31 |
| 44 | SECONDBIT uint32 = 1 << 30 |
| 45 | RESTBIT uint32 = ^uint32(0) &^ (FIRSTBIT | SECONDBIT) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 48 | // Serialization is always little endian |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 49 | var bo binary.ByteOrder = binary.LittleEndian |
| 50 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 51 | type mapping struct { |
| 52 | source int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 53 | target uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 56 | type bc struct { |
| 57 | base uint32 |
| 58 | check uint32 |
| 59 | } |
| 60 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 61 | // DaTokenizer represents a tokenizer implemented as a |
| 62 | // Double Array FSA. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 63 | type DaTokenizer struct { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 64 | sigma map[rune]int |
| 65 | sigmaASCII [256]int |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 66 | maxSize int |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 67 | transCount int |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 68 | array []bc |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 69 | |
| 70 | // Special symbols in sigma |
| 71 | epsilon int |
| 72 | unknown int |
| 73 | identity int |
| 74 | final int |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 75 | tokenend int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 78 | // ToDoubleArray turns the intermediate tokenizer representation |
| 79 | // into a double array representation. |
| 80 | // |
| 81 | // This is based on Mizobuchi et al (2000), p.128 |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 82 | func (auto *Automaton) ToDoubleArray() *DaTokenizer { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 83 | |
| 84 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 85 | sigma: make(map[rune]int), |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 86 | transCount: -1, |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 87 | final: auto.final, |
| 88 | unknown: auto.unknown, |
| 89 | identity: auto.identity, |
| 90 | epsilon: auto.epsilon, |
| 91 | tokenend: auto.tokenend, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 94 | dat.resize(dat.final) |
| 95 | |
Akron | 00cecd1 | 2021-12-05 13:14:03 +0100 | [diff] [blame] | 96 | // Init with identity |
| 97 | if dat.identity != -1 { |
| 98 | for i := 0; i < 256; i++ { |
| 99 | dat.sigmaASCII[i] = dat.identity |
| 100 | } |
| 101 | } |
| 102 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 103 | for num, sym := range auto.sigmaRev { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 104 | if int(sym) < 256 { |
| 105 | dat.sigmaASCII[int(sym)] = num |
| 106 | } |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 107 | dat.sigma[sym] = num |
| 108 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 109 | |
| 110 | mark := 0 |
| 111 | size := 0 |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 112 | var base uint32 |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 113 | var atrans *edge |
| 114 | var s, s1 int |
| 115 | var t, t1 uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 116 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 117 | // Create a mapping from s (in Ms aka Intermediate FSA) |
| 118 | // to t (in Mt aka Double Array FSA) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 119 | table := make([]*mapping, auto.arcCount+1) |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 120 | // tableQueue := make([]int, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 121 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 122 | // Initialize with the start state |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 123 | table[size] = &mapping{source: 1, target: 1} |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 124 | // tableQueue[size] = 1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 125 | size++ |
| 126 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 127 | // Allocate space for the outgoing symbol range |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 128 | A := make([]int, 0, auto.sigmaCount) |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 129 | // tableLookup := make([]uint32, tok.arcCount+2) // make(map[int]uint32) |
| 130 | // tableLookup[1] = 1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 131 | |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 132 | // block_begin_pos := uint32(1) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 133 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 134 | for mark < size { |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 135 | s = table[mark].source // This is a state in Ms |
| 136 | t = table[mark].target // This is a state in Mt |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 137 | // s = tableQueue[mark] |
| 138 | // t = tableLookup[s] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 139 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 140 | |
| 141 | // Following the paper, here the state t can be remembered |
| 142 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 143 | A = A[:0] |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 144 | auto.getSet(s, &A) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 145 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 146 | // Set base to the first free slot in the double array |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 147 | // base = dat.xCheck(A) |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 148 | // base = dat.xCheckSkip(A) |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 149 | // base = dat.xCheckNiu(A, &block_begin_pos) |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 150 | base = dat.xCheckSkipNiu(A) |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 151 | dat.array[t].setBase(base) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 152 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 153 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 154 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 155 | // outdegree of .end |
| 156 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 157 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 158 | for _, a := range A { |
| 159 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 160 | if a != auto.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 161 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 162 | atrans = auto.transitions[s][a] |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 163 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 164 | // Aka g(s, a) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 165 | s1 = atrans.end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 166 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 167 | // Store the transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 168 | t1 = base + uint32(a) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 169 | dat.array[t1].setCheck(t) |
| 170 | |
| 171 | // Set maxSize |
| 172 | if dat.maxSize < int(t1) { |
| 173 | dat.maxSize = int(t1) |
| 174 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 175 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 176 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 177 | log.Println("Translate transition", |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 178 | s, "->", s1, "(", a, ")", "to", t, "->", t1) |
| 179 | } |
| 180 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 181 | // Mark the state as being the target of a nontoken transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 182 | if atrans.nontoken { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 183 | dat.array[t1].setNonToken(true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 184 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 185 | log.Println("Set", t1, "to nontoken") |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 186 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 189 | // Mark the state as being the target of a tokenend transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 190 | if atrans.tokenend { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 191 | dat.array[t1].setTokenEnd(true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 192 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 193 | log.Println("Set", t1, "to tokenend") |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 194 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 197 | // Check for representative states |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 198 | r := stateAlreadyInTable(s1, table, size) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 199 | // r := tableLookup[s1] |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 200 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 201 | // No representative found |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 202 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 203 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 204 | table[size] = &mapping{source: s1, target: t1} |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 205 | // tableQueue[size] = s1 |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 206 | // tableLookup[s1] = t1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 207 | size++ |
| 208 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 209 | // Overwrite with the representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 210 | dat.array[t1].setBase(r) |
| 211 | dat.array[t1].setSeparate(true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 212 | } |
| 213 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 214 | // Store a final transition |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 215 | dat.array[base+uint32(dat.final)].setCheck(t) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 216 | |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 217 | if dat.maxSize < int(base)+dat.final { |
| 218 | dat.maxSize = int(base) + dat.final |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 219 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Following Mizobuchi et al (2000) the size of the |
| 225 | // FSA should be stored in check(1). |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 226 | // We make the size a bit larger so we never have to check for boundaries. |
| 227 | dat.setSize(dat.maxSize + dat.final) |
| 228 | if len(dat.array) < dat.maxSize+dat.final { |
| 229 | dat.array = append(dat.array, make([]bc, dat.final)...) |
| 230 | } |
| 231 | dat.array = dat.array[:dat.maxSize+dat.final] |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 232 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 235 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 236 | // exists and return this as a representative. |
| 237 | // Currently iterates through the whole table |
| 238 | // in a bruteforce manner. |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 239 | func stateAlreadyInTable(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 240 | for x := 0; x < size; x++ { |
| 241 | if table[x].source == s { |
| 242 | return table[x].target |
| 243 | } |
| 244 | } |
| 245 | return 0 |
| 246 | } |
| 247 | |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 248 | // Type of tokenizer |
| 249 | func (DaTokenizer) Type() string { |
| 250 | return DAMAGIC |
| 251 | } |
| 252 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 253 | // Resize double array when necessary |
| 254 | func (dat *DaTokenizer) resize(l int) { |
| 255 | // TODO: |
| 256 | // This is a bit too aggressive atm and should be calmed down. |
| 257 | if len(dat.array) <= l { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 258 | dat.array = append(dat.array, make([]bc, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 259 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 260 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 261 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 262 | // Set base value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 263 | func (bc *bc) setBase(v uint32) { |
| 264 | bc.base = v |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Get base value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 268 | func (bc *bc) getBase() uint32 { |
| 269 | return bc.base & RESTBIT |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Set check value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 273 | func (bc *bc) setCheck(v uint32) { |
| 274 | bc.check = v |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Get check value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 278 | func (bc *bc) getCheck() uint32 { |
| 279 | return bc.check & RESTBIT |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 282 | // Returns true if a state is separate pointing to a representative |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 283 | func (bc *bc) isSeparate() bool { |
| 284 | return bc.base&FIRSTBIT != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Mark a state as separate pointing to a representative |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 288 | func (bc *bc) setSeparate(sep bool) { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 289 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 290 | bc.base |= FIRSTBIT |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 291 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 292 | bc.base &= (RESTBIT | SECONDBIT) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 296 | // Returns true if a state is the target of a nontoken transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 297 | func (bc *bc) isNonToken() bool { |
| 298 | return bc.check&FIRSTBIT != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Mark a state as being the target of a nontoken transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 302 | func (bc *bc) setNonToken(sep bool) { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 303 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 304 | bc.check |= FIRSTBIT |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 305 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 306 | bc.check &= (RESTBIT | SECONDBIT) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 310 | // Returns true if a state is the target of a tokenend transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 311 | func (bc *bc) isTokenEnd() bool { |
| 312 | return bc.check&SECONDBIT != 0 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Mark a state as being the target of a tokenend transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 316 | func (bc *bc) setTokenEnd(sep bool) { |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 317 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 318 | bc.check |= SECONDBIT |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 319 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 320 | bc.check &= (RESTBIT | FIRSTBIT) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 324 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 325 | func (dat *DaTokenizer) setSize(v int) { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 326 | dat.array[1].setCheck(uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 330 | func (dat *DaTokenizer) GetSize() int { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 331 | return int(dat.array[1].getCheck()) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Based on Mizobuchi et al (2000), p. 124 |
| 335 | // This iterates for every state through the complete double array |
| 336 | // structure until it finds a gap that fits all outgoing transitions |
| 337 | // of the state. This is extremely slow, but is only necessary in the |
| 338 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 339 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 340 | |
| 341 | // Start at the first entry of the double array list |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 342 | base := uint32(1) |
| 343 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 344 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 345 | // Resize the array if necessary |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 346 | dat.resize(int(base) + dat.final) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 347 | for _, a := range symbols { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 348 | if dat.array[int(base)+a].getCheck() != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 349 | base++ |
| 350 | goto OVERLAP |
| 351 | } |
| 352 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 353 | return base |
| 354 | } |
| 355 | |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 356 | // This is an implementation of xCheck with the skip-improvement |
| 357 | // proposed by Morita et al. (2001) |
| 358 | func (dat *DaTokenizer) xCheckSkip(symbols []int) uint32 { |
| 359 | |
| 360 | // Start at the first entry of the double array list |
| 361 | base := uint32(math.Abs(float64(dat.maxSize-1) * .9)) |
| 362 | |
| 363 | OVERLAP: |
| 364 | // Resize the array if necessary |
| 365 | dat.resize(int(base) + dat.final) |
| 366 | for _, a := range symbols { |
| 367 | if dat.array[int(base)+a].getCheck() != 0 { |
| 368 | base++ |
| 369 | goto OVERLAP |
| 370 | } |
| 371 | } |
| 372 | return base |
| 373 | } |
| 374 | |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 375 | // This is an implementation of xCheck with the skip-improvement |
| 376 | // proposed by Morita et al. (2001) for higher outdegrees as |
| 377 | // proposed by Niu et al. (2013) |
| 378 | func (dat *DaTokenizer) xCheckSkipNiu(symbols []int) uint32 { |
| 379 | |
| 380 | // Start at the first entry of the double array list |
| 381 | base := uint32(1) |
| 382 | |
| 383 | // Or skip the first few entries |
| 384 | if len(symbols) >= 3 { |
| 385 | base = uint32(math.Abs(float64(dat.maxSize-1)*.9)) + 1 |
| 386 | } |
| 387 | |
| 388 | OVERLAP: |
| 389 | // Resize the array if necessary |
| 390 | dat.resize(int(base) + dat.final + 1) |
| 391 | for _, a := range symbols { |
| 392 | if dat.array[int(base)+a].getCheck() != 0 { |
| 393 | base++ |
| 394 | goto OVERLAP |
| 395 | } |
| 396 | } |
| 397 | return base |
| 398 | } |
| 399 | |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 400 | // This is an implementation of xCheck wit an improvement |
| 401 | // proposed by Niu et al. (2013) |
| 402 | func (dat *DaTokenizer) xCheckNiu(symbols []int, block_begin_pos *uint32) uint32 { |
| 403 | |
| 404 | // Start at the first entry of the double array list |
| 405 | base := uint32(1) |
| 406 | |
| 407 | if len(symbols) > 3 { |
| 408 | sort.Ints(symbols) |
| 409 | if *block_begin_pos > uint32(symbols[0]) { |
| 410 | dat.resize(int(*block_begin_pos) + dat.final) |
| 411 | *block_begin_pos += uint32(symbols[len(symbols)-1] + 1) |
| 412 | return *block_begin_pos - uint32(symbols[0]) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | OVERLAP: |
| 417 | // Resize the array if necessary |
| 418 | dat.resize(int(base) + dat.final) |
| 419 | for _, a := range symbols { |
| 420 | if dat.array[int(base)+a].getCheck() != 0 { |
| 421 | base++ |
| 422 | goto OVERLAP |
| 423 | } |
| 424 | } |
| 425 | return base |
| 426 | } |
| 427 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 428 | // List all outgoing transitions for a state |
| 429 | // for testing purposes |
| 430 | func (dat *DaTokenizer) outgoing(t uint32) []int { |
| 431 | |
| 432 | valid := make([]int, 0, len(dat.sigma)) |
| 433 | |
| 434 | for _, a := range dat.sigma { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 435 | t1 := dat.array[t].getBase() + uint32(a) |
| 436 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 437 | valid = append(valid, a) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | for _, a := range []int{dat.epsilon, dat.unknown, dat.identity, dat.final} { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 442 | t1 := dat.array[t].getBase() + uint32(a) |
| 443 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 444 | valid = append(valid, -1*a) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | sort.Ints(valid) |
| 449 | |
| 450 | return valid |
| 451 | } |
| 452 | |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 453 | // TransCount as the number of transitions aka arcs in the |
| 454 | // finite state automaton |
| 455 | func (dat *DaTokenizer) TransCount() int { |
| 456 | // Cache the transCount |
| 457 | if dat.transCount > 0 { |
| 458 | return dat.transCount |
| 459 | } |
| 460 | |
| 461 | dat.transCount = 0 |
| 462 | for x := 1; x < len(dat.array); x++ { |
| 463 | if dat.array[x].getBase() != 0 { |
| 464 | dat.transCount++ |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return dat.transCount |
| 469 | } |
| 470 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 471 | // LoadFactor as defined in Kanda et al (2018), |
| 472 | // i.e. the proportion of non-empty elements to all elements. |
| 473 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 474 | return float64(dat.TransCount()) / float64(len(dat.array)) * 100 |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 477 | // Save stores the double array data in a file |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 478 | func (dat *DaTokenizer) Save(file string) (n int64, err error) { |
| 479 | f, err := os.Create(file) |
| 480 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 481 | log.Println(err) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 482 | return 0, err |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 483 | } |
| 484 | defer f.Close() |
| 485 | gz := gzip.NewWriter(f) |
| 486 | defer gz.Close() |
| 487 | n, err = dat.WriteTo(gz) |
| 488 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 489 | log.Println(err) |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 490 | return n, err |
| 491 | } |
| 492 | gz.Flush() |
| 493 | return n, nil |
| 494 | } |
| 495 | |
| 496 | // WriteTo stores the double array data in an io.Writer. |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 497 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 498 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 499 | wb := bufio.NewWriter(w) |
| 500 | defer wb.Flush() |
| 501 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 502 | // Store magical header |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 503 | all, err := wb.Write([]byte(DAMAGIC)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 504 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 505 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 506 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // Get sigma as a list |
| 510 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 511 | max := 0 |
| 512 | for sym, num := range dat.sigma { |
| 513 | sigmalist[num] = sym |
| 514 | if num > max { |
| 515 | max = num |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | sigmalist = sigmalist[:max+1] |
| 520 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 521 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 522 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 523 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 524 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 525 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 526 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 527 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 528 | bo.PutUint32(buf[12:16], uint32(len(dat.array)*2)) // Legacy support |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 529 | more, err := wb.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 530 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 531 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 532 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | all += more |
| 536 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 537 | // Write sigma |
| 538 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 539 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 540 | more, err = wb.WriteRune(sym) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 541 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 542 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 543 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 544 | } |
| 545 | all += more |
| 546 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 547 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 548 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 549 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 550 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 551 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 552 | |
| 553 | // Test marker - could be checksum |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 554 | more, err = wb.Write([]byte("T")) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 555 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 556 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 557 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 558 | } |
| 559 | all += more |
| 560 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 561 | // for x := 0; x < len(dat.array); x++ { |
| 562 | for _, bc := range dat.array { |
| 563 | bo.PutUint32(buf[0:4], bc.base) |
| 564 | more, err = wb.Write(buf[0:4]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 565 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 566 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 567 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 568 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 569 | all += more |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 570 | if more != 4 { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 571 | log.Println("Can not write base uint32") |
| 572 | return int64(all), err |
| 573 | } |
| 574 | bo.PutUint32(buf[0:4], bc.check) |
| 575 | more, err = wb.Write(buf[0:4]) |
| 576 | if err != nil { |
| 577 | log.Println(err) |
| 578 | return int64(all), err |
| 579 | } |
| 580 | all += more |
| 581 | if more != 4 { |
| 582 | log.Println("Can not write check uint32") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 583 | return int64(all), err |
| 584 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return int64(all), err |
| 588 | } |
| 589 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 590 | // LoadDatokFile reads a double array represented tokenizer |
| 591 | // from a file. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 592 | func LoadDatokFile(file string) *DaTokenizer { |
| 593 | f, err := os.Open(file) |
| 594 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 595 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 596 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 597 | } |
| 598 | defer f.Close() |
| 599 | |
| 600 | gz, err := gzip.NewReader(f) |
| 601 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 602 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 603 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 604 | } |
| 605 | defer gz.Close() |
| 606 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 607 | // Todo: Read the whole file! |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 608 | return ParseDatok(gz) |
| 609 | } |
| 610 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 611 | // LoadDatokFile reads a double array represented tokenizer |
| 612 | // from an io.Reader |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 613 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 614 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 615 | // Initialize tokenizer with default values |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 616 | dat := &DaTokenizer{ |
| 617 | sigma: make(map[rune]int), |
| 618 | epsilon: 0, |
| 619 | unknown: 0, |
| 620 | identity: 0, |
| 621 | final: 0, |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 622 | transCount: 0, |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | r := bufio.NewReader(ior) |
| 626 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 627 | buf := make([]byte, 1024) |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 628 | buf = buf[0:len(DAMAGIC)] |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 629 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 630 | _, err := r.Read(buf) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 631 | |
| 632 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 633 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 634 | return nil |
| 635 | } |
| 636 | |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 637 | if string(DAMAGIC) != string(buf) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 638 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 639 | return nil |
| 640 | } |
| 641 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 642 | more, err := io.ReadFull(r, buf[0:16]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 643 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 644 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 645 | return nil |
| 646 | } |
| 647 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 648 | if more != 16 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 649 | log.Println("Read bytes do not fit") |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 650 | return nil |
| 651 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 652 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 653 | version := bo.Uint16(buf[0:2]) |
| 654 | |
| 655 | if version != VERSION { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 656 | log.Println("Version not compatible") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 657 | return nil |
| 658 | } |
| 659 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 660 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 661 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 662 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 663 | dat.final = int(bo.Uint16(buf[8:10])) |
| 664 | |
| 665 | sigmaCount := int(bo.Uint16(buf[10:12])) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 666 | arraySize := int(bo.Uint32(buf[12:16])) / 2 // Legacy support |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 667 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 668 | // Shouldn't be relevant though |
| 669 | dat.maxSize = arraySize - 1 |
| 670 | |
Akron | 00cecd1 | 2021-12-05 13:14:03 +0100 | [diff] [blame] | 671 | // Init with identity |
| 672 | if dat.identity != -1 { |
| 673 | for i := 0; i < 256; i++ { |
| 674 | dat.sigmaASCII[i] = dat.identity |
| 675 | } |
| 676 | } |
| 677 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 678 | for x := 0; x < sigmaCount; x++ { |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 679 | sym, _, err := r.ReadRune() |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 680 | if err == nil && sym != 0 { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 681 | if int(sym) < 256 { |
| 682 | dat.sigmaASCII[int(sym)] = x |
| 683 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 684 | dat.sigma[sym] = x |
| 685 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 688 | _, err = io.ReadFull(r, buf[0:1]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 689 | |
| 690 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 691 | log.Print(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 692 | return nil |
| 693 | } |
| 694 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 695 | if string("T") != string(buf[0:1]) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 696 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 697 | return nil |
| 698 | } |
| 699 | |
| 700 | // Read based on length |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 701 | dat.array = make([]bc, arraySize) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 702 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 703 | dataArray, err := io.ReadAll(r) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 704 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 705 | if err == io.EOF { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 706 | log.Println(err) |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 707 | return nil |
| 708 | } |
| 709 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 710 | if len(dataArray) < arraySize*8 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 711 | log.Println("Not enough bytes read") |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 712 | return nil |
| 713 | } |
| 714 | |
| 715 | for x := 0; x < arraySize; x++ { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 716 | dat.array[x].base = bo.Uint32(dataArray[x*8 : (x*8)+4]) |
| 717 | dat.array[x].check = bo.Uint32(dataArray[(x*8)+4 : (x*8)+8]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return dat |
| 721 | } |
| 722 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 723 | // Show the current state of the buffer, |
| 724 | // for testing puroses |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 725 | func showBuffer(buffer []rune, buffo int, buffi int) string { |
| 726 | out := make([]rune, 0, 1024) |
| 727 | for x := 0; x < len(buffer); x++ { |
| 728 | if buffi == x { |
| 729 | out = append(out, '^') |
| 730 | } |
| 731 | if buffo == x { |
| 732 | out = append(out, '[', buffer[x], ']') |
| 733 | } else { |
| 734 | out = append(out, buffer[x]) |
| 735 | } |
| 736 | } |
| 737 | return string(out) |
| 738 | } |
| 739 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 740 | // Show the current state of the buffer, |
| 741 | // for testing puroses |
| 742 | func showBufferNew(buffer []rune, bufft int, buffc int, buffi int) string { |
| 743 | out := make([]rune, 0, 1024) |
| 744 | for x := 0; x < len(buffer); x++ { |
| 745 | if buffi == x { |
| 746 | out = append(out, '^') |
| 747 | } |
| 748 | if bufft == x { |
| 749 | out = append(out, '|') |
| 750 | } |
| 751 | if buffc == x { |
| 752 | out = append(out, '[', buffer[x], ']') |
| 753 | } else { |
| 754 | out = append(out, buffer[x]) |
| 755 | } |
| 756 | } |
| 757 | return string(out) |
| 758 | } |
| 759 | |
| 760 | // Transduce input to ouutput |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 761 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 96fdc9b | 2021-10-27 21:11:17 +0200 | [diff] [blame] | 762 | return dat.TransduceTokenWriter(r, NewTokenWriter(w, SIMPLE)) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | // TransduceTokenWriter transduces an input string against |
| 766 | // the double array FSA. The rules are always greedy. If the |
| 767 | // automaton fails, it takes the last possible token ending |
| 768 | // branch. |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 769 | // |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 770 | // Based on Mizobuchi et al (2000), p. 129, |
| 771 | // with additional support for IDENTITY, UNKNOWN |
| 772 | // and EPSILON transitions and NONTOKEN and TOKENEND handling. |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 773 | func (dat *DaTokenizer) TransduceTokenWriter(r io.Reader, w *TokenWriter) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 774 | var a int |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 775 | var t0 uint32 |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 776 | t := uint32(1) // Initial state |
| 777 | var ok, rewindBuffer bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 778 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 779 | // Remember the last position of a possible tokenend, |
| 780 | // in case the automaton fails. |
| 781 | epsilonState := uint32(0) |
| 782 | epsilonOffset := 0 |
| 783 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 784 | // Remember if the last transition was epsilon |
| 785 | sentenceEnd := false |
| 786 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 787 | // Remember if a text end was already set |
| 788 | textEnd := false |
| 789 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 790 | // Implement a low level buffer for full control, |
| 791 | // however - it is probably better to introduce |
| 792 | // this on a higher level with a io.Reader interface |
| 793 | // The buffer stores a single word and may have white |
| 794 | // space at the end (but not at the beginning). |
| 795 | // |
| 796 | // This is the only backtracking requirement because of |
| 797 | // epsilon transitions, to support tokenizations like: |
| 798 | // "this is an example|.| And it works." vs |
| 799 | // "this is an example.com| application." |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 800 | // |
| 801 | // TODO: |
| 802 | // Store a translation buffer as well, so characters don't |
| 803 | // have to be translated multiple times! |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 804 | buffer := make([]rune, 1024) |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 805 | bufft := 0 // Buffer token offset |
| 806 | buffc := 0 // Buffer current symbol |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 807 | buffi := 0 // Buffer length |
| 808 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 809 | // The buffer is organized as follows: |
| 810 | // [ t[....c..]..i] |
| 811 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 812 | reader := bufio.NewReader(r) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 813 | defer w.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 814 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 815 | var char rune |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 816 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 817 | var err error |
| 818 | eof := false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 819 | eot := false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 820 | newchar := true |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 821 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 822 | PARSECHAR: |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 823 | for { |
| 824 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 825 | if newchar { |
| 826 | // Get from reader if buffer is empty |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 827 | if buffc >= buffi { |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 828 | if eof { |
| 829 | break |
| 830 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 831 | char, _, err = reader.ReadRune() |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 832 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 833 | // No more runes to read |
| 834 | if err != nil { |
| 835 | eof = true |
| 836 | break |
| 837 | } |
| 838 | buffer[buffi] = char |
| 839 | buffi++ |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 840 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 841 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 842 | char = buffer[buffc] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 843 | |
| 844 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 845 | log.Println("Current char", string(char), int(char), showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 848 | eot = false |
| 849 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 850 | // TODO: |
| 851 | // Better not repeatedly check for a! |
| 852 | // Possibly keep a buffer with a. |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 853 | if int(char) < 256 { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 854 | if int(char) == EOT { |
| 855 | eot = true |
| 856 | } |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 857 | a = dat.sigmaASCII[int(char)] |
| 858 | } else { |
| 859 | a, ok = dat.sigma[char] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 860 | |
Akron | 4880fb6 | 2021-12-05 12:03:05 +0100 | [diff] [blame] | 861 | // Use identity symbol if character is not in sigma |
| 862 | if !ok && dat.identity != -1 { |
| 863 | a = dat.identity |
| 864 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | t0 = t |
| 868 | |
| 869 | // Check for epsilon transitions and remember |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 870 | if dat.array[dat.array[t0].getBase()+uint32(dat.epsilon)].getCheck() == t0 { |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 871 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 872 | // Remember state for backtracking to last tokenend state |
| 873 | epsilonState = t0 |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 874 | epsilonOffset = buffc |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 875 | |
| 876 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 877 | log.Println("epsilonOffset is set to", buffc) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 878 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 879 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 880 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 881 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 882 | // Checks a transition based on t0, a and buffo |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 883 | t = dat.array[t0].getBase() + uint32(a) |
| 884 | ta := dat.array[t] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 885 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 886 | if DEBUG { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 887 | // Char is only relevant if set |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 888 | log.Println("Check", t0, "-", a, "(", string(char), ")", "->", t) |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 889 | if false { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 890 | log.Println(dat.outgoing(t0)) |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 891 | } |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 892 | } |
| 893 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 894 | // Check if the transition is invalid according to the double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 895 | if t > dat.array[1].getCheck() || ta.getCheck() != t0 { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 896 | |
| 897 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 898 | log.Println("Match is not fine!", t, "and", ta.getCheck(), "vs", t0) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if !ok && a == dat.identity { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 902 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 903 | // Try again with unknown symbol, in case identity failed |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 904 | // Char is only relevant when set |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 905 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 906 | log.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 907 | } |
| 908 | a = dat.unknown |
| 909 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 910 | } else if a != dat.epsilon && epsilonState != 0 { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 911 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 912 | // Try again with epsilon symbol, in case everything else failed |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 913 | t0 = epsilonState |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 914 | epsilonState = 0 // reset |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 915 | buffc = epsilonOffset |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 916 | a = dat.epsilon |
| 917 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 918 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 919 | log.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 920 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 921 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 922 | } else { |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 923 | |
| 924 | if DEBUG { |
| 925 | log.Println("Fail!") |
| 926 | } |
| 927 | |
| 928 | // w.Fail(bufft) |
| 929 | |
| 930 | // The following procedure means the automaton fails to consume a certain character. |
| 931 | // In the tokenization scenario, this means, the tokenizer will drop the old or current data as a |
| 932 | // token and start blank at the root node of the automaton for the remaining data. |
| 933 | // It may be beneficial to have something like a "drop()" event to capture these cases, |
| 934 | // as they are likely the result of a bad automaton design. |
Akron | cae3911 | 2023-04-26 19:43:16 +0200 | [diff] [blame^] | 935 | if buffc-bufft <= 0 { |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 936 | buffc++ |
Akron | cae3911 | 2023-04-26 19:43:16 +0200 | [diff] [blame^] | 937 | if buffc == 0 { |
| 938 | eof = true |
| 939 | break |
| 940 | } |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if DEBUG { |
| 944 | log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBufferNew(buffer, bufft, buffc, buffi)) |
| 945 | } |
| 946 | w.Token(bufft, buffer[:buffc]) |
| 947 | |
| 948 | sentenceEnd = false |
| 949 | textEnd = false |
| 950 | |
| 951 | if DEBUG { |
| 952 | log.Println("-> Rewind buffer", bufft, buffc, buffi, epsilonOffset) |
| 953 | } |
| 954 | |
| 955 | for x, i := range buffer[buffc:buffi] { |
| 956 | buffer[x] = i |
| 957 | } |
| 958 | |
| 959 | buffi -= buffc |
| 960 | epsilonState = 0 |
| 961 | |
| 962 | buffc = 0 |
| 963 | bufft = 0 |
| 964 | |
| 965 | a = dat.epsilon |
| 966 | |
| 967 | // Restart from root state |
| 968 | t = uint32(1) |
| 969 | newchar = true |
| 970 | // goto PARSECHARM |
| 971 | continue |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 972 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 973 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 974 | newchar = false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 975 | eot = false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 976 | continue |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 977 | } |
| 978 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 979 | // Transition was successful |
| 980 | rewindBuffer = false |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 981 | |
| 982 | // Transition consumes a character |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 983 | if a != dat.epsilon { |
| 984 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 985 | buffc++ |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 986 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 987 | // Transition does not produce a character |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 988 | if buffc-bufft == 1 && ta.isNonToken() { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 989 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 990 | log.Println("Nontoken forward", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 991 | } |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 992 | bufft++ |
| 993 | // rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 994 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 995 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 996 | } else { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 997 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 998 | // Transition marks the end of a token - so flush the buffer |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 999 | if buffc-bufft > 0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1000 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1001 | log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBuffer(buffer, buffc, buffi)) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1002 | } |
Akron | 32416ce | 2021-10-23 17:09:41 +0200 | [diff] [blame] | 1003 | w.Token(bufft, buffer[:buffc]) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1004 | rewindBuffer = true |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1005 | sentenceEnd = false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1006 | textEnd = false |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1007 | } else { |
| 1008 | sentenceEnd = true |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1009 | w.SentenceEnd(0) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 1010 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1011 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1012 | |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 1013 | if eot { |
| 1014 | eot = false |
| 1015 | textEnd = true |
| 1016 | w.TextEnd(0) |
| 1017 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1018 | log.Println("END OF TEXT") |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1022 | // Rewind the buffer if necessary |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1023 | if rewindBuffer { |
| 1024 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1025 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1026 | log.Println("-> Rewind buffer", bufft, buffc, buffi, epsilonOffset) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1029 | // TODO: Better as a ring buffer |
Akron | 04335c6 | 2021-10-28 11:56:00 +0200 | [diff] [blame] | 1030 | // buffer = buffer[buffc:] !slower |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1031 | for x, i := range buffer[buffc:buffi] { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1032 | buffer[x] = i |
| 1033 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1034 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1035 | buffi -= buffc |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 1036 | // epsilonOffset -= buffo |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1037 | epsilonOffset = 0 |
| 1038 | epsilonState = 0 |
| 1039 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1040 | buffc = 0 |
| 1041 | bufft = 0 |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1042 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1043 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1044 | log.Println("Remaining:", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1048 | // Move to representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1049 | if ta.isSeparate() { |
| 1050 | t = ta.getBase() |
| 1051 | ta = dat.array[t] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1052 | |
| 1053 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1054 | log.Println("Representative pointing to", t) |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1058 | newchar = true |
| 1059 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1060 | // TODO: |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 1061 | // Prevent endless epsilon loops by checking |
| 1062 | // the model has no epsilon loops1 |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1065 | // Input reader is not yet finished |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1066 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1067 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1068 | log.Println("Not at the end - problem", t0, ":", dat.outgoing(t0)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1069 | } |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 1070 | // This should never happen |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1071 | return false |
| 1072 | } |
| 1073 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1074 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1075 | log.Println("Entering final check") |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1078 | // Check epsilon transitions as long as possible |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1079 | t0 = t |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1080 | t = dat.array[t0].getBase() + uint32(dat.epsilon) |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1081 | a = dat.epsilon |
| 1082 | newchar = false |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1083 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1084 | if dat.array[t].getCheck() == t0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1085 | // Remember state for backtracking to last tokenend state |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1086 | goto PARSECHAR |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 1087 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1088 | } else if epsilonState != 0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1089 | t0 = epsilonState |
| 1090 | epsilonState = 0 // reset |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1091 | buffc = epsilonOffset |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1092 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1093 | log.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1094 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1095 | goto PARSECHAR |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1096 | } |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1097 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 1098 | // something left in buffer |
| 1099 | if buffc-bufft > 0 { |
| 1100 | if DEBUG { |
| 1101 | log.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBufferNew(buffer, bufft, buffc, buffi)) |
| 1102 | } |
| 1103 | w.Token(bufft, buffer[:buffc]) |
| 1104 | sentenceEnd = false |
| 1105 | textEnd = false |
| 1106 | } |
| 1107 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1108 | // Add an additional sentence ending, if the file is over but no explicit |
| 1109 | // sentence split was reached. This may be controversial and therefore |
| 1110 | // optional via parameter. |
| 1111 | if !sentenceEnd { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1112 | w.SentenceEnd(0) |
| 1113 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1114 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1115 | log.Println("Sentence end") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if !textEnd { |
| 1120 | w.TextEnd(0) |
| 1121 | |
| 1122 | if DEBUG { |
Akron | 9c3bf7f | 2021-11-03 19:52:12 +0100 | [diff] [blame] | 1123 | log.Println("Text end") |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | return true |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1128 | } |