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 | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 9 | // TODO: |
| 10 | // - replace maxSize with the check value |
| 11 | // - Strip first state and make everything start with 0! |
| 12 | // - Serialize! |
| 13 | // - Split Tokenizer and DATokenizer |
| 14 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 15 | import ( |
| 16 | "bufio" |
| 17 | "compress/gzip" |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 21 | "sort" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 22 | "strconv" |
| 23 | "strings" |
| 24 | "unicode/utf8" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 25 | |
| 26 | "github.com/rs/zerolog/log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const ( |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 30 | PROPS = 1 |
| 31 | SIGMA = 2 |
| 32 | STATES = 3 |
| 33 | NONE = 4 |
| 34 | NEWLINE = '\u000a' |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 35 | DEBUG = false |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | // Special symbols in sigma |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 39 | var EPSILON = -1 |
| 40 | var UNKNOWN = -1 |
| 41 | var IDENTITY = -1 |
| 42 | var FINAL = -1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 43 | |
| 44 | type mapping struct { |
| 45 | source int |
| 46 | target int |
| 47 | } |
| 48 | |
| 49 | type edge struct { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 50 | inSym int |
| 51 | outSym int |
| 52 | end int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type Tokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 56 | // sigma map[rune]int |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 57 | sigmaRev map[int]rune |
| 58 | arcCount int |
| 59 | stateCount int |
| 60 | sigmaCount int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 61 | transitions []map[int]*edge |
| 62 | } |
| 63 | |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 64 | type DaTokenizer struct { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 65 | // sigmaRev map[int]rune |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 66 | sigma map[rune]int |
| 67 | maxSize int |
| 68 | loadLevel float64 |
| 69 | array []int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 72 | func ParseFile(file string) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 73 | f, err := os.Open(file) |
| 74 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 75 | log.Error().Err(err) |
| 76 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 77 | } |
| 78 | defer f.Close() |
| 79 | |
| 80 | gz, err := gzip.NewReader(f) |
| 81 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 82 | log.Error().Err(err) |
| 83 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 84 | } |
| 85 | defer gz.Close() |
| 86 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 87 | return Parse(gz) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 90 | func Parse(ior io.Reader) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 91 | r := bufio.NewReader(ior) |
| 92 | |
| 93 | tok := &Tokenizer{ |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 94 | // sigma: make(map[rune]int), |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 95 | sigmaRev: make(map[int]rune), |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 98 | var state, inSym, outSym, end, final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 99 | |
| 100 | mode := 0 |
| 101 | var elem []string |
| 102 | var elemint [5]int |
| 103 | |
| 104 | for { |
| 105 | line, err := r.ReadString('\n') |
| 106 | if err != nil { |
| 107 | if err == io.EOF { |
| 108 | break |
| 109 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 110 | log.Error().Err(err) |
| 111 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 112 | } |
| 113 | if strings.HasPrefix(line, "##foma-net") { |
| 114 | continue |
| 115 | } |
| 116 | if strings.HasPrefix(line, "##props##") { |
| 117 | mode = PROPS |
| 118 | continue |
| 119 | } |
| 120 | if strings.HasPrefix(line, "##states##") { |
| 121 | mode = STATES |
| 122 | |
| 123 | // Adds a final transition symbol to sigma |
| 124 | // written as '#' in Mizobuchi et al (2000) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 125 | tok.sigmaCount++ |
| 126 | FINAL = tok.sigmaCount |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 127 | continue |
| 128 | } |
| 129 | if strings.HasPrefix(line, "##sigma##") { |
| 130 | mode = SIGMA |
| 131 | continue |
| 132 | } |
| 133 | if strings.HasPrefix(line, "##end##") { |
| 134 | mode = NONE |
| 135 | continue |
| 136 | } |
| 137 | |
| 138 | switch mode { |
| 139 | case PROPS: |
| 140 | { |
| 141 | elem = strings.Split(line, " ") |
| 142 | /* |
| 143 | fmt.Println("arity: " + elem[0]) |
| 144 | fmt.Println("arccount: " + elem[1]) |
| 145 | fmt.Println("statecount: " + elem[2]) |
| 146 | fmt.Println("linecount: " + elem[3]) |
| 147 | fmt.Println("finalcount: " + elem[4]) |
| 148 | fmt.Println("pathcount: " + elem[5]) |
| 149 | fmt.Println("is_deterministic: " + elem[6]) |
| 150 | fmt.Println("is_pruned: " + elem[7]) |
| 151 | fmt.Println("is_minimized: " + elem[8]) |
| 152 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 153 | fmt.Println("is_loop_free: " + elem[10]) |
| 154 | fmt.Println("extras: " + elem[11]) |
| 155 | fmt.Println("name: " + elem[12]) |
| 156 | */ |
| 157 | if elem[6] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 158 | log.Error().Msg("The FST needs to be deterministic") |
| 159 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 160 | } |
| 161 | if elem[9] != "1" { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 162 | log.Error().Msg("The FST needs to be epsilon free") |
| 163 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | elemint[0], err = strconv.Atoi(elem[1]) |
| 167 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 168 | log.Error().Msg("Can't read arccount") |
| 169 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 170 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 171 | tok.arcCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 172 | |
| 173 | // States start at 1 in Mizobuchi et al (2000), |
| 174 | // as the state 0 is associated with a fail. |
| 175 | // Initialize states and transitions |
| 176 | elemint[0], err = strconv.Atoi(elem[2]) |
| 177 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 178 | log.Error().Msg("Can't read statecount") |
| 179 | os.Exit(1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 180 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 181 | tok.stateCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 182 | tok.transitions = make([]map[int]*edge, elemint[0]+1) |
| 183 | continue |
| 184 | } |
| 185 | case STATES: |
| 186 | { |
| 187 | elem = strings.Split(line[0:len(line)-1], " ") |
| 188 | if elem[0] == "-1" { |
| 189 | continue |
| 190 | } |
| 191 | elemint[0], err = strconv.Atoi(elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 192 | if err != nil { |
| 193 | break |
| 194 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 195 | |
| 196 | if len(elem) > 1 { |
| 197 | elemint[1], err = strconv.Atoi(elem[1]) |
| 198 | if err != nil { |
| 199 | break |
| 200 | } |
| 201 | if len(elem) > 2 { |
| 202 | elemint[2], err = strconv.Atoi(elem[2]) |
| 203 | if err != nil { |
| 204 | break |
| 205 | } |
| 206 | if len(elem) > 3 { |
| 207 | elemint[3], err = strconv.Atoi(elem[3]) |
| 208 | if err != nil { |
| 209 | break |
| 210 | } |
| 211 | if len(elem) > 4 { |
| 212 | elemint[4], err = strconv.Atoi(elem[4]) |
| 213 | if err != nil { |
| 214 | break |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | switch len(elem) { |
| 222 | case 5: |
| 223 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 224 | state = elemint[0] |
| 225 | inSym = elemint[1] |
| 226 | outSym = elemint[2] |
| 227 | end = elemint[3] |
| 228 | final = elemint[4] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 229 | } |
| 230 | case 4: |
| 231 | { |
| 232 | if elemint[1] == -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 233 | state = elemint[0] |
| 234 | final = elemint[3] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 235 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 236 | state = elemint[0] |
| 237 | inSym = elemint[1] |
| 238 | end = elemint[2] |
| 239 | final = elemint[3] |
| 240 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | case 3: |
| 244 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 245 | inSym = elemint[0] |
| 246 | outSym = elemint[1] |
| 247 | end = elemint[2] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 248 | } |
| 249 | case 2: |
| 250 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 251 | inSym = elemint[0] |
| 252 | end = elemint[1] |
| 253 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 257 | // While the states in foma start with 0, the states in the |
| 258 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 259 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 260 | if inSym != outSym { |
| 261 | |
| 262 | // Allow any epsilon to become a newline |
| 263 | if !(inSym == EPSILON && tok.sigmaRev[outSym] == NEWLINE) && |
| 264 | |
| 265 | // Allow any whitespace to be ignored |
| 266 | !(inSym != EPSILON && outSym == EPSILON) && |
| 267 | |
| 268 | // Allow any whitespace to become a new line |
| 269 | !(tok.sigmaRev[outSym] == NEWLINE) { |
| 270 | |
| 271 | log.Error().Msg( |
| 272 | "Unsupported transition: " + |
| 273 | strconv.Itoa(state) + |
| 274 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 275 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 276 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 277 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 278 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 279 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 280 | string(tok.sigmaRev[inSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 281 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 282 | string(tok.sigmaRev[outSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 283 | ")") |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 284 | os.Exit(1) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 285 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 288 | // This collects all edges until arrstate changes |
| 289 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 290 | // TODO: |
| 291 | // if arrin == EPSILON && arrout == TOKENEND, mark state as newline |
| 292 | // if the next transition is the same, remove TOKENEND and add SENTENCEEND |
| 293 | // This requires to remove the transition alltogether and marks the state instead. |
| 294 | |
| 295 | // TODO: |
| 296 | // if arrout == EPSILON, mark the transition as NOTOKEN |
| 297 | |
| 298 | targetObj := &edge{ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 299 | inSym: inSym, |
| 300 | outSym: outSym, |
| 301 | end: end + 1, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 304 | // Initialize outgoing states |
| 305 | if tok.transitions[state+1] == nil { |
| 306 | tok.transitions[state+1] = make(map[int]*edge) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 309 | // Ignore transitions with invalid symbols |
| 310 | if inSym >= 0 { |
| 311 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 312 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 313 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 314 | // Add final transition |
| 315 | if final == 1 { |
| 316 | tok.transitions[state+1][FINAL] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 319 | if DEBUG { |
| 320 | fmt.Println("Add", |
| 321 | state+1, "->", end+1, |
| 322 | "(", |
| 323 | inSym, |
| 324 | ":", |
| 325 | outSym, |
| 326 | ") (", |
| 327 | string(tok.sigmaRev[inSym]), |
| 328 | ":", |
| 329 | string(tok.sigmaRev[outSym]), |
| 330 | ")") |
| 331 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 332 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 333 | continue |
| 334 | } |
| 335 | case SIGMA: |
| 336 | { |
| 337 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 338 | |
| 339 | // Turn string into sigma id |
| 340 | number, err := strconv.Atoi(elem[0]) |
| 341 | |
| 342 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 343 | log.Error().Err(err) |
| 344 | os.Exit(0) |
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 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 348 | |
| 349 | var symbol rune |
| 350 | |
| 351 | // Read rune |
| 352 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 353 | symbol = []rune(elem[1])[0] |
| 354 | |
| 355 | // Probably a MCS |
| 356 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 357 | switch elem[1] { |
| 358 | case "@_EPSILON_SYMBOL_@": |
| 359 | { |
| 360 | EPSILON = number |
| 361 | continue |
| 362 | } |
| 363 | case "@_UNKNOWN_SYMBOL_@": |
| 364 | { |
| 365 | UNKNOWN = number |
| 366 | continue |
| 367 | } |
| 368 | |
| 369 | case "@_IDENTITY_SYMBOL_@": |
| 370 | { |
| 371 | IDENTITY = number |
| 372 | continue |
| 373 | } |
| 374 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 375 | { |
| 376 | log.Error().Msg("MCS not supported: " + line) |
| 377 | os.Exit(1) |
| 378 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 381 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 382 | line, err = r.ReadString('\n') |
| 383 | if err != nil { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 384 | log.Error().Err(err) |
| 385 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 386 | } |
| 387 | if len(line) != 1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 388 | log.Error().Msg("MCS not supported:" + line) |
| 389 | os.Exit(0) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 390 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 391 | symbol = rune(NEWLINE) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 394 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return tok |
| 400 | } |
| 401 | |
| 402 | // Implementation of Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 403 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 404 | |
| 405 | dat := &DaTokenizer{ |
| 406 | sigma: make(map[rune]int), |
| 407 | } |
| 408 | |
| 409 | for num, sym := range tok.sigmaRev { |
| 410 | dat.sigma[sym] = num |
| 411 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 412 | |
| 413 | mark := 0 |
| 414 | size := 0 |
| 415 | |
| 416 | // Create a mapping from s to t |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 417 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 418 | |
| 419 | table[size] = &mapping{source: 1, target: 1} |
| 420 | size++ |
| 421 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 422 | // Allocate space for the outgoing symbol range |
| 423 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 424 | |
| 425 | for mark < size { |
| 426 | s := table[mark].source // This is a state in Ms |
| 427 | t := table[mark].target // This is a state in Mt |
| 428 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 429 | |
| 430 | // Following the paper, here the state t can be remembered |
| 431 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 432 | A = A[:0] |
| 433 | tok.get_set(s, &A) |
| 434 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 435 | // Set base to the first free slot in the double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 436 | dat.setBase(t, dat.xCheck(A)) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 437 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 438 | // TODO: |
| 439 | // Sort the outgoing transitions based onm the |
| 440 | // outdegree of .end |
| 441 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 442 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 443 | for _, a := range A { |
| 444 | |
| 445 | if a != FINAL { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 446 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 447 | // Aka g(s, a) |
| 448 | s1 := tok.transitions[s][a].end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 449 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 450 | // Store the transition |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 451 | t1 := dat.getBase(t) + a |
| 452 | dat.setCheck(t1, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 453 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 454 | // Check for representative states |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 455 | r := in_table(s1, table, size) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 456 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 457 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 458 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 459 | table[size] = &mapping{source: s1, target: t1} |
| 460 | size++ |
| 461 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 462 | // Overwrite with the representative state |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 463 | dat.setBase(t1, -1*r) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 464 | } |
| 465 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 466 | // Store a final transition |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 467 | dat.setCheck(dat.getBase(t)+FINAL, t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Following Mizobuchi et al (2000) the size of the |
| 473 | // FSA should be stored in check(1). |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 474 | dat.setCheck(1, dat.maxSize+1) |
| 475 | dat.array = dat.array[:dat.maxSize+1] |
| 476 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 479 | // Resize double array when necessary |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 480 | func (tok *DaTokenizer) resize(l int) { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 481 | // TODO: |
| 482 | // This is a bit too aggressive atm and should be calmed down. |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 483 | if len(tok.array) <= l { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 484 | tok.array = append(tok.array, make([]int, l)...) |
| 485 | } |
| 486 | } |
| 487 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 488 | // Set base value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 489 | func (tok *DaTokenizer) setBase(p int, v int) { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 490 | l := p*2 + 1 |
| 491 | tok.resize(l) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 492 | if tok.maxSize < l { |
| 493 | tok.maxSize = l |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 494 | } |
| 495 | tok.array[p*2] = v |
| 496 | } |
| 497 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 498 | // Get base value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 499 | func (tok *DaTokenizer) getBase(p int) int { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 500 | if p*2 >= len(tok.array) { |
| 501 | return 0 |
| 502 | } |
| 503 | return tok.array[p*2] |
| 504 | } |
| 505 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 506 | // Set check value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 507 | func (tok *DaTokenizer) setCheck(p int, v int) { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 508 | l := p*2 + 1 |
| 509 | tok.resize(l) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 510 | if tok.maxSize < l { |
| 511 | tok.maxSize = l |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 512 | } |
| 513 | tok.array[(p*2)+1] = v |
| 514 | } |
| 515 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 516 | // Get check value in double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 517 | func (tok *DaTokenizer) getCheck(p int) int { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 518 | if (p*2)+1 >= len(tok.array) { |
| 519 | return 0 |
| 520 | } |
| 521 | return tok.array[(p*2)+1] |
| 522 | } |
| 523 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 524 | // Set size of double array |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 525 | func (tok *DaTokenizer) setSize(p, v int) { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 526 | tok.setCheck(1, v) |
| 527 | } |
| 528 | |
| 529 | // Get size of double array |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 530 | func (tok *DaTokenizer) GetSize(p int) int { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 531 | return tok.getCheck(1) |
| 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 | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 538 | func in_table(s int, table []*mapping, size int) int { |
| 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 | |
| 547 | // Set alphabet A to the list of all symbols |
| 548 | // outgoing from s |
| 549 | func (tok *Tokenizer) get_set(s int, A *[]int) { |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 550 | for a := range tok.transitions[s] { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 551 | *A = append(*A, a) |
| 552 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 553 | |
| 554 | // Not required, but simplifies bug hunting |
| 555 | sort.Ints(*A) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // Based on Mizobuchi et al (2000), p. 124 |
| 559 | // This iterates for every state through the complete double array |
| 560 | // structure until it finds a gap that fits all outgoing transitions |
| 561 | // of the state. This is extremely slow, but is only necessary in the |
| 562 | // construction phase of the tokenizer. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 563 | func (dat *DaTokenizer) xCheck(symbols []int) int { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 564 | |
| 565 | // Start at the first entry of the double array list |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 566 | base := 1 |
| 567 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 568 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 569 | |
| 570 | // Resize the array if necessary |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 571 | dat.resize((base + FINAL) * 2) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 572 | for _, a := range symbols { |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 573 | if dat.getCheck(base+a) != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 574 | base++ |
| 575 | goto OVERLAP |
| 576 | } |
| 577 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 578 | return base |
| 579 | } |
| 580 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 581 | func (dat *DaTokenizer) LoadLevel() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 582 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 583 | if dat.loadLevel >= 0 { |
| 584 | return dat.loadLevel |
| 585 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 586 | nonEmpty := 0 |
| 587 | all := len(dat.array) / 2 |
| 588 | for x := 1; x <= len(dat.array); x = x + 2 { |
| 589 | if dat.array[x] != 0 { |
| 590 | nonEmpty++ |
| 591 | } |
| 592 | } |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 593 | dat.loadLevel = float64(nonEmpty) / float64(all) * 100 |
| 594 | return dat.loadLevel |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 597 | // Match an input string against the double array |
| 598 | // FSA. |
| 599 | // |
| 600 | // Based on Mizobuchi et al (2000), p. 129, |
| 601 | // with additional support for IDENTITY, UNKNOWN |
| 602 | // and EPSILON transitions. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 603 | func (tok *DaTokenizer) Match(input string) bool { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 604 | var a int |
| 605 | var tu int |
| 606 | var ok bool |
| 607 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 608 | t := 1 // Initial state |
| 609 | chars := []rune(input) |
| 610 | i := 0 |
| 611 | |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 612 | for i < len(chars) { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 613 | a, ok = tok.sigma[chars[i]] |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 614 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 615 | // Support identity symbol if character is not in sigma |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 616 | if !ok && IDENTITY != -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 617 | if DEBUG { |
| 618 | fmt.Println("IDENTITY symbol", string(chars[i]), "->", IDENTITY) |
| 619 | } |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 620 | a = IDENTITY |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 621 | } else if DEBUG { |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 622 | fmt.Println("Sigma transition is okay for [", string(chars[i]), "]") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 623 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 624 | tu = t |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 625 | CHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 626 | t = tok.getBase(tu) + a |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 627 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 628 | // Check if the transition is valid according to the double array |
| 629 | if t > tok.getCheck(1) || tok.getCheck(t) != tu { |
| 630 | |
| 631 | if DEBUG { |
| 632 | fmt.Println("Match is not fine!", t, "and", tok.getCheck(t), "vs", tu) |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 633 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 634 | |
| 635 | if !ok && a == IDENTITY { |
| 636 | // Try again with unknown symbol, in case identity failed |
| 637 | if DEBUG { |
| 638 | fmt.Println("UNKNOWN symbol", string(chars[i]), "->", UNKNOWN) |
| 639 | } |
| 640 | a = UNKNOWN |
| 641 | |
| 642 | } else if a != EPSILON { |
| 643 | // Try again with epsilon symbol, in case everything else failed |
| 644 | if DEBUG { |
| 645 | fmt.Println("EPSILON symbol", string(chars[i]), "->", EPSILON) |
| 646 | } |
| 647 | a = EPSILON |
| 648 | } else { |
| 649 | break |
| 650 | } |
| 651 | goto CHECK |
| 652 | } else if tok.getBase(t) < 0 { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 653 | // Move to representative state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 654 | t = -1 * tok.getBase(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 655 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 656 | |
| 657 | // Transition is fine |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 658 | if a != EPSILON { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 659 | // Character consumed |
Akron | 49d27ee | 2021-08-03 11:58:13 +0200 | [diff] [blame] | 660 | i++ |
| 661 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 662 | // TODO: |
| 663 | // Prevent endless epsilon loops! |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 666 | if i != len(chars) { |
| 667 | if DEBUG { |
| 668 | fmt.Println("Not at the end") |
| 669 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 670 | return false |
| 671 | } |
| 672 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 673 | FINALCHECK: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 674 | |
| 675 | // Automaton is in a final state |
| 676 | if tok.getCheck(tok.getBase(t)+FINAL) == t { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 677 | return true |
| 678 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 679 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 680 | // Check epsilon transitions until a final state is reached |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 681 | tu = t |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame^] | 682 | t = tok.getBase(tu) + EPSILON |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 683 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 684 | // Epsilon transition failed |
| 685 | if t > tok.getCheck(1) || tok.getCheck(t) != tu { |
| 686 | if DEBUG { |
| 687 | fmt.Println("Match is not fine!", t, "and", tok.getCheck(t), "vs", tu) |
| 688 | } |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 689 | return false |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 690 | |
| 691 | } else if tok.getBase(t) < 0 { |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 692 | // Move to representative state |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 693 | t = -1 * tok.getBase(t) |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 694 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 695 | |
Akron | 465a099 | 2021-08-03 11:28:48 +0200 | [diff] [blame] | 696 | goto FINALCHECK |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 697 | } |