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 | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 9 | // The maximum number of states is 1.073.741.823 (30bit), |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 10 | // with a loadfactor of ~70, this means roughly 70 million |
| 11 | // states in the FSA, which is sufficient for the current |
| 12 | // job. |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 13 | // |
| 14 | // Serialization is little endian. |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 15 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 16 | // TODO: |
| 17 | // - replace maxSize with the check value |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 18 | // - Add checksum to serialization. |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 19 | // - Replace/Enhance table with a map |
| 20 | // - Provide a bufio.Scanner compatible interface. |
Akron | 8e1d69b | 2021-08-12 17:38:49 +0200 | [diff] [blame] | 21 | // - Mark epsilon transitions in bytes |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 22 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 23 | import ( |
| 24 | "bufio" |
| 25 | "compress/gzip" |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 26 | "encoding/binary" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 27 | "fmt" |
| 28 | "io" |
| 29 | "os" |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 30 | "sort" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 31 | "strconv" |
| 32 | "strings" |
| 33 | "unicode/utf8" |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 34 | |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 35 | "log" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | const ( |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 39 | PROPS = 1 |
| 40 | SIGMA = 2 |
| 41 | STATES = 3 |
| 42 | NONE = 4 |
Akron | 2a4b929 | 2021-08-04 15:35:22 +0200 | [diff] [blame] | 43 | DEBUG = false |
| 44 | MAGIC = "DATOK" |
| 45 | VERSION = uint16(1) |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 46 | FIRSTBIT uint32 = 1 << 31 |
| 47 | SECONDBIT uint32 = 1 << 30 |
| 48 | RESTBIT uint32 = ^uint32(0) &^ (FIRSTBIT | SECONDBIT) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 49 | ) |
| 50 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 51 | // Serialization is always little endian |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 52 | var bo binary.ByteOrder = binary.LittleEndian |
| 53 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 54 | type mapping struct { |
| 55 | source int |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 56 | target uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type edge struct { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 60 | inSym int |
| 61 | outSym int |
| 62 | end int |
| 63 | nontoken bool |
| 64 | tokenend bool |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 67 | // Tokenizer is the intermediate representation |
| 68 | // of the tokenizer. |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 69 | type Tokenizer struct { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 70 | sigmaRev map[int]rune |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 71 | sigmaMCS map[int]string |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 72 | arcCount int |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 73 | sigmaCount int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 74 | transitions []map[int]*edge |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 75 | |
| 76 | // Special symbols in sigma |
| 77 | epsilon int |
| 78 | unknown int |
| 79 | identity int |
| 80 | final int |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 81 | tokenend int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 84 | type bc struct { |
| 85 | base uint32 |
| 86 | check uint32 |
| 87 | } |
| 88 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 89 | // DaTokenizer represents a tokenizer implemented as a |
| 90 | // Double Array FSA. |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 91 | type DaTokenizer struct { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 92 | sigma map[rune]int |
| 93 | sigmaASCII [256]int |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 94 | maxSize int |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 95 | transCount int |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 96 | array []bc |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 97 | |
| 98 | // Special symbols in sigma |
| 99 | epsilon int |
| 100 | unknown int |
| 101 | identity int |
| 102 | final int |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 103 | tokenend int |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 106 | // ParseFoma reads the FST from a foma file |
| 107 | // and creates an internal representation, |
| 108 | // in case it follows the tokenizer's convention. |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 109 | func LoadFomaFile(file string) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 110 | f, err := os.Open(file) |
| 111 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 112 | log.Print(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 113 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 114 | } |
| 115 | defer f.Close() |
| 116 | |
| 117 | gz, err := gzip.NewReader(f) |
| 118 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 119 | log.Print(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 120 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 121 | } |
| 122 | defer gz.Close() |
| 123 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 124 | return ParseFoma(gz) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 127 | // ParseFoma reads the FST from a foma file reader |
| 128 | // and creates an internal representation, |
| 129 | // in case it follows the tokenizer's convention. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 130 | func ParseFoma(ior io.Reader) *Tokenizer { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 131 | r := bufio.NewReader(ior) |
| 132 | |
| 133 | tok := &Tokenizer{ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 134 | sigmaRev: make(map[int]rune), |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 135 | sigmaMCS: make(map[int]string), |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 136 | epsilon: -1, |
| 137 | unknown: -1, |
| 138 | identity: -1, |
| 139 | final: -1, |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 140 | tokenend: -1, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 143 | var state, inSym, outSym, end, final int |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 144 | |
| 145 | mode := 0 |
| 146 | var elem []string |
| 147 | var elemint [5]int |
| 148 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 149 | // Iterate over all lines of the file. |
| 150 | // This is mainly based on foma2js, |
| 151 | // licensed under the Apache License, version 2, |
| 152 | // and written by Mans Hulden. |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 153 | for { |
| 154 | line, err := r.ReadString('\n') |
| 155 | if err != nil { |
| 156 | if err == io.EOF { |
| 157 | break |
| 158 | } |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 159 | log.Print(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 160 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 161 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 162 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 163 | // Read parser mode for the following lines |
| 164 | if strings.HasPrefix(line, "##") { |
| 165 | if strings.HasPrefix(line, "##props##") { |
| 166 | mode = PROPS |
| 167 | |
| 168 | } else if strings.HasPrefix(line, "##states##") { |
| 169 | mode = STATES |
| 170 | |
| 171 | // Adds a final transition symbol to sigma |
| 172 | // written as '#' in Mizobuchi et al (2000) |
| 173 | tok.sigmaCount++ |
| 174 | tok.final = tok.sigmaCount |
| 175 | |
| 176 | } else if strings.HasPrefix(line, "##sigma##") { |
| 177 | |
| 178 | mode = SIGMA |
| 179 | |
| 180 | } else if strings.HasPrefix(line, "##end##") { |
| 181 | |
| 182 | mode = NONE |
| 183 | |
| 184 | } else if !strings.HasPrefix(line, "##foma-net") { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 185 | log.Print("Unknown input line") |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 186 | break |
| 187 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 188 | continue |
| 189 | } |
| 190 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 191 | // Based on the current parser mode, interpret the lines |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 192 | switch mode { |
| 193 | case PROPS: |
| 194 | { |
| 195 | elem = strings.Split(line, " ") |
| 196 | /* |
| 197 | fmt.Println("arity: " + elem[0]) |
| 198 | fmt.Println("arccount: " + elem[1]) |
| 199 | fmt.Println("statecount: " + elem[2]) |
| 200 | fmt.Println("linecount: " + elem[3]) |
| 201 | fmt.Println("finalcount: " + elem[4]) |
| 202 | fmt.Println("pathcount: " + elem[5]) |
| 203 | fmt.Println("is_deterministic: " + elem[6]) |
| 204 | fmt.Println("is_pruned: " + elem[7]) |
| 205 | fmt.Println("is_minimized: " + elem[8]) |
| 206 | fmt.Println("is_epsilon_free: " + elem[9]) |
| 207 | fmt.Println("is_loop_free: " + elem[10]) |
| 208 | fmt.Println("extras: " + elem[11]) |
| 209 | fmt.Println("name: " + elem[12]) |
| 210 | */ |
| 211 | if elem[6] != "1" { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 212 | log.Print("The FST needs to be deterministic") |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 213 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 214 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 215 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 216 | if elem[9] != "1" { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 217 | log.Print("The FST needs to be epsilon free") |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 218 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | elemint[0], err = strconv.Atoi(elem[1]) |
| 222 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 223 | log.Print("Can't read arccount") |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 224 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 225 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 226 | tok.arcCount = elemint[0] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 227 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 228 | elemint[0], err = strconv.Atoi(elem[2]) |
| 229 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 230 | log.Print("Can't read statecount") |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 231 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 232 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 233 | |
| 234 | // States start at 1 in Mizobuchi et al (2000), |
| 235 | // as the state 0 is associated with a fail. |
| 236 | // Initialize states and transitions |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 237 | tok.transitions = make([]map[int]*edge, elemint[0]+1) |
| 238 | continue |
| 239 | } |
| 240 | case STATES: |
| 241 | { |
| 242 | elem = strings.Split(line[0:len(line)-1], " ") |
| 243 | if elem[0] == "-1" { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 244 | if DEBUG { |
| 245 | fmt.Println("Skip", elem) |
| 246 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 247 | continue |
| 248 | } |
| 249 | elemint[0], err = strconv.Atoi(elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 250 | if err != nil { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 251 | fmt.Println("Unable to translate", elem[0]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 252 | break |
| 253 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 254 | |
| 255 | if len(elem) > 1 { |
| 256 | elemint[1], err = strconv.Atoi(elem[1]) |
| 257 | if err != nil { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 258 | fmt.Println("Unable to translate", elem[1]) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 259 | break |
| 260 | } |
| 261 | if len(elem) > 2 { |
| 262 | elemint[2], err = strconv.Atoi(elem[2]) |
| 263 | if err != nil { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 264 | fmt.Println("Unable to translate", elem[2]) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 265 | break |
| 266 | } |
| 267 | if len(elem) > 3 { |
| 268 | elemint[3], err = strconv.Atoi(elem[3]) |
| 269 | if err != nil { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 270 | fmt.Println("Unable to translate", elem[3]) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 271 | break |
| 272 | } |
| 273 | if len(elem) > 4 { |
| 274 | elemint[4], err = strconv.Atoi(elem[4]) |
| 275 | if err != nil { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 276 | fmt.Println("Unable to translate", elem[4]) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 277 | break |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | switch len(elem) { |
| 285 | case 5: |
| 286 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 287 | state = elemint[0] |
| 288 | inSym = elemint[1] |
| 289 | outSym = elemint[2] |
| 290 | end = elemint[3] |
| 291 | final = elemint[4] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 292 | } |
| 293 | case 4: |
| 294 | { |
| 295 | if elemint[1] == -1 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 296 | state = elemint[0] |
| 297 | final = elemint[3] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 298 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 299 | state = elemint[0] |
| 300 | inSym = elemint[1] |
| 301 | end = elemint[2] |
| 302 | final = elemint[3] |
| 303 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | case 3: |
| 307 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 308 | inSym = elemint[0] |
| 309 | outSym = elemint[1] |
| 310 | end = elemint[2] |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 311 | } |
| 312 | case 2: |
| 313 | { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 314 | inSym = elemint[0] |
| 315 | end = elemint[1] |
| 316 | outSym = inSym |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 320 | nontoken := false |
| 321 | tokenend := false |
| 322 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 323 | // While the states in foma start with 0, the states in the |
| 324 | // Mizobuchi FSA start with one - so we increase every state by 1. |
| 325 | // We also increase sigma by 1, so there are no 0 transitions. |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 326 | inSym++ |
| 327 | outSym++ |
| 328 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 329 | // Only a limited list of transitions are allowed |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 330 | if inSym != outSym { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 331 | if outSym == tok.tokenend && inSym == tok.epsilon { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 332 | tokenend = true |
| 333 | } else if outSym == tok.epsilon { |
| 334 | nontoken = true |
| 335 | } else { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 336 | log.Println( |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 337 | "Unsupported transition: " + |
| 338 | strconv.Itoa(state) + |
| 339 | " -> " + strconv.Itoa(end) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 340 | " (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 341 | strconv.Itoa(inSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 342 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 343 | strconv.Itoa(outSym) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 344 | ") (" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 345 | string(tok.sigmaRev[inSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 346 | ":" + |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 347 | string(tok.sigmaRev[outSym]) + |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 348 | ")") |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 349 | return nil |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 350 | } |
Akron | 92704eb | 2021-08-27 10:59:46 +0200 | [diff] [blame] | 351 | } else if inSym == tok.tokenend { |
| 352 | // Ignore tokenend accepting arcs |
| 353 | continue |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 354 | } else if inSym == tok.epsilon { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 355 | log.Println("General epsilon transitions are not supported") |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 356 | return nil |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 357 | } else if tok.sigmaMCS[inSym] != "" { |
| 358 | log.Fatalln("Non supported character", tok.sigmaMCS[inSym]) |
| 359 | } else if tok.sigmaMCS[outSym] != "" { |
| 360 | log.Fatalln("Non supported character", tok.sigmaMCS[outSym]) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 363 | // Create an edge based on the collected information |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 364 | targetObj := &edge{ |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 365 | inSym: inSym, |
| 366 | outSym: outSym, |
| 367 | end: end + 1, |
| 368 | tokenend: tokenend, |
| 369 | nontoken: nontoken, |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 372 | // Initialize outgoing states |
| 373 | if tok.transitions[state+1] == nil { |
| 374 | tok.transitions[state+1] = make(map[int]*edge) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 377 | // Ignore transitions with invalid symbols |
| 378 | if inSym >= 0 { |
| 379 | tok.transitions[state+1][inSym] = targetObj |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 380 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 381 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 382 | // Add final transition |
| 383 | if final == 1 { |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 384 | // TODO: |
| 385 | // Maybe this is less relevant for tokenizers |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 386 | tok.transitions[state+1][tok.final] = &edge{} |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 389 | if DEBUG { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 390 | fmt.Println("Add", |
| 391 | state+1, "->", end+1, |
| 392 | "(", |
| 393 | inSym, |
| 394 | ":", |
| 395 | outSym, |
| 396 | ") (", |
| 397 | string(tok.sigmaRev[inSym]), |
| 398 | ":", |
| 399 | string(tok.sigmaRev[outSym]), |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 400 | ")", |
| 401 | ";", |
| 402 | "TE:", tokenend, |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 403 | "NT:", nontoken, |
| 404 | "FIN:", final) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 405 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 406 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 407 | continue |
| 408 | } |
| 409 | case SIGMA: |
| 410 | { |
| 411 | elem = strings.SplitN(line[0:len(line)-1], " ", 2) |
| 412 | |
| 413 | // Turn string into sigma id |
| 414 | number, err := strconv.Atoi(elem[0]) |
| 415 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 416 | // ID needs to be > 1 |
| 417 | number++ |
| 418 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 419 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 420 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 421 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 424 | tok.sigmaCount = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 425 | |
| 426 | var symbol rune |
| 427 | |
| 428 | // Read rune |
| 429 | if utf8.RuneCountInString(elem[1]) == 1 { |
| 430 | symbol = []rune(elem[1])[0] |
| 431 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 432 | } else if utf8.RuneCountInString(elem[1]) > 1 { |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 433 | |
| 434 | // Probably a MCS |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 435 | switch elem[1] { |
| 436 | case "@_EPSILON_SYMBOL_@": |
| 437 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 438 | tok.epsilon = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 439 | } |
| 440 | case "@_UNKNOWN_SYMBOL_@": |
| 441 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 442 | tok.unknown = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | case "@_IDENTITY_SYMBOL_@": |
| 446 | { |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 447 | tok.identity = number |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 448 | } |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 449 | |
| 450 | case "@_TOKEN_SYMBOL_@": |
| 451 | { |
| 452 | tok.tokenend = number |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 453 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 454 | default: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 455 | { |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 456 | // MCS not supported |
| 457 | tok.sigmaMCS[number] = line |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 458 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 459 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 460 | continue |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 461 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 462 | } else { // Probably a new line symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 463 | line, err = r.ReadString('\n') |
| 464 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 465 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 466 | return nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 467 | } |
| 468 | if len(line) != 1 { |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 469 | // MCS not supported |
| 470 | tok.sigmaMCS[number] = line |
| 471 | continue |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 472 | } |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 473 | symbol = rune('\n') |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 476 | tok.sigmaRev[number] = symbol |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 480 | tok.sigmaMCS = nil |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 481 | return tok |
| 482 | } |
| 483 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 484 | // Set alphabet A to the list of all symbols |
| 485 | // outgoing from s |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 486 | func (tok *Tokenizer) getSet(s int, A *[]int) { |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 487 | for a := range tok.transitions[s] { |
| 488 | *A = append(*A, a) |
| 489 | } |
| 490 | |
| 491 | // Not required, but simplifies bug hunting |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 492 | // sort.Ints(*A) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 495 | // ToDoubleArray turns the intermediate tokenizer representation |
| 496 | // into a double array representation. |
| 497 | // |
| 498 | // This is based on Mizobuchi et al (2000), p.128 |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 499 | func (tok *Tokenizer) ToDoubleArray() *DaTokenizer { |
| 500 | |
| 501 | dat := &DaTokenizer{ |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 502 | sigma: make(map[rune]int), |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 503 | transCount: -1, |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 504 | final: tok.final, |
| 505 | unknown: tok.unknown, |
| 506 | identity: tok.identity, |
| 507 | epsilon: tok.epsilon, |
Akron | 03c92fe | 2021-08-09 14:07:57 +0200 | [diff] [blame] | 508 | tokenend: tok.tokenend, |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 511 | dat.resize(dat.final) |
| 512 | |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 513 | for num, sym := range tok.sigmaRev { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 514 | if int(sym) < 256 { |
| 515 | dat.sigmaASCII[int(sym)] = num |
| 516 | } |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 517 | dat.sigma[sym] = num |
| 518 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 519 | |
| 520 | mark := 0 |
| 521 | size := 0 |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 522 | var base uint32 |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 523 | var atrans *edge |
| 524 | var s, s1 int |
| 525 | var t, t1 uint32 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 526 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 527 | // Create a mapping from s (in Ms aka Intermediate FSA) |
| 528 | // to t (in Mt aka Double Array FSA) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 529 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 530 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 531 | // Initialize with the start state |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 532 | table[size] = &mapping{source: 1, target: 1} |
| 533 | size++ |
| 534 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 535 | // Allocate space for the outgoing symbol range |
| 536 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 537 | |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 538 | // TODO: |
| 539 | // Table lookup for the moment |
| 540 | // only gives a minor performance benefit. |
| 541 | // should be rewritten and should preplace the |
| 542 | // table all together. |
| 543 | // tableLookup := make(map[int]uint32) |
| 544 | // tableLookup[1] = 1 |
| 545 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 546 | for mark < size { |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 547 | s = table[mark].source // This is a state in Ms |
| 548 | t = table[mark].target // This is a state in Mt |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 549 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 550 | |
| 551 | // Following the paper, here the state t can be remembered |
| 552 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 553 | A = A[:0] |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 554 | tok.getSet(s, &A) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 555 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 556 | // Set base to the first free slot in the double array |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 557 | base = dat.xCheck(A) |
| 558 | dat.array[t].setBase(base) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 559 | |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 560 | // TODO: |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 561 | // Sort the outgoing transitions based on the |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 562 | // outdegree of .end |
| 563 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 564 | // Iterate over all outgoing symbols |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 565 | for _, a := range A { |
| 566 | |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 567 | if a != tok.final { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 568 | |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 569 | atrans = tok.transitions[s][a] |
| 570 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 571 | // Aka g(s, a) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 572 | s1 = atrans.end |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 573 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 574 | // Store the transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 575 | t1 = base + uint32(a) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 576 | dat.array[t1].setCheck(t) |
| 577 | |
| 578 | // Set maxSize |
| 579 | if dat.maxSize < int(t1) { |
| 580 | dat.maxSize = int(t1) |
| 581 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 582 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 583 | if DEBUG { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 584 | fmt.Println("Translate transition", |
| 585 | s, "->", s1, "(", a, ")", "to", t, "->", t1) |
| 586 | } |
| 587 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 588 | // Mark the state as being the target of a nontoken transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 589 | if atrans.nontoken { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 590 | dat.array[t1].setNonToken(true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 591 | if DEBUG { |
| 592 | fmt.Println("Set", t1, "to nontoken") |
| 593 | } |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 596 | // Mark the state as being the target of a tokenend transition |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 597 | if atrans.tokenend { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 598 | dat.array[t1].setTokenEnd(true) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 599 | if DEBUG { |
| 600 | fmt.Println("Set", t1, "to tokenend") |
| 601 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 604 | // Check for representative states |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 605 | r := stateAlreadyInTable(s1, table, size) |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 606 | // r := tableLookup[s1] |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 607 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 608 | // No representative found |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 609 | if r == 0 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 610 | // Remember the mapping |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 611 | table[size] = &mapping{source: s1, target: t1} |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 612 | // tableLookup[s1] = t1 |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 613 | size++ |
| 614 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 615 | // Overwrite with the representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 616 | dat.array[t1].setBase(r) |
| 617 | dat.array[t1].setSeparate(true) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 618 | } |
| 619 | } else { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 620 | // Store a final transition |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 621 | dat.array[base+uint32(dat.final)].setCheck(t) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 622 | |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 623 | if dat.maxSize < int(base)+dat.final { |
| 624 | dat.maxSize = int(base) + dat.final |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 625 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // Following Mizobuchi et al (2000) the size of the |
| 631 | // FSA should be stored in check(1). |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 632 | // We make the size a bit smaller so we never have to check for boundaries. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 633 | dat.setSize(dat.maxSize + 1) |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 634 | dat.array = dat.array[:dat.maxSize+1] |
| 635 | return dat |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 638 | // Check the table if a mapping of s |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 639 | // exists and return this as a representative. |
| 640 | // Currently iterates through the whole table |
| 641 | // in a bruteforce manner. |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 642 | func stateAlreadyInTable(s int, table []*mapping, size int) uint32 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 643 | for x := 0; x < size; x++ { |
| 644 | if table[x].source == s { |
| 645 | return table[x].target |
| 646 | } |
| 647 | } |
| 648 | return 0 |
| 649 | } |
| 650 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 651 | // Resize double array when necessary |
| 652 | func (dat *DaTokenizer) resize(l int) { |
| 653 | // TODO: |
| 654 | // This is a bit too aggressive atm and should be calmed down. |
| 655 | if len(dat.array) <= l { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 656 | dat.array = append(dat.array, make([]bc, l)...) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 657 | } |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 658 | } |
Akron | c9d84a6 | 2021-08-03 15:56:03 +0200 | [diff] [blame] | 659 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 660 | // Set base value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 661 | func (bc *bc) setBase(v uint32) { |
| 662 | bc.base = v |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // Get base value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 666 | func (bc *bc) getBase() uint32 { |
| 667 | return bc.base & RESTBIT |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // Set check value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 671 | func (bc *bc) setCheck(v uint32) { |
| 672 | bc.check = v |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // Get check value in double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 676 | func (bc *bc) getCheck() uint32 { |
| 677 | return bc.check & RESTBIT |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 680 | // Returns true if a state is separate pointing to a representative |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 681 | func (bc *bc) isSeparate() bool { |
| 682 | return bc.base&FIRSTBIT != 0 |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // Mark a state as separate pointing to a representative |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 686 | func (bc *bc) setSeparate(sep bool) { |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 687 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 688 | bc.base |= FIRSTBIT |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 689 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 690 | bc.base &= (RESTBIT | SECONDBIT) |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 694 | // Returns true if a state is the target of a nontoken transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 695 | func (bc *bc) isNonToken() bool { |
| 696 | return bc.check&FIRSTBIT != 0 |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // Mark a state as being the target of a nontoken transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 700 | func (bc *bc) setNonToken(sep bool) { |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 701 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 702 | bc.check |= FIRSTBIT |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 703 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 704 | bc.check &= (RESTBIT | SECONDBIT) |
Akron | 83e75a2 | 2021-08-04 13:14:06 +0200 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 708 | // Returns true if a state is the target of a tokenend transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 709 | func (bc *bc) isTokenEnd() bool { |
| 710 | return bc.check&SECONDBIT != 0 |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // Mark a state as being the target of a tokenend transition |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 714 | func (bc *bc) setTokenEnd(sep bool) { |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 715 | if sep { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 716 | bc.check |= SECONDBIT |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 717 | } else { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 718 | bc.check &= (RESTBIT | FIRSTBIT) |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 722 | // Set size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 723 | func (dat *DaTokenizer) setSize(v int) { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 724 | dat.array[1].setCheck(uint32(v)) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // Get size of double array |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 728 | func (dat *DaTokenizer) GetSize() int { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 729 | return int(dat.array[1].getCheck()) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | // Based on Mizobuchi et al (2000), p. 124 |
| 733 | // This iterates for every state through the complete double array |
| 734 | // structure until it finds a gap that fits all outgoing transitions |
| 735 | // of the state. This is extremely slow, but is only necessary in the |
| 736 | // construction phase of the tokenizer. |
Akron | 3fdfec6 | 2021-08-04 11:40:10 +0200 | [diff] [blame] | 737 | func (dat *DaTokenizer) xCheck(symbols []int) uint32 { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 738 | |
| 739 | // Start at the first entry of the double array list |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 740 | base := uint32(1) |
| 741 | |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 742 | OVERLAP: |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 743 | // Resize the array if necessary |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 744 | dat.resize(int(base) + dat.final) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 745 | for _, a := range symbols { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 746 | if dat.array[int(base)+a].getCheck() != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 747 | base++ |
| 748 | goto OVERLAP |
| 749 | } |
| 750 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 751 | return base |
| 752 | } |
| 753 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 754 | // List all outgoing transitions for a state |
| 755 | // for testing purposes |
| 756 | func (dat *DaTokenizer) outgoing(t uint32) []int { |
| 757 | |
| 758 | valid := make([]int, 0, len(dat.sigma)) |
| 759 | |
| 760 | for _, a := range dat.sigma { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 761 | t1 := dat.array[t].getBase() + uint32(a) |
| 762 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 763 | valid = append(valid, a) |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | for _, a := range []int{dat.epsilon, dat.unknown, dat.identity, dat.final} { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 768 | t1 := dat.array[t].getBase() + uint32(a) |
| 769 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 770 | valid = append(valid, -1*a) |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | sort.Ints(valid) |
| 775 | |
| 776 | return valid |
| 777 | } |
| 778 | |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 779 | // TransCount as the number of transitions aka arcs in the |
| 780 | // finite state automaton |
| 781 | func (dat *DaTokenizer) TransCount() int { |
| 782 | // Cache the transCount |
| 783 | if dat.transCount > 0 { |
| 784 | return dat.transCount |
| 785 | } |
| 786 | |
| 787 | dat.transCount = 0 |
| 788 | for x := 1; x < len(dat.array); x++ { |
| 789 | if dat.array[x].getBase() != 0 { |
| 790 | dat.transCount++ |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return dat.transCount |
| 795 | } |
| 796 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 797 | // LoadFactor as defined in Kanda et al (2018), |
| 798 | // i.e. the proportion of non-empty elements to all elements. |
| 799 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 800 | return float64(dat.TransCount()) / float64(len(dat.array)) * 100 |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 803 | // Save stores the double array data in a file |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 804 | func (dat *DaTokenizer) Save(file string) (n int64, err error) { |
| 805 | f, err := os.Create(file) |
| 806 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 807 | log.Println(err) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 808 | return 0, err |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 809 | } |
| 810 | defer f.Close() |
| 811 | gz := gzip.NewWriter(f) |
| 812 | defer gz.Close() |
| 813 | n, err = dat.WriteTo(gz) |
| 814 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 815 | log.Println(err) |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 816 | return n, err |
| 817 | } |
| 818 | gz.Flush() |
| 819 | return n, nil |
| 820 | } |
| 821 | |
| 822 | // WriteTo stores the double array data in an io.Writer. |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 823 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 824 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 825 | wb := bufio.NewWriter(w) |
| 826 | defer wb.Flush() |
| 827 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 828 | // Store magical header |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 829 | all, err := wb.Write([]byte(MAGIC)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 830 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 831 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 832 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // Get sigma as a list |
| 836 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 837 | max := 0 |
| 838 | for sym, num := range dat.sigma { |
| 839 | sigmalist[num] = sym |
| 840 | if num > max { |
| 841 | max = num |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | sigmalist = sigmalist[:max+1] |
| 846 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 847 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 848 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 849 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 850 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 851 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 852 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 853 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 854 | bo.PutUint32(buf[12:16], uint32(len(dat.array)*2)) // Legacy support |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 855 | more, err := wb.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 856 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 857 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 858 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | all += more |
| 862 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 863 | // Write sigma |
| 864 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 865 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 866 | more, err = wb.WriteRune(sym) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 867 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 868 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 869 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 870 | } |
| 871 | all += more |
| 872 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 873 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 874 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 875 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 876 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 877 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 878 | |
| 879 | // Test marker - could be checksum |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 880 | more, err = wb.Write([]byte("T")) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 881 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 882 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 883 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 884 | } |
| 885 | all += more |
| 886 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 887 | // for x := 0; x < len(dat.array); x++ { |
| 888 | for _, bc := range dat.array { |
| 889 | bo.PutUint32(buf[0:4], bc.base) |
| 890 | more, err = wb.Write(buf[0:4]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 891 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 892 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 893 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 894 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 895 | all += more |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 896 | if more != 4 { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 897 | log.Println("Can not write base uint32") |
| 898 | return int64(all), err |
| 899 | } |
| 900 | bo.PutUint32(buf[0:4], bc.check) |
| 901 | more, err = wb.Write(buf[0:4]) |
| 902 | if err != nil { |
| 903 | log.Println(err) |
| 904 | return int64(all), err |
| 905 | } |
| 906 | all += more |
| 907 | if more != 4 { |
| 908 | log.Println("Can not write check uint32") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 909 | return int64(all), err |
| 910 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | return int64(all), err |
| 914 | } |
| 915 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 916 | // LoadDatokFile reads a double array represented tokenizer |
| 917 | // from a file. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 918 | func LoadDatokFile(file string) *DaTokenizer { |
| 919 | f, err := os.Open(file) |
| 920 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 921 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 922 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 923 | } |
| 924 | defer f.Close() |
| 925 | |
| 926 | gz, err := gzip.NewReader(f) |
| 927 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 928 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 929 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 930 | } |
| 931 | defer gz.Close() |
| 932 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 933 | // Todo: Read the whole file! |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 934 | return ParseDatok(gz) |
| 935 | } |
| 936 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 937 | // LoadDatokFile reads a double array represented tokenizer |
| 938 | // from an io.Reader |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 939 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 940 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 941 | // Initialize tokenizer with default values |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 942 | dat := &DaTokenizer{ |
| 943 | sigma: make(map[rune]int), |
| 944 | epsilon: 0, |
| 945 | unknown: 0, |
| 946 | identity: 0, |
| 947 | final: 0, |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 948 | transCount: 0, |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | r := bufio.NewReader(ior) |
| 952 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 953 | buf := make([]byte, 1024) |
| 954 | buf = buf[0:len(MAGIC)] |
| 955 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 956 | _, err := r.Read(buf) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 957 | |
| 958 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 959 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 960 | return nil |
| 961 | } |
| 962 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 963 | if string(MAGIC) != string(buf) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 964 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 965 | return nil |
| 966 | } |
| 967 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 968 | more, err := io.ReadFull(r, buf[0:16]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 969 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 970 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 971 | return nil |
| 972 | } |
| 973 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 974 | if more != 16 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 975 | log.Println("Read bytes do not fit") |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 976 | return nil |
| 977 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 978 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 979 | version := bo.Uint16(buf[0:2]) |
| 980 | |
| 981 | if version != VERSION { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 982 | log.Println("Version not compatible") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 983 | return nil |
| 984 | } |
| 985 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 986 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 987 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 988 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 989 | dat.final = int(bo.Uint16(buf[8:10])) |
| 990 | |
| 991 | sigmaCount := int(bo.Uint16(buf[10:12])) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 992 | arraySize := int(bo.Uint32(buf[12:16])) / 2 // Legacy support |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 993 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 994 | // Shouldn't be relevant though |
| 995 | dat.maxSize = arraySize - 1 |
| 996 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 997 | for x := 0; x < sigmaCount; x++ { |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 998 | sym, _, err := r.ReadRune() |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 999 | if err == nil && sym != 0 { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 1000 | if int(sym) < 256 { |
| 1001 | dat.sigmaASCII[int(sym)] = x |
| 1002 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1003 | dat.sigma[sym] = x |
| 1004 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1007 | _, err = io.ReadFull(r, buf[0:1]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1008 | |
| 1009 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 1010 | log.Print(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1011 | return nil |
| 1012 | } |
| 1013 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1014 | if string("T") != string(buf[0:1]) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 1015 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1016 | return nil |
| 1017 | } |
| 1018 | |
| 1019 | // Read based on length |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1020 | dat.array = make([]bc, arraySize) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1021 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1022 | dataArray, err := io.ReadAll(r) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1023 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1024 | if err == io.EOF { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 1025 | log.Println(err) |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1026 | return nil |
| 1027 | } |
| 1028 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1029 | if len(dataArray) < arraySize*8 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 1030 | log.Println("Not enough bytes read") |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1031 | return nil |
| 1032 | } |
| 1033 | |
| 1034 | for x := 0; x < arraySize; x++ { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1035 | dat.array[x].base = bo.Uint32(dataArray[x*8 : (x*8)+4]) |
| 1036 | dat.array[x].check = bo.Uint32(dataArray[(x*8)+4 : (x*8)+8]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | return dat |
| 1040 | } |
| 1041 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1042 | // Show the current state of the buffer, |
| 1043 | // for testing puroses |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1044 | func showBuffer(buffer []rune, buffo int, buffi int) string { |
| 1045 | out := make([]rune, 0, 1024) |
| 1046 | for x := 0; x < len(buffer); x++ { |
| 1047 | if buffi == x { |
| 1048 | out = append(out, '^') |
| 1049 | } |
| 1050 | if buffo == x { |
| 1051 | out = append(out, '[', buffer[x], ']') |
| 1052 | } else { |
| 1053 | out = append(out, buffer[x]) |
| 1054 | } |
| 1055 | } |
| 1056 | return string(out) |
| 1057 | } |
| 1058 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1059 | // Transduce an input string against the double array |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1060 | // FSA. The rules are always greedy. If the automaton fails, |
| 1061 | // it takes the last possible token ending branch. |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1062 | // |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 1063 | // Based on Mizobuchi et al (2000), p. 129, |
| 1064 | // with additional support for IDENTITY, UNKNOWN |
| 1065 | // and EPSILON transitions and NONTOKEN and TOKENEND handling. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1066 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1067 | var a int |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1068 | var t0 uint32 |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1069 | t := uint32(1) // Initial state |
| 1070 | var ok, rewindBuffer bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1071 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1072 | // Remember the last position of a possible tokenend, |
| 1073 | // in case the automaton fails. |
| 1074 | epsilonState := uint32(0) |
| 1075 | epsilonOffset := 0 |
| 1076 | |
| 1077 | // Implement a low level buffer for full control, |
| 1078 | // however - it is probably better to introduce |
| 1079 | // this on a higher level with a io.Reader interface |
| 1080 | // The buffer stores a single word and may have white |
| 1081 | // space at the end (but not at the beginning). |
| 1082 | // |
| 1083 | // This is the only backtracking requirement because of |
| 1084 | // epsilon transitions, to support tokenizations like: |
| 1085 | // "this is an example|.| And it works." vs |
| 1086 | // "this is an example.com| application." |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1087 | // |
| 1088 | // TODO: |
| 1089 | // Store a translation buffer as well, so characters don't |
| 1090 | // have to be translated multiple times! |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1091 | buffer := make([]rune, 1024) |
| 1092 | buffo := 0 // Buffer offset |
| 1093 | buffi := 0 // Buffer length |
| 1094 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1095 | reader := bufio.NewReader(r) |
| 1096 | writer := bufio.NewWriter(w) |
| 1097 | defer writer.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1098 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1099 | var char rune |
| 1100 | var err error |
| 1101 | eof := false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1102 | newchar := true |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1103 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1104 | PARSECHAR: |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1105 | for { |
| 1106 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1107 | if newchar { |
| 1108 | // Get from reader if buffer is empty |
| 1109 | if buffo >= buffi { |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 1110 | if eof { |
| 1111 | break |
| 1112 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1113 | char, _, err = reader.ReadRune() |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1114 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1115 | // No more runes to read |
| 1116 | if err != nil { |
| 1117 | eof = true |
| 1118 | break |
| 1119 | } |
| 1120 | buffer[buffi] = char |
| 1121 | buffi++ |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1122 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1123 | |
| 1124 | char = buffer[buffo] |
| 1125 | |
| 1126 | if DEBUG { |
| 1127 | fmt.Println("Current char", string(char), showBuffer(buffer, buffo, buffi)) |
| 1128 | } |
| 1129 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 1130 | // TODO: |
| 1131 | // Better not repeatedly check for a! |
| 1132 | // Possibly keep a buffer with a. |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 1133 | if int(char) < 256 { |
| 1134 | a = dat.sigmaASCII[int(char)] |
| 1135 | } else { |
| 1136 | a, ok = dat.sigma[char] |
| 1137 | if !ok { |
| 1138 | a = 0 |
| 1139 | } |
| 1140 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1141 | |
| 1142 | // Use identity symbol if character is not in sigma |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 1143 | if a == 0 && dat.identity != -1 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1144 | a = dat.identity |
| 1145 | } |
| 1146 | |
| 1147 | t0 = t |
| 1148 | |
| 1149 | // Check for epsilon transitions and remember |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1150 | if dat.array[dat.array[t0].getBase()+uint32(dat.epsilon)].getCheck() == t0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1151 | // Remember state for backtracking to last tokenend state |
| 1152 | epsilonState = t0 |
| 1153 | epsilonOffset = buffo |
| 1154 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1155 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1156 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1157 | // Checks a transition based on t0, a and buffo |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1158 | t = dat.array[t0].getBase() + uint32(a) |
| 1159 | ta := dat.array[t] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1160 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1161 | if DEBUG { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1162 | // Char is only relevant if set |
| 1163 | fmt.Println("Check", t0, "-", a, "(", string(char), ")", "->", t) |
| 1164 | if false { |
| 1165 | fmt.Println(dat.outgoing(t0)) |
| 1166 | } |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1169 | // Check if the transition is invalid according to the double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1170 | if t > dat.array[1].getCheck() || ta.getCheck() != t0 { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1171 | |
| 1172 | if DEBUG { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1173 | fmt.Println("Match is not fine!", t, "and", ta.getCheck(), "vs", t0) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | if !ok && a == dat.identity { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1177 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1178 | // Try again with unknown symbol, in case identity failed |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1179 | // Char is only relevant when set |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1180 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1181 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1182 | } |
| 1183 | a = dat.unknown |
| 1184 | |
| 1185 | } else if a != dat.epsilon { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1186 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1187 | // Try again with epsilon symbol, in case everything else failed |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1188 | t0 = epsilonState |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1189 | epsilonState = 0 // reset |
| 1190 | buffo = epsilonOffset |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1191 | a = dat.epsilon |
| 1192 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1193 | if DEBUG { |
| 1194 | fmt.Println("Get from epsilon stack and set buffo!", showBuffer(buffer, buffo, buffi)) |
| 1195 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1196 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1197 | } else { |
| 1198 | break |
| 1199 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1200 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1201 | newchar = false |
| 1202 | continue |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1205 | // Transition was successful |
| 1206 | rewindBuffer = false |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1207 | |
| 1208 | // Transition consumes a character |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1209 | if a != dat.epsilon { |
| 1210 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1211 | buffo++ |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1212 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1213 | // Transition does not produce a character |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1214 | if buffo == 1 && ta.isNonToken() { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1215 | if DEBUG { |
| 1216 | fmt.Println("Nontoken forward", showBuffer(buffer, buffo, buffi)) |
| 1217 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1218 | rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1219 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1220 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1221 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1222 | // Transition marks the end of a token - so flush the buffer |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1223 | if ta.isTokenEnd() { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1224 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1225 | if buffi > 0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1226 | if DEBUG { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1227 | fmt.Println("-> Flush buffer: [", string(buffer[:buffo]), "]", showBuffer(buffer, buffo, buffi)) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1228 | } |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1229 | writer.WriteString(string(buffer[:buffo])) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1230 | rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1231 | } |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 1232 | if DEBUG { |
| 1233 | fmt.Println("-> Newline") |
| 1234 | } |
| 1235 | writer.WriteRune('\n') |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1236 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1237 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1238 | // Rewind the buffer if necessary |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1239 | if rewindBuffer { |
| 1240 | |
| 1241 | // TODO: Better as a ring buffer |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1242 | for x, i := range buffer[buffo:buffi] { |
| 1243 | buffer[x] = i |
| 1244 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1245 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1246 | buffi -= buffo |
| 1247 | epsilonOffset -= buffo |
| 1248 | buffo = 0 |
| 1249 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1250 | fmt.Println("Remaining:", showBuffer(buffer, buffo, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1251 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1254 | // Move to representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1255 | if ta.isSeparate() { |
| 1256 | t = ta.getBase() |
| 1257 | ta = dat.array[t] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1258 | |
| 1259 | if DEBUG { |
| 1260 | fmt.Println("Representative pointing to", t) |
| 1261 | } |
| 1262 | } |
| 1263 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1264 | newchar = true |
| 1265 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1266 | // TODO: |
| 1267 | // Prevent endless epsilon loops! |
| 1268 | } |
| 1269 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1270 | // Input reader is not yet finished |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1271 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1272 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1273 | fmt.Println("Not at the end - problem", t0, ":", dat.outgoing(t0)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1274 | } |
| 1275 | return false |
| 1276 | } |
| 1277 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1278 | if DEBUG { |
| 1279 | fmt.Println("Entering final check") |
| 1280 | } |
| 1281 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1282 | // Automaton is in a final state, so flush the buffer and return |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1283 | x := dat.array[t].getBase() + uint32(dat.final) |
| 1284 | |
| 1285 | if x < dat.array[1].getCheck() && dat.array[x].getCheck() == t { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1286 | |
| 1287 | if buffi > 0 { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1288 | if DEBUG { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1289 | fmt.Println("-> Flush buffer: [", string(buffer[:buffi]), "]") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1290 | } |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1291 | writer.WriteString(string(buffer[:buffi])) |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1292 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1293 | if dat.array[t].isTokenEnd() { |
Akron | df0a3ef | 2021-08-09 15:53:45 +0200 | [diff] [blame] | 1294 | writer.WriteRune('\n') |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1295 | if DEBUG { |
| 1296 | fmt.Println("-> Newline") |
| 1297 | } |
Akron | df0a3ef | 2021-08-09 15:53:45 +0200 | [diff] [blame] | 1298 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1301 | // Add an additional sentence ending, if the file is over but no explicit |
| 1302 | // sentence split was reached. This may be controversial and therefore |
| 1303 | // optional via parameter. |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1304 | if !dat.array[t0].isTokenEnd() { |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1305 | writer.WriteRune('\n') |
| 1306 | if DEBUG { |
| 1307 | fmt.Println("-> Newline") |
| 1308 | } |
| 1309 | } |
| 1310 | |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 1311 | // TODO: |
| 1312 | // There may be a new line at the end, from an epsilon, |
| 1313 | // so we may need to go on! |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1314 | return true |
| 1315 | } |
| 1316 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1317 | // Check epsilon transitions until a final state is reached |
| 1318 | t0 = t |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1319 | t = dat.array[t0].getBase() + uint32(dat.epsilon) |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1320 | a = dat.epsilon |
| 1321 | newchar = false |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1322 | if dat.array[t].getCheck() == t0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1323 | // Remember state for backtracking to last tokenend state |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1324 | goto PARSECHAR |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 1325 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1326 | } else if epsilonState != 0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1327 | t0 = epsilonState |
| 1328 | epsilonState = 0 // reset |
| 1329 | buffo = epsilonOffset |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1330 | if DEBUG { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1331 | fmt.Println("Get from epsilon stack and set buffo!", showBuffer(buffer, buffo, buffi)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1332 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1333 | goto PARSECHAR |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1334 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1335 | return false |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1336 | } |