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