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 | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 727 | // Show the current state of the buffer, |
| 728 | // for testing puroses |
| 729 | func showBufferNew(buffer []rune, bufft int, buffc int, buffi int) string { |
| 730 | out := make([]rune, 0, 1024) |
| 731 | for x := 0; x < len(buffer); x++ { |
| 732 | if buffi == x { |
| 733 | out = append(out, '^') |
| 734 | } |
| 735 | if bufft == x { |
| 736 | out = append(out, '|') |
| 737 | } |
| 738 | if buffc == x { |
| 739 | out = append(out, '[', buffer[x], ']') |
| 740 | } else { |
| 741 | out = append(out, buffer[x]) |
| 742 | } |
| 743 | } |
| 744 | return string(out) |
| 745 | } |
| 746 | |
| 747 | // Transduce input to ouutput |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 748 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 749 | return dat.TransduceTokenWriter(r, NewTokenWriter(w)) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // TransduceTokenWriter transduces an input string against |
| 753 | // the double array FSA. The rules are always greedy. If the |
| 754 | // automaton fails, it takes the last possible token ending |
| 755 | // branch. |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 756 | // |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 757 | // Based on Mizobuchi et al (2000), p. 129, |
| 758 | // with additional support for IDENTITY, UNKNOWN |
| 759 | // and EPSILON transitions and NONTOKEN and TOKENEND handling. |
Akron | 4f6b28c | 2021-10-25 00:52:03 +0200 | [diff] [blame] | 760 | func (dat *DaTokenizer) TransduceTokenWriter(r io.Reader, w *TokenWriter) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 761 | var a int |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 762 | var t0 uint32 |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 763 | t := uint32(1) // Initial state |
| 764 | var ok, rewindBuffer bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 765 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 766 | // Remember the last position of a possible tokenend, |
| 767 | // in case the automaton fails. |
| 768 | epsilonState := uint32(0) |
| 769 | epsilonOffset := 0 |
| 770 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 771 | // Remember if the last transition was epsilon |
| 772 | sentenceEnd := false |
| 773 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 774 | // Remember if a text end was already set |
| 775 | textEnd := false |
| 776 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 777 | // Implement a low level buffer for full control, |
| 778 | // however - it is probably better to introduce |
| 779 | // this on a higher level with a io.Reader interface |
| 780 | // The buffer stores a single word and may have white |
| 781 | // space at the end (but not at the beginning). |
| 782 | // |
| 783 | // This is the only backtracking requirement because of |
| 784 | // epsilon transitions, to support tokenizations like: |
| 785 | // "this is an example|.| And it works." vs |
| 786 | // "this is an example.com| application." |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 787 | // |
| 788 | // TODO: |
| 789 | // Store a translation buffer as well, so characters don't |
| 790 | // have to be translated multiple times! |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 791 | buffer := make([]rune, 1024) |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 792 | bufft := 0 // Buffer token offset |
| 793 | buffc := 0 // Buffer current symbol |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 794 | buffi := 0 // Buffer length |
| 795 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 796 | // The buffer is organized as follows: |
| 797 | // [ t[....c..]..i] |
| 798 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 799 | reader := bufio.NewReader(r) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 800 | defer w.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 801 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 802 | var char rune |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 803 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 804 | var err error |
| 805 | eof := false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 806 | eot := false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 807 | newchar := true |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 808 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 809 | PARSECHAR: |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 810 | for { |
| 811 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 812 | if newchar { |
| 813 | // Get from reader if buffer is empty |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 814 | if buffc >= buffi { |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 815 | if eof { |
| 816 | break |
| 817 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 818 | char, _, err = reader.ReadRune() |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 819 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 820 | // No more runes to read |
| 821 | if err != nil { |
| 822 | eof = true |
| 823 | break |
| 824 | } |
| 825 | buffer[buffi] = char |
| 826 | buffi++ |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 827 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 828 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 829 | char = buffer[buffc] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 830 | |
| 831 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 832 | fmt.Println("Current char", string(char), int(char), showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 835 | eot = false |
| 836 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 837 | // TODO: |
| 838 | // Better not repeatedly check for a! |
| 839 | // Possibly keep a buffer with a. |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 840 | if int(char) < 256 { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 841 | if int(char) == EOT { |
| 842 | eot = true |
| 843 | } |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 844 | a = dat.sigmaASCII[int(char)] |
| 845 | } else { |
| 846 | a, ok = dat.sigma[char] |
| 847 | if !ok { |
| 848 | a = 0 |
| 849 | } |
| 850 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 851 | |
| 852 | // Use identity symbol if character is not in sigma |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 853 | if a == 0 && dat.identity != -1 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 854 | a = dat.identity |
| 855 | } |
| 856 | |
| 857 | t0 = t |
| 858 | |
| 859 | // Check for epsilon transitions and remember |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 860 | if dat.array[dat.array[t0].getBase()+uint32(dat.epsilon)].getCheck() == t0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 861 | // Remember state for backtracking to last tokenend state |
| 862 | epsilonState = t0 |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 863 | epsilonOffset = buffc |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 864 | |
| 865 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 866 | fmt.Println("epsilonOffset is set to", buffc) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 867 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 868 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 869 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 870 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 871 | // Checks a transition based on t0, a and buffo |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 872 | t = dat.array[t0].getBase() + uint32(a) |
| 873 | ta := dat.array[t] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 874 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 875 | if DEBUG { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 876 | // Char is only relevant if set |
| 877 | fmt.Println("Check", t0, "-", a, "(", string(char), ")", "->", t) |
| 878 | if false { |
| 879 | fmt.Println(dat.outgoing(t0)) |
| 880 | } |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 881 | } |
| 882 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 883 | // Check if the transition is invalid according to the double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 884 | if t > dat.array[1].getCheck() || ta.getCheck() != t0 { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 885 | |
| 886 | if DEBUG { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 887 | fmt.Println("Match is not fine!", t, "and", ta.getCheck(), "vs", t0) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if !ok && a == dat.identity { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 891 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 892 | // Try again with unknown symbol, in case identity failed |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 893 | // Char is only relevant when set |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 894 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 895 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 896 | } |
| 897 | a = dat.unknown |
| 898 | |
| 899 | } else if a != dat.epsilon { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 900 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 901 | // Try again with epsilon symbol, in case everything else failed |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 902 | t0 = epsilonState |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 903 | epsilonState = 0 // reset |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 904 | buffc = epsilonOffset |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 905 | a = dat.epsilon |
| 906 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 907 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 908 | fmt.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 909 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 910 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 911 | } else { |
| 912 | break |
| 913 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 914 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 915 | newchar = false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 916 | eot = false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 917 | continue |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 920 | // Transition was successful |
| 921 | rewindBuffer = false |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 922 | |
| 923 | // Transition consumes a character |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 924 | if a != dat.epsilon { |
| 925 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 926 | buffc++ |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 927 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 928 | // Transition does not produce a character |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 929 | if buffc-bufft == 1 && ta.isNonToken() { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 930 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 931 | fmt.Println("Nontoken forward", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 932 | } |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 933 | bufft++ |
| 934 | // rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 935 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 936 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 937 | } else { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 938 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 939 | // Transition marks the end of a token - so flush the buffer |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 940 | if buffc-bufft > 0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 941 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 942 | fmt.Println("-> Flush buffer: [", string(buffer[bufft:buffc]), "]", showBuffer(buffer, buffc, buffi)) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 943 | } |
Akron | 32416ce | 2021-10-23 17:09:41 +0200 | [diff] [blame] | 944 | w.Token(bufft, buffer[:buffc]) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 945 | rewindBuffer = true |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 946 | sentenceEnd = false |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 947 | textEnd = false |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 948 | } else { |
| 949 | sentenceEnd = true |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 950 | w.SentenceEnd(0) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 951 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 952 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 953 | |
Akron | 8cc2dd9 | 2021-10-25 19:49:41 +0200 | [diff] [blame] | 954 | if eot { |
| 955 | eot = false |
| 956 | textEnd = true |
| 957 | w.TextEnd(0) |
| 958 | if DEBUG { |
| 959 | fmt.Println("END OF TEXT") |
| 960 | } |
| 961 | } |
| 962 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 963 | // Rewind the buffer if necessary |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 964 | if rewindBuffer { |
| 965 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 966 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 967 | fmt.Println("-> Rewind buffer", bufft, buffc, buffi, epsilonOffset) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 970 | // TODO: Better as a ring buffer |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 971 | for x, i := range buffer[buffc:buffi] { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 972 | buffer[x] = i |
| 973 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 974 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 975 | buffi -= buffc |
Akron | 16c312e | 2021-09-26 13:11:12 +0200 | [diff] [blame] | 976 | // epsilonOffset -= buffo |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 977 | epsilonOffset = 0 |
| 978 | epsilonState = 0 |
| 979 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 980 | buffc = 0 |
| 981 | bufft = 0 |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 982 | |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 983 | if DEBUG { |
| 984 | fmt.Println("Remaining:", showBufferNew(buffer, bufft, buffc, buffi)) |
| 985 | } |
| 986 | } |
| 987 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 988 | // Move to representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 989 | if ta.isSeparate() { |
| 990 | t = ta.getBase() |
| 991 | ta = dat.array[t] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 992 | |
| 993 | if DEBUG { |
| 994 | fmt.Println("Representative pointing to", t) |
| 995 | } |
| 996 | } |
| 997 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 998 | newchar = true |
| 999 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1000 | // TODO: |
| 1001 | // Prevent endless epsilon loops! |
| 1002 | } |
| 1003 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1004 | // Input reader is not yet finished |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1005 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1006 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1007 | fmt.Println("Not at the end - problem", t0, ":", dat.outgoing(t0)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1008 | } |
| 1009 | return false |
| 1010 | } |
| 1011 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1012 | if DEBUG { |
| 1013 | fmt.Println("Entering final check") |
| 1014 | } |
| 1015 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1016 | /* |
| 1017 | The following code is for deprecated automata relying on |
| 1018 | final states. Datok now requires final states to be marked |
| 1019 | with tokenends. |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1020 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1021 | // Automaton is in a final state, so flush the buffer and return |
| 1022 | x := dat.array[t].getBase() + uint32(dat.final) |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1023 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1024 | if x < dat.array[1].getCheck() && dat.array[x].getCheck() == t { |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1025 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1026 | if buffi > 0 { |
| 1027 | if DEBUG { |
| 1028 | fmt.Println("-> Flush buffer: [", string(buffer[:buffi]), "]") |
| 1029 | } |
| 1030 | w.Token(0, buffer[:buffi]) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1031 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1032 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1033 | // Add an additional sentence ending, if the file is over but no explicit |
| 1034 | // sentence split was reached. This may be controversial and therefore |
| 1035 | // optional via parameter. |
| 1036 | if !dat.array[t0].isTokenEnd() { |
| 1037 | w.SentenceEnd() |
| 1038 | } |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1039 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1040 | // TODO: |
| 1041 | // There may be a new line at the end, from an epsilon, |
| 1042 | // so we may need to go on! |
| 1043 | return true |
| 1044 | } |
| 1045 | */ |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1046 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1047 | // Check epsilon transitions as long as possible |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1048 | t0 = t |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1049 | t = dat.array[t0].getBase() + uint32(dat.epsilon) |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1050 | a = dat.epsilon |
| 1051 | newchar = false |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1052 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1053 | if dat.array[t].getCheck() == t0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1054 | // Remember state for backtracking to last tokenend state |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1055 | goto PARSECHAR |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 1056 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1057 | } else if epsilonState != 0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1058 | t0 = epsilonState |
| 1059 | epsilonState = 0 // reset |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1060 | buffc = epsilonOffset |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1061 | if DEBUG { |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1062 | fmt.Println("Get from epsilon stack and set buffo!", showBufferNew(buffer, bufft, buffc, buffi)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1063 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1064 | goto PARSECHAR |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1065 | } |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1066 | |
| 1067 | // Add an additional sentence ending, if the file is over but no explicit |
| 1068 | // sentence split was reached. This may be controversial and therefore |
| 1069 | // optional via parameter. |
| 1070 | if !sentenceEnd { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1071 | w.SentenceEnd(0) |
| 1072 | |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1073 | if DEBUG { |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1074 | fmt.Println("Sentence end") |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if !textEnd { |
| 1079 | w.TextEnd(0) |
| 1080 | |
| 1081 | if DEBUG { |
| 1082 | fmt.Println("Text end") |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | return true |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1087 | } |