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