package iskra import "sort" // Token classes, dict.mx. const ( TokUnknown uint8 = 0 TokNoun uint8 = 1 // not followed by ( TokVerb uint8 = 2 // followed by ( - function or method call TokModifier uint8 = 3 // operators, punctuation, whitespace, metadata TokStructural uint8 = 4 // unused, kept for mesh compatibility TokLiteral uint8 = 5 // numbers and string literals ) // DictEntry points at one token in Dict.Pool. type DictEntry struct { Offset uint32 Len uint16 Class uint8 Count uint32 } // NewWordDict returns a dictionary for whole-word tokenisation. func NewWordDict() *Dict { return NewDict() } // Dict is the token dictionary: interned strings plus a token id sequence. // // A dictionary is either a chunk dictionary, filled by the corpus loader with // two-character chunks (the .meta layout this port wrote before Rechunk), or a // word dictionary, filled by Rechunk with whole words. TokenPool holds the ids // of the run(s) and belongs to one of them; Dict.Entries is whichever // dictionary the ids index. type Dict struct { Pool []byte Entries []DictEntry Index map[string]uint32 } // NewDict returns an empty dictionary. func NewDict() *Dict { return &Dict{Index: make(map[string]uint32)} } // Add interns one token, bumping its count if it is already present, and // returns its id. func (d *Dict) Add(token []byte, class uint8) uint32 { s := string(token) if idx, ok := d.Index[s]; ok { d.Entries[idx].Count++ return idx } idx := uint32(len(d.Entries)) off := uint32(len(d.Pool)) d.Pool = append(d.Pool, token...) d.Entries = append(d.Entries, DictEntry{Offset: off, Len: uint16(len(token)), Class: class, Count: 1}) d.Index[s] = idx return idx } // Get returns the token bytes for an id. func (d *Dict) Get(idx uint32) []byte { e := &d.Entries[idx] return d.Pool[e.Offset : e.Offset+uint32(e.Len)] } // Class returns the token class for an id. func (d *Dict) Class(idx uint32) uint8 { return d.Entries[idx].Class } // EntryCount is the number of distinct tokens. func (d *Dict) EntryCount() int { return len(d.Entries) } // PoolSize is the byte length of the token pool. func (d *Dict) PoolSize() int { return len(d.Pool) } // Tokenize splits content into tokens and returns their ids. func (d *Dict) Tokenize(content []byte) []uint32 { toks := Tokenize(content) out := make([]uint32, 0, len(toks)) for _, t := range toks { out = append(out, d.Add([]byte(t.Text), t.Class)) } return out } // Decode concatenates a token-id sequence with no separator. func (d *Dict) Decode(seq []uint32) []byte { var out []byte for _, idx := range seq { if int(idx) >= len(d.Entries) { continue } out = append(out, d.Get(idx)...) } return out } // SortByFrequency reorders entries by descending count and remaps a token // sequence through the renumbering. func (d *Dict) SortByFrequency(tokenPool []uint32) []uint32 { n := len(d.Entries) if n == 0 { return tokenPool } order := make([]int, n) for i := range order { order[i] = i } sort.SliceStable(order, func(a, b int) bool { ca, cb := d.Entries[order[a]].Count, d.Entries[order[b]].Count if ca != cb { return ca > cb } return order[a] < order[b] }) oldToNew := make([]uint32, n) newEntries := make([]DictEntry, n) for newIdx, oldIdx := range order { oldToNew[oldIdx] = uint32(newIdx) newEntries[newIdx] = d.Entries[oldIdx] } d.Entries = newEntries newIndex := make(map[string]uint32, len(d.Index)) for s, oldIdx := range d.Index { newIndex[s] = oldToNew[oldIdx] } d.Index = newIndex for i := range tokenPool { tokenPool[i] = oldToNew[tokenPool[i]] } return tokenPool }