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