Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 1 | package datok |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "compress/gzip" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "log" |
| 9 | "os" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "unicode/utf8" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | PROPS = 1 |
| 17 | SIGMA = 2 |
| 18 | STATES = 3 |
| 19 | NONE = 4 |
| 20 | ) |
| 21 | |
| 22 | type edge struct { |
| 23 | inSym int |
| 24 | outSym int |
| 25 | end int |
| 26 | nontoken bool |
| 27 | tokenend bool |
| 28 | } |
| 29 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 30 | type Tokenizer interface { |
| 31 | Transduce(r io.Reader, w io.Writer) bool |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame^] | 32 | Type() string |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Automaton is the intermediate representation |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 36 | // of the tokenizer. |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 37 | type Automaton struct { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 38 | sigmaRev map[int]rune |
| 39 | sigmaMCS map[int]string |
| 40 | arcCount int |
| 41 | sigmaCount int |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 42 | stateCount int |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 43 | transitions []map[int]*edge |
| 44 | |
| 45 | // Special symbols in sigma |
| 46 | epsilon int |
| 47 | unknown int |
| 48 | identity int |
| 49 | final int |
| 50 | tokenend int |
| 51 | } |
| 52 | |
| 53 | // ParseFoma reads the FST from a foma file |
| 54 | // and creates an internal representation, |
| 55 | // in case it follows the tokenizer's convention. |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 56 | func LoadFomaFile(file string) *Automaton { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 57 | f, err := os.Open(file) |
| 58 | if err != nil { |
| 59 | log.Print(err) |
| 60 | return nil |
| 61 | } |
| 62 | defer f.Close() |
| 63 | |
| 64 | gz, err := gzip.NewReader(f) |
| 65 | if err != nil { |
| 66 | log.Print(err) |
| 67 | return nil |
| 68 | } |
| 69 | defer gz.Close() |
| 70 | |
| 71 | return ParseFoma(gz) |
| 72 | } |
| 73 | |
| 74 | // ParseFoma reads the FST from a foma file reader |
| 75 | // and creates an internal representation, |
| 76 | // in case it follows the tokenizer's convention. |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 77 | func ParseFoma(ior io.Reader) *Automaton { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 78 | r := bufio.NewReader(ior) |
| 79 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 80 | auto := &Automaton{ |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 81 | sigmaRev: make(map[int]rune), |
| 82 | sigmaMCS: make(map[int]string), |
| 83 | epsilon: -1, |
| 84 | unknown: -1, |
| 85 | identity: -1, |
| 86 | final: -1, |
| 87 | tokenend: -1, |
| 88 | } |
| 89 | |
| 90 | var state, inSym, outSym, end, final int |
| 91 | |
| 92 | mode := 0 |
| 93 | var elem []string |
| 94 | var elemint [5]int |
| 95 | |
| 96 | // Iterate over all lines of the file. |
| 97 | // This is mainly based on foma2js, |
| 98 | // licensed under the Apache License, version 2, |
| 99 | // and written by Mans Hulden. |
| 100 | for { |
| 101 | line, err := r.ReadString('\n') |
| 102 | if err != nil { |
| 103 | if err == io.EOF { |
| 104 | break |
| 105 | } |
| 106 | log.Print(err) |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | // Read parser mode for the following lines |
| 111 | if strings.HasPrefix(line, "##") { |
| 112 | if strings.HasPrefix(line, "##props##") { |
| 113 | mode = PROPS |
| 114 | |
| 115 | } else if strings.HasPrefix(line, "##states##") { |
| 116 | mode = STATES |
| 117 | |
| 118 | // Adds a final transition symbol to sigma |
| 119 | // written as '#' in Mizobuchi et al (2000) |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 120 | auto.sigmaCount++ |
| 121 | auto.final = auto.sigmaCount |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 122 | |
| 123 | } else if strings.HasPrefix(line, "##sigma##") { |
| 124 | |
| 125 | mode = SIGMA |
| 126 | |
| 127 | } else if strings.HasPrefix(line, "##end##") { |
| 128 | |
| 129 | mode = NONE |
| 130 | |
| 131 | } else if !strings.HasPrefix(line, "##foma-net") { |
| 132 | log.Print("Unknown input line") |
| 133 | break |
| 134 | } |
| 135 | continue |
| 136 | } |
| 137 | |
| 138 | // Based on the current parser mode, interpret the lines |
| 139 | switch mode { |
| 140 | case PROPS: |
| 141 | { |
| 142 | elem = strings.Split(line, " ") |
| 143 | /* |
| 144 | fmt.Println("arity: " + elem[0]) |
| 145 | fmt.Println("arccount: " + elem[1]) |
| 146 | fmt.Println("statecount: " + elem[2]) |
| 147 | fmt.Println("linecount: " + elem[3]) |
| 148 | fmt.Println("finalcount: " + elem[4]) |
| 149 | fmt.Println("pathcount: " + elem[5]) |
| 150 | fmt.Println("is_deterministic: " + elem[6]) |
| 151 | fmt.Println("is_pruned: " + elem[7]) |
| 152 | fmt.Println("is_minimized: " + elem[8]) |
| 153 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 154 | fmt.Println("is_loop_free: " + elem[10]) |
| 155 | fmt.Println("extras: " + elem[11]) |
| 156 | fmt.Println("name: " + elem[12]) |
| 157 | */ |
| 158 | if elem[6] != "1" { |
| 159 | log.Print("The FST needs to be deterministic") |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | if elem[9] != "1" { |
| 164 | log.Print("The FST needs to be epsilon free") |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | elemint[0], err = strconv.Atoi(elem[1]) |
| 169 | if err != nil { |
| 170 | log.Print("Can't read arccount") |
| 171 | return nil |
| 172 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 173 | auto.arcCount = elemint[0] |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 174 | |
| 175 | elemint[0], err = strconv.Atoi(elem[2]) |
| 176 | if err != nil { |
| 177 | log.Print("Can't read statecount") |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | // States start at 1 in Mizobuchi et al (2000), |
| 182 | // as the state 0 is associated with a fail. |
| 183 | // Initialize states and transitions |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 184 | auto.stateCount = elemint[0] |
| 185 | auto.transitions = make([]map[int]*edge, elemint[0]+1) |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 186 | continue |
| 187 | } |
| 188 | case STATES: |
| 189 | { |
| 190 | elem = strings.Split(line[0:len(line)-1], " ") |
| 191 | if elem[0] == "-1" { |
| 192 | if DEBUG { |
| 193 | fmt.Println("Skip", elem) |
| 194 | } |
| 195 | continue |
| 196 | } |
| 197 | elemint[0], err = strconv.Atoi(elem[0]) |
| 198 | if err != nil { |
| 199 | fmt.Println("Unable to translate", elem[0]) |
| 200 | break |
| 201 | } |
| 202 | |
| 203 | if len(elem) > 1 { |
| 204 | elemint[1], err = strconv.Atoi(elem[1]) |
| 205 | if err != nil { |
| 206 | fmt.Println("Unable to translate", elem[1]) |
| 207 | break |
| 208 | } |
| 209 | if len(elem) > 2 { |
| 210 | elemint[2], err = strconv.Atoi(elem[2]) |
| 211 | if err != nil { |
| 212 | fmt.Println("Unable to translate", elem[2]) |
| 213 | break |
| 214 | } |
| 215 | if len(elem) > 3 { |
| 216 | elemint[3], err = strconv.Atoi(elem[3]) |
| 217 | if err != nil { |
| 218 | fmt.Println("Unable to translate", elem[3]) |
| 219 | break |
| 220 | } |
| 221 | if len(elem) > 4 { |
| 222 | elemint[4], err = strconv.Atoi(elem[4]) |
| 223 | if err != nil { |
| 224 | fmt.Println("Unable to translate", elem[4]) |
| 225 | break |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | switch len(elem) { |
| 233 | case 5: |
| 234 | { |
| 235 | state = elemint[0] |
| 236 | inSym = elemint[1] |
| 237 | outSym = elemint[2] |
| 238 | end = elemint[3] |
| 239 | final = elemint[4] |
| 240 | } |
| 241 | case 4: |
| 242 | { |
| 243 | if elemint[1] == -1 { |
| 244 | state = elemint[0] |
| 245 | final = elemint[3] |
| 246 | |
| 247 | // Final state that has no outgoing edges |
| 248 | if final == 1 { |
| 249 | |
| 250 | // Initialize outgoing states |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 251 | if auto.transitions[state+1] == nil { |
| 252 | auto.transitions[state+1] = make(map[int]*edge) |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // TODO: |
| 256 | // Maybe this is less relevant for tokenizers |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 257 | auto.transitions[state+1][auto.final] = &edge{} |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 258 | } |
| 259 | continue |
| 260 | } else { |
| 261 | state = elemint[0] |
| 262 | inSym = elemint[1] |
| 263 | end = elemint[2] |
| 264 | final = elemint[3] |
| 265 | outSym = inSym |
| 266 | } |
| 267 | } |
| 268 | case 3: |
| 269 | { |
| 270 | inSym = elemint[0] |
| 271 | outSym = elemint[1] |
| 272 | end = elemint[2] |
| 273 | } |
| 274 | case 2: |
| 275 | { |
| 276 | inSym = elemint[0] |
| 277 | end = elemint[1] |
| 278 | outSym = inSym |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | nontoken := false |
| 283 | tokenend := false |
| 284 | |
| 285 | // While the states in foma start with 0, the states in the |
| 286 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 287 | // We also increase sigma by 1, so there are no 0 transitions. |
| 288 | inSym++ |
| 289 | outSym++ |
| 290 | |
| 291 | // Only a limited list of transitions are allowed |
| 292 | if inSym != outSym { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 293 | if outSym == auto.tokenend && inSym == auto.epsilon { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 294 | tokenend = true |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 295 | } else if outSym == auto.epsilon { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 296 | nontoken = true |
| 297 | } else { |
| 298 | log.Println( |
| 299 | "Unsupported transition: " + |
| 300 | strconv.Itoa(state) + |
| 301 | " -> " + strconv.Itoa(end) + |
| 302 | " (" + |
| 303 | strconv.Itoa(inSym) + |
| 304 | ":" + |
| 305 | strconv.Itoa(outSym) + |
| 306 | ") (" + |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 307 | string(auto.sigmaRev[inSym]) + |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 308 | ":" + |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 309 | string(auto.sigmaRev[outSym]) + |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 310 | ")") |
| 311 | return nil |
| 312 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 313 | } else if inSym == auto.tokenend { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 314 | // Ignore tokenend accepting arcs |
| 315 | continue |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 316 | } else if inSym == auto.epsilon { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 317 | log.Println("General epsilon transitions are not supported") |
| 318 | return nil |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 319 | } else if auto.sigmaMCS[inSym] != "" { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 320 | // log.Fatalln("Non supported character", tok.sigmaMCS[inSym]) |
| 321 | // Ignore MCS transitions |
| 322 | continue |
| 323 | } |
| 324 | |
| 325 | // Create an edge based on the collected information |
| 326 | targetObj := &edge{ |
| 327 | inSym: inSym, |
| 328 | outSym: outSym, |
| 329 | end: end + 1, |
| 330 | tokenend: tokenend, |
| 331 | nontoken: nontoken, |
| 332 | } |
| 333 | |
| 334 | // Initialize outgoing states |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 335 | if auto.transitions[state+1] == nil { |
| 336 | auto.transitions[state+1] = make(map[int]*edge) |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | // Ignore transitions with invalid symbols |
| 340 | if inSym >= 0 { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 341 | auto.transitions[state+1][inSym] = targetObj |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // Add final transition |
| 345 | if final == 1 { |
| 346 | // TODO: |
| 347 | // Maybe this is less relevant for tokenizers |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 348 | auto.transitions[state+1][auto.final] = &edge{} |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if DEBUG { |
| 352 | fmt.Println("Add", |
| 353 | state+1, "->", end+1, |
| 354 | "(", |
| 355 | inSym, |
| 356 | ":", |
| 357 | outSym, |
| 358 | ") (", |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 359 | string(auto.sigmaRev[inSym]), |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 360 | ":", |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 361 | string(auto.sigmaRev[outSym]), |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 362 | ")", |
| 363 | ";", |
| 364 | "TE:", tokenend, |
| 365 | "NT:", nontoken, |
| 366 | "FIN:", final) |
| 367 | } |
| 368 | |
| 369 | continue |
| 370 | } |
| 371 | case SIGMA: |
| 372 | { |
| 373 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 374 | |
| 375 | // Turn string into sigma id |
| 376 | number, err := strconv.Atoi(elem[0]) |
| 377 | |
| 378 | // ID needs to be > 1 |
| 379 | number++ |
| 380 | |
| 381 | if err != nil { |
| 382 | log.Println(err) |
| 383 | return nil |
| 384 | } |
| 385 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 386 | auto.sigmaCount = number |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 387 | |
| 388 | var symbol rune |
| 389 | |
| 390 | // Read rune |
| 391 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 392 | symbol = []rune(elem[1])[0] |
| 393 | |
| 394 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
| 395 | |
| 396 | // Probably a MCS |
| 397 | switch elem[1] { |
| 398 | case "@_EPSILON_SYMBOL_@": |
| 399 | { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 400 | auto.epsilon = number |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 401 | } |
| 402 | case "@_UNKNOWN_SYMBOL_@": |
| 403 | { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 404 | auto.unknown = number |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | case "@_IDENTITY_SYMBOL_@": |
| 408 | { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 409 | auto.identity = number |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | case "@_TOKEN_SYMBOL_@": |
| 413 | { |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 414 | auto.tokenend = number |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 415 | } |
| 416 | default: |
| 417 | { |
| 418 | // MCS not supported |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 419 | auto.sigmaMCS[number] = line |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | continue |
| 423 | |
| 424 | } else { // Probably a new line symbol |
| 425 | line, err = r.ReadString('\n') |
| 426 | if err != nil { |
| 427 | log.Println(err) |
| 428 | return nil |
| 429 | } |
| 430 | if len(line) != 1 { |
| 431 | // MCS not supported |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 432 | auto.sigmaMCS[number] = line |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 433 | continue |
| 434 | } |
| 435 | symbol = rune('\n') |
| 436 | } |
| 437 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 438 | auto.sigmaRev[number] = symbol |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | } |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 442 | auto.sigmaMCS = nil |
| 443 | return auto |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame^] | 446 | func LoadTokenizerFile(file string) Tokenizer { |
| 447 | f, err := os.Open(file) |
| 448 | if err != nil { |
| 449 | log.Println(err) |
| 450 | return nil |
| 451 | } |
| 452 | defer f.Close() |
| 453 | |
| 454 | gz, err := gzip.NewReader(f) |
| 455 | if err != nil { |
| 456 | log.Println(err) |
| 457 | return nil |
| 458 | } |
| 459 | defer gz.Close() |
| 460 | |
| 461 | r := bufio.NewReader(gz) |
| 462 | |
| 463 | mstr, err := r.Peek(len(DAMAGIC)) |
| 464 | |
| 465 | if err != nil { |
| 466 | log.Println(err) |
| 467 | return nil |
| 468 | } |
| 469 | |
| 470 | if string(mstr) == MAMAGIC { |
| 471 | return ParseMatrix(r) |
| 472 | } else if string(mstr) == DAMAGIC { |
| 473 | return ParseDatok(r) |
| 474 | } |
| 475 | |
| 476 | log.Println("Neither a matrix nor a datok file") |
| 477 | return nil |
| 478 | } |
| 479 | |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 480 | // Set alphabet A to the list of all symbols |
| 481 | // outgoing from s |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 482 | func (auto *Automaton) getSet(s int, A *[]int) { |
| 483 | for a := range auto.transitions[s] { |
Akron | 0d0daa2 | 2021-09-21 16:32:23 +0200 | [diff] [blame] | 484 | *A = append(*A, a) |
| 485 | } |
| 486 | |
| 487 | // Not required, but simplifies bug hunting |
| 488 | // sort.Ints(*A) |
| 489 | } |