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