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 | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 503 | // Mark the state as being the target of a tokenend transition |
| 504 | if tok.transitions[s][a].tokenend { |
| 505 | dat.setTokenEnd(t1, true) |
| 506 | } |
| 507 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 508 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 509 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 510 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 511 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 512 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 513 | table[size] = &mapping{source: s1, target: t1} |
| 514 | size++ |
| 515 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 516 | // Overwrite with the representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 517 | dat.setBase(t1, r) |
| 518 | dat.setSeparate(t1, true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 519 | } |
| 520 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 521 | // Store a final transition |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 522 | dat.setCheck(dat.getBase(t)+uint32(dat.final), t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Following Mizobuchi et al (2000) the size of the |
| 528 | // FSA should be stored in check(1). |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 529 | dat.setSize(dat.maxSize + 1) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 530 | dat.array = dat.array[:dat.maxSize+1] |
| 531 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 534 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 535 | // exists and return this as a representative. |
| 536 | // Currently iterates through the whole table |
| 537 | // in a bruteforce manner. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 538 | func in_table(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 539 | for x := 0; x < size; x++ { |
| 540 | if table[x].source == s { |
| 541 | return table[x].target |
| 542 | } |
| 543 | } |
| 544 | return 0 |
| 545 | } |
| 546 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 547 | // Resize double array when necessary |
| 548 | func (dat *DaTokenizer) resize(l int) { |
| 549 | // TODO: |
| 550 | // This is a bit too aggressive atm and should be calmed down. |
| 551 | if len(dat.array) <= l { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 552 | dat.array = append(dat.array, make([]uint32, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 553 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 554 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 555 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 556 | // Set base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 557 | func (dat *DaTokenizer) setBase(p uint32, v uint32) { |
| 558 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 559 | dat.resize(l) |
| 560 | if dat.maxSize < l { |
| 561 | dat.maxSize = l |
| 562 | } |
| 563 | dat.array[p*2] = v |
| 564 | } |
| 565 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 566 | // Returns true if a state is separate pointing to a representative |
| 567 | func (dat *DaTokenizer) isSeparate(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 568 | return dat.array[p*2]&firstBit != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // Mark a state as separate pointing to a representative |
| 572 | func (dat *DaTokenizer) setSeparate(p uint32, sep bool) { |
| 573 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 574 | dat.array[p*2] |= firstBit |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 575 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 576 | dat.array[p*2] &= (restBit | secondBit) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 580 | // Returns true if a state is the target of a nontoken transition |
| 581 | func (dat *DaTokenizer) isNonToken(p uint32) bool { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 582 | return dat.array[p*2+1]&firstBit != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // Mark a state as being the target of a nontoken transition |
| 586 | func (dat *DaTokenizer) setNonToken(p uint32, sep bool) { |
| 587 | if sep { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 588 | dat.array[p*2+1] |= firstBit |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 589 | } else { |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 590 | dat.array[p*2+1] &= (restBit | secondBit) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 594 | // Returns true if a state is the target of a tokenend transition |
| 595 | func (dat *DaTokenizer) isTokenEnd(p uint32) bool { |
| 596 | return dat.array[p*2+1]&secondBit != 0 |
| 597 | } |
| 598 | |
| 599 | // Mark a state as being the target of a tokenend transition |
| 600 | func (dat *DaTokenizer) setTokenEnd(p uint32, sep bool) { |
| 601 | if sep { |
| 602 | dat.array[p*2+1] |= secondBit |
| 603 | } else { |
| 604 | dat.array[p*2+1] &= (restBit | firstBit) |
| 605 | } |
| 606 | } |
| 607 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 608 | // Get base value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 609 | func (dat *DaTokenizer) getBase(p uint32) uint32 { |
| 610 | if int(p*2) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 611 | return 0 |
| 612 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 613 | return dat.array[p*2] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // Set check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 617 | func (dat *DaTokenizer) setCheck(p uint32, v uint32) { |
| 618 | l := int(p*2 + 1) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 619 | dat.resize(l) |
| 620 | if dat.maxSize < l { |
| 621 | dat.maxSize = l |
| 622 | } |
| 623 | dat.array[(p*2)+1] = v |
| 624 | } |
| 625 | |
| 626 | // Get check value in double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 627 | func (dat *DaTokenizer) getCheck(p uint32) uint32 { |
| 628 | if int((p*2)+1) >= len(dat.array) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 629 | return 0 |
| 630 | } |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 631 | return dat.array[(p*2)+1] & restBit |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 635 | func (dat *DaTokenizer) setSize(v int) { |
| 636 | dat.setCheck(1, uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 640 | func (dat *DaTokenizer) GetSize() int { |
| 641 | return int(dat.getCheck(1)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // Based on Mizobuchi et al (2000), p. 124 |
| 645 | // This iterates for every state through the complete double array |
| 646 | // structure until it finds a gap that fits all outgoing transitions |
| 647 | // of the state. This is extremely slow, but is only necessary in the |
| 648 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 649 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 650 | |
| 651 | // Start at the first entry of the double array list |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 652 | base := uint32(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 653 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 654 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 655 | |
| 656 | // Resize the array if necessary |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 657 | dat.resize((int(base) + dat.final) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 658 | for _, a := range symbols { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 659 | if dat.getCheck(base+uint32(a)) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 660 | base++ |
| 661 | goto OVERLAP |
| 662 | } |
| 663 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 664 | return base |
| 665 | } |
| 666 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 667 | // LoadFactor as defined in Kanda et al (2018), |
| 668 | // i.e. the proportion of non-empty elements to all elements. |
| 669 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 670 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 671 | // Cache the loadfactor |
| 672 | if dat.loadFactor >= 0 { |
| 673 | return dat.loadFactor |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 674 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 675 | nonEmpty := 0 |
| 676 | all := len(dat.array) / 2 |
| 677 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 678 | if dat.array[x] != 0 { |
| 679 | nonEmpty++ |
| 680 | } |
| 681 | } |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 682 | dat.loadFactor = float64(nonEmpty) / float64(all) * 100 |
| 683 | return dat.loadFactor |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 686 | // WriteTo stores the double array data in an io.Writer. |
| 687 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 688 | |
| 689 | // Store magical header |
| 690 | all, err := w.Write([]byte(MAGIC)) |
| 691 | if err != nil { |
| 692 | log.Error().Msg("Unable to write data") |
| 693 | } |
| 694 | |
| 695 | // Get sigma as a list |
| 696 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 697 | max := 0 |
| 698 | for sym, num := range dat.sigma { |
| 699 | sigmalist[num] = sym |
| 700 | if num > max { |
| 701 | max = num |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | sigmalist = sigmalist[:max+1] |
| 706 | |
| 707 | buf := make([]byte, 0, 12) |
| 708 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 709 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 710 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 711 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 712 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 713 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
| 714 | more, err := w.Write(buf[0:12]) |
| 715 | if err != nil { |
| 716 | log.Error().Msg("Unable to write data") |
| 717 | } |
| 718 | |
| 719 | all += more |
| 720 | |
| 721 | wbuf := bytes.NewBuffer(nil) |
| 722 | wbufWrap := bufio.NewWriter(wbuf) |
| 723 | |
| 724 | // Write sigma |
| 725 | for _, sym := range sigmalist { |
| 726 | more, err = wbufWrap.WriteRune(sym) |
| 727 | if err != nil { |
| 728 | log.Error().Msg("Unable to write data") |
| 729 | } |
| 730 | all += more |
| 731 | } |
| 732 | wbufWrap.Flush() |
| 733 | more, err = w.Write(wbuf.Bytes()) |
| 734 | if err != nil { |
| 735 | log.Error().Msg("Unable to write data") |
| 736 | } |
| 737 | all += more |
| 738 | |
| 739 | // Test marker - could be checksum |
| 740 | more, err = w.Write([]byte("T")) |
| 741 | if err != nil { |
| 742 | log.Error().Msg("Unable to write data") |
| 743 | } |
| 744 | all += more |
| 745 | |
| 746 | wbuf.Reset() |
| 747 | |
| 748 | for _, d := range dat.array { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 749 | bo.PutUint32(buf[0:4], d) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 750 | more, err := w.Write(buf[0:4]) |
| 751 | if err != nil { |
| 752 | log.Error().Msg("Unable to write data") |
| 753 | } |
| 754 | all += more |
| 755 | } |
| 756 | |
| 757 | return int64(all), err |
| 758 | } |
| 759 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 760 | // Match an input string against the double array |
| 761 | // FSA. |
| 762 | // |
| 763 | // Based on Mizobuchi et al (2000), p. 129, |
| 764 | // with additional support for IDENTITY, UNKNOWN |
| 765 | // and EPSILON transitions. |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 766 | func (dat *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 767 | var a int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 768 | var tu uint32 |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 769 | var ok bool |
| 770 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 771 | t := uint32(1) // Initial state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 772 | chars := []rune(input) |
| 773 | i := 0 |
| 774 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 775 | for i < len(chars) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 776 | a, ok = dat.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 777 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 778 | // Support identity symbol if character is not in sigma |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 779 | if !ok && dat.identity != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 780 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 781 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 782 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 783 | a = dat.identity |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 784 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 785 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 786 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 787 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 788 | CHECK: |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 789 | t = dat.getBase(tu) + uint32(a) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 790 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 791 | // Check if the transition is valid according to the double array |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 792 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 793 | |
| 794 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 795 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 796 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 797 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 798 | if !ok && a == dat.identity { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 799 | // Try again with unknown symbol, in case identity failed |
| 800 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 801 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 802 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 803 | a = dat.unknown |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 804 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 805 | } else if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 806 | // Try again with epsilon symbol, in case everything else failed |
| 807 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 808 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 809 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 810 | a = dat.epsilon |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 811 | } else { |
| 812 | break |
| 813 | } |
| 814 | goto CHECK |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 815 | } else if dat.isSeparate(t) { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 816 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 817 | t = dat.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 818 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 819 | |
| 820 | // Transition is fine |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 821 | if a != dat.epsilon { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 822 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 823 | i++ |
| 824 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 825 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 826 | // TODO: |
| 827 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 830 | if i != len(chars) { |
| 831 | if DEBUG { |
| 832 | fmt.Println("Not at the end") |
| 833 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 834 | return false |
| 835 | } |
| 836 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 837 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 838 | |
| 839 | // Automaton is in a final state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 840 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 841 | return true |
| 842 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 843 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 844 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 845 | tu = t |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 846 | t = dat.getBase(tu) + uint32(dat.epsilon) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 847 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 848 | // Epsilon transition failed |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 849 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 850 | if DEBUG { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 851 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 852 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 853 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 854 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 855 | } else if dat.isSeparate(t) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 856 | // Move to representative state |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 857 | t = dat.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 858 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 859 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 860 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 861 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 862 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 863 | // Transduce an input string against the double array |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 864 | // FSA. |
| 865 | // |
| 866 | // Based on Match with additional support |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 867 | // for NONTOKEN and TOKENEND handling |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 868 | func (dat *DaTokenizer) Transduce(input string) bool { |
| 869 | var a int |
| 870 | var tu uint32 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 871 | var ok, nontoken, tokenend bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 872 | |
| 873 | t := uint32(1) // Initial state |
| 874 | chars := []rune(input) |
| 875 | i := 0 |
| 876 | |
| 877 | for i < len(chars) { |
| 878 | a, ok = dat.sigma[chars[i]] |
| 879 | |
| 880 | // Support identity symbol if character is not in sigma |
| 881 | if !ok && dat.identity != -1 { |
| 882 | if DEBUG { |
| 883 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", dat.identity) |
| 884 | } |
| 885 | a = dat.identity |
| 886 | } else if DEBUG { |
| 887 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
| 888 | } |
| 889 | tu = t |
| 890 | CHECK: |
| 891 | nontoken = false |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 892 | tokenend = false |
| 893 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 894 | t = dat.getBase(tu) + uint32(a) |
| 895 | |
| 896 | // Check if the transition is valid according to the double array |
| 897 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 898 | |
| 899 | if DEBUG { |
| 900 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 901 | } |
| 902 | |
| 903 | if !ok && a == dat.identity { |
| 904 | // Try again with unknown symbol, in case identity failed |
| 905 | if DEBUG { |
| 906 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", dat.unknown) |
| 907 | } |
| 908 | a = dat.unknown |
| 909 | |
| 910 | } else if a != dat.epsilon { |
| 911 | // Try again with epsilon symbol, in case everything else failed |
| 912 | if DEBUG { |
| 913 | fmt.Println("EPSILON symbol", string(chars[i]), "->", dat.epsilon) |
| 914 | } |
| 915 | a = dat.epsilon |
| 916 | } else { |
| 917 | break |
| 918 | } |
| 919 | goto CHECK |
| 920 | } else if dat.isSeparate(t) { |
| 921 | // Move to representative state |
| 922 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 923 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 924 | |
| 925 | t = dat.getBase(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 926 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 927 | } else { |
| 928 | nontoken = dat.isNonToken(t) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 929 | tokenend = dat.isTokenEnd(t) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // Transition is fine |
| 933 | if a != dat.epsilon { |
| 934 | // Character consumed |
| 935 | |
| 936 | if !nontoken { |
| 937 | fmt.Print("[", string(chars[i]), "]") |
| 938 | } |
| 939 | i++ |
| 940 | } |
| 941 | |
| 942 | if nontoken { |
| 943 | fmt.Print("<|>") |
| 944 | } |
| 945 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 946 | if tokenend { |
| 947 | fmt.Print("< !!! >") |
| 948 | } |
| 949 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 950 | // TODO: |
| 951 | // Prevent endless epsilon loops! |
| 952 | } |
| 953 | |
| 954 | if i != len(chars) { |
| 955 | if DEBUG { |
| 956 | fmt.Println("Not at the end") |
| 957 | } |
| 958 | return false |
| 959 | } |
| 960 | |
| 961 | FINALCHECK: |
| 962 | |
| 963 | // Automaton is in a final state |
| 964 | if dat.getCheck(dat.getBase(t)+uint32(dat.final)) == t { |
| 965 | if dat.isNonToken(t) { |
| 966 | fmt.Print("<|>") |
| 967 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame^] | 968 | if dat.isTokenEnd(t) { |
| 969 | fmt.Print("< !!! >") |
| 970 | } |
| 971 | |
| 972 | // 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] | 973 | return true |
| 974 | } |
| 975 | |
| 976 | // Check epsilon transitions until a final state is reached |
| 977 | tu = t |
| 978 | t = dat.getBase(tu) + uint32(dat.epsilon) |
| 979 | |
| 980 | // Epsilon transition failed |
| 981 | if t > dat.getCheck(1) || dat.getCheck(t) != tu { |
| 982 | if DEBUG { |
| 983 | fmt.Println("Match is not fine!", t, "and", dat.getCheck(t), "vs", tu) |
| 984 | } |
| 985 | return false |
| 986 | |
| 987 | } else if dat.isSeparate(t) { |
| 988 | nontoken = dat.isNonToken(t) |
| 989 | // Move to representative state |
| 990 | t = dat.getBase(t) |
| 991 | } else { |
| 992 | nontoken = dat.isNonToken(t) |
| 993 | } |
| 994 | |
| 995 | if nontoken { |
| 996 | fmt.Print("<|>") |
| 997 | } |
| 998 | |
| 999 | goto FINALCHECK |
| 1000 | } |