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 | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 17 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 18 | import ( |
| 19 | "bufio" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 20 | "bytes" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 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 | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 84 | |
| 85 | // Special symbols in sigma |
| 86 | epsilon int |
| 87 | unknown int |
| 88 | identity int |
| 89 | final int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 92 | func LoadFomaFile(file string) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 93 | f, err := os.Open(file) |
| 94 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 95 | log.Error().Err(err) |
| 96 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 97 | } |
| 98 | defer f.Close() |
| 99 | |
| 100 | gz, err := gzip.NewReader(f) |
| 101 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 102 | log.Error().Err(err) |
| 103 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 104 | } |
| 105 | defer gz.Close() |
| 106 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 107 | return ParseFoma(gz) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 110 | func ParseFoma(ior io.Reader) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 111 | r := bufio.NewReader(ior) |
| 112 | |
| 113 | tok := &Tokenizer{ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 114 | sigmaRev: make(map[int]rune), |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 115 | epsilon: -1, |
| 116 | unknown: -1, |
| 117 | identity: -1, |
| 118 | final: -1, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 121 | var state, inSym, outSym, end, final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 122 | |
| 123 | mode := 0 |
| 124 | var elem []string |
| 125 | var elemint [5]int |
| 126 | |
| 127 | for { |
| 128 | line, err := r.ReadString('\n') |
| 129 | if err != nil { |
| 130 | if err == io.EOF { |
| 131 | break |
| 132 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 133 | log.Error().Err(err) |
| 134 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 135 | } |
| 136 | if strings.HasPrefix(line, "##foma-net") { |
| 137 | continue |
| 138 | } |
| 139 | if strings.HasPrefix(line, "##props##") { |
| 140 | mode = PROPS |
| 141 | continue |
| 142 | } |
| 143 | if strings.HasPrefix(line, "##states##") { |
| 144 | mode = STATES |
| 145 | |
| 146 | // Adds a final transition symbol to sigma |
| 147 | // written as '#' in Mizobuchi et al (2000) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 148 | tok.sigmaCount++ |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 149 | tok.final = tok.sigmaCount |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 150 | continue |
| 151 | } |
| 152 | if strings.HasPrefix(line, "##sigma##") { |
| 153 | mode = SIGMA |
| 154 | continue |
| 155 | } |
| 156 | if strings.HasPrefix(line, "##end##") { |
| 157 | mode = NONE |
| 158 | continue |
| 159 | } |
| 160 | |
| 161 | switch mode { |
| 162 | case PROPS: |
| 163 | { |
| 164 | elem = strings.Split(line, " ") |
| 165 | /* |
| 166 | fmt.Println("arity: " + elem[0]) |
| 167 | fmt.Println("arccount: " + elem[1]) |
| 168 | fmt.Println("statecount: " + elem[2]) |
| 169 | fmt.Println("linecount: " + elem[3]) |
| 170 | fmt.Println("finalcount: " + elem[4]) |
| 171 | fmt.Println("pathcount: " + elem[5]) |
| 172 | fmt.Println("is_deterministic: " + elem[6]) |
| 173 | fmt.Println("is_pruned: " + elem[7]) |
| 174 | fmt.Println("is_minimized: " + elem[8]) |
| 175 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 176 | fmt.Println("is_loop_free: " + elem[10]) |
| 177 | fmt.Println("extras: " + elem[11]) |
| 178 | fmt.Println("name: " + elem[12]) |
| 179 | */ |
| 180 | if elem[6] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 181 | log.Error().Msg("The FST needs to be deterministic") |
| 182 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 183 | } |
| 184 | if elem[9] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 185 | log.Error().Msg("The FST needs to be epsilon free") |
| 186 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | elemint[0], err = strconv.Atoi(elem[1]) |
| 190 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 191 | log.Error().Msg("Can't read arccount") |
| 192 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 193 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 194 | tok.arcCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 195 | |
| 196 | // States start at 1 in Mizobuchi et al (2000), |
| 197 | // as the state 0 is associated with a fail. |
| 198 | // Initialize states and transitions |
| 199 | elemint[0], err = strconv.Atoi(elem[2]) |
| 200 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 201 | log.Error().Msg("Can't read statecount") |
| 202 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 203 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 204 | tok.stateCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 205 | tok.transitions = make([]map[int]*edge, elemint[0]+1) |
| 206 | continue |
| 207 | } |
| 208 | case STATES: |
| 209 | { |
| 210 | elem = strings.Split(line[0:len(line)-1], " ") |
| 211 | if elem[0] == "-1" { |
| 212 | continue |
| 213 | } |
| 214 | elemint[0], err = strconv.Atoi(elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 215 | if err != nil { |
| 216 | break |
| 217 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 218 | |
| 219 | if len(elem) > 1 { |
| 220 | elemint[1], err = strconv.Atoi(elem[1]) |
| 221 | if err != nil { |
| 222 | break |
| 223 | } |
| 224 | if len(elem) > 2 { |
| 225 | elemint[2], err = strconv.Atoi(elem[2]) |
| 226 | if err != nil { |
| 227 | break |
| 228 | } |
| 229 | if len(elem) > 3 { |
| 230 | elemint[3], err = strconv.Atoi(elem[3]) |
| 231 | if err != nil { |
| 232 | break |
| 233 | } |
| 234 | if len(elem) > 4 { |
| 235 | elemint[4], err = strconv.Atoi(elem[4]) |
| 236 | if err != nil { |
| 237 | break |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | switch len(elem) { |
| 245 | case 5: |
| 246 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 247 | state = elemint[0] |
| 248 | inSym = elemint[1] |
| 249 | outSym = elemint[2] |
| 250 | end = elemint[3] |
| 251 | final = elemint[4] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 252 | } |
| 253 | case 4: |
| 254 | { |
| 255 | if elemint[1] == -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 256 | state = elemint[0] |
| 257 | final = elemint[3] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 258 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 259 | state = elemint[0] |
| 260 | inSym = elemint[1] |
| 261 | end = elemint[2] |
| 262 | final = elemint[3] |
| 263 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | case 3: |
| 267 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 268 | inSym = elemint[0] |
| 269 | outSym = elemint[1] |
| 270 | end = elemint[2] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 271 | } |
| 272 | case 2: |
| 273 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 274 | inSym = elemint[0] |
| 275 | end = elemint[1] |
| 276 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 280 | // While the states in foma start with 0, the states in the |
| 281 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 282 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 283 | nontoken := false |
| 284 | tokenend := false |
| 285 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 286 | if inSym != outSym { |
| 287 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 288 | if tok.sigmaRev[outSym] == NEWLINE { |
| 289 | tokenend = true |
| 290 | } else if outSym == tok.epsilon { |
| 291 | nontoken = true |
| 292 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 293 | log.Error().Msg( |
| 294 | "Unsupported transition: " + |
| 295 | strconv.Itoa(state) + |
| 296 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 297 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 298 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 299 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 300 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 301 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 302 | string(tok.sigmaRev[inSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 303 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 304 | string(tok.sigmaRev[outSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 305 | ")") |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 306 | os.Exit(1) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 307 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 308 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 309 | } else if inSym == tok.epsilon { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 310 | log.Error().Msg("Epsilon transitions not supported") |
| 311 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 314 | // This collects all edges until arrstate changes |
| 315 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 316 | // TODO: |
| 317 | // if arrin == EPSILON && arrout == TOKENEND, mark state as newline |
| 318 | // if the next transition is the same, remove TOKENEND and add SENTENCEEND |
| 319 | // This requires to remove the transition alltogether and marks the state instead. |
| 320 | |
| 321 | // TODO: |
| 322 | // if arrout == EPSILON, mark the transition as NOTOKEN |
| 323 | |
| 324 | targetObj := &edge{ |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 325 | inSym: inSym, |
| 326 | outSym: outSym, |
| 327 | end: end + 1, |
| 328 | tokenend: tokenend, |
| 329 | nontoken: nontoken, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 332 | // Initialize outgoing states |
| 333 | if tok.transitions[state+1] == nil { |
| 334 | tok.transitions[state+1] = make(map[int]*edge) |
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 | // Ignore transitions with invalid symbols |
| 338 | if inSym >= 0 { |
| 339 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 340 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 341 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 342 | // Add final transition |
| 343 | if final == 1 { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 344 | tok.transitions[state+1][tok.final] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 347 | if DEBUG { |
| 348 | fmt.Println("Add", |
| 349 | state+1, "->", end+1, |
| 350 | "(", |
| 351 | inSym, |
| 352 | ":", |
| 353 | outSym, |
| 354 | ") (", |
| 355 | string(tok.sigmaRev[inSym]), |
| 356 | ":", |
| 357 | string(tok.sigmaRev[outSym]), |
| 358 | ")") |
| 359 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 360 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 361 | continue |
| 362 | } |
| 363 | case SIGMA: |
| 364 | { |
| 365 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 366 | |
| 367 | // Turn string into sigma id |
| 368 | number, err := strconv.Atoi(elem[0]) |
| 369 | |
| 370 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 371 | log.Error().Err(err) |
| 372 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 375 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 376 | |
| 377 | var symbol rune |
| 378 | |
| 379 | // Read rune |
| 380 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 381 | symbol = []rune(elem[1])[0] |
| 382 | |
| 383 | // Probably a MCS |
| 384 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 385 | switch elem[1] { |
| 386 | case "@_EPSILON_SYMBOL_@": |
| 387 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 388 | tok.epsilon = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 389 | continue |
| 390 | } |
| 391 | case "@_UNKNOWN_SYMBOL_@": |
| 392 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 393 | tok.unknown = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 394 | continue |
| 395 | } |
| 396 | |
| 397 | case "@_IDENTITY_SYMBOL_@": |
| 398 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 399 | tok.identity = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 400 | continue |
| 401 | } |
| 402 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 403 | { |
| 404 | log.Error().Msg("MCS not supported: " + line) |
| 405 | os.Exit(1) |
| 406 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 409 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 410 | line, err = r.ReadString('\n') |
| 411 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 412 | log.Error().Err(err) |
| 413 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 414 | } |
| 415 | if len(line) != 1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 416 | log.Error().Msg("MCS not supported:" + line) |
| 417 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 418 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 419 | symbol = rune(NEWLINE) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 422 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return tok |
| 428 | } |
| 429 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 430 | // Set alphabet A to the list of all symbols |
| 431 | // outgoing from s |
| 432 | func (tok *Tokenizer) get_set(s int, A *[]int) { |
| 433 | for a := range tok.transitions[s] { |
| 434 | *A = append(*A, a) |
| 435 | } |
| 436 | |
| 437 | // Not required, but simplifies bug hunting |
| 438 | sort.Ints(*A) |
| 439 | } |
| 440 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 441 | // Implementation of Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 442 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 443 | |
| 444 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 445 | sigma: make(map[rune]int), |
| 446 | loadFactor: -1, |
| 447 | final: tok.final, |
| 448 | unknown: tok.unknown, |
| 449 | identity: tok.identity, |
| 450 | epsilon: tok.epsilon, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | for num, sym := range tok.sigmaRev { |
| 454 | dat.sigma[sym] = num |
| 455 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 456 | |
| 457 | mark := 0 |
| 458 | size := 0 |
| 459 | |
| 460 | // Create a mapping from s to t |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 461 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 462 | |
| 463 | table[size] = &mapping{source: 1, target: 1} |
| 464 | size++ |
| 465 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 466 | // Allocate space for the outgoing symbol range |
| 467 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 468 | |
| 469 | for mark < size { |
| 470 | s := table[mark].source // This is a state in Ms |
| 471 | t := table[mark].target // This is a state in Mt |
| 472 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 473 | |
| 474 | // Following the paper, here the state t can be remembered |
| 475 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 476 | A = A[:0] |
| 477 | tok.get_set(s, &A) |
| 478 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 479 | // Set base to the first free slot in the double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 480 | dat.setBase(t, dat.xCheck(A)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 481 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 482 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 483 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 484 | // outdegree of .end |
| 485 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 486 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 487 | for _, a := range A { |
| 488 | |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 489 | if a != tok.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 490 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 491 | // Aka g(s, a) |
| 492 | s1 := tok.transitions[s][a].end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 493 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 494 | // Store the transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 495 | t1 := dat.getBase(t) + uint32(a) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 496 | dat.setCheck(t1, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 497 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 498 | // Mark the state as being the target of a nontoken transition |
| 499 | if tok.transitions[s][a].nontoken { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 500 | dat.setNonToken(t1, true) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 503 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 504 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 505 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 506 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 507 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 508 | table[size] = &mapping{source: s1, target: t1} |
| 509 | size++ |
| 510 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 511 | // Overwrite with the representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 512 | dat.setBase(t1, r) |
| 513 | dat.setSeparate(t1, true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 514 | } |
| 515 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 516 | // Store a final transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 517 | dat.setCheck(dat.getBase(t)+uint32(dat.final), t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Following Mizobuchi et al (2000) the size of the |
| 523 | // FSA should be stored in check(1). |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 524 | dat.setSize(dat.maxSize + 1) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 525 | dat.array = dat.array[:dat.maxSize+1] |
| 526 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 529 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 530 | // exists and return this as a representative. |
| 531 | // Currently iterates through the whole table |
| 532 | // in a bruteforce manner. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 533 | func in_table(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 534 | for x := 0; x < size; x++ { |
| 535 | if table[x].source == s { |
| 536 | return table[x].target |
| 537 | } |
| 538 | } |
| 539 | return 0 |
| 540 | } |
| 541 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 542 | // Resize double array when necessary |
| 543 | func (dat *DaTokenizer) resize(l int) { |
| 544 | // TODO: |
| 545 | // This is a bit too aggressive atm and should be calmed down. |
| 546 | if len(dat.array) <= l { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 547 | dat.array = append(dat.array, make([]uint32, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 548 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 549 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 550 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 551 | // Set base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 552 | func (dat *DaTokenizer) setBase(p uint32, v uint32) { |
| 553 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 554 | dat.resize(l) |
| 555 | if dat.maxSize < l { |
| 556 | dat.maxSize = l |
| 557 | } |
| 558 | dat.array[p*2] = v |
| 559 | } |
| 560 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 561 | // Returns true if a state is separate pointing to a representative |
| 562 | func (dat *DaTokenizer) isSeparate(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame^] | 563 | return dat.array[p*2]&firstBit != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Mark a state as separate pointing to a representative |
| 567 | func (dat *DaTokenizer) setSeparate(p uint32, sep bool) { |
| 568 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame^] | 569 | dat.array[p*2] |= firstBit |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 570 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame^] | 571 | dat.array[p*2] &= (restBit | secondBit) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 575 | // Returns true if a state is the target of a nontoken transition |
| 576 | func (dat *DaTokenizer) isNonToken(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame^] | 577 | return dat.array[p*2+1]&firstBit != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Mark a state as being the target of a nontoken transition |
| 581 | func (dat *DaTokenizer) setNonToken(p uint32, sep bool) { |
| 582 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame^] | 583 | dat.array[p*2+1] |= firstBit |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 584 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame^] | 585 | dat.array[p*2+1] &= (restBit | secondBit) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 589 | // Get base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 590 | func (dat *DaTokenizer) getBase(p uint32) uint32 { |
| 591 | if int(p*2) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 592 | return 0 |
| 593 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 594 | return dat.array[p*2] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Set check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 598 | func (dat *DaTokenizer) setCheck(p uint32, v uint32) { |
| 599 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 600 | dat.resize(l) |
| 601 | if dat.maxSize < l { |
| 602 | dat.maxSize = l |
| 603 | } |
| 604 | dat.array[(p*2)+1] = v |
| 605 | } |
| 606 | |
| 607 | // Get check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 608 | func (dat *DaTokenizer) getCheck(p uint32) uint32 { |
| 609 | if int((p*2)+1) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 610 | return 0 |
| 611 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 612 | return dat.array[(p*2)+1] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 616 | func (dat *DaTokenizer) setSize(v int) { |
| 617 | dat.setCheck(1, uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 621 | func (dat *DaTokenizer) GetSize() int { |
| 622 | return int(dat.getCheck(1)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // Based on Mizobuchi et al (2000), p. 124 |
| 626 | // This iterates for every state through the complete double array |
| 627 | // structure until it finds a gap that fits all outgoing transitions |
| 628 | // of the state. This is extremely slow, but is only necessary in the |
| 629 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 630 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 631 | |
| 632 | // Start at the first entry of the double array list |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 633 | base := uint32(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 634 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 635 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 636 | |
| 637 | // Resize the array if necessary |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 638 | dat.resize((int(base) + dat.final) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 639 | for _, a := range symbols { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 640 | if dat.getCheck(base+uint32(a)) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 641 | base++ |
| 642 | goto OVERLAP |
| 643 | } |
| 644 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 645 | return base |
| 646 | } |
| 647 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 648 | // LoadFactor as defined in Kanda et al (2018), |
| 649 | // i.e. the proportion of non-empty elements to all elements. |
| 650 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 651 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 652 | // Cache the loadfactor |
| 653 | if dat.loadFactor >= 0 { |
| 654 | return dat.loadFactor |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 655 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 656 | nonEmpty := 0 |
| 657 | all := len(dat.array) / 2 |
| 658 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 659 | if dat.array[x] != 0 { |
| 660 | nonEmpty++ |
| 661 | } |
| 662 | } |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 663 | dat.loadFactor = float64(nonEmpty) / float64(all) * 100 |
| 664 | return dat.loadFactor |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 667 | // WriteTo stores the double array data in an io.Writer. |
| 668 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 669 | |
| 670 | // Store magical header |
| 671 | all, err := w.Write([]byte(MAGIC)) |
| 672 | if err != nil { |
| 673 | log.Error().Msg("Unable to write data") |
| 674 | } |
| 675 | |
| 676 | // Get sigma as a list |
| 677 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 678 | max := 0 |
| 679 | for sym, num := range dat.sigma { |
| 680 | sigmalist[num] = sym |
| 681 | if num > max { |
| 682 | max = num |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | sigmalist = sigmalist[:max+1] |
| 687 | |
| 688 | buf := make([]byte, 0, 12) |
| 689 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 690 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 691 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 692 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 693 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 694 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
| 695 | more, err := w.Write(buf[0:12]) |
| 696 | if err != nil { |
| 697 | log.Error().Msg("Unable to write data") |
| 698 | } |
| 699 | |
| 700 | all += more |
| 701 | |
| 702 | wbuf := bytes.NewBuffer(nil) |
| 703 | wbufWrap := bufio.NewWriter(wbuf) |
| 704 | |
| 705 | // Write sigma |
| 706 | for _, sym := range sigmalist { |
| 707 | more, err = wbufWrap.WriteRune(sym) |
| 708 | if err != nil { |
| 709 | log.Error().Msg("Unable to write data") |
| 710 | } |
| 711 | all += more |
| 712 | } |
| 713 | wbufWrap.Flush() |
| 714 | more, err = w.Write(wbuf.Bytes()) |
| 715 | if err != nil { |
| 716 | log.Error().Msg("Unable to write data") |
| 717 | } |
| 718 | all += more |
| 719 | |
| 720 | // Test marker - could be checksum |
| 721 | more, err = w.Write([]byte("T")) |
| 722 | if err != nil { |
| 723 | log.Error().Msg("Unable to write data") |
| 724 | } |
| 725 | all += more |
| 726 | |
| 727 | wbuf.Reset() |
| 728 | |
| 729 | for _, d := range dat.array { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 730 | bo.PutUint32(buf[0:4], d) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 731 | more, err := w.Write(buf[0:4]) |
| 732 | if err != nil { |
| 733 | log.Error().Msg("Unable to write data") |
| 734 | } |
| 735 | all += more |
| 736 | } |
| 737 | |
| 738 | return int64(all), err |
| 739 | } |
| 740 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 741 | // Match an input string against the double array |
| 742 | // FSA. |
| 743 | // |
| 744 | // Based on Mizobuchi et al (2000), p. 129, |
| 745 | // with additional support for IDENTITY, UNKNOWN |
| 746 | // and EPSILON transitions. |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 747 | func (dat *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 748 | var a int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 749 | var tu uint32 |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 750 | var ok bool |
| 751 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 752 | t := uint32(1) // Initial state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 753 | chars := []rune(input) |
| 754 | i := 0 |
| 755 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 756 | for i < len(chars) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 757 | a, ok = dat.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 758 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 759 | // Support identity symbol if character is not in sigma |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 760 | if !ok && dat.identity != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 761 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 762 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 763 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 764 | a = dat.identity |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 765 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 766 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 767 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 768 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 769 | CHECK: |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 770 | t = dat.getBase(tu) + uint32(a) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 771 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 772 | // Check if the transition is valid according to the double array |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 773 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 774 | |
| 775 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 776 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 777 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 778 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 779 | if !ok && a == dat.identity { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 780 | // Try again with unknown symbol, in case identity failed |
| 781 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 782 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 783 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 784 | a = dat.unknown |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 785 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 786 | } else if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 787 | // Try again with epsilon symbol, in case everything else failed |
| 788 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 789 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 790 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 791 | a = dat.epsilon |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 792 | } else { |
| 793 | break |
| 794 | } |
| 795 | goto CHECK |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 796 | } else if dat.isSeparate(t) { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 797 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 798 | t = dat.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 799 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 800 | |
| 801 | // Transition is fine |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 802 | if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 803 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 804 | i++ |
| 805 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 806 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 807 | // TODO: |
| 808 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 811 | if i != len(chars) { |
| 812 | if DEBUG { |
| 813 | fmt.Println("Not at the end") |
| 814 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 815 | return false |
| 816 | } |
| 817 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 818 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 819 | |
| 820 | // Automaton is in a final state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 821 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 822 | return true |
| 823 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 824 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 825 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 826 | tu = t |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 827 | t = dat.getBase(tu) + uint32(dat.epsilon) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 828 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 829 | // Epsilon transition failed |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 830 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 831 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 832 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 833 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 834 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 835 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 836 | } else if dat.isSeparate(t) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 837 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 838 | t = dat.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 839 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 840 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 841 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 842 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 843 | |
| 844 | // Match an input string against the double array |
| 845 | // FSA. |
| 846 | // |
| 847 | // Based on Match with additional support |
| 848 | // for NONTOKEN handling |
| 849 | func (dat *DaTokenizer) Transduce(input string) bool { |
| 850 | var a int |
| 851 | var tu uint32 |
| 852 | var ok, nontoken bool |
| 853 | |
| 854 | t := uint32(1) // Initial state |
| 855 | chars := []rune(input) |
| 856 | i := 0 |
| 857 | |
| 858 | for i < len(chars) { |
| 859 | a, ok = dat.sigma[chars[i]] |
| 860 | |
| 861 | // Support identity symbol if character is not in sigma |
| 862 | if !ok && dat.identity != -1 { |
| 863 | if DEBUG { |
| 864 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
| 865 | } |
| 866 | a = dat.identity |
| 867 | } else if DEBUG { |
| 868 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
| 869 | } |
| 870 | tu = t |
| 871 | CHECK: |
| 872 | nontoken = false |
| 873 | t = dat.getBase(tu) + uint32(a) |
| 874 | |
| 875 | // Check if the transition is valid according to the double array |
| 876 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 877 | |
| 878 | if DEBUG { |
| 879 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 880 | } |
| 881 | |
| 882 | if !ok && a == dat.identity { |
| 883 | // Try again with unknown symbol, in case identity failed |
| 884 | if DEBUG { |
| 885 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
| 886 | } |
| 887 | a = dat.unknown |
| 888 | |
| 889 | } else if a != dat.epsilon { |
| 890 | // Try again with epsilon symbol, in case everything else failed |
| 891 | if DEBUG { |
| 892 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
| 893 | } |
| 894 | a = dat.epsilon |
| 895 | } else { |
| 896 | break |
| 897 | } |
| 898 | goto CHECK |
| 899 | } else if dat.isSeparate(t) { |
| 900 | // Move to representative state |
| 901 | nontoken = dat.isNonToken(t) |
| 902 | |
| 903 | t = dat.getBase(t) |
| 904 | } else { |
| 905 | nontoken = dat.isNonToken(t) |
| 906 | } |
| 907 | |
| 908 | // Transition is fine |
| 909 | if a != dat.epsilon { |
| 910 | // Character consumed |
| 911 | |
| 912 | if !nontoken { |
| 913 | fmt.Print("[", string(chars[i]), "]") |
| 914 | } |
| 915 | i++ |
| 916 | } |
| 917 | |
| 918 | if nontoken { |
| 919 | fmt.Print("<|>") |
| 920 | } |
| 921 | |
| 922 | // TODO: |
| 923 | // Prevent endless epsilon loops! |
| 924 | } |
| 925 | |
| 926 | if i != len(chars) { |
| 927 | if DEBUG { |
| 928 | fmt.Println("Not at the end") |
| 929 | } |
| 930 | return false |
| 931 | } |
| 932 | |
| 933 | FINALCHECK: |
| 934 | |
| 935 | // Automaton is in a final state |
| 936 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
| 937 | if dat.isNonToken(t) { |
| 938 | fmt.Print("<|>") |
| 939 | } |
| 940 | return true |
| 941 | } |
| 942 | |
| 943 | // Check epsilon transitions until a final state is reached |
| 944 | tu = t |
| 945 | t = dat.getBase(tu) + uint32(dat.epsilon) |
| 946 | |
| 947 | // Epsilon transition failed |
| 948 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 949 | if DEBUG { |
| 950 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 951 | } |
| 952 | return false |
| 953 | |
| 954 | } else if dat.isSeparate(t) { |
| 955 | nontoken = dat.isNonToken(t) |
| 956 | // Move to representative state |
| 957 | t = dat.getBase(t) |
| 958 | } else { |
| 959 | nontoken = dat.isNonToken(t) |
| 960 | } |
| 961 | |
| 962 | if nontoken { |
| 963 | fmt.Print("<|>") |
| 964 | } |
| 965 | |
| 966 | goto FINALCHECK |
| 967 | } |