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