Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1 | package datokenizer |
| 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 | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 9 | // The maximum number of states is 107.3741.823 (30bit), |
| 10 | // with a loadfactor of ~70, this means roughly 70 million |
| 11 | // states in the FSA, which is sufficient for the current |
| 12 | // job. |
| 13 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 14 | // TODO: |
| 15 | // - replace maxSize with the check value |
| 16 | // - Strip first state and make everything start with 0! |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 17 | // - Add checksum to serialization. |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 18 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 19 | import ( |
| 20 | "bufio" |
| 21 | "compress/gzip" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 22 | "encoding/binary" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 23 | "fmt" |
| 24 | "io" |
| 25 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 26 | "sort" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 27 | "strconv" |
| 28 | "strings" |
| 29 | "unicode/utf8" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 30 | |
| 31 | "github.com/rs/zerolog/log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | const ( |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 35 | PROPS = 1 |
| 36 | SIGMA = 2 |
| 37 | STATES = 3 |
| 38 | NONE = 4 |
| 39 | NEWLINE = '\u000a' |
| 40 | DEBUG = false |
| 41 | MAGIC = "DATOK" |
| 42 | VERSION = uint16(1) |
| 43 | firstBit uint32 = 1 << 31 |
| 44 | secondBit uint32 = 1 << 30 |
| 45 | restBit uint32 = ^uint32(0) &^ (firstBit | secondBit) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 48 | var bo binary.ByteOrder = binary.LittleEndian |
| 49 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 50 | type mapping struct { |
| 51 | source int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 52 | target uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type edge struct { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 56 | inSym int |
| 57 | outSym int |
| 58 | end int |
| 59 | nontoken bool |
| 60 | tokenend bool |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | type Tokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 64 | // sigma map[rune]int |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 65 | sigmaRev map[int]rune |
| 66 | arcCount int |
| 67 | stateCount int |
| 68 | sigmaCount int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 69 | transitions []map[int]*edge |
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 | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 78 | type DaTokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 79 | // sigmaRev map[int]rune |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 80 | sigma map[rune]int |
| 81 | maxSize int |
| 82 | loadFactor float64 |
| 83 | array []uint32 |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 84 | // lastFilledBase uint32 |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 85 | |
| 86 | // Special symbols in sigma |
| 87 | epsilon int |
| 88 | unknown int |
| 89 | identity int |
| 90 | final int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 93 | func LoadFomaFile(file string) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 94 | f, err := os.Open(file) |
| 95 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 96 | log.Error().Err(err) |
| 97 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 98 | } |
| 99 | defer f.Close() |
| 100 | |
| 101 | gz, err := gzip.NewReader(f) |
| 102 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 103 | log.Error().Err(err) |
| 104 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 105 | } |
| 106 | defer gz.Close() |
| 107 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 108 | return ParseFoma(gz) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 111 | func ParseFoma(ior io.Reader) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 112 | r := bufio.NewReader(ior) |
| 113 | |
| 114 | tok := &Tokenizer{ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 115 | sigmaRev: make(map[int]rune), |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 116 | epsilon: -1, |
| 117 | unknown: -1, |
| 118 | identity: -1, |
| 119 | final: -1, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 122 | var state, inSym, outSym, end, final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 123 | |
| 124 | mode := 0 |
| 125 | var elem []string |
| 126 | var elemint [5]int |
| 127 | |
| 128 | for { |
| 129 | line, err := r.ReadString('\n') |
| 130 | if err != nil { |
| 131 | if err == io.EOF { |
| 132 | break |
| 133 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 134 | log.Error().Err(err) |
| 135 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 136 | } |
| 137 | if strings.HasPrefix(line, "##foma-net") { |
| 138 | continue |
| 139 | } |
| 140 | if strings.HasPrefix(line, "##props##") { |
| 141 | mode = PROPS |
| 142 | continue |
| 143 | } |
| 144 | if strings.HasPrefix(line, "##states##") { |
| 145 | mode = STATES |
| 146 | |
| 147 | // Adds a final transition symbol to sigma |
| 148 | // written as '#' in Mizobuchi et al (2000) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 149 | tok.sigmaCount++ |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 150 | tok.final = tok.sigmaCount |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 151 | continue |
| 152 | } |
| 153 | if strings.HasPrefix(line, "##sigma##") { |
| 154 | mode = SIGMA |
| 155 | continue |
| 156 | } |
| 157 | if strings.HasPrefix(line, "##end##") { |
| 158 | mode = NONE |
| 159 | continue |
| 160 | } |
| 161 | |
| 162 | switch mode { |
| 163 | case PROPS: |
| 164 | { |
| 165 | elem = strings.Split(line, " ") |
| 166 | /* |
| 167 | fmt.Println("arity: " + elem[0]) |
| 168 | fmt.Println("arccount: " + elem[1]) |
| 169 | fmt.Println("statecount: " + elem[2]) |
| 170 | fmt.Println("linecount: " + elem[3]) |
| 171 | fmt.Println("finalcount: " + elem[4]) |
| 172 | fmt.Println("pathcount: " + elem[5]) |
| 173 | fmt.Println("is_deterministic: " + elem[6]) |
| 174 | fmt.Println("is_pruned: " + elem[7]) |
| 175 | fmt.Println("is_minimized: " + elem[8]) |
| 176 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 177 | fmt.Println("is_loop_free: " + elem[10]) |
| 178 | fmt.Println("extras: " + elem[11]) |
| 179 | fmt.Println("name: " + elem[12]) |
| 180 | */ |
| 181 | if elem[6] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 182 | log.Error().Msg("The FST needs to be deterministic") |
| 183 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 184 | } |
| 185 | if elem[9] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 186 | log.Error().Msg("The FST needs to be epsilon free") |
| 187 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | elemint[0], err = strconv.Atoi(elem[1]) |
| 191 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 192 | log.Error().Msg("Can't read arccount") |
| 193 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 194 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 195 | tok.arcCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 196 | |
| 197 | // States start at 1 in Mizobuchi et al (2000), |
| 198 | // as the state 0 is associated with a fail. |
| 199 | // Initialize states and transitions |
| 200 | elemint[0], err = strconv.Atoi(elem[2]) |
| 201 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 202 | log.Error().Msg("Can't read statecount") |
| 203 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 204 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 205 | tok.stateCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 206 | tok.transitions = make([]map[int]*edge, elemint[0]+1) |
| 207 | continue |
| 208 | } |
| 209 | case STATES: |
| 210 | { |
| 211 | elem = strings.Split(line[0:len(line)-1], " ") |
| 212 | if elem[0] == "-1" { |
| 213 | continue |
| 214 | } |
| 215 | elemint[0], err = strconv.Atoi(elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 216 | if err != nil { |
| 217 | break |
| 218 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 219 | |
| 220 | if len(elem) > 1 { |
| 221 | elemint[1], err = strconv.Atoi(elem[1]) |
| 222 | if err != nil { |
| 223 | break |
| 224 | } |
| 225 | if len(elem) > 2 { |
| 226 | elemint[2], err = strconv.Atoi(elem[2]) |
| 227 | if err != nil { |
| 228 | break |
| 229 | } |
| 230 | if len(elem) > 3 { |
| 231 | elemint[3], err = strconv.Atoi(elem[3]) |
| 232 | if err != nil { |
| 233 | break |
| 234 | } |
| 235 | if len(elem) > 4 { |
| 236 | elemint[4], err = strconv.Atoi(elem[4]) |
| 237 | if err != nil { |
| 238 | break |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | switch len(elem) { |
| 246 | case 5: |
| 247 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 248 | state = elemint[0] |
| 249 | inSym = elemint[1] |
| 250 | outSym = elemint[2] |
| 251 | end = elemint[3] |
| 252 | final = elemint[4] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 253 | } |
| 254 | case 4: |
| 255 | { |
| 256 | if elemint[1] == -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 257 | state = elemint[0] |
| 258 | final = elemint[3] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 259 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 260 | state = elemint[0] |
| 261 | inSym = elemint[1] |
| 262 | end = elemint[2] |
| 263 | final = elemint[3] |
| 264 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | case 3: |
| 268 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 269 | inSym = elemint[0] |
| 270 | outSym = elemint[1] |
| 271 | end = elemint[2] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 272 | } |
| 273 | case 2: |
| 274 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 275 | inSym = elemint[0] |
| 276 | end = elemint[1] |
| 277 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 281 | // While the states in foma start with 0, the states in the |
| 282 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 283 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 284 | nontoken := false |
| 285 | tokenend := false |
| 286 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 287 | // ID needs to be > 1 |
| 288 | inSym++ |
| 289 | outSym++ |
| 290 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 291 | if inSym != outSym { |
| 292 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 293 | if tok.sigmaRev[outSym] == NEWLINE { |
| 294 | tokenend = true |
| 295 | } else if outSym == tok.epsilon { |
| 296 | nontoken = true |
| 297 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 298 | log.Error().Msg( |
| 299 | "Unsupported transition: " + |
| 300 | strconv.Itoa(state) + |
| 301 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 302 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 303 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 304 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 305 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 306 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 307 | string(tok.sigmaRev[inSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 308 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 309 | string(tok.sigmaRev[outSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 310 | ")") |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 311 | os.Exit(1) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 312 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 313 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 314 | } else if inSym == tok.epsilon { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 315 | log.Error().Msg("Epsilon transitions not supported") |
| 316 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 319 | // This collects all edges until arrstate changes |
| 320 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 321 | // TODO: |
| 322 | // if arrin == EPSILON && arrout == TOKENEND, mark state as newline |
| 323 | // if the next transition is the same, remove TOKENEND and add SENTENCEEND |
| 324 | // This requires to remove the transition alltogether and marks the state instead. |
| 325 | |
| 326 | // TODO: |
| 327 | // if arrout == EPSILON, mark the transition as NOTOKEN |
| 328 | |
| 329 | targetObj := &edge{ |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 330 | inSym: inSym, |
| 331 | outSym: outSym, |
| 332 | end: end + 1, |
| 333 | tokenend: tokenend, |
| 334 | nontoken: nontoken, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 337 | // Initialize outgoing states |
| 338 | if tok.transitions[state+1] == nil { |
| 339 | tok.transitions[state+1] = make(map[int]*edge) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 342 | // Ignore transitions with invalid symbols |
| 343 | if inSym >= 0 { |
| 344 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 345 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 346 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 347 | // Add final transition |
| 348 | if final == 1 { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 349 | tok.transitions[state+1][tok.final] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 352 | if DEBUG { |
| 353 | fmt.Println("Add", |
| 354 | state+1, "->", end+1, |
| 355 | "(", |
| 356 | inSym, |
| 357 | ":", |
| 358 | outSym, |
| 359 | ") (", |
| 360 | string(tok.sigmaRev[inSym]), |
| 361 | ":", |
| 362 | string(tok.sigmaRev[outSym]), |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 363 | ")", |
| 364 | ";", |
| 365 | "TE:", tokenend, |
| 366 | "NT:", nontoken) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 367 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 368 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 369 | continue |
| 370 | } |
| 371 | case SIGMA: |
| 372 | { |
| 373 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 374 | |
| 375 | // Turn string into sigma id |
| 376 | number, err := strconv.Atoi(elem[0]) |
| 377 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 378 | // ID needs to be > 1 |
| 379 | number++ |
| 380 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 381 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 382 | log.Error().Err(err) |
| 383 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 386 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 387 | |
| 388 | var symbol rune |
| 389 | |
| 390 | // Read rune |
| 391 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 392 | symbol = []rune(elem[1])[0] |
| 393 | |
| 394 | // Probably a MCS |
| 395 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 396 | switch elem[1] { |
| 397 | case "@_EPSILON_SYMBOL_@": |
| 398 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 399 | tok.epsilon = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 400 | continue |
| 401 | } |
| 402 | case "@_UNKNOWN_SYMBOL_@": |
| 403 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 404 | tok.unknown = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 405 | continue |
| 406 | } |
| 407 | |
| 408 | case "@_IDENTITY_SYMBOL_@": |
| 409 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 410 | tok.identity = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 411 | continue |
| 412 | } |
| 413 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 414 | { |
| 415 | log.Error().Msg("MCS not supported: " + line) |
| 416 | os.Exit(1) |
| 417 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 420 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 421 | line, err = r.ReadString('\n') |
| 422 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 423 | log.Error().Err(err) |
| 424 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 425 | } |
| 426 | if len(line) != 1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 427 | log.Error().Msg("MCS not supported:" + line) |
| 428 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 429 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 430 | symbol = rune(NEWLINE) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 433 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return tok |
| 439 | } |
| 440 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 441 | // Set alphabet A to the list of all symbols |
| 442 | // outgoing from s |
| 443 | func (tok *Tokenizer) get_set(s int, A *[]int) { |
| 444 | for a := range tok.transitions[s] { |
| 445 | *A = append(*A, a) |
| 446 | } |
| 447 | |
| 448 | // Not required, but simplifies bug hunting |
| 449 | sort.Ints(*A) |
| 450 | } |
| 451 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 452 | // Implementation of Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 453 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 454 | |
| 455 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 456 | sigma: make(map[rune]int), |
| 457 | loadFactor: -1, |
| 458 | final: tok.final, |
| 459 | unknown: tok.unknown, |
| 460 | identity: tok.identity, |
| 461 | epsilon: tok.epsilon, |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 462 | // lastFilledBase: 1, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | for num, sym := range tok.sigmaRev { |
| 466 | dat.sigma[sym] = num |
| 467 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 468 | |
| 469 | mark := 0 |
| 470 | size := 0 |
| 471 | |
| 472 | // Create a mapping from s to t |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 473 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 474 | |
| 475 | table[size] = &mapping{source: 1, target: 1} |
| 476 | size++ |
| 477 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 478 | // Allocate space for the outgoing symbol range |
| 479 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 480 | |
| 481 | for mark < size { |
| 482 | s := table[mark].source // This is a state in Ms |
| 483 | t := table[mark].target // This is a state in Mt |
| 484 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 485 | |
| 486 | // Following the paper, here the state t can be remembered |
| 487 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 488 | A = A[:0] |
| 489 | tok.get_set(s, &A) |
| 490 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 491 | // Set base to the first free slot in the double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 492 | dat.setBase(t, dat.xCheck(A)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 493 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 494 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 495 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 496 | // outdegree of .end |
| 497 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 498 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 499 | for _, a := range A { |
| 500 | |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 501 | if a != tok.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 502 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 503 | // Aka g(s, a) |
| 504 | s1 := tok.transitions[s][a].end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 505 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 506 | // Store the transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 507 | t1 := dat.getBase(t) + uint32(a) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 508 | dat.setCheck(t1, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 509 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 510 | if DEBUG { |
| 511 | fmt.Println("Translate transition", |
| 512 | s, "->", s1, "(", a, ")", "to", t, "->", t1) |
| 513 | } |
| 514 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 515 | // Mark the state as being the target of a nontoken transition |
| 516 | if tok.transitions[s][a].nontoken { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 517 | dat.setNonToken(t1, true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 518 | if DEBUG { |
| 519 | fmt.Println("Set", t1, "to nontoken") |
| 520 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 523 | // Mark the state as being the target of a tokenend transition |
| 524 | if tok.transitions[s][a].tokenend { |
| 525 | dat.setTokenEnd(t1, true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 526 | if DEBUG { |
| 527 | fmt.Println("Set", t1, "to tokenend") |
| 528 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 531 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 532 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 533 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 534 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 535 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 536 | table[size] = &mapping{source: s1, target: t1} |
| 537 | size++ |
| 538 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 539 | // Overwrite with the representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 540 | dat.setBase(t1, r) |
| 541 | dat.setSeparate(t1, true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 542 | } |
| 543 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 544 | // Store a final transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 545 | dat.setCheck(dat.getBase(t)+uint32(dat.final), t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // Following Mizobuchi et al (2000) the size of the |
| 551 | // FSA should be stored in check(1). |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 552 | dat.setSize(dat.maxSize + 1) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 553 | dat.array = dat.array[:dat.maxSize+1] |
| 554 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 557 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 558 | // exists and return this as a representative. |
| 559 | // Currently iterates through the whole table |
| 560 | // in a bruteforce manner. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 561 | func in_table(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 562 | for x := 0; x < size; x++ { |
| 563 | if table[x].source == s { |
| 564 | return table[x].target |
| 565 | } |
| 566 | } |
| 567 | return 0 |
| 568 | } |
| 569 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 570 | // Resize double array when necessary |
| 571 | func (dat *DaTokenizer) resize(l int) { |
| 572 | // TODO: |
| 573 | // This is a bit too aggressive atm and should be calmed down. |
| 574 | if len(dat.array) <= l { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 575 | dat.array = append(dat.array, make([]uint32, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 576 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 577 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 578 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 579 | // Set base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 580 | func (dat *DaTokenizer) setBase(p uint32, v uint32) { |
| 581 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 582 | dat.resize(l) |
| 583 | if dat.maxSize < l { |
| 584 | dat.maxSize = l |
| 585 | } |
| 586 | dat.array[p*2] = v |
| 587 | } |
| 588 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 589 | // Returns true if a state is separate pointing to a representative |
| 590 | func (dat *DaTokenizer) isSeparate(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 591 | return dat.array[p*2]&firstBit != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Mark a state as separate pointing to a representative |
| 595 | func (dat *DaTokenizer) setSeparate(p uint32, sep bool) { |
| 596 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 597 | dat.array[p*2] |= firstBit |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 598 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 599 | dat.array[p*2] &= (restBit | secondBit) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 603 | // Returns true if a state is the target of a nontoken transition |
| 604 | func (dat *DaTokenizer) isNonToken(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 605 | return dat.array[p*2+1]&firstBit != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Mark a state as being the target of a nontoken transition |
| 609 | func (dat *DaTokenizer) setNonToken(p uint32, sep bool) { |
| 610 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 611 | dat.array[p*2+1] |= firstBit |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 612 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 613 | dat.array[p*2+1] &= (restBit | secondBit) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 617 | // Returns true if a state is the target of a tokenend transition |
| 618 | func (dat *DaTokenizer) isTokenEnd(p uint32) bool { |
| 619 | return dat.array[p*2+1]&secondBit != 0 |
| 620 | } |
| 621 | |
| 622 | // Mark a state as being the target of a tokenend transition |
| 623 | func (dat *DaTokenizer) setTokenEnd(p uint32, sep bool) { |
| 624 | if sep { |
| 625 | dat.array[p*2+1] |= secondBit |
| 626 | } else { |
| 627 | dat.array[p*2+1] &= (restBit | firstBit) |
| 628 | } |
| 629 | } |
| 630 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 631 | // Get base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 632 | func (dat *DaTokenizer) getBase(p uint32) uint32 { |
| 633 | if int(p*2) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 634 | return 0 |
| 635 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 636 | return dat.array[p*2] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Set check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 640 | func (dat *DaTokenizer) setCheck(p uint32, v uint32) { |
| 641 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 642 | dat.resize(l) |
| 643 | if dat.maxSize < l { |
| 644 | dat.maxSize = l |
| 645 | } |
| 646 | dat.array[(p*2)+1] = v |
| 647 | } |
| 648 | |
| 649 | // Get check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 650 | func (dat *DaTokenizer) getCheck(p uint32) uint32 { |
| 651 | if int((p*2)+1) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 652 | return 0 |
| 653 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 654 | return dat.array[(p*2)+1] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 658 | func (dat *DaTokenizer) setSize(v int) { |
| 659 | dat.setCheck(1, uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 663 | func (dat *DaTokenizer) GetSize() int { |
| 664 | return int(dat.getCheck(1)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // Based on Mizobuchi et al (2000), p. 124 |
| 668 | // This iterates for every state through the complete double array |
| 669 | // structure until it finds a gap that fits all outgoing transitions |
| 670 | // of the state. This is extremely slow, but is only necessary in the |
| 671 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 672 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 673 | |
| 674 | // Start at the first entry of the double array list |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 675 | base := uint32(1) // dat.lastFilledBase |
| 676 | // skip := false |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 677 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 678 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 679 | /* |
| 680 | if !skip { |
| 681 | if dat.getCheck(base) != 0 { |
| 682 | dat.lastFilledBase = base |
| 683 | } else { |
| 684 | skip = true |
| 685 | } |
| 686 | } |
| 687 | */ |
| 688 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 689 | // Resize the array if necessary |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 690 | dat.resize((int(base) + dat.final) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 691 | for _, a := range symbols { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 692 | if dat.getCheck(base+uint32(a)) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 693 | base++ |
| 694 | goto OVERLAP |
| 695 | } |
| 696 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 697 | return base |
| 698 | } |
| 699 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 700 | // LoadFactor as defined in Kanda et al (2018), |
| 701 | // i.e. the proportion of non-empty elements to all elements. |
| 702 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 703 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 704 | // Cache the loadfactor |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 705 | if dat.loadFactor > 0 { |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 706 | return dat.loadFactor |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 707 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 708 | nonEmpty := 0 |
| 709 | all := len(dat.array) / 2 |
| 710 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 711 | if dat.array[x] != 0 { |
| 712 | nonEmpty++ |
| 713 | } |
| 714 | } |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 715 | dat.loadFactor = float64(nonEmpty) / float64(all) * 100 |
| 716 | return dat.loadFactor |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 719 | // WriteTo stores the double array data in an io.Writer. |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 720 | func (dat *DaTokenizer) Save(file string) (n int64, err error) { |
| 721 | f, err := os.Create(file) |
| 722 | if err != nil { |
| 723 | log.Error().Err(err) |
| 724 | return 0, nil |
| 725 | } |
| 726 | defer f.Close() |
| 727 | gz := gzip.NewWriter(f) |
| 728 | defer gz.Close() |
| 729 | n, err = dat.WriteTo(gz) |
| 730 | if err != nil { |
| 731 | log.Error().Err(err) |
| 732 | return n, err |
| 733 | } |
| 734 | gz.Flush() |
| 735 | return n, nil |
| 736 | } |
| 737 | |
| 738 | // WriteTo stores the double array data in an io.Writer. |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 739 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 740 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 741 | wb := bufio.NewWriter(w) |
| 742 | defer wb.Flush() |
| 743 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 744 | // Store magical header |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 745 | all, err := wb.Write([]byte(MAGIC)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 746 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 747 | log.Error().Err(err) |
| 748 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | // Get sigma as a list |
| 752 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 753 | max := 0 |
| 754 | for sym, num := range dat.sigma { |
| 755 | sigmalist[num] = sym |
| 756 | if num > max { |
| 757 | max = num |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | sigmalist = sigmalist[:max+1] |
| 762 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 763 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 764 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 765 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 766 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 767 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 768 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 769 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 770 | bo.PutUint32(buf[12:16], uint32(len(dat.array))) |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 771 | more, err := wb.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 772 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 773 | log.Error().Err(err) |
| 774 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | all += more |
| 778 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 779 | // wbuf := bytes.NewBuffer(nil) |
| 780 | // wbufWrap := bufio.NewWriter(wbuf) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 781 | |
| 782 | // Write sigma |
| 783 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 784 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 785 | more, err = wb.WriteRune(sym) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 786 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 787 | log.Error().Err(err) |
| 788 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 789 | } |
| 790 | all += more |
| 791 | } |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 792 | // wbufWrap.Flush() |
| 793 | // more, err = w.Write(wbuf.Bytes()) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 794 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 795 | log.Error().Err(err) |
| 796 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 797 | } |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 798 | // all += more |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 799 | |
| 800 | // Test marker - could be checksum |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 801 | more, err = wb.Write([]byte("T")) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 802 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 803 | log.Error().Err(err) |
| 804 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 805 | } |
| 806 | all += more |
| 807 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 808 | // wbuf.Reset() |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 809 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 810 | for x := 0; x < len(dat.array); x++ { |
| 811 | // for _, d := range dat.array { |
| 812 | bo.PutUint32(buf[0:4], dat.array[x]) |
| 813 | more, err := wb.Write(buf[0:4]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 814 | if err != nil { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 815 | log.Error().Err(err) |
| 816 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 817 | } |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 818 | if more != 4 { |
| 819 | log.Error().Msg("Can not write uint32") |
| 820 | return int64(all), err |
| 821 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 822 | all += more |
| 823 | } |
| 824 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 825 | // wbufWrap.Flush() |
| 826 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 827 | return int64(all), err |
| 828 | } |
| 829 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 830 | func LoadDatokFile(file string) *DaTokenizer { |
| 831 | f, err := os.Open(file) |
| 832 | if err != nil { |
| 833 | log.Error().Err(err) |
| 834 | os.Exit(0) |
| 835 | } |
| 836 | defer f.Close() |
| 837 | |
| 838 | gz, err := gzip.NewReader(f) |
| 839 | if err != nil { |
| 840 | log.Error().Err(err) |
| 841 | os.Exit(0) |
| 842 | } |
| 843 | defer gz.Close() |
| 844 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 845 | // Todo: Read the whole file! |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 846 | return ParseDatok(gz) |
| 847 | } |
| 848 | |
| 849 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 850 | |
| 851 | dat := &DaTokenizer{ |
| 852 | sigma: make(map[rune]int), |
| 853 | epsilon: 0, |
| 854 | unknown: 0, |
| 855 | identity: 0, |
| 856 | final: 0, |
| 857 | loadFactor: 0, |
| 858 | } |
| 859 | |
| 860 | r := bufio.NewReader(ior) |
| 861 | |
| 862 | all := 0 |
| 863 | |
| 864 | buf := make([]byte, 1024) |
| 865 | buf = buf[0:len(MAGIC)] |
| 866 | |
| 867 | more, err := r.Read(buf) |
| 868 | |
| 869 | if err != nil { |
| 870 | log.Error().Err(err) |
| 871 | return nil |
| 872 | } |
| 873 | |
| 874 | all += more |
| 875 | |
| 876 | if string(MAGIC) != string(buf) { |
| 877 | log.Error().Msg("Not a datok file") |
| 878 | return nil |
| 879 | } |
| 880 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 881 | more, err = io.ReadFull(r, buf[0:16]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 882 | if err != nil { |
| 883 | log.Error().Err(err) |
| 884 | return nil |
| 885 | } |
| 886 | |
| 887 | all += more |
| 888 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 889 | version := bo.Uint16(buf[0:2]) |
| 890 | |
| 891 | if version != VERSION { |
| 892 | log.Error().Msg("Version not compatible") |
| 893 | return nil |
| 894 | } |
| 895 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 896 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 897 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 898 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 899 | dat.final = int(bo.Uint16(buf[8:10])) |
| 900 | |
| 901 | sigmaCount := int(bo.Uint16(buf[10:12])) |
| 902 | arraySize := int(bo.Uint32(buf[12:16])) |
| 903 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 904 | // Shouldn't be relevant though |
| 905 | dat.maxSize = arraySize - 1 |
| 906 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 907 | for x := 0; x < sigmaCount; x++ { |
| 908 | sym, more, err := r.ReadRune() |
| 909 | if err == nil && sym != 0 { |
| 910 | dat.sigma[sym] = x |
| 911 | } |
| 912 | all += more |
| 913 | } |
| 914 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 915 | more, err = io.ReadFull(r, buf[0:1]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 916 | |
| 917 | if err != nil { |
| 918 | log.Error().Err(err) |
| 919 | return nil |
| 920 | } |
| 921 | |
| 922 | all += more |
| 923 | |
| 924 | if string("T") != string(buf[0:1]) { |
| 925 | log.Error().Msg("Not a datok file") |
| 926 | return nil |
| 927 | } |
| 928 | |
| 929 | // Read based on length |
| 930 | dat.array = make([]uint32, arraySize) |
| 931 | |
| 932 | for x := 0; x < arraySize; x++ { |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 933 | more, err = io.ReadFull(r, buf[0:4]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 934 | if err != nil { |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame^] | 935 | if err == io.EOF { |
| 936 | fmt.Println(arraySize, x) |
| 937 | break |
| 938 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 939 | log.Error().Err(err) |
| 940 | return nil |
| 941 | } |
| 942 | all += more |
| 943 | dat.array[x] = bo.Uint32(buf[0:4]) |
| 944 | } |
| 945 | |
| 946 | return dat |
| 947 | } |
| 948 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 949 | // Match an input string against the double array |
| 950 | // FSA. |
| 951 | // |
| 952 | // Based on Mizobuchi et al (2000), p. 129, |
| 953 | // with additional support for IDENTITY, UNKNOWN |
| 954 | // and EPSILON transitions. |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 955 | func (dat *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 956 | var a int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 957 | var tu uint32 |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 958 | var ok bool |
| 959 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 960 | t := uint32(1) // Initial state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 961 | chars := []rune(input) |
| 962 | i := 0 |
| 963 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 964 | for i < len(chars) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 965 | a, ok = dat.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 966 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 967 | // Support identity symbol if character is not in sigma |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 968 | if !ok && dat.identity != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 969 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 970 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 971 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 972 | a = dat.identity |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 973 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 974 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 975 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 976 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 977 | CHECK: |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 978 | t = dat.getBase(tu) + uint32(a) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 979 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 980 | // Check if the transition is valid according to the double array |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 981 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 982 | |
| 983 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 984 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 985 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 986 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 987 | if !ok && a == dat.identity { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 988 | // Try again with unknown symbol, in case identity failed |
| 989 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 990 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 991 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 992 | a = dat.unknown |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 993 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 994 | } else if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 995 | // Try again with epsilon symbol, in case everything else failed |
| 996 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 997 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 998 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 999 | a = dat.epsilon |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1000 | } else { |
| 1001 | break |
| 1002 | } |
| 1003 | goto CHECK |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1004 | } else if dat.isSeparate(t) { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 1005 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1006 | t = dat.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1007 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1008 | |
| 1009 | // Transition is fine |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 1010 | if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1011 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 1012 | i++ |
| 1013 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 1014 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1015 | // TODO: |
| 1016 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1019 | if i != len(chars) { |
| 1020 | if DEBUG { |
| 1021 | fmt.Println("Not at the end") |
| 1022 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1023 | return false |
| 1024 | } |
| 1025 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1026 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1027 | |
| 1028 | // Automaton is in a final state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1029 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1030 | return true |
| 1031 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1032 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1033 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1034 | tu = t |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1035 | t = dat.getBase(tu) + uint32(dat.epsilon) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1036 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1037 | // Epsilon transition failed |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 1038 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1039 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 1040 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1041 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1042 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1043 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1044 | } else if dat.isSeparate(t) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1045 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 1046 | t = dat.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1047 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 1048 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 1049 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 1050 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1051 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1052 | // Transduce an input string against the double array |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1053 | // FSA. |
| 1054 | // |
| 1055 | // Based on Match with additional support |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1056 | // for NONTOKEN and TOKENEND handling |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1057 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1058 | var a int |
| 1059 | var tu uint32 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1060 | var ok, nontoken, tokenend bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1061 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1062 | reader := bufio.NewReader(r) |
| 1063 | writer := bufio.NewWriter(w) |
| 1064 | defer writer.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1065 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1066 | t := uint32(1) // Initial state |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1067 | skip := false |
| 1068 | |
| 1069 | var char rune |
| 1070 | var err error |
| 1071 | eof := false |
| 1072 | |
| 1073 | for { |
| 1074 | |
| 1075 | if !skip { |
| 1076 | char, _, err = reader.ReadRune() |
| 1077 | if err != nil { |
| 1078 | eof = true |
| 1079 | break |
| 1080 | } |
| 1081 | } |
| 1082 | skip = false |
| 1083 | |
| 1084 | a, ok = dat.sigma[char] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1085 | |
| 1086 | // Support identity symbol if character is not in sigma |
| 1087 | if !ok && dat.identity != -1 { |
| 1088 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1089 | fmt.Println("IDENTITY symbol", string(char), "->", dat.identity) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1090 | } |
| 1091 | a = dat.identity |
| 1092 | } else if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1093 | fmt.Println("Sigma transition is okay for [", string(char), "]") |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1094 | } |
| 1095 | tu = t |
| 1096 | CHECK: |
| 1097 | nontoken = false |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1098 | tokenend = false |
| 1099 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1100 | t = dat.getBase(tu) + uint32(a) |
| 1101 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1102 | if DEBUG { |
| 1103 | fmt.Println("Check", tu, "-", a, "(", string(char), ")", "->", t) |
| 1104 | } |
| 1105 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1106 | // Check if the transition is valid according to the double array |
| 1107 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 1108 | |
| 1109 | if DEBUG { |
| 1110 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 1111 | } |
| 1112 | |
| 1113 | if !ok && a == dat.identity { |
| 1114 | // Try again with unknown symbol, in case identity failed |
| 1115 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1116 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1117 | } |
| 1118 | a = dat.unknown |
| 1119 | |
| 1120 | } else if a != dat.epsilon { |
| 1121 | // Try again with epsilon symbol, in case everything else failed |
| 1122 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1123 | fmt.Println("EPSILON symbol", string(char), "->", dat.epsilon) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1124 | } |
| 1125 | a = dat.epsilon |
| 1126 | } else { |
| 1127 | break |
| 1128 | } |
| 1129 | goto CHECK |
| 1130 | } else if dat.isSeparate(t) { |
| 1131 | // Move to representative state |
| 1132 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1133 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1134 | |
| 1135 | t = dat.getBase(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1136 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1137 | if DEBUG { |
| 1138 | fmt.Println("Representative pointing to", t) |
| 1139 | } |
| 1140 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1141 | } else { |
| 1142 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1143 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | // Transition is fine |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1147 | if a == dat.epsilon { |
| 1148 | skip = true |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1149 | // Character consumed |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1150 | } else if !nontoken { |
| 1151 | writer.WriteRune(char) |
| 1152 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1153 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1154 | if DEBUG { |
| 1155 | fmt.Println(" --> ok!") |
| 1156 | } |
| 1157 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1158 | /* |
| 1159 | if nontoken { |
| 1160 | writer.WriteRune(("<|>") |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1161 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1162 | */ |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1163 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1164 | if tokenend { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1165 | writer.WriteRune('\n') |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1168 | // TODO: |
| 1169 | // Prevent endless epsilon loops! |
| 1170 | } |
| 1171 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1172 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1173 | if DEBUG { |
| 1174 | fmt.Println("Not at the end") |
| 1175 | } |
| 1176 | return false |
| 1177 | } |
| 1178 | |
| 1179 | FINALCHECK: |
| 1180 | |
| 1181 | // Automaton is in a final state |
| 1182 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1183 | /* |
| 1184 | if dat.isNonToken(t) { |
| 1185 | fmt.Print("<|>") |
| 1186 | } |
| 1187 | */ |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1188 | if dat.isTokenEnd(t) { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1189 | writer.WriteRune('\n') |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | // There may be a new line at the end, from an epsilon, so we go on! |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1193 | return true |
| 1194 | } |
| 1195 | |
| 1196 | // Check epsilon transitions until a final state is reached |
| 1197 | tu = t |
| 1198 | t = dat.getBase(tu) + uint32(dat.epsilon) |
| 1199 | |
| 1200 | // Epsilon transition failed |
| 1201 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 1202 | if DEBUG { |
| 1203 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 1204 | } |
| 1205 | return false |
| 1206 | |
| 1207 | } else if dat.isSeparate(t) { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1208 | // nontoken = dat.isNonToken(t) |
| 1209 | tokenend = dat.isTokenEnd(t) |
| 1210 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1211 | // Move to representative state |
| 1212 | t = dat.getBase(t) |
| 1213 | } else { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1214 | tokenend = dat.isTokenEnd(t) |
| 1215 | // nontoken = dat.isNonToken(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1218 | /* |
| 1219 | if nontoken { |
| 1220 | fmt.Print("<|>") |
| 1221 | } |
| 1222 | */ |
| 1223 | |
| 1224 | if tokenend { |
| 1225 | writer.WriteRune('\n') |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | goto FINALCHECK |
| 1229 | } |