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