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