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