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