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