// Package lattice is the storage engine behind .iskr files: a C=8 B-tree per // branch, 136-byte nodes, 48-byte records, format version 5 (64-bit keys). // // Everything in this package is a port of iskradb/lattice (*.mx). Where the // moxie source never wrote the data a structure needs, the structure is left // out with a comment naming the file that is missing, rather than stubbed. package lattice // C is the coordination number: 8 keys and 9 children per node. const C = 8 // Branch selects one of the eight per-branch B-trees held by a Tree. type Branch uint8 const ( Bsemantic Branch = 0 // ontological category (animacy, abstractness) Bgrammatical Branch = 1 // syntactic role (nominal/verbal/function) Bcooccur Branch = 2 // PVN co-occurrence context Bmorphology Branch = 3 // morphological state (tense/polarity/aspect) Bpragmatic Branch = 4 // domain/register context Bphonology Branch = 5 // phonological (dormant: all coordinates 0) Bvalency Branch = 6 // argument count Bregister Branch = 7 // social register ) // Key is a 64-bit key. iskradb calls it SipHash-2-4, but the word keys the // iskra ingest wrote were made by iskra/hash.mx HashBytes, which is FNV-1a // truncated to 56 bits; see HashWord. type Key uint64 // KeyZero is the zero key, used as the null sentinel in node arrays. const KeyZero = Key(0) // NullIndex and NullRec are the 0xFFFFFFFF sentinels for empty child pointers // and empty record pointers. const ( NullIndex uint32 = 0xFFFFFFFF NullRec uint32 = 0xFFFFFFFF ) // Branch aliases kept from iskradb's C=3 era, which iskra/record.mx still uses. const ( Bnoun = Bgrammatical Bverb = Bmorphology Bmodifier = Bpragmatic ) // MakeKey is the go-ref two-argument key builder: language byte in the top // eight bits, 56-bit hash below. It differs from MakeCodeKey (stage byte, same // layout) and from iskra/stage.mx MakeCodeKey (hash, stage argument order). func MakeKey(lang uint8, hash uint64) Key { return Key(uint64(lang)<<56 | hash&0x00FFFFFFFFFFFFFF) } // KeyLang extracts the language byte from a key. func KeyLang(key Key) uint8 { return uint8(key >> 56) } // Language tags for the go-ref load path. iskra does not use them: its keys put // the stage byte in the same high eight bits, see MakeCodeKey. const ( LangBG uint8 = 0x01 LangEN uint8 = 0x02 LangHR uint8 = 0x03 LangSR uint8 = 0x04 LangMK uint8 = 0x05 LangRU uint8 = 0x06 ) // Corpus source tags, go-ref's provenance field on MetaEntry. Extra[6] in the // go-ref layout. iskra never writes one: ja-test.meta has a zero in every // entry's Extra[6]. const ( SourceBase uint8 = 0 SourceBible uint8 = 1 SourceWikdict uint8 = 2 SourceModern uint8 = 3 SourceFormal uint8 = 4 ) // SetSourceTag writes the corpus provenance. go-ref layout only. func (m *MetaEntry) SetSourceTag(src uint8) { m.Extra[6] = src } // GetSourceTag reads the corpus provenance. go-ref layout only. func (m *MetaEntry) GetSourceTag() uint8 { return m.Extra[6] } // SetPredictedBranch stores the predicted next branch and confidence in // Extra[5], the go-ref layout. In iskra's layout Extra[5] is the low byte of // the signature hash, so a tree carrying both in one MetaEntry is corrupt. func (m *MetaEntry) SetPredictedBranch(branch, confidence uint8) { if confidence > 3 { confidence = 3 } m.Extra[5] = (branch & 0x3) | ((confidence & 0x3) << 2) } // GetPredictedBranch reads the prediction stored by SetPredictedBranch. func (m *MetaEntry) GetPredictedBranch() (branch, confidence uint8) { return m.Extra[5] & 0x3, (m.Extra[5] >> 2) & 0x3 } // POSToTritPath maps a POS bitfield to a trit path, go-ref's rule: the highest // priority branch wins, noun over verb over modifier. func POSToTritPath(pos POS) TritPath { switch { case pos.Has(POSProperNoun): return MakeTritPath(BranchType, 2) case pos.Has(POSPronoun): return MakeTritPath(BranchType, 3) case pos.Has(POSNumeral): return MakeTritPath(BranchType, SubField, 2) case pos.Has(POSNoun): return MakeTritPath(BranchType, SubField) case pos.Has(POSAuxiliary): return MakeTritPath(BranchFunc, 2) case pos.Has(POSVerb): return MakeTritPath(BranchFunc, SubField) case pos.Has(POSAdjective): return MakeTritPath(BranchData, SubField) case pos.Has(POSAdverb): return MakeTritPath(BranchData, 2) case pos.Has(POSPreposition): return MakeTritPath(BranchData, 3, SubField) case pos.Has(POSConjunction): return MakeTritPath(BranchData, 3, 2) case pos.Has(POSParticle): return MakeTritPath(BranchData, 3, 3) case pos.Has(POSInterjection): return MakeTritPath(BranchData, 2, 2) case pos.Has(POSPrefix): return MakeTritPath(BranchData, SubField, 3) case pos.Has(POSSuffix): return MakeTritPath(BranchData, 2, 3) default: return MakeTritPath() } }