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 | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 520 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 521 | // Create a mapping from s (in Ms aka Intermediate FSA) |
| 522 | // to t (in Mt aka Double Array FSA) |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 523 | table := make([]*mapping, tok.arcCount+1) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 524 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 525 | // Initialize with the start state |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 526 | table[size] = &mapping{source: 1, target: 1} |
| 527 | size++ |
| 528 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 529 | // Allocate space for the outgoing symbol range |
| 530 | A := make([]int, 0, tok.sigmaCount) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 531 | |
| 532 | for mark < size { |
| 533 | s := table[mark].source // This is a state in Ms |
| 534 | t := table[mark].target // This is a state in Mt |
| 535 | mark++ |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 536 | |
| 537 | // Following the paper, here the state t can be remembered |
| 538 | // in the set of states St |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 539 | A = A[:0] |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 540 | tok.getSet(s, &A) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 541 | |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 542 | // Set base to the first free slot in the double array |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame^] | 543 | base = dat.xCheck(A) |
| 544 | dat.array[t].setBase(base) |
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 | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame^] | 559 | t1 := base + uint32(a) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 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 | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame^] | 603 | dat.array[base+uint32(dat.final)].setCheck(t) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 604 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame^] | 605 | if dat.maxSize < int(base+uint32(dat.final)) { |
| 606 | dat.maxSize = int(base + uint32(dat.final)) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 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 | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame^] | 722 | base := uint32(1) |
| 723 | |
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 | // Resize the array if necessary |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 726 | dat.resize(int(base) + dat.final) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 727 | for _, a := range symbols { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 728 | if dat.array[int(base)+a].getCheck() != 0 { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 729 | base++ |
| 730 | goto OVERLAP |
| 731 | } |
| 732 | } |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 733 | return base |
| 734 | } |
| 735 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 736 | // List all outgoing transitions for a state |
| 737 | // for testing purposes |
| 738 | func (dat *DaTokenizer) outgoing(t uint32) []int { |
| 739 | |
| 740 | valid := make([]int, 0, len(dat.sigma)) |
| 741 | |
| 742 | for _, a := range dat.sigma { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 743 | t1 := dat.array[t].getBase() + uint32(a) |
| 744 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 745 | valid = append(valid, a) |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | for _, a := range []int{dat.epsilon, dat.unknown, dat.identity, dat.final} { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 750 | t1 := dat.array[t].getBase() + uint32(a) |
| 751 | if t1 <= dat.array[1].getCheck() && dat.array[t1].getCheck() == t { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 752 | valid = append(valid, -1*a) |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | sort.Ints(valid) |
| 757 | |
| 758 | return valid |
| 759 | } |
| 760 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 761 | // LoadFactor as defined in Kanda et al (2018), |
| 762 | // i.e. the proportion of non-empty elements to all elements. |
| 763 | func (dat *DaTokenizer) LoadFactor() float64 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 764 | |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 765 | // Cache the loadfactor |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 766 | if dat.loadFactor > 0 { |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 767 | return dat.loadFactor |
Akron | 773b1ef | 2021-08-03 17:37:20 +0200 | [diff] [blame] | 768 | } |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 769 | nonEmpty := 0 |
| 770 | all := len(dat.array) / 2 |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 771 | for x := 1; x < len(dat.array); x++ { |
| 772 | if dat.array[x].getBase() != 0 { |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 773 | nonEmpty++ |
| 774 | } |
| 775 | } |
Akron | 03a3c61 | 2021-08-04 11:51:27 +0200 | [diff] [blame] | 776 | dat.loadFactor = float64(nonEmpty) / float64(all) * 100 |
| 777 | return dat.loadFactor |
Akron | d66a926 | 2021-08-03 17:09:09 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 780 | // Save stores the double array data in a file |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 781 | func (dat *DaTokenizer) Save(file string) (n int64, err error) { |
| 782 | f, err := os.Create(file) |
| 783 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 784 | log.Println(err) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 785 | return 0, err |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 786 | } |
| 787 | defer f.Close() |
| 788 | gz := gzip.NewWriter(f) |
| 789 | defer gz.Close() |
| 790 | n, err = dat.WriteTo(gz) |
| 791 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 792 | log.Println(err) |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 793 | return n, err |
| 794 | } |
| 795 | gz.Flush() |
| 796 | return n, nil |
| 797 | } |
| 798 | |
| 799 | // WriteTo stores the double array data in an io.Writer. |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 800 | func (dat *DaTokenizer) WriteTo(w io.Writer) (n int64, err error) { |
| 801 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 802 | wb := bufio.NewWriter(w) |
| 803 | defer wb.Flush() |
| 804 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 805 | // Store magical header |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 806 | all, err := wb.Write([]byte(MAGIC)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 807 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 808 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 809 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Get sigma as a list |
| 813 | sigmalist := make([]rune, len(dat.sigma)+16) |
| 814 | max := 0 |
| 815 | for sym, num := range dat.sigma { |
| 816 | sigmalist[num] = sym |
| 817 | if num > max { |
| 818 | max = num |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | sigmalist = sigmalist[:max+1] |
| 823 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 824 | buf := make([]byte, 0, 16) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 825 | bo.PutUint16(buf[0:2], VERSION) |
Akron | c17f1ca | 2021-08-03 19:47:27 +0200 | [diff] [blame] | 826 | bo.PutUint16(buf[2:4], uint16(dat.epsilon)) |
| 827 | bo.PutUint16(buf[4:6], uint16(dat.unknown)) |
| 828 | bo.PutUint16(buf[6:8], uint16(dat.identity)) |
| 829 | bo.PutUint16(buf[8:10], uint16(dat.final)) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 830 | bo.PutUint16(buf[10:12], uint16(len(sigmalist))) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 831 | bo.PutUint32(buf[12:16], uint32(len(dat.array)*2)) // Legacy support |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 832 | more, err := wb.Write(buf[0:16]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 833 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 834 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 835 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | all += more |
| 839 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 840 | // Write sigma |
| 841 | for _, sym := range sigmalist { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 842 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 843 | more, err = wb.WriteRune(sym) |
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 | all += more |
| 849 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 850 | |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 851 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 852 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 853 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 854 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 855 | |
| 856 | // Test marker - could be checksum |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 857 | more, err = wb.Write([]byte("T")) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 858 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 859 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 860 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 861 | } |
| 862 | all += more |
| 863 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 864 | // for x := 0; x < len(dat.array); x++ { |
| 865 | for _, bc := range dat.array { |
| 866 | bo.PutUint32(buf[0:4], bc.base) |
| 867 | more, err = wb.Write(buf[0:4]) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 868 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 869 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 870 | return int64(all), err |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 871 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 872 | all += more |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 873 | if more != 4 { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 874 | log.Println("Can not write base uint32") |
| 875 | return int64(all), err |
| 876 | } |
| 877 | bo.PutUint32(buf[0:4], bc.check) |
| 878 | more, err = wb.Write(buf[0:4]) |
| 879 | if err != nil { |
| 880 | log.Println(err) |
| 881 | return int64(all), err |
| 882 | } |
| 883 | all += more |
| 884 | if more != 4 { |
| 885 | log.Println("Can not write check uint32") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 886 | return int64(all), err |
| 887 | } |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | return int64(all), err |
| 891 | } |
| 892 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 893 | // LoadDatokFile reads a double array represented tokenizer |
| 894 | // from a file. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 895 | func LoadDatokFile(file string) *DaTokenizer { |
| 896 | f, err := os.Open(file) |
| 897 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 898 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 899 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 900 | } |
| 901 | defer f.Close() |
| 902 | |
| 903 | gz, err := gzip.NewReader(f) |
| 904 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 905 | log.Println(err) |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 906 | return nil |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 907 | } |
| 908 | defer gz.Close() |
| 909 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 910 | // Todo: Read the whole file! |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 911 | return ParseDatok(gz) |
| 912 | } |
| 913 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 914 | // LoadDatokFile reads a double array represented tokenizer |
| 915 | // from an io.Reader |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 916 | func ParseDatok(ior io.Reader) *DaTokenizer { |
| 917 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 918 | // Initialize tokenizer with default values |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 919 | dat := &DaTokenizer{ |
| 920 | sigma: make(map[rune]int), |
| 921 | epsilon: 0, |
| 922 | unknown: 0, |
| 923 | identity: 0, |
| 924 | final: 0, |
| 925 | loadFactor: 0, |
| 926 | } |
| 927 | |
| 928 | r := bufio.NewReader(ior) |
| 929 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 930 | buf := make([]byte, 1024) |
| 931 | buf = buf[0:len(MAGIC)] |
| 932 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 933 | _, err := r.Read(buf) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 934 | |
| 935 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 936 | log.Println(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 937 | return nil |
| 938 | } |
| 939 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 940 | if string(MAGIC) != string(buf) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 941 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 942 | return nil |
| 943 | } |
| 944 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 945 | more, err := io.ReadFull(r, buf[0:16]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 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 | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 951 | if more != 16 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 952 | log.Println("Read bytes do not fit") |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 953 | return nil |
| 954 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 955 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 956 | version := bo.Uint16(buf[0:2]) |
| 957 | |
| 958 | if version != VERSION { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 959 | log.Println("Version not compatible") |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 960 | return nil |
| 961 | } |
| 962 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 963 | dat.epsilon = int(bo.Uint16(buf[2:4])) |
| 964 | dat.unknown = int(bo.Uint16(buf[4:6])) |
| 965 | dat.identity = int(bo.Uint16(buf[6:8])) |
| 966 | dat.final = int(bo.Uint16(buf[8:10])) |
| 967 | |
| 968 | sigmaCount := int(bo.Uint16(buf[10:12])) |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 969 | arraySize := int(bo.Uint32(buf[12:16])) / 2 // Legacy support |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 970 | |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 971 | // Shouldn't be relevant though |
| 972 | dat.maxSize = arraySize - 1 |
| 973 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 974 | for x := 0; x < sigmaCount; x++ { |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 975 | sym, _, err := r.ReadRune() |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 976 | if err == nil && sym != 0 { |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 977 | if int(sym) < 256 { |
| 978 | dat.sigmaASCII[int(sym)] = x |
| 979 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 980 | dat.sigma[sym] = x |
| 981 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 984 | _, err = io.ReadFull(r, buf[0:1]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 985 | |
| 986 | if err != nil { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 987 | log.Print(err) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 988 | return nil |
| 989 | } |
| 990 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 991 | if string("T") != string(buf[0:1]) { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 992 | log.Println("Not a datok file") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 993 | return nil |
| 994 | } |
| 995 | |
| 996 | // Read based on length |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 997 | dat.array = make([]bc, arraySize) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 998 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 999 | dataArray, err := io.ReadAll(r) |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1000 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1001 | if err == io.EOF { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 1002 | log.Println(err) |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1003 | return nil |
| 1004 | } |
| 1005 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1006 | if len(dataArray) < arraySize*8 { |
Akron | 527c10c | 2021-08-13 01:45:18 +0200 | [diff] [blame] | 1007 | log.Println("Not enough bytes read") |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1008 | return nil |
| 1009 | } |
| 1010 | |
| 1011 | for x := 0; x < arraySize; x++ { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1012 | dat.array[x].base = bo.Uint32(dataArray[x*8 : (x*8)+4]) |
| 1013 | dat.array[x].check = bo.Uint32(dataArray[(x*8)+4 : (x*8)+8]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | return dat |
| 1017 | } |
| 1018 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1019 | // Show the current state of the buffer, |
| 1020 | // for testing puroses |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1021 | func showBuffer(buffer []rune, buffo int, buffi int) string { |
| 1022 | out := make([]rune, 0, 1024) |
| 1023 | for x := 0; x < len(buffer); x++ { |
| 1024 | if buffi == x { |
| 1025 | out = append(out, '^') |
| 1026 | } |
| 1027 | if buffo == x { |
| 1028 | out = append(out, '[', buffer[x], ']') |
| 1029 | } else { |
| 1030 | out = append(out, buffer[x]) |
| 1031 | } |
| 1032 | } |
| 1033 | return string(out) |
| 1034 | } |
| 1035 | |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1036 | // Transduce an input string against the double array |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1037 | // FSA. The rules are always greedy. If the automaton fails, |
| 1038 | // it takes the last possible token ending branch. |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1039 | // |
Akron | 4db3ecf | 2021-08-11 18:49:03 +0200 | [diff] [blame] | 1040 | // Based on Mizobuchi et al (2000), p. 129, |
| 1041 | // with additional support for IDENTITY, UNKNOWN |
| 1042 | // and EPSILON transitions and NONTOKEN and TOKENEND handling. |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1043 | func (dat *DaTokenizer) Transduce(r io.Reader, w io.Writer) bool { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1044 | var a int |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1045 | var t0 uint32 |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1046 | t := uint32(1) // Initial state |
| 1047 | var ok, rewindBuffer bool |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1048 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1049 | // Remember the last position of a possible tokenend, |
| 1050 | // in case the automaton fails. |
| 1051 | epsilonState := uint32(0) |
| 1052 | epsilonOffset := 0 |
| 1053 | |
| 1054 | // Implement a low level buffer for full control, |
| 1055 | // however - it is probably better to introduce |
| 1056 | // this on a higher level with a io.Reader interface |
| 1057 | // The buffer stores a single word and may have white |
| 1058 | // space at the end (but not at the beginning). |
| 1059 | // |
| 1060 | // This is the only backtracking requirement because of |
| 1061 | // epsilon transitions, to support tokenizations like: |
| 1062 | // "this is an example|.| And it works." vs |
| 1063 | // "this is an example.com| application." |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1064 | // |
| 1065 | // TODO: |
| 1066 | // Store a translation buffer as well, so characters don't |
| 1067 | // have to be translated multiple times! |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1068 | buffer := make([]rune, 1024) |
| 1069 | buffo := 0 // Buffer offset |
| 1070 | buffi := 0 // Buffer length |
| 1071 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1072 | reader := bufio.NewReader(r) |
| 1073 | writer := bufio.NewWriter(w) |
| 1074 | defer writer.Flush() |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1075 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1076 | var char rune |
| 1077 | var err error |
| 1078 | eof := false |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1079 | newchar := true |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1080 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1081 | PARSECHAR: |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1082 | for { |
| 1083 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1084 | if newchar { |
| 1085 | // Get from reader if buffer is empty |
| 1086 | if buffo >= buffi { |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 1087 | if eof { |
| 1088 | break |
| 1089 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1090 | char, _, err = reader.ReadRune() |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1091 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1092 | // No more runes to read |
| 1093 | if err != nil { |
| 1094 | eof = true |
| 1095 | break |
| 1096 | } |
| 1097 | buffer[buffi] = char |
| 1098 | buffi++ |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1099 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1100 | |
| 1101 | char = buffer[buffo] |
| 1102 | |
| 1103 | if DEBUG { |
| 1104 | fmt.Println("Current char", string(char), showBuffer(buffer, buffo, buffi)) |
| 1105 | } |
| 1106 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame^] | 1107 | // TODO: |
| 1108 | // Better not repeatedly check for a! |
| 1109 | // Possibly keep a buffer with a. |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 1110 | if int(char) < 256 { |
| 1111 | a = dat.sigmaASCII[int(char)] |
| 1112 | } else { |
| 1113 | a, ok = dat.sigma[char] |
| 1114 | if !ok { |
| 1115 | a = 0 |
| 1116 | } |
| 1117 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1118 | |
| 1119 | // Use identity symbol if character is not in sigma |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 1120 | if a == 0 && dat.identity != -1 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1121 | a = dat.identity |
| 1122 | } |
| 1123 | |
| 1124 | t0 = t |
| 1125 | |
| 1126 | // Check for epsilon transitions and remember |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1127 | if dat.array[dat.array[t0].getBase()+uint32(dat.epsilon)].getCheck() == t0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1128 | // Remember state for backtracking to last tokenend state |
| 1129 | epsilonState = t0 |
| 1130 | epsilonOffset = buffo |
| 1131 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1132 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1133 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1134 | // Checks a transition based on t0, a and buffo |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1135 | t = dat.array[t0].getBase() + uint32(a) |
| 1136 | ta := dat.array[t] |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1137 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1138 | if DEBUG { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1139 | // Char is only relevant if set |
| 1140 | fmt.Println("Check", t0, "-", a, "(", string(char), ")", "->", t) |
| 1141 | if false { |
| 1142 | fmt.Println(dat.outgoing(t0)) |
| 1143 | } |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1146 | // Check if the transition is invalid according to the double array |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1147 | if t > dat.array[1].getCheck() || ta.getCheck() != t0 { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1148 | |
| 1149 | if DEBUG { |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1150 | fmt.Println("Match is not fine!", t, "and", ta.getCheck(), "vs", t0) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if !ok && a == dat.identity { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1154 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1155 | // Try again with unknown symbol, in case identity failed |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1156 | // Char is only relevant when set |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1157 | if DEBUG { |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1158 | fmt.Println("UNKNOWN symbol", string(char), "->", dat.unknown) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1159 | } |
| 1160 | a = dat.unknown |
| 1161 | |
| 1162 | } else if a != dat.epsilon { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1163 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1164 | // Try again with epsilon symbol, in case everything else failed |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1165 | t0 = epsilonState |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1166 | epsilonState = 0 // reset |
| 1167 | buffo = epsilonOffset |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1168 | a = dat.epsilon |
| 1169 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1170 | if DEBUG { |
| 1171 | fmt.Println("Get from epsilon stack and set buffo!", showBuffer(buffer, buffo, buffi)) |
| 1172 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1173 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1174 | } else { |
| 1175 | break |
| 1176 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1177 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1178 | newchar = false |
| 1179 | continue |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1182 | // Transition was successful |
| 1183 | rewindBuffer = false |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1184 | |
| 1185 | // Transition consumes a character |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1186 | if a != dat.epsilon { |
| 1187 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1188 | buffo++ |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1189 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1190 | // Transition does not produce a character |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1191 | if buffo == 1 && ta.isNonToken() { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1192 | if DEBUG { |
| 1193 | fmt.Println("Nontoken forward", showBuffer(buffer, buffo, buffi)) |
| 1194 | } |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1195 | rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1196 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1197 | } |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1198 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1199 | // Transition marks the end of a token - so flush the buffer |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1200 | if ta.isTokenEnd() { |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 1201 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1202 | if buffi > 0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1203 | if DEBUG { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1204 | fmt.Println("-> Flush buffer: [", string(buffer[:buffo]), "]", showBuffer(buffer, buffo, buffi)) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1205 | } |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1206 | writer.WriteString(string(buffer[:buffo])) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1207 | rewindBuffer = true |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1208 | } |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 1209 | if DEBUG { |
| 1210 | fmt.Println("-> Newline") |
| 1211 | } |
| 1212 | writer.WriteRune('\n') |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1213 | } |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1214 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1215 | // Rewind the buffer if necessary |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1216 | if rewindBuffer { |
| 1217 | |
| 1218 | // TODO: Better as a ring buffer |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1219 | for x, i := range buffer[buffo:buffi] { |
| 1220 | buffer[x] = i |
| 1221 | } |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1222 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1223 | buffi -= buffo |
| 1224 | epsilonOffset -= buffo |
| 1225 | buffo = 0 |
| 1226 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1227 | fmt.Println("Remaining:", showBuffer(buffer, buffo, buffi)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 1228 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1229 | } |
| 1230 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1231 | // Move to representative state |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1232 | if ta.isSeparate() { |
| 1233 | t = ta.getBase() |
| 1234 | ta = dat.array[t] |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1235 | |
| 1236 | if DEBUG { |
| 1237 | fmt.Println("Representative pointing to", t) |
| 1238 | } |
| 1239 | } |
| 1240 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1241 | newchar = true |
| 1242 | |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1243 | // TODO: |
| 1244 | // Prevent endless epsilon loops! |
| 1245 | } |
| 1246 | |
Akron | 439f4ec | 2021-08-09 15:45:38 +0200 | [diff] [blame] | 1247 | // Input reader is not yet finished |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1248 | if !eof { |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1249 | if DEBUG { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1250 | fmt.Println("Not at the end - problem", t0, ":", dat.outgoing(t0)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1251 | } |
| 1252 | return false |
| 1253 | } |
| 1254 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1255 | if DEBUG { |
| 1256 | fmt.Println("Entering final check") |
| 1257 | } |
| 1258 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1259 | // Automaton is in a final state, so flush the buffer and return |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1260 | x := dat.array[t].getBase() + uint32(dat.final) |
| 1261 | |
| 1262 | if x < dat.array[1].getCheck() && dat.array[x].getCheck() == t { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1263 | |
| 1264 | if buffi > 0 { |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 1265 | if DEBUG { |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1266 | fmt.Println("-> Flush buffer: [", string(buffer[:buffi]), "]") |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 1267 | } |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1268 | writer.WriteString(string(buffer[:buffi])) |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1269 | |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1270 | if dat.array[t].isTokenEnd() { |
Akron | df0a3ef | 2021-08-09 15:53:45 +0200 | [diff] [blame] | 1271 | writer.WriteRune('\n') |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1272 | if DEBUG { |
| 1273 | fmt.Println("-> Newline") |
| 1274 | } |
Akron | df0a3ef | 2021-08-09 15:53:45 +0200 | [diff] [blame] | 1275 | } |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1278 | // Add an additional sentence ending, if the file is over but no explicit |
| 1279 | // sentence split was reached. This may be controversial and therefore |
| 1280 | // optional via parameter. |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1281 | if !dat.array[t0].isTokenEnd() { |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 1282 | writer.WriteRune('\n') |
| 1283 | if DEBUG { |
| 1284 | fmt.Println("-> Newline") |
| 1285 | } |
| 1286 | } |
| 1287 | |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 1288 | // TODO: |
| 1289 | // There may be a new line at the end, from an epsilon, |
| 1290 | // so we may need to go on! |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1291 | return true |
| 1292 | } |
| 1293 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1294 | // Check epsilon transitions until a final state is reached |
| 1295 | t0 = t |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1296 | t = dat.array[t0].getBase() + uint32(dat.epsilon) |
Akron | 01912fc | 2021-08-12 11:41:58 +0200 | [diff] [blame] | 1297 | a = dat.epsilon |
| 1298 | newchar = false |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1299 | if dat.array[t].getCheck() == t0 { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1300 | // Remember state for backtracking to last tokenend state |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1301 | goto PARSECHAR |
Akron | e61380b | 2021-08-16 10:10:46 +0200 | [diff] [blame] | 1302 | |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1303 | } else if epsilonState != 0 { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 1304 | t0 = epsilonState |
| 1305 | epsilonState = 0 // reset |
| 1306 | buffo = epsilonOffset |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1307 | if DEBUG { |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1308 | fmt.Println("Get from epsilon stack and set buffo!", showBuffer(buffer, buffo, buffi)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1309 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1310 | goto PARSECHAR |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1311 | } |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 1312 | return false |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 1313 | } |