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